mirror of
https://github.com/mshick/add-pr-comment.git
synced 2026-01-01 22:49:45 +11:00
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:
parent
150a3ccfe9
commit
f533dc4046
7 changed files with 91 additions and 49 deletions
|
|
@ -20,12 +20,7 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"camelcase": "off",
|
"camelcase": "off",
|
||||||
"@typescript-eslint/camelcase": [
|
"@typescript-eslint/camelcase": "off",
|
||||||
"error",
|
|
||||||
{
|
|
||||||
"properties": "never"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"@typescript-eslint/no-non-null-assertion": "off"
|
"@typescript-eslint/no-non-null-assertion": "off"
|
||||||
},
|
},
|
||||||
"env": {
|
"env": {
|
||||||
|
|
|
||||||
21
.github/workflows/integration.yml
vendored
21
.github/workflows/integration.yml
vendored
|
|
@ -32,9 +32,30 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
npm test
|
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
|
- name: Add Comment
|
||||||
uses: ./
|
uses: ./
|
||||||
with:
|
with:
|
||||||
message: |
|
message: |
|
||||||
**It works!**
|
**It works!**
|
||||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
repo-token-user-login: "github-actions[bot]"
|
||||||
|
|
|
||||||
16
README.md
16
README.md
|
|
@ -1,6 +1,6 @@
|
||||||
# add-pr-comment
|
# 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
|
## Usage
|
||||||
|
|
||||||
|
|
@ -19,6 +19,7 @@ jobs:
|
||||||
🌏
|
🌏
|
||||||
!
|
!
|
||||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
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
|
allow-repeats: false # This is the default
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -35,13 +36,24 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
uses: mshick/add-pr-comment@v1
|
uses: mshick/add-pr-comment@v1
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
with:
|
with:
|
||||||
message: |
|
message: |
|
||||||
**Hello MASTER**
|
**Hello MASTER**
|
||||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
allow-repeats: true
|
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
|
## Features
|
||||||
|
|
||||||
- Fast, runs in the GitHub Actions node.js runtime; no Docker pull needed.
|
- Fast, runs in the GitHub Actions node.js runtime; no Docker pull needed.
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import run from '../add-pr-comment'
|
||||||
|
|
||||||
const repoFullName = 'foo/bar'
|
const repoFullName = 'foo/bar'
|
||||||
const repoToken = '12345'
|
const repoToken = '12345'
|
||||||
|
const userLogin = 'github-actions[bot]'
|
||||||
const commitSha = 'abc123'
|
const commitSha = 'abc123'
|
||||||
const issueNumber = 1
|
const issueNumber = 1
|
||||||
const simpleMessage = 'hello world'
|
const simpleMessage = 'hello world'
|
||||||
|
|
@ -17,6 +18,7 @@ const multilineMessageWindows = fs.readFileSync(path.resolve(__dirname, './messa
|
||||||
const inputs = {
|
const inputs = {
|
||||||
message: '',
|
message: '',
|
||||||
'repo-token': '',
|
'repo-token': '',
|
||||||
|
'repo-token-user-login': '',
|
||||||
'allow-repeats': 'false',
|
'allow-repeats': 'false',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,9 +86,10 @@ describe('add-pr-comment action', () => {
|
||||||
await expect(run()).resolves.not.toThrow()
|
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.message = simpleMessage
|
||||||
inputs['repo-token'] = repoToken
|
|
||||||
inputs['allow-repeats'] = 'true'
|
inputs['allow-repeats'] = 'true'
|
||||||
|
|
||||||
github.context.payload = {
|
github.context.payload = {
|
||||||
|
|
@ -111,9 +114,10 @@ describe('add-pr-comment action', () => {
|
||||||
await run()
|
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.message = simpleMessage
|
||||||
inputs['repo-token'] = repoToken
|
inputs['repo-token'] = repoToken
|
||||||
|
inputs['repo-token-user-login'] = userLogin
|
||||||
inputs['allow-repeats'] = 'false'
|
inputs['allow-repeats'] = 'false'
|
||||||
|
|
||||||
const originalSetOutput = core.setOutput
|
const originalSetOutput = core.setOutput
|
||||||
|
|
@ -126,21 +130,21 @@ describe('add-pr-comment action', () => {
|
||||||
return originalSetOutput(key, value)
|
return originalSetOutput(key, value)
|
||||||
})
|
})
|
||||||
|
|
||||||
nock('https://api.github.com')
|
const replyBody = [
|
||||||
.get(`/repos/${repoFullName}/issues/1/comments`)
|
|
||||||
.reply(200, [
|
|
||||||
{
|
{
|
||||||
body: simpleMessage,
|
body: simpleMessage,
|
||||||
user: {
|
user: {
|
||||||
login: 'github-actions[bot]',
|
login: userLogin,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
])
|
]
|
||||||
|
|
||||||
|
nock('https://api.github.com').get(`/repos/${repoFullName}/issues/1/comments`).reply(200, replyBody)
|
||||||
|
|
||||||
await run()
|
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.message = multilineMessageWindows
|
||||||
inputs['repo-token'] = repoToken
|
inputs['repo-token'] = repoToken
|
||||||
inputs['allow-repeats'] = 'false'
|
inputs['allow-repeats'] = 'false'
|
||||||
|
|
@ -155,16 +159,16 @@ describe('add-pr-comment action', () => {
|
||||||
return originalSetOutput(key, value)
|
return originalSetOutput(key, value)
|
||||||
})
|
})
|
||||||
|
|
||||||
nock('https://api.github.com')
|
const replyBody = [
|
||||||
.get(`/repos/${repoFullName}/issues/1/comments`)
|
|
||||||
.reply(200, [
|
|
||||||
{
|
{
|
||||||
body: multilineMessage,
|
body: multilineMessage,
|
||||||
user: {
|
user: {
|
||||||
login: 'github-actions[bot]',
|
login: userLogin,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
])
|
]
|
||||||
|
|
||||||
|
nock('https://api.github.com').get(`/repos/${repoFullName}/issues/1/comments`).reply(200, replyBody)
|
||||||
|
|
||||||
await run()
|
await run()
|
||||||
})
|
})
|
||||||
|
|
|
||||||
13
action.yml
13
action.yml
|
|
@ -1,14 +1,17 @@
|
||||||
name: "Add PR Comment"
|
name: "Add PR Comment"
|
||||||
description: "Add a comment to a PR"
|
description: "Add a comment to a pull request"
|
||||||
inputs:
|
inputs:
|
||||||
message:
|
message:
|
||||||
description: "the message to print"
|
description: "The message to print."
|
||||||
required: true
|
required: true
|
||||||
repo-token:
|
repo-token:
|
||||||
description: "a github token for API access"
|
description: "A GitHub token for API access."
|
||||||
required: true
|
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:
|
allow-repeats:
|
||||||
description: "allow messages to be repeated"
|
description: "Allow messages to be repeated."
|
||||||
required: false
|
required: false
|
||||||
default: "false"
|
default: "false"
|
||||||
branding:
|
branding:
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,10 @@ import {Octokit} from '@octokit/rest'
|
||||||
type ListCommitPullsResponse = Endpoints['GET /repos/:owner/:repo/commits/:commit_sha/pulls']['response']
|
type ListCommitPullsResponse = Endpoints['GET /repos/:owner/:repo/commits/:commit_sha/pulls']['response']
|
||||||
|
|
||||||
interface AddPrCommentInputs {
|
interface AddPrCommentInputs {
|
||||||
message: string
|
|
||||||
repoToken: string
|
|
||||||
allowRepeats: boolean
|
allowRepeats: boolean
|
||||||
|
message: string
|
||||||
|
repoToken?: string
|
||||||
|
repoTokenUserLogin?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ListCommitPullsParams {
|
interface ListCommitPullsParams {
|
||||||
|
|
@ -43,31 +44,37 @@ const getIssueNumberFromCommitPullsList = (commitPullsList: ListCommitPullsRespo
|
||||||
const isMessagePresent = (
|
const isMessagePresent = (
|
||||||
message: AddPrCommentInputs['message'],
|
message: AddPrCommentInputs['message'],
|
||||||
comments: Octokit.IssuesListCommentsResponse,
|
comments: Octokit.IssuesListCommentsResponse,
|
||||||
|
login?: string,
|
||||||
): boolean => {
|
): boolean => {
|
||||||
const cleanRe = new RegExp('\\R|\\s', 'g')
|
const cleanRe = new RegExp('\\R|\\s', 'g')
|
||||||
const messageClean = message.replace(cleanRe, '')
|
const messageClean = message.replace(cleanRe, '')
|
||||||
|
|
||||||
return comments.some(
|
return comments.some(({user, body}) => {
|
||||||
({user, body}) =>
|
// If a username is provided we can save on a bit of processing
|
||||||
// First find candidate bot messages to avoid extra processing
|
if (login && user.login !== login) {
|
||||||
user.login === 'github-actions[bot]' && body.replace(cleanRe, '') === messageClean,
|
return false
|
||||||
)
|
}
|
||||||
|
|
||||||
|
return body.replace(cleanRe, '') === messageClean
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const getInputs = (): AddPrCommentInputs => {
|
const getInputs = (): AddPrCommentInputs => {
|
||||||
return {
|
return {
|
||||||
message: core.getInput('message'),
|
|
||||||
repoToken: core.getInput('repo-token'),
|
|
||||||
allowRepeats: Boolean(core.getInput('allow-repeats') === 'true'),
|
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> => {
|
const run = async (): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const {message, repoToken, allowRepeats} = getInputs()
|
const {allowRepeats, message, repoToken, repoTokenUserLogin} = getInputs()
|
||||||
|
|
||||||
core.debug(`input message: ${message}`)
|
if (!repoToken) {
|
||||||
core.debug(`input allow-repeats: ${allowRepeats}`)
|
throw new Error('no github token provided, set one with the repo-token input or GITHUB_TOKEN env variable')
|
||||||
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
payload: {pull_request: pullRequest, repository},
|
payload: {pull_request: pullRequest, repository},
|
||||||
|
|
@ -112,7 +119,7 @@ const run = async (): Promise<void> => {
|
||||||
issue_number: issueNumber,
|
issue_number: issueNumber,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (isMessagePresent(message, comments)) {
|
if (isMessagePresent(message, comments, repoTokenUserLogin)) {
|
||||||
core.info('the issue already contains an identical message')
|
core.info('the issue already contains an identical message')
|
||||||
shouldCreateComment = false
|
shouldCreateComment = false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue