From 654a6e3ed946e422e5a131a1e7deffe98c527341 Mon Sep 17 00:00:00 2001 From: Vinson Chuong Date: Mon, 10 Apr 2023 11:22:26 -0700 Subject: [PATCH] fix(ci): Ensure that automated tests are run against pull requests --- src/bin/create-npm.js | 2 ++ src/templates/ci-action.js | 8 +++++--- src/templates/dependabot-action.js | 13 +++++++++++-- src/templates/index.js | 1 + src/templates/pr-action.js | 21 +++++++++++++++++++++ 5 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/templates/pr-action.js diff --git a/src/bin/create-npm.js b/src/bin/create-npm.js index b5244e0d..57f3ffe2 100755 --- a/src/bin/create-npm.js +++ b/src/bin/create-npm.js @@ -17,6 +17,7 @@ import { main, test, ciAction, + prAction, dependabotAction, dependabot, } from '../templates/index.js' @@ -60,6 +61,7 @@ await writeTemplate(projectDirectory, npmignore({})) await writeTemplate(projectDirectory, main({})) await writeTemplate(projectDirectory, test({})) await writeTemplate(projectDirectory, ciAction({branchName})) +await writeTemplate(projectDirectory, prAction()) await writeTemplate(projectDirectory, dependabotAction({})) await writeTemplate(projectDirectory, dependabot({})) diff --git a/src/templates/ci-action.js b/src/templates/ci-action.js index f23cdd5a..dee7f512 100644 --- a/src/templates/ci-action.js +++ b/src/templates/ci-action.js @@ -3,7 +3,10 @@ export default function ({branchName}) { path: '.github/workflows/ci.yml', content: ` name: CI - on: [push, pull_request] + on: + push: + branches: + - ${branchName} jobs: ci: runs-on: ubuntu-latest @@ -15,8 +18,7 @@ export default function ({branchName}) { cache: yarn - run: yarn - run: yarn test - - if: github.ref == 'refs/heads/${branchName}' - run: yarn release + - run: yarn release env: GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: \${{ secrets.NPM_TOKEN }} diff --git a/src/templates/dependabot-action.js b/src/templates/dependabot-action.js index 5c8d2e27..31e687ca 100644 --- a/src/templates/dependabot-action.js +++ b/src/templates/dependabot-action.js @@ -6,10 +6,19 @@ export default function () { on: pull_request_target jobs: dependabot: + if: \${{ github.event.pull_request.user.login == 'dependabot[bot]' }} runs-on: ubuntu-latest steps: - - if: \${{ github.event.pull_request.user.login == 'dependabot[bot]' }} - run: gh pr merge --auto --rebase \${{ github.event.pull_request.html_url }} + - uses: actions/checkout@v3 + with: + ref: \${{ github.event.pull_request.head.sha }} + - uses: actions/setup-node@v3 + with: + node-version: latest + cache: yarn + - run: yarn + - run: yarn test + - run: gh pr merge --auto --rebase \${{ github.event.pull_request.html_url }} env: GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }} `, diff --git a/src/templates/index.js b/src/templates/index.js index 681acf91..04aeddd0 100644 --- a/src/templates/index.js +++ b/src/templates/index.js @@ -6,5 +6,6 @@ export {default as packagejson} from './packagejson.js' export {default as readme} from './readme.js' export {default as test} from './test.js' export {default as ciAction} from './ci-action.js' +export {default as prAction} from './pr-action.js' export {default as dependabotAction} from './dependabot-action.js' export {default as dependabot} from './dependabot.js' diff --git a/src/templates/pr-action.js b/src/templates/pr-action.js new file mode 100644 index 00000000..881b9717 --- /dev/null +++ b/src/templates/pr-action.js @@ -0,0 +1,21 @@ +export default function () { + return { + path: '.github/workflows/pr.yml', + content: ` + name: PR + on: pull_request + jobs: + pr: + if: \${{ github.event.pull_request.user.login != 'dependabot[bot]' }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: latest + cache: yarn + - run: yarn + - run: yarn test + `, + } +}