Skip to content

Commit 1f27a6c

Browse files
committed
Fix creation of private repos
1 parent 404d811 commit 1f27a6c

File tree

2 files changed

+123
-52
lines changed

2 files changed

+123
-52
lines changed

.github/workflows/CreatePrivateFork.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ env:
2323
ACTIONS_BRANCH: ${{ fromJSON(inputs.actions_ref).branch }}
2424
SCRIPT_DIR: ${{ github.workspace }}/asterisk-ci-actions/scripts
2525
GITHUB_TOKEN: ${{ secrets.ASTERISKTEAM_PAT }}
26+
SRC_REPO: ${{ inputs.src_repo }}
27+
DST_REPO: ${{ inputs.dst_repo }}
2628

2729
jobs:
2830
CreatePrivateFork:
@@ -37,9 +39,11 @@ jobs:
3739
3840
- name: CreateFork
3941
run: |
40-
${SCRIPT_DIR}/createPrivateFork.sh --src-repo=${{ inputs.src_repo }} \
41-
--dst-repo=${{ inputs.dst_repo }}
42-
42+
fix_branches=$(gh -R asterisk/${SRC_REPO} variable get SECURITY_FIX_BRANCHES | sed -n -r -e 's/("|\[|\])//gp')
43+
${SCRIPT_DIR}/createPrivateFork.sh \
44+
--src-repo=${SRC_REPO} \
45+
--dst-repo=${DST_REPO} \
46+
--security-fix-branches=${fix_branches}
4347
4448
- name: CopyVars
4549
uses: ./asterisk-ci-actions/CopyRepoVariables

scripts/createPrivateFork.sh

Lines changed: 116 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ if [ -z "${GITHUB_TOKEN}" ] ; then
55
exit 1
66
fi
77

8-
SCRIPT_DIR=$(dirname $(readlink -fn $0))
9-
. $SCRIPT_DIR/ci.functions
8+
: ${SCRIPT_DIR:=$(dirname $(readlink -fn $0))}
9+
10+
if [ ! -f "${SCRIPT_DIR}/ci.functions" ] ; then
11+
echo "Functions script '${SCRIPT_DIR}/ci.functions' doesn't exist."
12+
exit 1
13+
fi
14+
. "$SCRIPT_DIR/ci.functions"
1015

1116
set -e
1217

@@ -20,6 +25,11 @@ if [ -z "${DST_REPO}" ] ; then
2025
exit 1
2126
fi
2227

28+
if [ -z "${SECURITY_FIX_BRANCHES}" ] ; then
29+
echo "--security-fix-branches=<branch>[,<branch>]... must be provided"
30+
exit 1
31+
fi
32+
2333
if [ -z "${WORK_DIR}" ] && [ -z "${GITHUB_WORKSPACE}" ] ; then
2434
echo "--work-dir=<dir> must be provided on the command line or GITHUB_WORKSPACE must be provided in the environment"
2535
exit 1
@@ -32,60 +42,117 @@ fi
3242
export GH_TOKEN=${GITHUB_TOKEN}
3343
export GIT_TOKEN=${GITHUB_TOKEN}
3444

35-
REPO_DIR=${GITHUB_WORKSPACE}/$(basename ${DST_REPO})
36-
37-
cd ${GITHUB_WORKSPACE}
38-
39-
# Create the new repo and set it's parameters
40-
gh repo create asterisk/${DST_REPO} --private
41-
gh repo edit asterisk/${DST_REPO} --allow-forking=false --enable-auto-merge=false \
42-
--enable-discussions=false --enable-issues=false --enable-merge-commit=false \
43-
--enable-wiki=false
44-
45-
# Do a bare clone of the source repo
46-
git clone --bare https://github.com/asterisk/${SRC_REPO}.git ${REPO_DIR}
47-
48-
# Make sure the directory is trusted
49-
git config --global --add safe.directory ${REPO_DIR}
50-
51-
cd ${REPO_DIR}
52-
5345
gh auth setup-git -h github.com
5446

55-
# Push everything to the new repo
56-
git push --mirror https://github.com/asterisk/${DST_REPO}.git &> /tmp/push || \
57-
{ cat /tmp/push ; exit 1 ; }
58-
59-
gh repo edit asterisk/${DST_REPO} --default-branch master
60-
61-
# Clone all the labels from the soure repo.
62-
63-
gh label clone asterisk/${SRC_REPO} -f
64-
65-
# Sleep for a bit to allow github to catch up and recognize the
66-
# workflows on the master branch.
67-
sleep 5
47+
REPO_DIR=${GITHUB_WORKSPACE}/$(basename ${DST_REPO})
48+
echo "Source repository: asterisk/${SRC_REPO}"
49+
echo "Local repo directory: ${REPO_DIR}"
50+
echo "Destination repository: asterisk/${DST_REPO}"
51+
IFS=,
52+
echo -n "Populating branches: "
53+
for b in ${SECURITY_FIX_BRANCHES} ; do
54+
echo -n "$b "
55+
done
56+
echo
57+
unset IFS
58+
59+
echo "Changing directory to ${GITHUB_WORKSPACE}"
60+
cd "${GITHUB_WORKSPACE}"
61+
62+
# Clone the source repo with only the master branch.
63+
# This way when we create the remote repo, master
64+
# will become the default branch instead of the lowest
65+
# numbered one.
66+
echo "Cloning asterisk/${SRC_REPO} to ./${DST_REPO}"
67+
gh repo clone "asterisk/${SRC_REPO}" "./${DST_REPO}" -- --branch master
68+
git config --global --add safe.directory "${REPO_DIR}"
69+
70+
# gh repo create tries to set origin in the source
71+
# directory so we need to rename the current origin
72+
# to upstream first.
73+
git -C "${DST_REPO}" remote rename origin upstream
74+
# Prevent accidental pushes to the public repo
75+
git -C "${DST_REPO}" remote set-url --push upstream none
76+
77+
# Create the private repo from the source directory
78+
# and push the branch up.
79+
echo "Creating remote repository asterisk/${DST_REPO} from local directory ./${DST_REPO} and pushing master branch"
80+
gh repo create "asterisk/${DST_REPO}" --source "./${DST_REPO}" --private --disable-issues --disable-wiki --push
81+
82+
echo "Setting repo asterisk/${DST_REPO} parameters"
83+
gh repo edit "asterisk/${DST_REPO}" --allow-forking=false --enable-auto-merge=false \
84+
--enable-discussions=false --enable-issues=false --enable-merge-commit=false \
85+
--enable-wiki=false --default-branch=master
6886

