Skip to content

Commit 1db4d46

Browse files
committed
Enhance onboarding workflow by adding validation for base branch name and refactoring variable usage for improved clarity and consistency.
1 parent 5b979e3 commit 1db4d46

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

.github/workflows/onboard-new-repo.yml

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ jobs:
7070
BASE_BRANCH="main"
7171
fi
7272
73+
if ! echo "$BASE_BRANCH" | grep -qE '^[a-zA-Z0-9._/-]+$'; then
74+
echo "::error::Branch name contains invalid characters (only alphanumeric, dots, hyphens, slashes, and underscores are allowed)"
75+
exit 1
76+
fi
77+
7378
echo "repository=$REPO" >> "$GITHUB_OUTPUT"
7479
echo "base_branch=$BASE_BRANCH" >> "$GITHUB_OUTPUT"
7580
shell: bash
@@ -84,8 +89,6 @@ jobs:
8489
- name: Check for opt-out file
8590
id: check_opt_out
8691
run: |
87-
REPO="${{ steps.target.outputs.repository }}"
88-
BASE_BRANCH="${{ steps.target.outputs.base_branch }}"
8992
if gh api "repos/$REPO/contents/.github/no-security-scanner?ref=$BASE_BRANCH" > /dev/null 2>&1; then
9093
echo "Repository has opted out via .github/no-security-scanner"
9194
echo "opted_out=true" >> "$GITHUB_OUTPUT"
@@ -95,17 +98,20 @@ jobs:
9598
shell: bash
9699
env:
97100
GH_TOKEN: ${{ secrets.ONBOARDING_TOKEN }}
101+
REPO: ${{ steps.target.outputs.repository }}
102+
BASE_BRANCH: ${{ steps.target.outputs.base_branch }}
98103

99104
- name: Skip onboarding (repository opted out)
100105
if: steps.check_opt_out.outputs.opted_out == 'true'
101106
run: |
102-
echo "::notice::Skipping onboarding — repository ${{ steps.target.outputs.repository }} has a .github/no-security-scanner opt-out file"
107+
echo "::notice::Skipping onboarding — repository $REPO has a .github/no-security-scanner opt-out file"
108+
env:
109+
REPO: ${{ steps.target.outputs.repository }}
103110

104111
- name: Check if target repository is empty
105112
if: steps.check_opt_out.outputs.opted_out != 'true'
106113
id: check_empty
107114
run: |
108-
REPO="${{ steps.target.outputs.repository }}"
109115
# Try to list branches in the repository
110116
BRANCHES=$(gh api "repos/$REPO/branches" --jq 'length' 2>/dev/null) || BRANCHES="0"
111117
@@ -122,6 +128,7 @@ jobs:
122128
shell: bash
123129
env:
124130
GH_TOKEN: ${{ secrets.ONBOARDING_TOKEN }}
131+
REPO: ${{ steps.target.outputs.repository }}
125132

126133
- name: Checkout target repository
127134
if: steps.check_opt_out.outputs.opted_out != 'true' && steps.check_empty.outputs.is_empty == 'false'
@@ -138,19 +145,22 @@ jobs:
138145
mkdir -p target-repo
139146
cd target-repo
140147
git init
141-
git remote add origin "https://x-access-token:${{ secrets.ONBOARDING_TOKEN }}@github.com/${{ steps.target.outputs.repository }}.git"
148+
git remote add origin "https://x-access-token:${ONBOARDING_TOKEN}@github.com/${REPO}.git"
142149
shell: bash
150+
env:
151+
ONBOARDING_TOKEN: ${{ secrets.ONBOARDING_TOKEN }}
152+
REPO: ${{ steps.target.outputs.repository }}
143153

144154
- name: Create branch and add SAST workflow
145155
if: steps.check_opt_out.outputs.opted_out != 'true'
146156
working-directory: target-repo
157+
env:
158+
IS_EMPTY: ${{ steps.check_empty.outputs.is_empty }}
159+
BASE_BRANCH: ${{ steps.target.outputs.base_branch }}
147160
run: |
148161
git config user.name "MetaMask Security Bot"
149162
git config user.email "security-bot@metamask.io"
150163
151-
IS_EMPTY="${{ steps.check_empty.outputs.is_empty }}"
152-
BASE_BRANCH="${{ steps.target.outputs.base_branch }}"
153-
154164
if [ "$IS_EMPTY" = "true" ]; then
155165
# For empty repos, create initial commit on main
156166
BRANCH_NAME="$BASE_BRANCH"
@@ -196,11 +206,11 @@ jobs:
196206
env:
197207
GH_TOKEN: ${{ secrets.ONBOARDING_TOKEN }}
198208
REPO_NAME: ${{ steps.target.outputs.repository }}
209+
BASE_BRANCH: ${{ steps.target.outputs.base_branch }}
199210
run: |
200211
# Extract owner and repo name for URL construction
201212
OWNER=$(echo "$REPO_NAME" | cut -d'/' -f1)
202213
REPO=$(echo "$REPO_NAME" | cut -d'/' -f2)
203-
BASE_BRANCH="${{ steps.target.outputs.base_branch }}"
204214
SECURITY_URL="https://github.com/${OWNER}/${REPO}/security/code-scanning"
205215
206216
# Read PR body template and substitute variables
@@ -228,8 +238,9 @@ jobs:
228238
- name: Output commit info for empty repo
229239
if: steps.check_opt_out.outputs.opted_out != 'true' && steps.check_empty.outputs.is_empty == 'true'
230240
run: |
231-
REPO="${{ steps.target.outputs.repository }}"
232-
BASE_BRANCH="${{ steps.target.outputs.base_branch }}"
233241
echo "✅ Initial commit pushed to https://github.com/$REPO/tree/$BASE_BRANCH"
234242
echo "Repository was empty - workflow file added directly to $BASE_BRANCH branch"
235243
shell: bash
244+
env:
245+
REPO: ${{ steps.target.outputs.repository }}
246+
BASE_BRANCH: ${{ steps.target.outputs.base_branch }}

0 commit comments

Comments
 (0)