Skip to content

Commit

Permalink
Merge pull request #224 from gm3dmo/addrepostate
Browse files Browse the repository at this point in the history
Adding user team permission scripts
  • Loading branch information
gm3dmo authored Dec 20, 2024
2 parents 519eef7 + 8d68d18 commit 0e61492
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
6 changes: 3 additions & 3 deletions add-user-to-team.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
. ./.gh-api-examples.conf

# https://docs.github.com/en/rest/reference/teams#add-or-update-team-membership-for-a-user
# PUT /orgs/:org/teams/:team_slug/memberships/:username
# https://docs.github.com/en/enterprise-cloud@latest/rest/teams/members?apiVersion=2022-11-28#add-or-update-team-membership-for-a-user
# PUT /orgs/{org}/teams/{team_slug}/memberships/{username}

# Adds a single user to the default team.

Expand All @@ -27,4 +27,4 @@ curl ${curl_custom_flags} \
-X PUT \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
${GITHUB_API_BASE_URL}/teams/${team_id}/memberships/${team_member}
"${GITHUB_API_BASE_URL}/teams/${team_id}/memberships/${team_member}"
32 changes: 32 additions & 0 deletions add-users-to-teams-for-repository-permission.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
. ./.gh-api-examples.conf


# run this script after create-teams-for-repository-permission.sh
#
#

declare -A permission_to_user
permissions=("pull" "triage" "push" "maintain" "admin")
team_members_array=($team_members)

for i in "${!permissions[@]}"; do
permission_to_user["${permissions[i]}"]="${team_members_array[i]}"
done


prefix=pwr

for team_permission in "${permissions[@]}";

do
team_name=${prefix}-team-${team_permission}
team_slug=${team_name}
team_id=$(curl ${curl_custom_flags} -H "Authorization: Bearer ${GITHUB_TOKEN}" ${GITHUB_API_BASE_URL}/orgs/${org}/teams/$team_slug | jq '.id')
team_member="${permission_to_user[$team_permission]}"

curl ${curl_custom_flags} \
-X PUT \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${GITHUB_API_BASE_URL}/teams/${team_id}/memberships/${team_member}"
done
32 changes: 32 additions & 0 deletions create-teams-for-repository-permission.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
. ./.gh-api-examples.conf

# https://docs.github.com/en/enterprise-cloud@latest/rest/teams/teams?apiVersion=2022-11-28#create-a-team
# POST /orgs/{org}/teams

# Permissions for teams are from:
# https://docs.github.com/en/enterprise-cloud@latest/rest/teams/teams?apiVersion=2022-11-28#add-or-update-team-repository-permissions

for team_type in pull triage push maintain admin
do

prefix=pwr-team
team_name="${prefix}-${team_type}"
team=$team_name
privacy="closed"
#privacy="secret"
json_file=tmp/team-permission-data.json

jq -n \
--arg name "${team}" \
--arg description "${team} is a ${privacy} team. See: https://docs.github.com/en/enterprise-cloud@latest/rest/teams/teams?apiVersion=2022-11-28#add-or-update-team-repository-permissions" \
--arg privacy "$privacy" \
'{name: $name, description: $description, privacy: $privacy }' > ${json_file}

cat $json_file | jq -r

curl ${curl_custom_flags} \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${GITHUB_API_BASE_URL}/orgs/${org}/teams" --data @${json_file}

done
4 changes: 4 additions & 0 deletions gh-graphql-repo-users-with-permission-sources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ gh api graphql -F owner="${org}" -F repo="${repo}" -f query='
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
nameWithOwner
visibility
updatedAt
pushedAt
archivedAt
collaborators(first: 100) {
edges {
permission
Expand Down

0 comments on commit 0e61492

Please sign in to comment.