Skip to content

Commit

Permalink
Merge branch 'main' of github.com:rockem/create-opsgenie-alert-action
Browse files Browse the repository at this point in the history
  • Loading branch information
rockem committed Jun 13, 2024
2 parents 8a3dd08 + 3ba6903 commit ba2be58
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 20 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/assert_alert_created_with.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
}
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,6 +55,7 @@ jobs:
priority: P5
tags: kuku
using_eu_url: false
responders: ${{ env.RESPONDER }}

- name: Assert all values alert created
env:
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: \<id type>:\<id>:\<responder type>)
### Outputs
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
28 changes: 19 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
? []
Expand All @@ -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,
};
Expand Down Expand Up @@ -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);
}
});
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
26 changes: 18 additions & 8 deletions src/alert.js
Original file line number Diff line number Diff line change
@@ -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
? []
Expand All @@ -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,
};

0 comments on commit ba2be58

Please sign in to comment.