feat: do not test user.login by default (#15)

* feat: do not test user.login by default

* ci: define user login in integration test

* ci: define dogfood test separately

* build: new dist build
This commit is contained in:
Michael Shick 2020-05-28 11:43:27 -04:00 committed by GitHub
parent 150a3ccfe9
commit f533dc4046
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 91 additions and 49 deletions

View file

@ -20,12 +20,7 @@
}
],
"camelcase": "off",
"@typescript-eslint/camelcase": [
"error",
{
"properties": "never"
}
],
"@typescript-eslint/camelcase": "off",
"@typescript-eslint/no-non-null-assertion": "off"
},
"env": {

View file

@ -32,9 +32,30 @@ jobs:
run: |
npm test
dogfood:
name: Dogfood
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Setup node.js
uses: actions/setup-node@v1
with:
node-version: "12.x"
- name: Install dependencies
run: |
npm ci
- name: Build action
run: |
npm run build
- name: Add Comment
uses: ./
with:
message: |
**It works!**
repo-token: ${{ secrets.GITHUB_TOKEN }}
repo-token-user-login: "github-actions[bot]"

View file

@ -1,6 +1,6 @@
# add-pr-comment
> A GitHub Action which adds a comment to a Pull Request Issue.
> A GitHub Action which adds a comment to a pull request's issue.
## Usage
@ -19,6 +19,7 @@ jobs:
🌏
!
repo-token: ${{ secrets.GITHUB_TOKEN }}
repo-token-user-login: 'github-actions[bot]' # The user.login for temporary GitHub tokens
allow-repeats: false # This is the default
```
@ -35,13 +36,24 @@ jobs:
runs-on: ubuntu-latest
steps:
uses: mshick/add-pr-comment@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
message: |
**Hello MASTER**
repo-token: ${{ secrets.GITHUB_TOKEN }}
allow-repeats: true
```
## Configuration options
| Variable or Argument | Location | Description | Required | Default |
| --------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------- | -------- | ------- |
| message | with | The message you'd like displayed, supports Markdown and all valid Unicode characters | yes | |
| repo-token | with | A valid GitHub token, either the temporary token GitHub provides or a personal access token | maybe | |
| repo-token-user-login | with | Define this to save on comment processing time when checking for repeats. GitHub's default token uses `github-actions[bot]` | no | |
| allow-repeats | with | A boolean flag to allow identical messages to be posted each time this action is run | no | false |
| GITHUB_TOKEN | env | A valid GitHub token, can alternatively be defined in the env | maybe | |
## Features
- Fast, runs in the GitHub Actions node.js runtime; no Docker pull needed.

View file

@ -8,6 +8,7 @@ import run from '../add-pr-comment'
const repoFullName = 'foo/bar'
const repoToken = '12345'
const userLogin = 'github-actions[bot]'
const commitSha = 'abc123'
const issueNumber = 1
const simpleMessage = 'hello world'
@ -17,6 +18,7 @@ const multilineMessageWindows = fs.readFileSync(path.resolve(__dirname, './messa
const inputs = {
message: '',
'repo-token': '',
'repo-token-user-login': '',
'allow-repeats': 'false',
}
@ -84,9 +86,10 @@ describe('add-pr-comment action', () => {
await expect(run()).resolves.not.toThrow()
})
it('safely exits when no issue can be found', async () => {
it('safely exits when no issue can be found [using GITHUB_TOKEN in env]', async () => {
process.env['GITHUB_TOKEN'] = repoToken
inputs.message = simpleMessage
inputs['repo-token'] = repoToken
inputs['allow-repeats'] = 'true'
github.context.payload = {
@ -111,9 +114,10 @@ describe('add-pr-comment action', () => {
await run()
})
it('identifies repeat messages and does not create a comment', async () => {
it('identifies repeat messages and does not create a comment [user login provided]', async () => {
inputs.message = simpleMessage
inputs['repo-token'] = repoToken
inputs['repo-token-user-login'] = userLogin
inputs['allow-repeats'] = 'false'
const originalSetOutput = core.setOutput
@ -126,21 +130,21 @@ describe('add-pr-comment action', () => {
return originalSetOutput(key, value)
})
nock('https://api.github.com')
.get(`/repos/${repoFullName}/issues/1/comments`)
.reply(200, [
{
body: simpleMessage,
user: {
login: 'github-actions[bot]',
},
const replyBody = [
{
body: simpleMessage,
user: {
login: userLogin,
},
])
},
]
nock('https://api.github.com').get(`/repos/${repoFullName}/issues/1/comments`).reply(200, replyBody)
await run()
})
it('matches multiline messages with windows line feeds against api responses with unix linefeeds', async () => {
it('matches multiline messages with windows line feeds against api responses with unix linefeeds [no user login provided]', async () => {
inputs.message = multilineMessageWindows
inputs['repo-token'] = repoToken
inputs['allow-repeats'] = 'false'
@ -155,16 +159,16 @@ describe('add-pr-comment action', () => {
return originalSetOutput(key, value)
})
nock('https://api.github.com')
.get(`/repos/${repoFullName}/issues/1/comments`)
.reply(200, [
{
body: multilineMessage,
user: {
login: 'github-actions[bot]',
},
const replyBody = [
{
body: multilineMessage,
user: {
login: userLogin,
},
])
},
]
nock('https://api.github.com').get(`/repos/${repoFullName}/issues/1/comments`).reply(200, replyBody)
await run()
})

View file

@ -1,14 +1,17 @@
name: "Add PR Comment"
description: "Add a comment to a PR"
description: "Add a comment to a pull request"
inputs:
message:
description: "the message to print"
description: "The message to print."
required: true
repo-token:
description: "a github token for API access"
required: true
description: "A GitHub token for API access."
required: false
repo-token-user-login:
description: "A user login associated with your token, for temporary repo tokens this is `github-actions[bot]`."
required: false
allow-repeats:
description: "allow messages to be repeated"
description: "Allow messages to be repeated."
required: false
default: "false"
branding:

View file

@ -7,9 +7,10 @@ import {Octokit} from '@octokit/rest'
type ListCommitPullsResponse = Endpoints['GET /repos/:owner/:repo/commits/:commit_sha/pulls']['response']
interface AddPrCommentInputs {
message: string
repoToken: string
allowRepeats: boolean
message: string
repoToken?: string
repoTokenUserLogin?: string
}
interface ListCommitPullsParams {
@ -43,31 +44,37 @@ const getIssueNumberFromCommitPullsList = (commitPullsList: ListCommitPullsRespo
const isMessagePresent = (
message: AddPrCommentInputs['message'],
comments: Octokit.IssuesListCommentsResponse,
login?: string,
): boolean => {
const cleanRe = new RegExp('\\R|\\s', 'g')
const messageClean = message.replace(cleanRe, '')
return comments.some(
({user, body}) =>
// First find candidate bot messages to avoid extra processing
user.login === 'github-actions[bot]' && body.replace(cleanRe, '') === messageClean,
)
return comments.some(({user, body}) => {
// If a username is provided we can save on a bit of processing
if (login && user.login !== login) {
return false
}
return body.replace(cleanRe, '') === messageClean
})
}
const getInputs = (): AddPrCommentInputs => {
return {
message: core.getInput('message'),
repoToken: core.getInput('repo-token'),
allowRepeats: Boolean(core.getInput('allow-repeats') === 'true'),
message: core.getInput('message'),
repoToken: core.getInput('repo-token') || process.env['GITHUB_TOKEN'],
repoTokenUserLogin: core.getInput('repo-token-user-login'),
}
}
const run = async (): Promise<void> => {
try {
const {message, repoToken, allowRepeats} = getInputs()
const {allowRepeats, message, repoToken, repoTokenUserLogin} = getInputs()
core.debug(`input message: ${message}`)
core.debug(`input allow-repeats: ${allowRepeats}`)
if (!repoToken) {
throw new Error('no github token provided, set one with the repo-token input or GITHUB_TOKEN env variable')
}
const {
payload: {pull_request: pullRequest, repository},
@ -112,7 +119,7 @@ const run = async (): Promise<void> => {
issue_number: issueNumber,
})
if (isMessagePresent(message, comments)) {
if (isMessagePresent(message, comments, repoTokenUserLogin)) {
core.info('the issue already contains an identical message')
shouldCreateComment = false
}

2
dist/index.js vendored

File diff suppressed because one or more lines are too long