diff --git a/.github/workflows/slack-pre.yml b/.github/workflows/slack-pre.yml new file mode 100644 index 00000000..c17d263c --- /dev/null +++ b/.github/workflows/slack-pre.yml @@ -0,0 +1,57 @@ +name: Slack Checks +on: + push: + branches: + - pre + +jobs: + notification: + runs-on: ubuntu-latest + steps: + - name: Succeeded Check + uses: 8398a7/action-slack@pre + with: + type: success + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + - name: Failed Check + uses: 8398a7/action-slack@pre + with: + type: failure + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + - name: Custom Field Check + uses: 8398a7/action-slack@pre + with: + payload: | + { + "text": "Custom Field Check", + "attachments": [{ + "author_name": "slack-actions", + "fallback": "fallback", + "color": "good", + "title": "CI Result", + "text": "Succeeded", + "fields": [{ + "title": "short title1", + "value": "short value1", + "short": true + }, + { + "title": "short title2", + "value": "short value2", + "short": true + }, + { + "title": "long title1", + "value": "long value1", + "short": false + }], + "actions": [{ + }] + }] + } + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} diff --git a/.github/workflows/slack.yml b/.github/workflows/slack-stable.yml similarity index 93% rename from .github/workflows/slack.yml rename to .github/workflows/slack-stable.yml index 47bc26d5..33ddd88f 100644 --- a/.github/workflows/slack.yml +++ b/.github/workflows/slack-stable.yml @@ -3,7 +3,6 @@ on: push: branches: - master - - v0 - v1 jobs: @@ -15,12 +14,14 @@ jobs: with: type: success env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} - name: Failed Check uses: 8398a7/action-slack@v1 with: type: failure env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} - name: Custom Field Check uses: 8398a7/action-slack@v1 diff --git a/README.md b/README.md index a61e0e1c..2157931f 100644 --- a/README.md +++ b/README.md @@ -13,43 +13,47 @@ See [action.yml](action.yml), [checkin.yml](.github/workflows/checkin.yml) ### Succeeded Notification -![](https://user-images.githubusercontent.com/8043276/63113235-10a59a00-bfcd-11e9-83be-2dd8662c9ebb.png) +![](https://user-images.githubusercontent.com/8043276/63276385-3e952200-c2de-11e9-9f07-7e0fc4cf8d3a.png) ```yaml - uses: 8398a7/action-slack@v1 with: type: success env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required - uses: 8398a7/action-slack@v1 with: type: success text: overwrite text env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required ``` -### Failed Notification +### Failure Notification -![](https://user-images.githubusercontent.com/8043276/63113244-14392100-bfcd-11e9-962b-03a19ba86680.png) +![](https://user-images.githubusercontent.com/8043276/63276493-6edcc080-c2de-11e9-97df-861d6564a888.png) ```yaml - uses: 8398a7/action-slack@v1 with: type: failure env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required - uses: 8398a7/action-slack@v1 with: type: failure - failedMention: channel + failedMention: channel # The default is here. No mention if empty character specified. env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required ``` ### Custom Notification -![](https://user-images.githubusercontent.com/8043276/63113021-9f65e700-bfcc-11e9-97cf-9a962c7ce611.png) +![](https://user-images.githubusercontent.com/8043276/63276528-86b44480-c2de-11e9-9ad9-42a33d638c4c.png) ```yaml - uses: 8398a7/action-slack@v0 @@ -83,7 +87,7 @@ See [action.yml](action.yml), [checkin.yml](.github/workflows/checkin.yml) }] } env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required ``` ### Auto Notification diff --git a/src/main.ts b/src/main.ts index e22bb116..ef7ba0c7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,10 +16,10 @@ async function run() { case 'auto': throw new Error('not implement'); case 'success': - payload = successPayload(text); + payload = await successPayload(text); break; case 'failure': - payload = failurePayload(text, failedMention); + payload = await failurePayload(text, failedMention); break; default: payload = JSON.parse(core.getInput('payload')); diff --git a/src/slack.ts b/src/slack.ts index a14fb37e..21729b48 100644 --- a/src/slack.ts +++ b/src/slack.ts @@ -9,7 +9,14 @@ export async function Send(payload: IncomingWebhookSendArguments) { core.debug('send message'); } -export function successPayload(text: string) { +const client = new github.GitHub(process.env.GITHUB_TOKEN as string); + +export async function successPayload(text: string) { + const { sha } = github.context; + const { owner, repo } = github.context.repo; + const commit = await client.repos.getCommit({ owner, repo, ref: sha }); + const { author } = commit.data.commit; + const payload: IncomingWebhookSendArguments = { text: 'Succeeded Workflow', attachments: [ @@ -17,9 +24,18 @@ export function successPayload(text: string) { color: 'good', author_name: 'action-slack', fields: [ - { title: 'repo', value: github.context.repo.repo, short: true }, - { title: 'sha', value: github.context.sha, short: true }, - { title: 'actor', value: github.context.actor, short: true }, + { title: 'repo', value: repo, short: true }, + { title: 'message', value: commit.data.commit.message, short: true }, + { + title: 'commit', + value: ``, + short: true, + }, + { + title: 'author', + value: `${author.name}<${author.email}>`, + short: true, + }, { title: 'eventName', value: github.context.eventName, short: true }, { title: 'ref', value: github.context.ref, short: true }, { title: 'workflow', value: github.context.workflow, short: true }, @@ -33,8 +49,8 @@ export function successPayload(text: string) { return payload; } -export function failurePayload(text: string, mention: string) { - const payload: IncomingWebhookSendArguments = successPayload(text); +export async function failurePayload(text: string, mention: string) { + const payload: IncomingWebhookSendArguments = await successPayload(text); payload.text = ''; if (mention !== '') { payload.text = ` `;