Bash script to automate to adding multiple members to multiple teams using GitHub REST API - bash

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

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.

Related

extract a value from the output of a script and store in a variable winodws?

According to the document Get Azure AD tokens for service principals:
curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' \
https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/token \
-d 'client_id=<client-id>' \
-d 'grant_type=client_credentials' \
-d 'scope=2ff814a6-3304-4ab8-85cb-cd0e6f879c1d%2F.default' \
-d 'client_secret=<client-secret>'
Now, I could get the correct output,like:
The Azure AD access token is in the access_token value within the output of the call.
What I want is that I need the get the value of the access_token and set it to the variable, so that I could use it in next REST API scripts.
But I'm not very familiar with Bash and curl, can anyone offer advice?
use jq to extract access_token from the json, and VAR=$(...) to store it in a variable,
ACCESS_TOKEN=$(curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' \
https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/token \
-d 'client_id=<client-id>' \
-d 'grant_type=client_credentials' \
-d 'scope=2ff814a6-3304-4ab8-85cb-cd0e6f879c1d%2F.default' \
-d 'client_secret=<client-secret>' \
| jq -r .access_token )
then you can use ACCESS_TOKEN like
curl -d access_token="$ACCESS_TOKEN"
but be wary, bash is a shitty scripting language, you should not attempt to use bash for complex logic, you should probably switch to a better scripting language like Python, Perl, or PHP, rather than implementing complex logic in Bash. (same goes for Windows's cmd and PowerShell. all 3 are languages unsuitable, but not incapable, of complex logic)

Getting azure subscription accesToken without az installed (for authorizing private endpoint from snowflake)

I am trying to setup azure privatelink on several snowflake accounts and I am terraforming all of the steps like creating the private endpoints and authorizing the connection from snowflake side (I am following this tutorial).
In the docs they are using command: az account get-access-token --subscription <SubscriptionID> and than you copy the accessToken and this works for the authorization, but my problem is that I am running all of this in a CI/CD and I don't have az installed so the only way to get this token is with a bash script. I've tried using azure RestAPI:
TOKEN="$(curl -s -X POST \
-H "Accept: application/json" \
-d "subscription_id=${SUBSCRIPTION_ID}"\
-d "client_id=${CLIENT_ID}" \
-d "client_secret=${CLIENT_SECRET}" \
-d "grant_type=client_credentials" \
-d "resource=https://ossrdbms-aad.database.windows.net" \
"https://login.microsoftonline.com/${TENANT_ID}/oauth2/token" \
| jq -r .access_token)"
but this apparently gives a different access token (for azure ad) that is not working for the snowflake authorization.
Is there any Azure RestAPI command (similar to this curl command above) that gives the same output like: az account get-access-token --subscription <SubscriptionID> so I can use it in my script?

Sending large file as data inside JSON via cURL

I'm trying to send a file as base64-encoded data via POST to the Bugzilla REST API as follows:
curl -X POST https://www.example.com/rest/bug/$id/attachment -H "Content-Type: application/json" \
-d "{\
\"login\" : \"$username\", \
\"password\" : \"$password\", \
\"ids\" : [ $id ], \
\"summary\" : \"...\", \
\"content_type\" : \"application/gzip\", \
\"data\" : \"$data\"\
}"
What I'm getting is an error from cURL that the argument list is too long. Presumably, this is because the file ($data) I'm trying to send is more than the shell maximum (the file is 11M). What I've seen online is that the best way to get around that is to have cURL read the data from a file using --data-binary. But since I need to send a username and password, I'd prefer not to have to save the entire file with them inside.
Is there some way to get around this maximum, or is there another way to send a large amount of data this way? I prefer native Linux tools, as I want this script to be portable.
You can try using a file like this, which is the recommended way.
curl -i \
-H 'Accept:application/json' \
-H 'Authorization:Basic $username:$password' \
-X POST -d #datafile.txt https://www.example.com/rest/bug/$id/attachment

How to upload a file to a slack channel using a bot

I have a slack bot and the token starting with xoxb is used to upload a file to a channel.
I am using below format
curl -F token="${SLACK_TOKEN}" -F file=e2e.sh -F channel="${SLACK_CHANNEL}" -F as_user=true https://slack.com/api/files.upload
This throws
{"ok":false,"error":"no_file_data"}
You are missing the # in your file=e2e.sh argument to let curl know you want to transmit a file. The following should do the trick:
curl \
-F token="${SLACK_TOKEN}" \
-F file=#e2e.sh \
-F channel="${SLACK_CHANNEL}" \
-F as_user=true \
https://slack.com/api/files.upload
p.s. Breaking a long curl into multiple lines can help you see things more clearly ;)

Triggering builds of dependent projects in Travis CI

We have our single page javascript app in one repository and our backend server in another. Is there any way for a passing build on the backend server to trigger a build of the single page app?
We don't want to combine them into a single repository, but we do want to make sure that changes to one don't break the other.
Yes, it is possible to trigger another Travis job after a first one succeeds. You can use the trigger-travis.sh script.
The script's documentation tells how to use it -- set an environment variable and add a few lines to your .travis.yml file.
It's possible yes and it's also possible to wait related build result.
I discover trigger-travis.sh from the previous answer but before that I was implementing my own solution (for full working source code: cf. pending pull request PR196 and live result)
References
Based on travis API v3 documentation:
trigger a build triggering-builds
get build information resource/builds
You will need a travis token, and setup this token as secreet environment variable on travis portal.
Following this doc, I were able to trigger a build, and wait for him.
1) make .travis_hook_qa.sh
(extract) - to trigger a new build :
REQUEST_RESULT=$(curl -s -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Travis-API-Version: 3" \
-H "Authorization: token ${QA_TOKEN}" \
-d "$body" \
https://api.travis-ci.org/repo/${QA_SLUG}/requests)
(it's trigger-travis.sh equivalent) You could make some customization on the build definition (with $body)
2) make .travis_wait_build.sh
(extract) - to wait a just created build, get build info :
BUILD_INFO=$(curl -s -X GET \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Travis-API-Version: 3" \
-H "Authorization: token ${QA_TOKEN}" \
https://api.travis-ci.org/repo/${QA_SLUG}/builds?include=build.state\&include=build.id\&include=build.started_at\&branch.name=master\&sort_by=started_atdesc\&limit=1 )
BUILD_STATE=$(echo "${BUILD_INFO}" | grep -Po '"state":.*?[^\\]",'|head -n1| awk -F "\"" '{print $4}')
BUILD_ID=$(echo "${BUILD_INFO}" | grep '"id": '|head -n1| awk -F'[ ,]' '{print $8}')
You will have to wait until your timeout or expected final state..
Reminder: possible travis build states are created|started (and then) passed|failed

Resources