|
| 1 | +name: 'Onboard New Repository with SAST' |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + repository: |
| 7 | + description: 'Repository to onboard (format: owner/repo)' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + base_branch: |
| 11 | + description: 'Base branch to create PR against' |
| 12 | + required: false |
| 13 | + default: 'main' |
| 14 | + type: string |
| 15 | + repository_dispatch: |
| 16 | + types: [new_repository_created] |
| 17 | + |
| 18 | +jobs: |
| 19 | + create-sast-pr: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + permissions: |
| 22 | + contents: write |
| 23 | + pull-requests: write |
| 24 | + steps: |
| 25 | + - name: Checkout scanner action repository |
| 26 | + uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + path: scanner-repo |
| 29 | + |
| 30 | + - name: Determine target repository |
| 31 | + id: target |
| 32 | + run: | |
| 33 | + if [ "${{ github.event_name }}" = "repository_dispatch" ]; then |
| 34 | + echo "repository=${{ github.event.client_payload.repository }}" >> $GITHUB_OUTPUT |
| 35 | + echo "base_branch=${{ github.event.client_payload.base_branch || 'main' }}" >> $GITHUB_OUTPUT |
| 36 | + else |
| 37 | + echo "repository=${{ inputs.repository }}" >> $GITHUB_OUTPUT |
| 38 | + echo "base_branch=${{ inputs.base_branch }}" >> $GITHUB_OUTPUT |
| 39 | + fi |
| 40 | + shell: bash |
| 41 | + |
| 42 | + - name: Checkout target repository |
| 43 | + uses: actions/checkout@v4 |
| 44 | + with: |
| 45 | + repository: ${{ steps.target.outputs.repository }} |
| 46 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 47 | + path: target-repo |
| 48 | + ref: ${{ steps.target.outputs.base_branch }} |
| 49 | + |
| 50 | + - name: Create branch and add SAST workflow |
| 51 | + working-directory: target-repo |
| 52 | + run: | |
| 53 | + git config user.name "MetaMask Security Bot" |
| 54 | + git config user.email "[email protected]" |
| 55 | +
|
| 56 | + BRANCH_NAME="security/add-sast-scanner" |
| 57 | + git checkout -b "$BRANCH_NAME" |
| 58 | +
|
| 59 | + # Create .github/workflows directory if it doesn't exist |
| 60 | + mkdir -p .github/workflows |
| 61 | +
|
| 62 | + # Copy the security scanner workflow template |
| 63 | + cp ../scanner-repo/examples/security-code-scanner.yml .github/workflows/security-code-scanner.yml |
| 64 | +
|
| 65 | + git add .github/workflows/security-code-scanner.yml |
| 66 | + git commit -m "chore: add MetaMask Security Code Scanner workflow |
| 67 | +
|
| 68 | +This PR adds the MetaMask Security Code Scanner workflow to enable |
| 69 | +automated security scanning of the codebase. |
| 70 | + |
| 71 | +The scanner will run on: |
| 72 | +- Push to main branch |
| 73 | +- Pull requests to main branch |
| 74 | +- Manual workflow dispatch |
| 75 | + |
| 76 | +To configure the scanner for your repository's specific needs, |
| 77 | +please review the workflow file and adjust as necessary." |
| 78 | + |
| 79 | + git push origin "$BRANCH_NAME" |
| 80 | + shell: bash |
| 81 | + |
| 82 | + - name: Create Pull Request |
| 83 | + working-directory: target-repo |
| 84 | + env: |
| 85 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 86 | + REPO_NAME: ${{ steps.target.outputs.repository }} |
| 87 | + run: | |
| 88 | + # Extract owner and repo name for URL construction |
| 89 | + OWNER=$(echo "$REPO_NAME" | cut -d'/' -f1) |
| 90 | + REPO=$(echo "$REPO_NAME" | cut -d'/' -f2) |
| 91 | + BASE_BRANCH="${{ steps.target.outputs.base_branch }}" |
| 92 | + SECURITY_URL="https://github.com/${OWNER}/${REPO}/security/code-scanning" |
| 93 | +
|
| 94 | + # Read PR body template and substitute variables |
| 95 | + PR_BODY=$(cat ../scanner-repo/.github/templates/onboarding-pr-body-automated.md) |
| 96 | + PR_BODY="${PR_BODY//\{\{SECURITY_SCANNING_URL\}\}/$SECURITY_URL}" |
| 97 | +
|
| 98 | + gh pr create \ |
| 99 | + --title "🔒 Add MetaMask Security Code Scanner" \ |
| 100 | + --body "$PR_BODY" \ |
| 101 | + --base "$BASE_BRANCH" \ |
| 102 | + --head "security/add-sast-scanner" |
| 103 | + shell: bash |
| 104 | + |
| 105 | + - name: Output PR URL |
| 106 | + working-directory: target-repo |
| 107 | + env: |
| 108 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 109 | + run: | |
| 110 | + PR_URL=$(gh pr view security/add-sast-scanner --json url -q .url) |
| 111 | + echo "✅ Pull Request created: $PR_URL" |
| 112 | + echo "PR_URL=$PR_URL" >> $GITHUB_OUTPUT |
| 113 | + shell: bash |
0 commit comments