Skip to content

Commit

Permalink
feat: add comment toggle ability (#2)
Browse files Browse the repository at this point in the history
Updates also the following:

- Adds linter to pull request
- Changes ci step name for clarity
- Add `token` for README
  • Loading branch information
jef authored Feb 23, 2021
1 parent cb9c21c commit b6b3642
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 14 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/pr-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Pull Request Linter
on:
pull_request:
types:
- opened
- edited
- reopened
jobs:
lint:
name: Lint pull request title
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Lint pull request title
uses: ./
with:
token: ${{ secrets.GITHUB_TOKEN }}
12 changes: 11 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
- main
jobs:
build-tag-release:
name: Build, tag, and release Docker image
name: Build, tag, and release
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Expand All @@ -18,3 +18,13 @@ jobs:
release-type: node
changelog-path: docs/changelog.md
package-name: streetmerchant
- name: Tag major version
if: ${{ steps.release.outputs.release_created }}
run: |
git config user.name github-actions[bot]
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
git remote add gh-token "https://${{ secrets.GITHUB_TOKEN}}@github.com/google-github-actions/release-please-action.git"
git tag -d v${{ steps.release.outputs.major }} || true
git push origin :v${{ steps.release.outputs.major }} || true
git tag -a v${{ steps.release.outputs.major }} -m "Release v${{ steps.release.outputs.major }}"
git push origin v${{ steps.release.outputs.major }}
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Lints pull requests based on [Conventional Commits v1.0.0](https://www.conventionalcommits.org/en/v1.0.0/).

Also has the ability to post a comment in the pull request conversation with examples.

![image](https://user-images.githubusercontent.com/12074633/108867820-91325700-75c3-11eb-8820-4b55abe01c35.png)

## Usage

```yaml
Expand All @@ -19,11 +23,14 @@ jobs:
steps:
- name: Lint pull request title
uses: jef/conventional-commits-pr-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
```
## Inputs
- `token` [**Required**]: Access token to the repository. Usually `GITHUB_TOKEN`.
- `comment`: Post a comment in the pull request conversation with examples. Default is `true`.
- `token` [**Required**]: Access token to the repository. Usually `${{ secrets.GITHUB_TOKEN }}`.

## Outputs

Expand Down
4 changes: 4 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: 'conventional-commits-pr-action'
description: 'Lints a pull request title based on Conventional Commits'
inputs:
comment:
required: false
description: 'Post a comment in the pull request conversation with examples.'
default: "true"
token:
required: true
description: 'Access token to the repository.'
Expand Down
19 changes: 15 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ export async function getPullRequest() {
return pr;
}

function buildMessage(): string {
export function buildMessage(): string {
const header = '## Pull request title linting :rotating_light:\n\n';
const preface =
'In order to merge this pull request, the title of the pull request ' +
'should be prefixed by one of the available types.\n\n';
const availableTypes = `### Available types:\n\n${getConventionalCommitTypes()}\n\n`;
const separator = '---\n\n';
const examples =
`<details>
const examples = `<details>
<summary>Examples</summary>
\`\`\`
Expand All @@ -44,7 +43,7 @@ ci: update pull request linter
style: change format of strings
\`\`\`
</details>` + '\n\n';
</details>\n\n`;
const footer =
':tipping_hand_person: For more examples, visit https://www.conventionalcommits.org/en/v1.0.0/#examples.';

Expand Down
22 changes: 19 additions & 3 deletions src/lint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {createPrComment, deletePrComment, getPullRequest} from './github';
import {
buildMessage,
createPrComment,
deletePrComment,
getPullRequest,
} from './github';
import * as conventionalCommitTypes from 'conventional-commit-types';
import {getInput} from '@actions/core';

const types = Object.keys(conventionalCommitTypes.types);

Expand All @@ -19,7 +25,10 @@ export async function lintPullRequest(title: string) {
});

if (!matches.some(regex => regex.test(title))) {
await createPrComment();
if (getInput('comment') === 'true') {
await createPrComment();
}

return false;
}

Expand All @@ -29,7 +38,14 @@ export async function lintPullRequest(title: string) {

export async function lint() {
const pr = await getPullRequest();
let errorMessage: string;
if (!(await lintPullRequest(pr.title))) {
throw new Error('pr linting failed. see pull request comment.');
if (getInput('comment') !== 'true') {
errorMessage = `pr linting failed.\n\n${buildMessage()}`;
} else {
errorMessage = 'pr linting failed. see pull request conversation.';
}

throw new Error(errorMessage);
}
}

0 comments on commit b6b3642

Please sign in to comment.