Monday, February 5, 2024

[SOLVED] Add GitHub Team in Org to a REPO

Issue

I'm trying to do a Curl request to add a team to a repo like this:

curl --user $USERNAME:$TOKEN -X PUT -d "" "https://api.github.com/teams/<team name>/repos/<org>/$REPONAME"

My variables are the correct one, but I keep getting a message not found error. Anyone have an advice on how to proceed?


Solution

The correct endpoint from here is :

PUT /teams/:id/repos/:org/:repo

You have to specify the team ID not the team name

The following will get the team ID for the Developers team and perform the PUT request to add repo to the specified team :

username=your_user
password=your_password

org=your_org
repo=your_repo

team=Developers

teamid=$(curl -s --user $username:$password "https://api.github.com/orgs/$org/teams" | \
    jq --arg team "$team" '.[] | select(.name==$team) | .id')

curl -v --user $username:$password -d "" -X PUT "https://api.github.com/teams/$teamid/repos/$org/$repo"

Note for this example JSON parsing is done with jq

Also consider to use Personal access token with scope admin:org instead of your username/password (then use -H "Authorization: Token $TOKEN")



Answered By - Bertrand Martel
Answer Checked By - Timothy Miller (WPSolving Admin)