Skip to content

Commit

Permalink
Merge pull request #9 from stempler/feat/custom-event
Browse files Browse the repository at this point in the history
feat: support custom change events
  • Loading branch information
adamvaughan authored May 31, 2022
2 parents 4e3e22c + 59cec62 commit 25417ab
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
```
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 25417ab

Please sign in to comment.