0

I am using following command to add or remove users in to GitHub teams . where username , orgname, team name and role are passed as arguments.

gh api \
  --method PUT \
  -H "Accept: application/vnd.github+json" \
  /orgs/$orgname/teams/$teamname/memberships/$membername \
  -f role='$role'

how can i make this script to run for multiple team names and multiple usernames. i want to pass them as arguments in rundeck job

I tried using for loop , but cant make to run for multiple teams and multiple users at once

  • Show an example, how you are going to pass the information, and how you implemented the _for_ loop which you mentioned; then we have something to discuss. BTW, with this complexity of the input data, passing the information via a configuration file having _orgname,teamname,membername_ in it, would perhaps make more sense. – user1934428 Jan 20 '23 at 06:59
  • You may wish to consider the question, details and discussion for this [**Question**](https://stackoverflow.com/questions/63545376/calling-rest-api-in-a-loop). – Eric Marceau Jan 20 '23 at 18:57

1 Answers1

0
teams=$(jq -r '.teams[]' $json_file)
users=$(jq -r '.users[]' $json_file)

for team in "${teams[@]}"

do
  for user in "${users[@]}"
    do
      gh api \
      --method PUT \
      -H "Accept: application/vnd.github+json" \
      /orgs/$orgname/teams/$team/memberships/$user \
      -f role='$role'
     done
  done

Pass all team names and usernames as a single JSON file in the Rundeck job arguments, and use jq to parse the JSON and extract the team names and usernames. Then use a for loop to iterate through the arrays of team names and usernames.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • `teams` and `users` aren't arrays here, and `$role` won't expand in single quotes, so I don't see how this would work. – Benjamin W. Mar 31 '23 at 14:53