From 72c9e313c7347ece7209bc3fa61d083b6b815c50 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Fri, 22 Oct 2021 03:47:37 +0900 Subject: [PATCH 1/7] Add 'pullRequest' field --- src/fields.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/fields.ts b/src/fields.ts index 90059f00..493af0cb 100644 --- a/src/fields.ts +++ b/src/fields.ts @@ -73,6 +73,9 @@ export class FieldFactory { this.includes('workflow') ? createAttachment('workflow', await this.workflow()) : undefined, + this.includes('pullRequest') + ? createAttachment('pullRequest', await this.pullRequest()) + : undefined, ], undefined, ); @@ -193,6 +196,17 @@ export class FieldFactory { return value; } + private async pullRequest(): Promise { + let value; + if (context.eventName === 'pull_request') { + value = `<${context.payload.pull_request?.html_url}|${context.payload.pull_request?.title} #${context.payload.pull_request?.number}>`; + } else { + value = 'n/a'; + } + process.env.AS_PULL_REQUEST = value; + return value; + } + private async action(): Promise { const sha = context.payload.pull_request?.head.sha ?? context.sha; const { owner, repo } = context.repo; From cff1fd442fc187692ca6185db73fc8b82440c20b Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Fri, 22 Oct 2021 06:25:30 +0900 Subject: [PATCH 2/7] Add tests for 'pulRequest' field --- __tests__/client.test.ts | 4 ++-- __tests__/helper.ts | 17 ++++++++++++++++- __tests__/pull_request.test.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/__tests__/client.test.ts b/__tests__/client.test.ts index 90bc8b3a..6eb270ef 100644 --- a/__tests__/client.test.ts +++ b/__tests__/client.test.ts @@ -80,7 +80,7 @@ describe('8398a7/action-slack', () => { ...newWith(), status: Success, fields: - 'repo,message,commit,author,job,action,eventName,ref,workflow,took', + 'repo,message,commit,author,job,action,eventName,ref,workflow,took,pullRequest', }; const client = new Client( withParams, @@ -585,7 +585,7 @@ describe('8398a7/action-slack', () => { ...newWith(), status: Success, fields: - 'repo,message,commit,author,job,action,eventName,ref,workflow,took', + 'repo,message,commit,author,job,action,eventName,ref,workflow,took,pullRequest', }; const client = new Client( withParams, diff --git a/__tests__/helper.ts b/__tests__/helper.ts index 5e843c80..03b53881 100644 --- a/__tests__/helper.ts +++ b/__tests__/helper.ts @@ -3,7 +3,7 @@ import { readFileSync } from 'fs'; import { resolve } from 'path'; import { Field, With } from '../src/client'; import { FieldFactory } from '../src/fields'; -import { getOctokit } from '@actions/github'; +import github, {context, getOctokit} from '@actions/github'; export const gitHubToken = 'github-token'; export const gitHubBaseUrl = ''; @@ -92,6 +92,7 @@ export const fixedFields = (fields: string, sha?: string) => { ff.includes('eventName') ? eventName() : undefined, ff.includes('ref') ? ref() : undefined, ff.includes('workflow') ? workflow(sha) : undefined, + ff.includes('pullRequest') ? pullRequest() : undefined, ], undefined, ); @@ -175,3 +176,17 @@ export const took = (): Field => { value: '1 hour 1 min 1 sec', }; }; + +export const pullRequest = (): Field => { + let value; + if (context.eventName === 'pull_request') { + value = ''; + } else { + value = 'n/a'; + } + return { + short: true, + title: 'pullRequest', + value: value, + }; +}; diff --git a/__tests__/pull_request.test.ts b/__tests__/pull_request.test.ts index adb44587..dd41fd75 100644 --- a/__tests__/pull_request.test.ts +++ b/__tests__/pull_request.test.ts @@ -11,6 +11,7 @@ import { webhookUrl, } from './helper'; import { Client, Success } from '../src/client'; +import github from "@actions/github"; beforeAll(() => { nock.disableNetConnect(); @@ -51,4 +52,34 @@ describe('pull request event', () => { payload.attachments[0].color = 'good'; expect(await client.prepare(msg)).toStrictEqual(payload); }); + + it('pull request information in pullRequest field', async () => { + const github = require('@actions/github'); + const sha = 'expected-sha-for-pull_request_event'; + github.context.payload = { + pull_request: { + html_url: 'https://github.com/8398a7/action-slack/pull/123', + title: 'Add pullRequest field', + number: 123, + head: { sha }, + }, + }; + github.context.eventName = 'pull_request'; + + const withParams = { + ...newWith(), + status: Success, + fields: 'pullRequest', + }; + const client = new Client( + withParams, + gitHubToken, + gitHubBaseUrl, + webhookUrl, + ); + const msg = 'pullRequest test'; + const payload = getTemplate(withParams.fields, msg, sha); + payload.attachments[0].color = 'good'; + expect(await client.prepare(msg)).toStrictEqual(payload); + }); }); From c57d23986c451e70bbccf2b76be43b1b5ba2f735 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Fri, 22 Oct 2021 07:49:55 +0900 Subject: [PATCH 3/7] Add document for 'pullRequest' fiels --- docs/content/fields.md | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/docs/content/fields.md b/docs/content/fields.md index 1bf64aac..74c62d5b 100644 --- a/docs/content/fields.md +++ b/docs/content/fields.md @@ -6,7 +6,7 @@ metaDescription: This explains the values that can be specified in Fields. caution: Additional configuration is required to work with matrix. -Don't forget to add `MATRIX_CONTEXT`. +Don't forget to add `MATRIX_CONTEXT`. Not required if the fields do not contain jobs or tooks. ```yaml @@ -19,22 +19,23 @@ steps: MATRIX_CONTEXT: ${{ toJson(matrix) }} # required ``` -If you have more than one, please enter it in csv format. +If you have more than one, please enter it in csv format. Corresponding types are as follows. success -| Field | Environment Variable | Description | -| --------- | ----------------------- | ------------------------------------------------------------ | -| repo | `AS_REPO` | A working repository name | -| commit | `AS_COMMIT` | commit hash | -| eventName | `AS_EVENT_NAME` | trigger event name | -| ref | `AS_REF` | git refrence | -| workflow | `AS_WORKFLOW` | Generate a workflow link from git sha | -| message | `AS_MESSAGE` | commit message | -| author | `AS_AUTHOR` | The author who pushed | -| job | `AS_JOB` | Generate a job run link of the job that was executed | -| took | `AS_TOOK` | Execution time for the job | +| Field | Environment Variable | Description | +| ----------- | ----------------------- | ----------------------------------------------------------- | +| repo | `AS_REPO` | A working repository name | +| commit | `AS_COMMIT` | commit hash | +| eventName | `AS_EVENT_NAME` | trigger event name | +| ref | `AS_REF` | git refrence | +| workflow | `AS_WORKFLOW` | Generate a workflow link from git sha | +| message | `AS_MESSAGE` | commit message | +| author | `AS_AUTHOR` | The author who pushed | +| job | `AS_JOB` | Generate a job run link of the job that was executed | +| took | `AS_TOOK` | Execution time for the job | +| pullRequest | `AS_PULL_REQUEST` | Pull Request title, number with link | ```yaml steps: From d6f1f16c4efd38c31ec0c323db3bea545a70f145 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Fri, 22 Oct 2021 07:52:04 +0900 Subject: [PATCH 4/7] Fix typo --- docs/content/fields.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/fields.md b/docs/content/fields.md index 74c62d5b..9e266be8 100644 --- a/docs/content/fields.md +++ b/docs/content/fields.md @@ -29,7 +29,7 @@ Corresponding types are as follows. | repo | `AS_REPO` | A working repository name | | commit | `AS_COMMIT` | commit hash | | eventName | `AS_EVENT_NAME` | trigger event name | -| ref | `AS_REF` | git refrence | +| ref | `AS_REF` | git reference | | workflow | `AS_WORKFLOW` | Generate a workflow link from git sha | | message | `AS_MESSAGE` | commit message | | author | `AS_AUTHOR` | The author who pushed | From bd4982e5f220ac61545579c57bcc038a37eaccbf Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Fri, 22 Oct 2021 08:12:07 +0900 Subject: [PATCH 5/7] Apply format by prettier --- __tests__/helper.ts | 5 +++-- __tests__/pull_request.test.ts | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/__tests__/helper.ts b/__tests__/helper.ts index 03b53881..b658d6b2 100644 --- a/__tests__/helper.ts +++ b/__tests__/helper.ts @@ -3,7 +3,7 @@ import { readFileSync } from 'fs'; import { resolve } from 'path'; import { Field, With } from '../src/client'; import { FieldFactory } from '../src/fields'; -import github, {context, getOctokit} from '@actions/github'; +import github, { context, getOctokit } from '@actions/github'; export const gitHubToken = 'github-token'; export const gitHubBaseUrl = ''; @@ -180,7 +180,8 @@ export const took = (): Field => { export const pullRequest = (): Field => { let value; if (context.eventName === 'pull_request') { - value = ''; + value = + ''; } else { value = 'n/a'; } diff --git a/__tests__/pull_request.test.ts b/__tests__/pull_request.test.ts index dd41fd75..e54dfe87 100644 --- a/__tests__/pull_request.test.ts +++ b/__tests__/pull_request.test.ts @@ -11,7 +11,7 @@ import { webhookUrl, } from './helper'; import { Client, Success } from '../src/client'; -import github from "@actions/github"; +import github from '@actions/github'; beforeAll(() => { nock.disableNetConnect(); @@ -72,10 +72,10 @@ describe('pull request event', () => { fields: 'pullRequest', }; const client = new Client( - withParams, - gitHubToken, - gitHubBaseUrl, - webhookUrl, + withParams, + gitHubToken, + gitHubBaseUrl, + webhookUrl, ); const msg = 'pullRequest test'; const payload = getTemplate(withParams.fields, msg, sha); From 841db889f9c1ad1f7ed640488c85251e7dcd5e3f Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 26 Oct 2021 07:53:37 +0900 Subject: [PATCH 6/7] Fix pullRequest field on pull_request event variation Add support variations: - pull_request_review - pull_request_review_comment - pull_request_target --- __tests__/helper.ts | 2 +- __tests__/pull_request.test.ts | 15 +++++++++++++-- src/fields.ts | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/__tests__/helper.ts b/__tests__/helper.ts index b658d6b2..8a4aada1 100644 --- a/__tests__/helper.ts +++ b/__tests__/helper.ts @@ -179,7 +179,7 @@ export const took = (): Field => { export const pullRequest = (): Field => { let value; - if (context.eventName === 'pull_request') { + if (context.eventName.startsWith('pull_request')) { value = ''; } else { diff --git a/__tests__/pull_request.test.ts b/__tests__/pull_request.test.ts index e54dfe87..de6285ad 100644 --- a/__tests__/pull_request.test.ts +++ b/__tests__/pull_request.test.ts @@ -52,8 +52,16 @@ describe('pull request event', () => { payload.attachments[0].color = 'good'; expect(await client.prepare(msg)).toStrictEqual(payload); }); +}); - it('pull request information in pullRequest field', async () => { +describe.each` + eventName + ${`pull_request`} + ${`pull_request_review`} + ${`pull_request_review_comment`} + ${`pull_request_target`} +`('pullRequest field on pull_request events', ({ eventName }) => { + test(`${eventName}`, async () => { const github = require('@actions/github'); const sha = 'expected-sha-for-pull_request_event'; github.context.payload = { @@ -64,7 +72,7 @@ describe('pull request event', () => { head: { sha }, }, }; - github.context.eventName = 'pull_request'; + github.context.eventName = eventName; const withParams = { ...newWith(), @@ -81,5 +89,8 @@ describe('pull request event', () => { const payload = getTemplate(withParams.fields, msg, sha); payload.attachments[0].color = 'good'; expect(await client.prepare(msg)).toStrictEqual(payload); + expect(process.env.AS_PULL_REQUEST).toStrictEqual( + '', + ); }); }); diff --git a/src/fields.ts b/src/fields.ts index 493af0cb..04cd2993 100644 --- a/src/fields.ts +++ b/src/fields.ts @@ -198,7 +198,7 @@ export class FieldFactory { private async pullRequest(): Promise { let value; - if (context.eventName === 'pull_request') { + if (context.eventName.startsWith('pull_request')) { value = `<${context.payload.pull_request?.html_url}|${context.payload.pull_request?.title} #${context.payload.pull_request?.number}>`; } else { value = 'n/a'; From 83b844f3df6461828f13538459321127ecfdcba7 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Thu, 11 Nov 2021 20:46:08 +0900 Subject: [PATCH 7/7] Fix docs --- README.md | 4 ++-- docs/content/usecase/01-general.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c91200dc..f3b56191 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ steps: - uses: 8398a7/action-slack@v3 with: status: ${{ job.status }} - fields: repo,message,commit,author,action,eventName,ref,workflow,job,took # selectable (default: repo,message) + fields: repo,message,commit,author,action,eventName,ref,workflow,job,took,pullRequest # selectable (default: repo,message) env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required if: always() # Pick up events even if the job fails or is canceled. @@ -57,7 +57,7 @@ steps: with: github_base_url: https://your.ghe.com # Specify your GHE status: ${{ job.status }} - fields: repo,message,commit,author,action,eventName,ref,workflow,job,took + fields: repo,message,commit,author,action,eventName,ref,workflow,job,took,pullRequest env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} if: always() diff --git a/docs/content/usecase/01-general.md b/docs/content/usecase/01-general.md index 7bf4e6f8..c87e9059 100644 --- a/docs/content/usecase/01-general.md +++ b/docs/content/usecase/01-general.md @@ -11,13 +11,13 @@ steps: - uses: 8398a7/action-slack@v3 with: status: ${{ job.status }} - fields: repo,message,commit,author,action,eventName,ref,workflow,job,took # selectable (default: repo,message) + fields: repo,message,commit,author,action,eventName,ref,workflow,job,took,pullRequest # selectable (default: repo,message) env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required if: always() # Pick up events even if the job fails or is canceled. ``` -`status: ${{ job.status }}` allows a job to succeed, fail or cancel etc. to action-slack. +`status: ${{ job.status }}` allows a job to succeed, fail or cancel etc. to action-slack. `if: always()` to trigger action-slack even if the job fails Let them. For the fields, look at [Fields](/fields) to determine what you want.