Skip to content

Commit 998c8cf

Browse files
author
Nabeel Shakeel
authored
Merge pull request #23 from Techwards/feature/PRK-16-add-github-action
Feature | Issue #16 | GitHub Action Added
2 parents 0630780 + 4c28854 commit 998c8cf

File tree

15 files changed

+8873
-786
lines changed

15 files changed

+8873
-786
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: 'build-test'
22
on: # rebuild any PRs and main branch changes
33
pull_request:
4+
types: [opened, edited, synchronize, reopened]
45
push:
56
branches:
67
- main
@@ -10,15 +11,15 @@ jobs:
1011
build: # make sure build/ci work properly
1112
runs-on: ubuntu-latest
1213
steps:
13-
- uses: actions/checkout@v2
14+
- uses: actions/checkout@v3
1415
- run: |
1516
npm install
1617
- run: |
1718
npm run all
1819
test: # make sure the action works on a clean machine without building
1920
runs-on: ubuntu-latest
2021
steps:
21-
- uses: actions/checkout@v2
22+
- uses: actions/checkout@v3
2223
- uses: ./
2324
with:
24-
milliseconds: 1000
25+
token: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 133 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,169 @@
1+
<h1 align="center">PR Keeper</h1>
2+
3+
<!-- PROJECT SHIELDS -->
14
<p align="center">
5+
<!-- <a href="package_link_here">
6+
<img src="https://img.shields.io/badge/node-18.x.x-brightgreen.svg" alt="version" />
7+
</a>
8+
&nbsp; -->
29
<a href="https://github.com/actions/typescript-action/actions"><img alt="typescript-action status" src="https://github.com/actions/typescript-action/workflows/build-test/badge.svg"></a>
10+
<!-- &nbsp;
11+
<a href="package_link_here">
12+
<img src="https://img.shields.io/npm/dm/prompts.svg" alt="downloads" />
13+
</a> -->
314
</p>
415

5-
# Create a JavaScript Action using TypeScript
16+
<p align="center">
17+
A GitHub action to validate Pull Request's title and description consistent convention
18+
</p>
619

7-
Use this template to bootstrap the creation of a TypeScript action.:rocket:
20+
<br />
821

9-
This template includes compilation support, tests, a validation workflow, publishing, and versioning guidance.
22+
- **Pull Request Title**: Validate Pull Request's title convention against the regular expression.
23+
- **Pull Request Description**: Validate Pull Request's description convention against the regular expression.
24+
- **Valid Pull Request Label**: If the Pull Request's title and description convention is valid then a label will be attached on the Pull Request.
25+
- **Custom Regular Expression & Label name**: User can add custom regex to validate the Pull Request's title and description and can also add custom label name for valid Pull Request status.
1026

11-
If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action)
27+
##### Valid Pull Request
1228

13-
## Create an action from this template
29+
<img src="./assets/images/check-list-passed.png" />
1430

15-
Click the `Use this Template` and provide the new repo details for your action
31+
##### Invalid Pull Request
1632

17-
## Code in Main
33+
<img src="./assets/images/check-list-failed.png" />
1834

