Trouble inviting a collaborator with the {gh} package via Github Actions

Hi all! I hope you’re doing well.

I’m having trouble running some {gh} functions as part of a Github Action workflow. Specifically, I am unable to invite a collaborator to a repository, though I can perform other API actions. For example, the following request works locally, but not in a workflow. Here are the logs for a Github Action that successfully creates an issue using the {gh} package but fails when attempting to invite a collaborator. I must be missing something and would greatly appreciate any suggestions! The final workflow will be used in private and/or public repos for students to get feedback from their professor, though ideally it should work with repos in a Github organization context, too.


# Locally this works, but when I use it as Github Action, it does not.
gh::gh("PUT /repos/{owner}/{repo}/collaborators/{username}",
owner = "jdtrat",
repo = "actions",
username = "smasongarrison", #username included with permission (:
.token = Sys.getenv("GITHUB_PAT"))

The default GITHUB_TOKEN does not seem to have the needed permissions to invite collaborators. You will have to generate a GITHUB_PAT yourself with the needed permissions, add it to the repositories secrets and then expose it in your workflow to do what you are trying to do. There is some documentation on how to do this.

Hi @jimhester! Firstly, thanks for all you do in the R community -- I'm a huge fan of your work! Secondly, that made a lot of sense. I just tested it out, and it worked perfectly!

Thirdly, do you have any recommendations for how I might automate creating the GITHUB_PAT for each student in the Github organization? The goal is for them to call usethis::use_github_action(url = "link_to_action") when they're ready to submit their labs and the workflow will take care of inviting the TA/professor and assigning issues, etc. This documentation is helpful, but I'm not sure whether it will work for organization members' private repos?

Thanks again for your help!

You can use usethis::create_github_token(scopes = c("foo", "bar")), it will open a browser window with the scopes you select for them. They will still have to click the button to create the token themselves, I don't think you can really automate that completely.

Ah, I forgot all about that function. Thanks so much! :smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.