diff --git a/.github/workflows/assert_alert_created_with.js b/.github/workflows/assert_alert_created_with.js index c366752..1b2ee2f 100755 --- a/.github/workflows/assert_alert_created_with.js +++ b/.github/workflows/assert_alert_created_with.js @@ -10,6 +10,7 @@ const description = process.argv[5]; const priority = process.argv[6]; const tag = process.argv[7]; const source = process.argv[8]; +const responder = process.argv[9]; opsgenie.configure({ api_key: process.env.OPSGENIE_API_KEY, @@ -36,10 +37,18 @@ function assert_created_alert(alert) { if (tag) { expect(alert.data.tags).to.include(tag); } + if (responder) { + // Sadly we can't validate responders with a free account + } compare_optional_str(alert.data.source, source); } -opsgenie.alertV2.getRequestStatus(request_id, function (_, status) { +opsgenie.alertV2.getRequestStatus(request_id, function (error, status) { + if (error) { + throw new Error( + `Failed to get status for alert with error: ${error.message}`, + ); + } if (!status.data.success) { throw new Error("Alert wasn't (yet?) created"); } diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2ce71cd..12f900b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,11 +35,13 @@ jobs: export REQUIRED_VALUES_MESSAGE="Alert created with required values" export DESCRIPTION="alert description" export SOURCE="create alert integration test" + export RESPONDER="name:NOC:team" echo "ALERT_ALIAS=$(echo -n $ALIAS)" >> $GITHUB_ENV echo "ALL_VALUES_MESSAGE=$(echo -n ALL_VALUES_MESSAGE)" >> $GITHUB_ENV echo "REQUIRED_VALUES_MESSAGE=$(echo -n REQUIRED_VALUES_MESSAGE)" >> $GITHUB_ENV echo "DESCRIPTION=$(echo -n $DESCRIPTION)" >> $GITHUB_ENV echo "SOURCE=$(echo -n $SOURCE)" >> $GITHUB_ENV + echo "RESPONDER=$(echo -n $RESPONDER)" >> $GITHUB_ENV - name: Create OpsGenie Alert with All Values id: create-alert-all-values @@ -53,6 +55,7 @@ jobs: priority: P5 tags: kuku using_eu_url: false + responders: ${{ env.RESPONDER }} - name: Assert all values alert created env: @@ -65,7 +68,8 @@ jobs: "${{ env.DESCRIPTION }}" \ P5 \ kuku \ - "${{ env.SOURCE }}" + "${{ env.SOURCE }}" \ + "${{ env.RESPONDER }}" - name: Create OpsGenie Alert with Required Values id: create-alert-required-values diff --git a/README.md b/README.md index 6a003e4..cf8049f 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ This GitHub Action allows you to create alerts in [OpsGenie](https://www.atlassi - **\`tags\`**: Tags of the alert, separated by commas. - **\`priority\`**: Priority level of the alert. Possible values are P1, P2, P3, P4 and P5. Default value is P3. - **\`using_eu_url\`**: Set the action to use OpsGenie europe endpoint 'https://api.eu.opsgenie.com'. Defaults to false +- **\`responders\`**: List of comma seperated targets that the alert will be routed to. (each target should be in the format: \:\:\) ### Outputs diff --git a/action.yml b/action.yml index 46ded05..a5ba0e4 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,9 @@ inputs: description: Boolean, true if using Opsgenie EU API endpoint required: false default: "false" + responders: + description: List of targets that the alert will be routed to + required: false runs: using: "node16" main: "dist/index.js" diff --git a/dist/index.js b/dist/index.js index 45c49bb..1365e8f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -78956,6 +78956,19 @@ define(function () { /***/ 4199: /***/ ((module) => { +const createAlertRequestFrom = (alertDetails) => { + const request = {}; + + Object.assign(request, alertDetails, { + tags: createArrayFrom(alertDetails.tags), + responders: createArrayFrom(alertDetails.responders).map( + createResponderObjFrom, + ), + }); + + return withoutEmptyProperties(request); +}; + function createArrayFrom(tags) { return !tags ? [] @@ -78964,18 +78977,15 @@ function createArrayFrom(tags) { }); } +function createResponderObjFrom(responderStr) { + const parts = responderStr.split(":"); + return { [parts[0]]: parts[1], type: parts[2] }; +} + function withoutEmptyProperties(obj) { return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v !== "")); } -const createAlertRequestFrom = (alertDetails) => { - const request = {}; - Object.assign(request, alertDetails, { - tags: createArrayFrom(alertDetails.tags), - }); - return withoutEmptyProperties(request); -}; - module.exports = { createAlertRequestFrom, }; @@ -81135,7 +81145,7 @@ opsgenie.alertV2.create(alertRequest, function (error, result) { if (error) { core.setFailed(error.message); } else { - console.log(`Request sent for creating new alert: ${alertRequest.message}`); + console.log(`Request sent for creating new alert: ${result.requestId}`); core.setOutput("request_id", result.requestId); } }); diff --git a/index.js b/index.js index c7972f5..28dc8ba 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ opsgenie.alertV2.create(alertRequest, function (error, result) { if (error) { core.setFailed(error.message); } else { - console.log(`Request sent for creating new alert: ${alertRequest.message}`); + console.log(`Request sent for creating new alert: ${result.requestId}`); core.setOutput("request_id", result.requestId); } }); diff --git a/src/alert.js b/src/alert.js index e0aaf85..bc504c8 100644 --- a/src/alert.js +++ b/src/alert.js @@ -1,3 +1,16 @@ +const createAlertRequestFrom = (alertDetails) => { + const request = {}; + + Object.assign(request, alertDetails, { + tags: createArrayFrom(alertDetails.tags), + responders: createArrayFrom(alertDetails.responders).map( + createResponderObjFrom, + ), + }); + + return withoutEmptyProperties(request); +}; + function createArrayFrom(tags) { return !tags ? [] @@ -6,18 +19,15 @@ function createArrayFrom(tags) { }); } +function createResponderObjFrom(responderStr) { + const parts = responderStr.split(":"); + return { [parts[0]]: parts[1], type: parts[2] }; +} + function withoutEmptyProperties(obj) { return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v !== "")); } -const createAlertRequestFrom = (alertDetails) => { - const request = {}; - Object.assign(request, alertDetails, { - tags: createArrayFrom(alertDetails.tags), - }); - return withoutEmptyProperties(request); -}; - module.exports = { createAlertRequestFrom, };