19-
> First, you'll need to have a reasonably modern version of `node` handy. This won't work with versions older than 9, for instance.
35+
![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
2036

21-
Install the dependencies
22-
```bash
23-
$ npm install
24-
```
37+
## Usage
2538

26-
Build the typescript and package it for distribution
27-
```bash
28-
$ npm run build && npm run package
29-
```
39+
Create a `.github/workflows/${YOUR_WORKFLOW_NAME}.yml` file in your GitHub repo and add the following code.
3040

31-
Run the tests :heavy_check_mark:
32-
```bash
33-
$ npm test
34-
35-
PASS ./index.test.js
36-
✓ throws invalid number (3ms)
37-
wait 500 ms (504ms)
38-
test runs (95ms)
39-
40-
...
41+
```
42+
on:
43+
pull_request:
44+
types: [opened, edited, synchronize, reopened]
45+
46+
jobs:
47+
validate_pr:
48+
runs-on: ubuntu-latest
49+
name: Validate Pull Request
50+
steps:
51+
- name: validate PR
52+
id: validatePR
53+
uses: Techwards/pr-keeper@1.0
54+
with:
55+
token: ${{ secrets.GITHUB_TOKEN }}
4156
```
4257

43-
## Change action.yml
44-
45-
The action.yml defines the inputs and output for your action.
46-
47-
Update the action.yml with your name, description, inputs and outputs for your action.
48-
49-
See the [documentation](https://help.github.com/en/articles/metadata-syntax-for-github-actions)
58+
This GitHub action uses default regular expression for title and description convention validation and default label name if the pull request is valid.
5059

51-
## Change the Code
60+
**Custom Action Values:** Users can also add their custom regular expression and custom label name. Follow the examples section.
5261

53-
Most toolkit and CI/CD operations involve async operations so the action is run in an async function.
62+
![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
5463

55-
```javascript
56-
import * as core from '@actions/core';
57-
...
64+
## Examples
5865

59-
async function run() {
60-
try {
61-
...
62-
}
63-
catch (error) {
64-
core.setFailed(error.message);
65-
}
66-
}
66+
**Example 1:** Using custom title regular expression and default description regular expression and default label name
6767

68-
run()
68+
```
69+
on:
70+
pull_request:
71+
types: [opened, edited, synchronize, reopened]
72+
73+
jobs:
74+
validate_pr:
75+
runs-on: ubuntu-latest
76+
name: Validate Pull Request
77+
steps:
78+
- name: validate PR
79+
id: validatePR
80+
uses: Techwards/pr-keeper@1.0
81+
with:
82+
token: ${{ secrets.GITHUB_TOKEN }}
83+
title-regex: >
84+
^Ticket\s\#[1-9]{1,}$
6985
```
7086

71-
See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages.
87+
**Example 2:** Using custom description and default title regular expression and default label name
7288

73-
## Publish to a distribution branch
89+
```
90+
on:
91+
pull_request:
92+
types: [opened, edited, synchronize, reopened]
93+
94+
jobs:
95+
validate_pr:
96+
runs-on: ubuntu-latest
97+
name: Validate Pull Request
98+
steps:
99+
- name: validate PR
100+
id: validatePR
101+
uses: Techwards/pr-keeper@1.0
102+
with:
103+
token: ${{ secrets.GITHUB_TOKEN }}
104+
description-regex: >
105+
This\sPull Request\s(closes|fixes)\s\[\#[0-9]{1,}\]
106+
```
74107

75-
Actions are run from GitHub repos so we will checkin the packed dist folder.
108+
**Example 3:** Using default title and description regular expression and custom label name
76109

77-
Then run [ncc](https://github.com/zeit/ncc) and push the results:
78-
```bash
79-
$ npm run package
80-
$ git add dist
81-
$ git commit -a -m "prod dependencies"
82-
$ git push origin releases/v1
110+
```
111+
on:
112+
pull_request:
113+
types: [opened, edited, synchronize, reopened]
114+
115+
jobs:
116+
validate_pr:
117+
runs-on: ubuntu-latest
118+
name: Validate Pull Request
119+
steps:
120+
- name: validate PR
121+
id: validatePR
122+
uses: Techwards/pr-keeper@1.0
123+
with:
124+
token: ${{ secrets.GITHUB_TOKEN }}
125+
validation-label: Valid Pull Request Convention
83126
```
84127

85-
Note: We recommend using the `--license` option for ncc, which will create a license file for all of the production node modules used in your project.
128+
**Example 4:** Using custom title and description regular expression and custom label name
86129

87-
Your action is now published! :rocket:
130+
```
131+
on:
132+
pull_request:
133+
types: [opened, edited, synchronize, reopened]
134+
135+
jobs:
136+
validate_pr:
137+
runs-on: ubuntu-latest
138+
name: Validate Pull Request
139+
steps:
140+
- name: validate PR
141+
id: validatePR
142+
uses: Techwards/pr-keeper@1.0
143+
with:
144+
token: ${{ secrets.GITHUB_TOKEN }}
145+
title-regex: >
146+
^Ticket\s\#[1-9]{1,}$
147+
description-regex: >
148+
This\sPull Request\s(closes|fixes)\s\[\#[0-9]{1,}\]
149+
validation-label: Valid Pull Request Convention
150+
```
88151

89-
See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)
152+
![split](https://github.com/terkelg/prompts/raw/master/media/split.png)
90153

91-
## Validate
154+
## Inputs table
92155

93-
You can now validate the action by referencing `./` in a workflow in your repo (see [test.yml](.github/workflows/test.yml))
156+
| Name | Required | Default | Description |
157+
| ---- | -------- | ------- | ----------- |
158+
| `token` | `true` | `none` | A GitHub authentication token |
159+
| `title-regex` | `false` | ```^(Feature\|Fix\|Task)\s\|\sIssue\s\#[1-9]{1,}\s\|\s[\w\s\']\*$``` | A regular expression to validate the PR's title convention. Can also provide custom regular expression |
160+
| `description-regex` | `false` | ```This\sPR\scloses\s\[\#[0-9]{1,}\]\((https?:\/\/)?(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~\#\?\&\/\=]*)\)``` | A regular expression to validate the PR's description convention. Can also provide custom regular expression |
161+
| `validation-label` | `false` | Ready for Review | A label will be attached on the PR if the PR is valid. Can also provide custom label name |
94162

95-
```yaml
96-
uses: ./
97-
with:
98-
milliseconds: 1000
99-
```
163+
## Credits
100164

101-
See the [actions tab](https://github.com/actions/typescript-action/actions) for runs of this action! :rocket:
165+
[TypeScript GitHub Action Template](https://github.com/actions/typescript-action) by [GitHub Actions](https://github.com/actions).
102166

103-
## Usage:
167+
## License
104168

105-
After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and latest V1 action
169+
Distributed under the MIT License. See `LICENSE` for more information.

__tests__/main.test.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
1-
import {wait} from '../src/wait'
2-
import * as process from 'process'
3-
import * as cp from 'child_process'
4-
import * as path from 'path'
51
import {expect, test} from '@jest/globals'
62

7-
test('throws invalid number', async () => {
8-
const input = parseInt('foo', 10)
9-
await expect(wait(input)).rejects.toThrow('milliseconds not a number')
10-
})
11-
12-
test('wait 500 ms', async () => {
13-
const start = new Date()
14-
await wait(500)
15-
const end = new Date()
16-
var delta = Math.abs(end.getTime() - start.getTime())
17-
expect(delta).toBeGreaterThan(450)
18-
})
19-
20-
// shows how the runner will run a javascript action with env / stdout protocol
21-
test('test runs', () => {
22-
process.env['INPUT_MILLISECONDS'] = '500'
23-
const np = process.execPath
24-
const ip = path.join(__dirname, '..', 'lib', 'main.js')
25-
const options: cp.ExecFileSyncOptions = {
26-
env: process.env
27-
}
28-
console.log(cp.execFileSync(np, [ip], options).toString())
3+
test('dummy test', () => {
4+
const input = true
5+
expect(input).toBe(true)
296
})

action.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
name: 'Your name here'
2-
description: 'Provide a description here'
3-
author: 'Your name or organization here'
1+
name: 'PR Keeper'
2+
description: 'A GitHub action that will validate the pull request'
3+
on: [pull_request]
44
inputs:
5-
milliseconds: # change this
5+
token:
6+
description: 'GitHub secret'
67
required: true
7-
description: 'input description here'
8-
default: 'default value if applicable'
8+
title-regex:
9+
description: 'Regex for pull request title validation'
10+
default: >
11+
^(Feature|Fix|Task)\s\|\sIssue\s\#[1-9]{1,}\s\|\s[\w\s\']*$
12+
description-regex:
13+
description: 'Regex for pull request description validation'
14+
default: >
15+
This\sPR\scloses\s\[\#[0-9]{1,}\]\((https?:\/\/)?(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~\#\?\&\/\=]*)\)
16+
validation-label:
17+
description: 'Label to add when the pull request format is valid'
18+
default: Ready for Review
919
runs:
1020
using: 'node16'
1121
main: 'dist/index.js'
139 KB
Loading
116 KB
Loading

0 commit comments

Comments
 (0)