From 4d80caf98b0503093328289b532c9b9064f33bab Mon Sep 17 00:00:00 2001 From: Nikolay Blagoev Date: Wed, 17 Mar 2021 16:51:22 +0200 Subject: [PATCH] Save the PR number during build and use it for the approval action if dependabot is making a PR --- .github/workflows/build-pr.yml | 38 ++++++++++------ .github/workflows/build.yml | 26 +++++------ .github/workflows/dependabot-pr.yml | 70 ++++++++++++++++------------- .github/workflows/draft-release.yml | 24 +++++----- .github/workflows/publish.yml | 36 +++++++-------- 5 files changed, 107 insertions(+), 87 deletions(-) diff --git a/.github/workflows/build-pr.yml b/.github/workflows/build-pr.yml index 8ea280f0..8043f523 100644 --- a/.github/workflows/build-pr.yml +++ b/.github/workflows/build-pr.yml @@ -10,16 +10,28 @@ jobs: matrix: node-version: [12.x, 14.x] steps: - - uses: actions/checkout@v2 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node-version }} - - name: install and build - run: | - yarn install --frozen-lockfile - yarn run coverage - yarn run build - env: - CI: true - - run: ./node_modules/.bin/codecov -f coverage/*.json + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - name: install and build + run: | + yarn install --frozen-lockfile + yarn run coverage + yarn run build + env: + CI: true + - run: ./node_modules/.bin/codecov -f coverage/*.json + save-pr: + runs-on: ubuntu-latest + steps: + - name: Save PR number + run: | + mkdir -p ./pr + echo ${{ github.event.number }} > ./pr/number + + - uses: actions/upload-artifact@v2 + with: + name: pr + path: pr/ diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index eed90e2e..6af34827 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,16 +12,16 @@ jobs: matrix: node-version: [12.x, 14.x] steps: - - uses: actions/checkout@v2 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node-version }} - - name: install and build - run: | - yarn install --frozen-lockfile - yarn run coverage - yarn run build - env: - CI: true - - run: ./node_modules/.bin/codecov -f coverage/*.json + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - name: install and build + run: | + yarn install --frozen-lockfile + yarn run coverage + yarn run build + env: + CI: true + - run: ./node_modules/.bin/codecov -f coverage/*.json diff --git a/.github/workflows/dependabot-pr.yml b/.github/workflows/dependabot-pr.yml index 7ebc7442..3ac7e639 100644 --- a/.github/workflows/dependabot-pr.yml +++ b/.github/workflows/dependabot-pr.yml @@ -1,7 +1,7 @@ name: dependabot-pr on: workflow_run: - workflows: ["build-pr", "lint-pr"] + workflows: ["build-pr"] types: - completed jobs: @@ -13,41 +13,49 @@ jobs: github.event.workflow_run.conclusion == 'success' && github.actor == 'dependabot[bot]' }} steps: - - name: Approve - uses: actions/github-script@v2 + - name: 'Download artifact' + uses: actions/github-script@v3 + with: + script: | + var artifacts = await github.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: ${{github.event.workflow_run.id }}, + }); + var matchArtifact = artifacts.data.artifacts.filter(artifact => artifact.name == "pr")[0]; + var download = await github.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + + var fs = require('fs'); + fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data)); + + - run: unzip pr.zip + + - name: Approve and Merge PR + uses: actions/github-script@v3 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | - const { pull_request: pr, repository: repo } = context.payload + var fs = require('fs'); + var pr_number = Number(fs.readFileSync('./number')); - core.debug(`Creating APPROVE review for pull request #${pr.number}`) + core.debug(`Creating APPROVE review for pull request #${pr_number}`) await github.pulls.createReview({ - owner: repo.owner.login, - repo: repo.name, - pull_number: pr.number, + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pr_number, event: "APPROVE" }) - core.debug(`Approved pull request #${pr.number}`) - - # Handled by GitHub's auto-merge feature - # - # automerge: - # name: Merge pull request - # runs-on: ubuntu-latest - # needs: autoapprove - # if: github.base_ref == 'master' && github.actor == 'dependabot[bot]' - # steps: - # - name: Merge - # uses: actions/github-script@v2 - # with: - # github-token: ${{ secrets.GITHUB_TOKEN }} - # script: | - # const { pull_request: pr, repository: repo } = context.payload + core.debug(`Approved pull request #${pr_number}`) - # core.debug(`Merging pull request #${pr.number}`) - # await github.pulls.merge({ - # owner: repo.owner.login, - # repo: repo.name, - # pull_number: pr.number - # }) - # core.debug(`Merged pull request #${pr.number}`) + core.debug(`Merging pull request #${pr_number}`) + await github.pulls.merge({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pr_number, + }) + core.debug(`Merged pull request #${pr_number}`) diff --git a/.github/workflows/draft-release.yml b/.github/workflows/draft-release.yml index c5755724..dca82f3d 100644 --- a/.github/workflows/draft-release.yml +++ b/.github/workflows/draft-release.yml @@ -60,20 +60,20 @@ jobs: matrix: node-version: [12.x, 14.x] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v2 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node-version }} + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} - - name: install and build - run: | - yarn install --frozen-lockfile - yarn run coverage - yarn run build - env: - CI: true + - name: install and build + run: | + yarn install --frozen-lockfile + yarn run coverage + yarn run build + env: + CI: true release: name: Create release diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 83c9b112..d46d26b6 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -7,26 +7,26 @@ jobs: name: Publish to NPM runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: '12.x' - registry-url: 'https://registry.npmjs.org' - - run: yarn install --frozen-lockfile - - run: yarn publish --access public - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: '12.x' + registry-url: 'https://registry.npmjs.org' + - run: yarn install --frozen-lockfile + - run: yarn publish --access public + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} publish-ghp: name: Publish to GitHub Packages runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: '12.x' - registry-url: 'https://npm.pkg.github.com' - - run: yarn install --frozen-lockfile - - run: yarn publish --access public - env: - NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: '12.x' + registry-url: 'https://npm.pkg.github.com' + - run: yarn install --frozen-lockfile + - run: yarn publish --access public + env: + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}