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