From 59cec6241164b05392db4d0bcfb7c1670a736583 Mon Sep 17 00:00:00 2001 From: Simon Templer Date: Tue, 10 May 2022 10:47:12 +0200 Subject: [PATCH] feat: support custom change events If a custom summary is provided the existing logic is ignored and the provided summary is used. Also, a link to the run is added to the change event. --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ action.yml | 3 +++ index.js | 26 +++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ea7b6d8..493ea85 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ workflow configuration. **Required** The integration key that identifies the PagerDuty service the change was made to, added as a GitHub secret for the repository. +### `custom-event` + +Custom event summary. If provided the GitHub event type is ignored and the given summary used. A link to the run is included in the change event. + ## Example usage ```yaml @@ -43,3 +47,39 @@ jobs: with: integration-key: ${{ secrets.PAGERDUTY_CHANGE_INTEGRATION_KEY }} ``` + +### Custom event + +Custom events can for instance be used for notifying about the result of a job: + +```yaml +on: + push: + branches: + - master + - main + +jobs: + deploy: + runs-on: ubuntu-latest + name: Deploying the application (dummy) + steps: + - name: Dummy step + run: echo "Dummy deployment" + + notification: + runs-on: ubuntu-latest + name: Notify PagerDuty + needs: [deploy] + if: always() + steps: + # make deploy job status available + # see https://github.com/marketplace/actions/workflow-status-action + - uses: martialonline/workflow-status@v3 + id: check + - name: Create a change event + uses: PagerDuty/pagerduty-change-events-action@master + with: + integration-key: ${{ secrets.PAGERDUTY_CHANGE_INTEGRATION_KEY }} + custom-event: Deployment ${{ steps.check.outputs.status }} +``` diff --git a/action.yml b/action.yml index e9495a0..dce2586 100644 --- a/action.yml +++ b/action.yml @@ -4,6 +4,9 @@ inputs: integration-key: description: 'The integration key that identifies the service the change was made to.' required: true + custom-event: + description: 'Custom event summary. If provided the GitHub event type is ignored and the given summary used. A link to the run is included in the event.' + required: false runs: using: 'node12' main: 'index.js' diff --git a/index.js b/index.js index 6088f3f..cb7e3a7 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,26 @@ async function sendChangeEvent(changeEvent) { } } +function handleCustomEvent(summary, integrationKey) { + const changeEvent = { + routing_key: integrationKey, + payload: { + summary: summary, + source: 'GitHub', + timestamp: (new Date()).toISOString(), + custom_details: {} + }, + links: [ + { + href: `https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`, + text: "View run" + } + ] + }; + + sendChangeEvent(changeEvent); +} + function handlePushEvent(data, integrationKey) { const { ref, @@ -125,9 +145,13 @@ function handlePullRequestEvent(data, integrationKey) { try { const integrationKey = core.getInput('integration-key'); + const customEvent = core.getInput('custom-event'); const data = github.context.payload; - if (github.context.eventName === 'push') { + if (typeof customEvent === 'string') { + // if custom event is described, prefer emitting custom event + handleCustomEvent(customEvent, integrationKey); + } else if (github.context.eventName === 'push') { handlePushEvent(data, integrationKey); } else if (github.context.eventName === 'pull_request' && data.action === 'closed' && data.pull_request.merged) { handlePullRequestEvent(data, integrationKey);