cardano-foundation / cardano-token-registry

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create pull requests programmatically

adakondacoin opened this issue · comments

Hey-yo folks,
i already created an issue but i completely lost track of it.
My apologize for that.

--> #3693 | #3693

My question is: there is any possibility to implement an automatism to create PRs to add token registry entries?
I let users mint token from my website and i would like to add the file to the entry by github api.

In the code portion below i use Oktokit.GitHubClient nuget.

`
//Create a new branch
var master = await _gitHubClient.Git.
Reference.Get("cardano-foundation", "cardano-token-registry", "heads/master");
var newBranchRef = await _gitHubClient.Git.
Reference.Create("MyOrganization", "cardano-token-registry", new NewReference($"refs/heads/{ticker}Token", master.Object.Sha));
_logger.LogInformation("Created branch with name {name}", newBranchRef.Ref);

        //Upload json
        var changeSet = await _gitHubClient.Repository.
            Content.CreateFile("MyOrganization", "cardano-token-registry", filePath,
             new CreateFileRequest($"Added token registry for {ticker} token", entryJson, newBranchRef.Ref));
        _logger.LogInformation("Uploaded file {name} successfully with changeset: {cs}", filePath, changeSet.Commit.Sha);

        //Create pull request to cardano-foundation
        var npr = new NewPullRequest($"Add {ticker} token to token registry",
            "MyOrganization:" + newBranchRef.Ref,
            "heads/master");
        var pr = await _gitHubClient.PullRequest.Create("cardano-foundation", "cardano-token-registry", npr);

`

I can create the branch and add the file, but when i try to create the pr it throws an exception:

image

Not sure at this point if i wrote anything wrong or if this is really a permission related issue.

Can you guys help me figure this out?
The reasons could be 2 imo:

  • my access token for the API does not have permission to PR to cardano-token-registry
  • i think for public repo being a collaborator could be a requirement to have permission

Thank you a lot and have a great start of 2024!!

Hello @adakondacoin

Happy new year to you as well!

I remember the previous requests indeed! A few minutes ago I merged another automated request coming from https://github.com/cardano-tools-nft/cardano-token-registry/branches/all . I feel they have achieved what you are looking to do, and they do not have any specific permissions. Creating forks, creating pull requests is something which is fairly common and should just work through the Github API imho.

I've done quick Google searches and I believe it's related to the permissions on your own Github account and your own fork of the Token Registry. I believe your Personal Access Token needs to get these permissions?

Resource: https://stackoverflow.com/questions/70435286/resource-not-accessible-by-integration-on-github-post-repos-owner-repo-ac

Let me know how this goes!

Thanks a lot for the quick reply mate! i will give it a try in the evening and let you know

i solved it bro, it was an issue with the base ref i was passing to the pr.

If anyone will ever need it i solved it like this:

//Create a new branch
            var master = await _gitHubClient.Git.
                Reference.Get("cardano-foundation", "cardano-token-registry", "heads/master");
            var branchName = $"{ ticker }Token";
              var newBranchRef = await _gitHubClient.Git.
                Reference.Create("MyOrganization", "cardano-token-registry", new NewReference($"refs/heads/{branchName}", master.Object.Sha));
            _logger.LogInformation("Created branch with name {name}", newBranchRef.Ref);

            //Upload json
            var changeSet = await _gitHubClient.Repository.
                Content.CreateFile("MyOrganization", "cardano-token-registry", filePath,
                 new CreateFileRequest($"Added token registry for {ticker} token", entryJson, newBranchRef.Ref));
            _logger.LogInformation("Uploaded file {name} successfully with changeset: {cs}", filePath, changeSet.Commit.Sha);

            var registryRepo = await _gitHubClient.Repository.Get("cardano-foundation", "cardano-token-registry");

            //Create pull request to cardano-foundation
            var npr = new NewPullRequest($"Add {ticker} token to token registry",
                $"MyOrganization:refs/heads/{branchName}",
                "master");
            var pr = await _gitHubClient.PullRequest.Create(registryRepo.Id, npr);

Cool, thanks for the update and good job! 💪