Skip to content

Commit 0143bce

Browse files
authored
feat(Script): initial github-repo-from-template.sh (#2)
* feat(Script): initial github-repo-from-template.sh * feat(Script): update
1 parent 2fa4c1a commit 0143bce

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env /bin/bash
2+
set -euo pipefail
3+
4+
###
5+
## GLOBAL VARIABLES
6+
###
7+
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
8+
ORG=${ORG:-''}
9+
API_URL_PREFIX=${API_URL_PREFIX:-'https://api.github.com'}
10+
TEMPLATE_REPO=${TEMPLATE_REPO:-''}
11+
REPO_NAME=$1
12+
CD_USERNAME=${CD_USERNAME:-''}
13+
CD_GITHUB_TOKEN=${CD_GITHUB_TOKEN:-''}
14+
PORTX_TENANT_FLUX_ADMINS=${PORTX_TENANT_FLUX_ADMINS:-''}
15+
PORTX_INFRASTRUCTURE=${PORTX_INFRASTRUCTURE:-''}
16+
17+
# Create repo from template
18+
curl -X POST -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.baptiste-preview+json" "${API_URL_PREFIX}/repos/${ORG}/${TEMPLATE_REPO}/generate" -d '{"name":"'${REPO_NAME}'", "owner":"'${ORG}'", "private":true, "include_all_branches":true}';
19+
20+
# Give internal teams permissions on the new repo
21+
curl -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/orgs/${ORG}/teams/${PORTX_TENANT_FLUX_ADMINS}/repos/${ORG}/${REPO_NAME}" -d '{"permission":"admin"}';
22+
curl -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/orgs/${ORG}/teams/${PORTX_INFRASTRUCTURE}/repos/${ORG}/${REPO_NAME}" -d '{"permission":"admin"}';
23+
24+
# Give cd user write permissions on the new repo
25+
curl -X PUT -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/repos/${ORG}/${REPO_NAME}/collaborators/${CD_USERNAME}" -d '{"permission":"push"}';
26+
27+
# Accept the invite automatically
28+
CD_INVITES=$(curl -H "Authorization: token ${CD_GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/user/repository_invitations")
29+
CD_INVITES_INVITE_ID=$(echo "$CD_INVITES" | jq -r --arg REPO_NAME "${REPO_NAME}" 'select(.[].repository.name==$REPO_NAME) | .[].id')
30+
curl -X PATCH -H "Authorization: token ${CD_GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" "${API_URL_PREFIX}/user/repository_invitations/${CD_INVITES_INVITE_ID}"

0 commit comments

Comments
 (0)