Skip to content

Commit

Permalink
[Issue #4] Retrigger action on PR update (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakputhraya authored Oct 3, 2020
1 parent 2371b3f commit c21a28d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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 }}
16 changes: 15 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,30 @@ 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) {
core.setFailed(`Invalid event: ${eventName}`);
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
Expand Down

0 comments on commit c21a28d

Please sign in to comment.