69-
gh api \
70-
--method PUT \
87+
echo "Enabling actions on repo asterisk/${DST_REPO}"
88+
gh api --method PUT \
7189
-H "Accept: application/vnd.github+json" \
7290
-H "X-GitHub-Api-Version: 2022-11-28" \
73-
/orgs/asterisk/actions/permissions/repositories/${DST_REPO} || :
91+
"/repos/asterisk/${DST_REPO}/actions/permissions" \
92+
-F "enabled=true" -f "allowed_actions=all"
7493

75-
gh api --method PUT \
76-
-H "Accept: application/vnd.github+json" \
94+
# A "GitHub Hack" to enable workflows on the repo.
95+
echo "Renaming master branch to main and back again to trigger workflow"
96+
gh api --method POST -H "Accept: application/vnd.github+json" \
7797
-H "X-GitHub-Api-Version: 2022-11-28" \
78-
/repos/asterisk/${DST_REPO}/actions/permissions \
79-
-F "enabled=true" -f "allowed_actions=all" || :
98+
/repos/asterisk/${DST_REPO}/branches/master/rename -f "new_name=main" >/dev/null
99+
sleep 2
100+
gh api --method POST -H "Accept: application/vnd.github+json" \
101+
-H "X-GitHub-Api-Version: 2022-11-28" \
102+
/repos/asterisk/${DST_REPO}/branches/main/rename -f "new_name=master" >/dev/null
80103

81-
sleep 5
104+
# Clone all the labels from the soure repo.
105+
echo "Copyinglabels from asterisk/${SRC_REPO} to asterisk/${DST_REPO}"
106+
gh -R "asterisk/${DST_REPO}" label clone "asterisk/${SRC_REPO}" -f
107+
108+
echo "Pushing branches..."
109+
cd "./${DST_REPO}"
110+
IFS=,
111+
for b in ${SECURITY_FIX_BRANCHES} ; do
112+
if [ "$b" == "master" ] ; then
113+
continue
114+
fi
115+
echo " Pulling $b from asterisk/${SRC_REPO}"
116+
git checkout -b "$b" "upstream/$b"
117+
echo " Pushing $b to asterisk/${DST_REPO}"
118+
git push -u origin "$b"
119+
done
120+
unset IFS
121+
122+
# Now that workflows have been enabled, we need yet
123+
# another "GitHub Hack" to get the workflow files
124+
# recognized.
125+
high_branch=$(gh api --paginate \
126+
-H "Accept: application/vnd.github+json" \
127+
-H "X-GitHub-Api-Version: 2022-11-28" \
128+
"/repos/asterisk/${DST_REPO}/branches?per_page=100" \
129+
--jq '.[] | .name' | grep -E "^[0-9.]+$" | sort -r -V | head -1)
130+
131+
gh repo edit "asterisk/${DST_REPO}" --default-branch=${high_branch}
132+
sleep 1
133+
gh repo edit "asterisk/${DST_REPO}" --default-branch=master
134+
sleep 2
135+
136+
declare -i wfcount=0
137+
wfcount=$(gh api "/repos/asterisk/${DST_REPO}/actions/workflows" --jq '.total_count')
138+
if [ $wfcount -eq 0 ] ; then
139+
echo "Waiting for workflows to become available"
140+
declare -i start_sec=0
141+
declare -i elapsed=0
142+
start_sec=$SECONDS
143+
while true ; do
144+
sleep 1m
145+
wfcount=$(gh api "/repos/asterisk/${DST_REPO}/actions/workflows" --jq '.total_count')
146+
[ $wfcount -gt 0 ] && break
147+
elapsed=$(( (SECONDS - start_sec) / 60 ))
148+
echo "No workflows after ${elapsed} minutes. Sleeping for 1 minute"
149+
done
150+
echo "$wfcount workflows available after ${elapsed} minutes"
151+
fi
82152

83153
# Disable the workflows we never want to run in the private repo.
84-
# These will probably fail due to GitHub not enabling actions on the
85-
# repo in the first place.
86-
gh -R asterisk/${DST_REPO} workflow disable CreateDocs || :
87-
gh -R asterisk/${DST_REPO} workflow disable "Issue Opened" || :
88-
gh -R asterisk/${DST_REPO} workflow disable NightlyAdmin || :
89-
gh -R asterisk/${DST_REPO} workflow disable NightlyTests || :
90-
gh -R asterisk/${DST_REPO} workflow disable PRMergeApproved || :
91-
gh -R asterisk/${DST_REPO} workflow disable Releaser || :
154+
echo "Disabling workflows in asterisk/${DST_REPO}"
155+
declare -a DISABLE_WORKFLOWS=( CreateDocs "Issue Opened" MergePR NightlyAdmin NightlyTests Releaser WeeklyTests )
156+
for w in "${DISABLE_WORKFLOWS[@]}" ; do
157+
gh -R asterisk/${DST_REPO} workflow disable "$w" || :
158+
done

0 commit comments

Comments
 (0)