Create a comment in exist PR (#17)

* Without pullRequest

* Try

* Use the last octokit

* Fix

* Update tests

* Fix

* Update package-lock.json
This commit is contained in:
Aleksey Leshko 2020-07-06 23:48:01 +08:00 committed by GitHub
parent 3033f06feb
commit 1fbf288a23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 16 deletions

View file

@ -5,12 +5,13 @@ import * as github from '@actions/github'
import {WebhookPayload} from '@actions/github/lib/interfaces' import {WebhookPayload} from '@actions/github/lib/interfaces'
import nock from 'nock' import nock from 'nock'
import run from '../add-pr-comment' import run from '../add-pr-comment'
import apiResponse from '../docs/sample-pulls-api-response.json'
const repoFullName = 'foo/bar' const repoFullName = 'foo/bar'
const repoToken = '12345' const repoToken = '12345'
const userLogin = 'github-actions[bot]' const userLogin = 'github-actions[bot]'
const commitSha = 'abc123' const commitSha = 'abc123'
const issueNumber = 1 let issueNumber = 1
const simpleMessage = 'hello world' const simpleMessage = 'hello world'
const multilineMessage = fs.readFileSync(path.resolve(__dirname, './message-windows.txt')).toString() const multilineMessage = fs.readFileSync(path.resolve(__dirname, './message-windows.txt')).toString()
const multilineMessageWindows = fs.readFileSync(path.resolve(__dirname, './message-windows.txt')).toString() const multilineMessageWindows = fs.readFileSync(path.resolve(__dirname, './message-windows.txt')).toString()
@ -23,6 +24,7 @@ const inputs = {
} }
beforeEach(() => { beforeEach(() => {
issueNumber = 1
jest.resetModules() jest.resetModules()
jest.spyOn(core, 'getInput').mockImplementation((name: string): string => { jest.spyOn(core, 'getInput').mockImplementation((name: string): string => {
switch (name) { switch (name) {
@ -86,6 +88,44 @@ describe('add-pr-comment action', () => {
await expect(run()).resolves.not.toThrow() await expect(run()).resolves.not.toThrow()
}) })
it('creates a comment in an existing PR', async () => {
process.env['GITHUB_TOKEN'] = repoToken
inputs.message = simpleMessage
inputs['repo-token'] = repoToken
inputs['allow-repeats'] = 'true'
github.context.payload = {
...github.context.payload,
pull_request: {
number: 0,
},
} as WebhookPayload
const originalSetOutput = core.setOutput
jest.spyOn(core, 'setOutput').mockImplementation((key: string, value: string): void => {
if (key === 'comment-created') {
expect(value).toBe('true')
}
return originalSetOutput(key, value)
})
issueNumber = apiResponse.result[0].number
nock('https://api.github.com')
.get(`/repos/${repoFullName}/commits/${commitSha}/pulls`)
.reply(200, apiResponse.result)
nock('https://api.github.com')
.post(`/repos/${repoFullName}/issues/${issueNumber}/comments`, ({body}) => body === simpleMessage)
.reply(200, {
url: 'https://github.com/#example',
})
await expect(run()).resolves.not.toThrow()
})
it('safely exits when no issue can be found [using GITHUB_TOKEN in env]', async () => { it('safely exits when no issue can be found [using GITHUB_TOKEN in env]', async () => {
process.env['GITHUB_TOKEN'] = repoToken process.env['GITHUB_TOKEN'] = repoToken

View file

@ -4,7 +4,7 @@ import {HttpClient} from '@actions/http-client'
import {Endpoints, RequestHeaders} from '@octokit/types' import {Endpoints, RequestHeaders} from '@octokit/types'
import {Octokit} from '@octokit/rest' 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']['data']
interface AddPrCommentInputs { interface AddPrCommentInputs {
allowRepeats: boolean allowRepeats: boolean
@ -39,7 +39,7 @@ const listCommitPulls = async (params: ListCommitPullsParams): Promise<ListCommi
} }
const getIssueNumberFromCommitPullsList = (commitPullsList: ListCommitPullsResponse): number | null => const getIssueNumberFromCommitPullsList = (commitPullsList: ListCommitPullsResponse): number | null =>
commitPullsList.data && commitPullsList.data.length ? commitPullsList.data[0].number : null commitPullsList.length ? commitPullsList[0].number : null
const isMessagePresent = ( const isMessagePresent = (
message: AddPrCommentInputs['message'], message: AddPrCommentInputs['message'],

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

17
package-lock.json generated
View file

@ -844,10 +844,7 @@
"@octokit/auth-token": { "@octokit/auth-token": {
"version": "2.4.1", "version": "2.4.1",
"resolved": "https://registry.npmjs.org/@octokit/auth-token/-/auth-token-2.4.1.tgz", "resolved": "https://registry.npmjs.org/@octokit/auth-token/-/auth-token-2.4.1.tgz",
"integrity": "sha512-NB81O5h39KfHYGtgfWr2booRxp2bWOJoqbWwbyUg2hw6h35ArWYlAST5B3XwAkbdcx13yt84hFXyFP5X0QToWA==", "integrity": "sha512-NB81O5h39KfHYGtgfWr2booRxp2bWOJoqbWwbyUg2hw6h35ArWYlAST5B3XwAkbdcx13yt84hFXyFP5X0QToWA=="
"requires": {
"@octokit/types": "^4.0.1"
}
}, },
"@octokit/endpoint": { "@octokit/endpoint": {
"version": "6.0.1", "version": "6.0.1",
@ -880,7 +877,6 @@
"integrity": "sha512-StJWfn0M1QfhL3NKBz31e1TdDNZrHLLS57J2hin92SIfzlOVBuUaRkp31AGkGOAFOAVtyEX6ZiZcsjcJDjeb5g==", "integrity": "sha512-StJWfn0M1QfhL3NKBz31e1TdDNZrHLLS57J2hin92SIfzlOVBuUaRkp31AGkGOAFOAVtyEX6ZiZcsjcJDjeb5g==",
"requires": { "requires": {
"@octokit/request": "^5.3.0", "@octokit/request": "^5.3.0",
"@octokit/types": "^4.0.1",
"universal-user-agent": "^5.0.0" "universal-user-agent": "^5.0.0"
} }
}, },
@ -971,7 +967,6 @@
"resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-2.0.1.tgz", "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-2.0.1.tgz",
"integrity": "sha512-5lqBDJ9/TOehK82VvomQ6zFiZjPeSom8fLkFVLuYL3sKiIb5RB8iN/lenLkY7oBmyQcGP7FBMGiIZTO8jufaRQ==", "integrity": "sha512-5lqBDJ9/TOehK82VvomQ6zFiZjPeSom8fLkFVLuYL3sKiIb5RB8iN/lenLkY7oBmyQcGP7FBMGiIZTO8jufaRQ==",
"requires": { "requires": {
"@octokit/types": "^4.0.1",
"deprecation": "^2.0.0", "deprecation": "^2.0.0",
"once": "^1.4.0" "once": "^1.4.0"
} }
@ -1033,9 +1028,10 @@
} }
}, },
"@octokit/types": { "@octokit/types": {
"version": "4.0.1", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/@octokit/types/-/types-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@octokit/types/-/types-5.0.1.tgz",
"integrity": "sha512-Ho6h7w2h9y8RRE8r656hIj1oiSbwbIHJGF5r9G5FOwS2VdDPq8QLGvsG4x6pKHpvyGK7j+43sAc2cJKMiFoIJw==", "integrity": "sha512-GorvORVwp244fGKEt3cgt/P+M0MGy4xEDbckw+K5ojEezxyMDgCaYPKVct+/eWQfZXOT7uq0xRpmrl/+hliabA==",
"dev": true,
"requires": { "requires": {
"@types/node": ">= 8" "@types/node": ">= 8"
} }
@ -1164,7 +1160,8 @@
"@types/node": { "@types/node": {
"version": "12.12.42", "version": "12.12.42",
"resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.42.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.42.tgz",
"integrity": "sha512-R/9QdYFLL9dE9l5cWWzWIZByVGFd7lk7JVOJ7KD+E1SJ4gni7XJRLz9QTjyYQiHIqEAgku9VgxdLjMlhhUaAFg==" "integrity": "sha512-R/9QdYFLL9dE9l5cWWzWIZByVGFd7lk7JVOJ7KD+E1SJ4gni7XJRLz9QTjyYQiHIqEAgku9VgxdLjMlhhUaAFg==",
"dev": true
}, },
"@types/normalize-package-data": { "@types/normalize-package-data": {
"version": "2.4.0", "version": "2.4.0",

View file

@ -33,7 +33,7 @@
"@actions/http-client": "^1.0.8" "@actions/http-client": "^1.0.8"
}, },
"devDependencies": { "devDependencies": {
"@octokit/types": "^4.0.1", "@octokit/types": "^5.0.1",
"@types/jest": "^25.2.3", "@types/jest": "^25.2.3",
"@types/node": "^12.12.42", "@types/node": "^12.12.42",
"@typescript-eslint/eslint-plugin": "^3.0.0", "@typescript-eslint/eslint-plugin": "^3.0.0",

View file

@ -10,7 +10,8 @@
"noUnusedParameters": true, "noUnusedParameters": true,
"noImplicitAny": true, "noImplicitAny": true,
"removeComments": false, "removeComments": false,
"preserveConstEnums": true "preserveConstEnums": true,
"resolveJsonModule": true
}, },
"include": ["**/*.ts"], "include": ["**/*.ts"],
"exclude": ["node_modules"] "exclude": ["node_modules"]