Created
April 21, 2021 09:21
-
-
Save axdotl/8231abd46793ea23160662c3d81f4ba9 to your computer and use it in GitHub Desktop.
[Backstage] Add catalog-info.yaml for all repos in a GitHub org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## Running this script will do following for all passed repos: | |
## - Create a branch `backstage-integration` | |
## - Add catalog-info.yaml | |
## - Creates a PullRequest | |
## | |
## This script expects following files | |
## - repos.txt: a file with repository names (one per line) | |
## - catalog-info.yaml: A minimal manifest template with content like | |
## apiVersion: backstage.io/v1alpha1 | |
## kind: Component | |
## metadata: | |
## name: <NAME> | |
## annotations: | |
## github.com/project-slug: exampleOrg/<NAME> | |
## tags: | |
## - <LANGUAGE> | |
## spec: | |
## type: service | |
## lifecycle: production | |
# set -x | |
GH_TOKEN=ghp_HereComesYourToken | |
ORG=exampleOrg | |
while read -r repo; do | |
echo "" | |
if [[ $repo == \#* ]] | |
then | |
echo "Ignore $repo" | |
continue | |
fi | |
echo "Repo: $repo" | |
rc=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GH_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/$ORG/$repo/contents/catalog-info.yaml) | |
if [[ $rc == '200' ]] | |
then | |
echo "catalog-info.yaml found, no more actions required." | |
continue | |
elif [[ $rc == '404' ]] | |
then | |
echo "No catalog-info.yaml found" | |
else | |
echo "Failed to check for catalog-info.yaml, status $rc" | |
echo "Will continue with next item..." | |
continue | |
fi | |
hasMain=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GH_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/$ORG/$repo/git/refs/heads/main) | |
mainName=main | |
if [[ $hasMain == '200' ]] | |
then | |
echo "has main branch" | |
elif [[ $hasMain == '404' ]] | |
then | |
echo "has master branch" | |
mainName=master | |
else | |
echo "Failed to check for main" | |
fi | |
headsRef=$(curl -s -H "Authorization: token $GH_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/$ORG/$repo/git/refs/heads/$mainName) | |
headsSha=$(echo "$headsRef" | jq '.object.sha' -r) | |
headsUrl=$(echo "$headsRef" | jq '.object.url' -r) | |
hasCodeowners=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GH_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/$ORG/$repo/contents/.github/CODEOWNERS) | |
if [[ $hasCodeowners == '200' ]] | |
then | |
echo "CODEOWNERS ✅" | |
elif [[ $hasCodeowners == '404' ]] | |
then | |
echo "CODEOWNERS ❌" | |
else | |
echo "Failed to check for CODEOWNERS" | |
fi | |
## This ugly jq and awk foo is needed to get the top lang of the repo | |
language=$(curl -s -H "Authorization: token $GH_TOKEN" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/$ORG/$repo/languages | jq 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' -r | awk '{split($0,a,"="); print tolower(a[1])}' | head -1) | |
echo "Language: $language" | |
createBranch=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GH_TOKEN" -H "Accept: application/vnd.github.v3+json" -X POST https://api.github.com/repos/$ORG/$repo/git/refs -d "{\"ref\":\"refs/heads/backstage-integration\", \"sha\":\"$headsSha\"}") | |
if [[ $createBranch == '201' ]] | |
then | |
echo "Branch backstage-integration ✅" | |
else | |
echo "Branch backstage-integration ❌" | |
continue | |
fi | |
catalogInfo=$(sed "s/<NAME>/$repo/" < catalog-info.yaml | sed "s/<LANGUAGE>/$language/" | base64 -w 0) | |
commitRc=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GH_TOKEN" -H "Accept: application/vnd.github.v3+json" -X PUT https://api.github.com/repos/$ORG/$repo/contents/catalog-info.yaml -d "{\"message\":\"Add catalog-info.yaml\",\"content\": \"$catalogInfo\",\"branch\": \"refs/heads/backstage-integration\"}") | |
if [[ $commitRc == '201' ]] | |
then | |
echo "catalog-info.yaml added ✅" | |
else | |
echo "catalog-info.yaml added ❌" | |
continue | |
fi | |
createPrRc=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $GH_TOKEN" -H "Accept: application/vnd.github.v3+json" -X POST https://api.github.com/repos/$ORG/$repo/pulls -d "{\"head\":\"backstage-integration\",\"base\":\"$mainName\",\"title\": \"Add catalog-info.yaml for Backstage\",\"body\":\"This pull request adds a **Backstage entity metadata file** to this repository so that the component can be added to the $ORG Backstage App software catalog.\n\nAfter this pull request is merged, the component will become available.\n\n**To make ownership visible add a [CODEOWNERS](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners) file to this repo.**\n\nFeel free to make changes to the file. For more information, read about the [descriptor format of catalog entities](https://backstage.io/docs/features/software-catalog/descriptor-format).\"}") | |
if [[ $createPrRc == '201' ]] | |
then | |
echo "PullRequest ✅" | |
else | |
echo "PullRequest ❌" | |
continue | |
fi | |
done <repos.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment