Skip to content

Instantly share code, notes, and snippets.

@mrfyda
Created March 17, 2023 09:54
Show Gist options
  • Save mrfyda/be88ba852232297a1a7e733f98d08444 to your computer and use it in GitHub Desktop.
Save mrfyda/be88ba852232297a1a7e733f98d08444 to your computer and use it in GitHub Desktop.
Adding GitHub repositories as Jira Components programmatically
#!/bin/bash
GITHUB_AUTH_TOKEN="<your GitHub personal access token>"
GITHUB_ORG_NAME="<your GitHub organization name>"
JIRA_SITE="<your Jira site URL>" # example: https://company.atlassian.net
JIRA_USERNAME="<your Jira user email>" # example: bots+jira@company.com
JIRA_PROJECT="<your Jira project key>" # example: PUL
# API token will be requested when executing. You can get one from https://id.atlassian.com/manage-profile/security/api-tokens
printf "Obtaining all repositories in the $GITHUB_ORG_NAME organization\n"
for repo in $(curl -s https://api.github.com/orgs/$GITHUB_ORG_NAME/repos -H "Authorization: Bearer $GITHUB_AUTH_TOKEN" | jq -r '.[] | .name'); do
printf "Adding $repo to Jira\n"
http_status=$(curl -X POST $JIRA_SITE/rest/api/2/component \
-H "Content-Type: application/json" \
-u $JIRA_USERNAME \
-d '{"name": "'$repo'", "project": "'$JIRA_PROJECT'"}' \
-sSo /dev/null -w "%{http_code}")
case "$http_status" in
200) printf "$repo added successfully\n"
;;
401) printf "Error: 401 Unauthorized, check your Jira authentication\n"
break
;;
*) printf "Error: $http_status HTTP status code\n"
break
;;
esac
sleep 5 # Pause between repositories
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment