|
| 1 | +# This file is automatically added by @npmcli/template-oss. Do not edit. |
| 2 | + |
| 3 | +name: Post Dependabot |
| 4 | + |
| 5 | +on: pull_request |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + template-oss: |
| 12 | + name: template-oss |
| 13 | + if: github.repository_owner == 'npm' && github.actor == 'dependabot[bot]' |
| 14 | + runs-on: ubuntu-latest |
| 15 | + defaults: |
| 16 | + run: |
| 17 | + shell: bash |
| 18 | + steps: |
| 19 | + - name: Checkout |
| 20 | + uses: actions/checkout@v3 |
| 21 | + with: |
| 22 | + ref: ${{ github.event.pull_request.head.ref }} |
| 23 | + - name: Setup Git User |
| 24 | + run: | |
| 25 | + git config --global user.email "[email protected]" |
| 26 | + git config --global user.name "npm CLI robot" |
| 27 | + - name: Setup Node |
| 28 | + uses: actions/setup-node@v3 |
| 29 | + with: |
| 30 | + node-version: 18.x |
| 31 | + - name: Install npm@latest |
| 32 | + run: npm i --prefer-online --no-fund --no-audit -g npm@latest |
| 33 | + - name: npm Version |
| 34 | + run: npm -v |
| 35 | + - name: Install Dependencies |
| 36 | + run: npm i --ignore-scripts --no-audit --no-fund |
| 37 | + - name: Fetch Dependabot Metadata |
| 38 | + id: metadata |
| 39 | + uses: dependabot/fetch-metadata@v1 |
| 40 | + with: |
| 41 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 42 | + |
| 43 | + # Dependabot can update multiple directories so we output which directory |
| 44 | + # it is acting on so we can run the command for the correct root or workspace |
| 45 | + - name: Get Dependabot Directory |
| 46 | + if: contains(steps.metadata.outputs.dependency-names, '@npmcli/template-oss') |
| 47 | + id: flags |
| 48 | + run: | |
| 49 | + dependabot_dir="${{ steps.metadata.outputs.directory }}" |
| 50 | + if [[ "$dependabot_dir" == "/" ]]; then |
| 51 | + echo "::set-output name=workspace::-iwr" |
| 52 | + else |
| 53 | + # strip leading slash from directory so it works as a |
| 54 | + # a path to the workspace flag |
| 55 | + echo "::set-output name=workspace::-w ${dependabot_dir#/}" |
| 56 | + fi |
| 57 | +
|
| 58 | + - name: Apply Changes |
| 59 | + if: steps.flags.outputs.workspace |
| 60 | + id: apply |
| 61 | + run: | |
| 62 | + npm run template-oss-apply ${{ steps.flags.outputs.workspace }} |
| 63 | + if [[ `git status --porcelain` ]]; then |
| 64 | + echo "::set-output name=changes::true" |
| 65 | + fi |
| 66 | + # This only sets the conventional commit prefix. This workflow can't reliably determine |
| 67 | + # what the breaking change is though. If a BREAKING CHANGE message is required then |
| 68 | + # this PR check will fail and the commit will be amended with stafftools |
| 69 | + if [[ "${{ steps.dependabot-metadata.outputs.update-type }}" == "version-update:semver-major" ]]; then |
| 70 | + prefix='feat!' |
| 71 | + else |
| 72 | + prefix='chore!' |
| 73 | + fi |
| 74 | + echo "::set-output name=message::$prefix: postinstall for dependabot template-oss PR" |
| 75 | +
|
| 76 | + # This step will fail if template-oss has made any workflow updates. It is impossible |
| 77 | + # for a workflow to update other workflows. In the case it does fail, we continue |
| 78 | + # and then try to apply only a portion of the changes in the next step |
| 79 | + - name: Push All Changes |
| 80 | + if: steps.apply.outputs.changes |
| 81 | + id: push |
| 82 | + continue-on-error: true |
| 83 | + env: |
| 84 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 85 | + run: | |
| 86 | + git commit -am "${{ steps.apply.outputs.message }}" |
| 87 | + git push |
| 88 | +
|
| 89 | + # If the previous step failed, then reset the commit and remove any workflow changes |
| 90 | + # and attempt to commit and push again. This is helpful because we will have a commit |
| 91 | + # with the correct prefix that we can then --amend with @npmcli/stafftools later. |
| 92 | + - name: Push All Changes Except Workflows |
| 93 | + if: steps.apply.outputs.changes && steps.push-all.outcome == 'failure' |
| 94 | + env: |
| 95 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 96 | + run: | |
| 97 | + git reset HEAD~ |
| 98 | + git checkout HEAD -- .github/workflows/ |
| 99 | + git clean -fd .github/workflows/ |
| 100 | + git commit -am "${{ steps.apply.outputs.message }}" |
| 101 | + git push |
| 102 | +
|
| 103 | + # Check if all the necessary template-oss changes were applied. Since we continued |
| 104 | + # on errors in one of the previous steps, this check will fail if our follow up |
| 105 | + # only applied a portion of the changes and we need to followup manually. |
| 106 | + # |
| 107 | + # Note that this used to run `lint` and `postlint` but that will fail this action |
| 108 | + # if we've also shipped any linting changes separate from template-oss. We do |
| 109 | + # linting in another action, so we want to fail this one only if there are |
| 110 | + # template-oss changes that could not be applied. |
| 111 | + - name: Check Changes |
| 112 | + if: steps.apply.outputs.changes |
| 113 | + run: | |
| 114 | + npm exec --offline ${{ steps.flags.outputs.workspace }} -- template-oss-check |
| 115 | +
|
| 116 | + - name: Fail on Breaking Change |
| 117 | + if: steps.apply.outputs.changes && startsWith(steps.apply.outputs.message, 'feat!') |
| 118 | + run: | |
| 119 | + echo "This PR has a breaking change. Run 'npx -p @npmcli/stafftools gh template-oss-fix'" |
| 120 | + echo "for more information on how to fix this with a BREAKING CHANGE footer." |
| 121 | + exit 1 |
0 commit comments