From c21a28d317f15d7d3579e849bb7760502973fb18 Mon Sep 17 00:00:00 2001 From: Deepak Puthraya Date: Sat, 3 Oct 2020 17:45:43 +0530 Subject: [PATCH] [Issue #4] Retrigger action on PR update (#10) --- .github/workflows/main.yml | 8 ++++++-- Readme.md | 16 +++++++++++++++- action.yml | 9 +++++++++ index.js | 18 +++++++++++++++++- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 087aa587..03d8b806 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,6 +1,8 @@ name: main # This workflow is triggered on pushes to the repository. -on: [pull_request] +on: + pull_request: + types: [opened, edited, synchronize, reopened] jobs: build: @@ -15,4 +17,6 @@ jobs: node-version: '10.x' # This step prints an output (time) from the previous step's action. - run: npm install - name: Validate - run: node index.js + uses: ./ + with: + github_token: ${{ github.token }} diff --git a/Readme.md b/Readme.md index 7de0b892..fb0b86da 100644 --- a/Readme.md +++ b/Readme.md @@ -12,11 +12,25 @@ steps: - uses: deepakputhraya/action-pr-title@master with: regex: '([a-z])+\/([a-z])+' # Regex the title should match. - allowed_prefixes: 'feature,stable,fix' # title should start with the given prefix + allowed_prefixes: 'feature,fix,JIRA' # title should start with the given prefix + disallowed_prefixes: 'feat/,hotfix' # title should not start with the given prefix prefix_case_sensitive: false # title prefix are case insensitive min_length: 5 # Min length of the title max_length: 20 # Max length of the title + github_token: ${{ github.token }} # Default: ${{ github.token }} ``` +### Note: +Ensure to add `types` to the Pull requests webhook event as by default workflows are triggered only +for `opened`, `synchronize`, or `reopened` pull request events. Read more about +it [here](https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#pull_request). +```yaml +on: + pull_request: + types: [opened, edited, synchronize, reopened] +``` + +Triggering the action on anything other than `pull_request` will cause a failure. + ## License The scripts and documentation in this project are released under the [MIT License](./LICENSE) diff --git a/action.yml b/action.yml index 30fc4bea..409e2d74 100644 --- a/action.yml +++ b/action.yml @@ -26,6 +26,15 @@ inputs: description: 'Max length of title. -1 to ignore the rule' required: false default: '-1' + github_token: + description: > + Personal access token (PAT) used to fetch the repository. The PAT is configured + with the local git config, which enables your scripts to run authenticated git + commands. The post-job step removes the PAT. + We recommend using a service account with the least permissions necessary. + Also when generating a new PAT, select the least scopes necessary. + required: false + default: ${{ github.token }} runs: using: 'node12' diff --git a/index.js b/index.js index fd20eb50..f7e60399 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,7 @@ function validateTitlePrefix(title, prefix, caseSensitive) { async function run() { try { + const authToken = core.getInput('github_token', {required: true}) const eventName = github.context.eventName; core.info(`Event name: ${eventName}`); if (validEvent.indexOf(eventName) < 0) { @@ -20,7 +21,22 @@ async function run() { return; } - const title = github.context.payload.pull_request.title; + const owner = github.context.payload.pull_request.base.user.login; + const repo = github.context.payload.pull_request.base.repo.name; + + const client = new github.GitHub(authToken); + // The pull request info on the context isn't up to date. When + // the user updates the title and re-runs the workflow, it would + // be outdated. Therefore fetch the pull request via the REST API + // to ensure we use the current title. + const {data: pullRequest} = await client.pulls.get({ + owner, + repo, + pull_number: github.context.payload.pull_request.number + }); + + const title = pullRequest.title; + core.info(`Pull Request title: "${title}"`); // Check if title pass regex