Skip to content

Commit

Permalink
Merge pull request #14 from neonidian/feature/#9-add-tests
Browse files Browse the repository at this point in the history
Feature/#9 add more tests, refactor
  • Loading branch information
neonidian authored Apr 10, 2022
2 parents af441b5 + 1f2f34d commit b8fc57f
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Check lint
run: npm run lint

- name: Run unit tests
run: npm test

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ See the actions tab in your GitHub repository for runs of this action! :rocket:
| 2 |message | Yes | Message to be sent |
| 3 |status | No | [Status](https://docs.github.com/en/actions/learn-github-actions/expressions#status-check-functions) of a step or a job, or a custom text |
| # | Environment variable | Default value | Description |
|-----|-----------------------------------|---------------|--------------------------------------------------------------------|
| 1 | SHOULD_DISPLAY_VIEW_RUN_BUTTON | false | Clicking on this button redirects to the action run page in GitHub |
| 2 | SHOULD_DISPLAY_VIEW_COMMIT_BUTTON | false | Clicking on this button redirects to SHA commit page in GitHub |
| # | Environment variable | Allowed values | Default value | Description |
|-----|-----------------------------------|-------------------|---------------|--------------------------------------------------------------------|
| 1 | SHOULD_DISPLAY_VIEW_RUN_BUTTON | 'true' or 'false' | false | Clicking on this button redirects to the action run page in GitHub |
| 2 | SHOULD_DISPLAY_VIEW_COMMIT_BUTTON | 'true' or 'false' | false | Clicking on this button redirects to SHA commit page in GitHub |
## Examples
Expand Down
40 changes: 21 additions & 19 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ async function run() {
const message = core.getInput('message', { required: true });
const status = core.getInput('status');

const options = {
status,
};
await main(webhookUrl, message, options);
await main(webhookUrl, message, { status, });
core.notice('Message has been sent to Teams');
} catch (error) {
core.setFailed(error.message);
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const postRequest = require('./requests/post-request');
const constructPayLoad = require("./payload/payload");

let main = function (webhookUrl, message, options) {
let main = function (webhookUrl, message, { status, }) {
return new Promise((resolve) => {
validateUrl(webhookUrl);
const requestPayload = constructPayLoad(message, options);
const requestPayload = constructPayLoad(message, { status, });
return postRequest(webhookUrl, requestPayload)
.then(responseData => resolve(responseData));
});
Expand Down
2 changes: 1 addition & 1 deletion src/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ const main = require('./main');

describe('Main:', () => {
test('Throws when not a valid webhook url', async () => {
await expect(main('foo', '')).rejects.toThrow('Webhook url is not a valid url');
await expect(main('foo', '', { status: '' })).rejects.toThrow('Webhook url is not a valid url');
});
});
27 changes: 16 additions & 11 deletions src/payload/customize-card/CustomizeCard.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const envs = require('./envs');

class CustomizeCard {
constructor(message, options) {
constructor(message, { status, }) {
this.message = message;
this.options = options;
this.status = status;
}

_constructJson() {
Expand All @@ -15,7 +15,7 @@ class CustomizeCard {
"contentType": "application/vnd.microsoft.card.adaptive",
"contentUrl": null,
"content": {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"$schema": "https://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.2",
"msteams": {
Expand All @@ -24,7 +24,7 @@ class CustomizeCard {
"body": [
{
"type": "RichTextBlock",
"isVisible": !!this.options?.status,
"isVisible": this.status !== '',
"inlines": [
{
"type": "TextRun",
Expand All @@ -34,9 +34,9 @@ class CustomizeCard {
},
{
"type": "TextRun",
"text": this.options?.status,
"text": this.status ?? '',
"wrap": true,
"color": !!this.options?.status && this._statusColour(this.options?.status),
"color": this._statusColour(this.status),
"weight": "bolder",
"fontType": "monospace"
}
Expand Down Expand Up @@ -96,7 +96,10 @@ class CustomizeCard {
}

_statusColour(jobOrStepStatus) {
const status = jobOrStepStatus?.trim().toLowerCase();
if (!jobOrStepStatus) {
return "default";
}
const status = jobOrStepStatus?.toLowerCase();
if (status === "success") {
return "good";
} else if (status === "failure") {
Expand All @@ -123,9 +126,11 @@ class CustomizeCard {
}
}

const GITHUB_SERVER_URL = process.env.GITHUB_SERVER_URL;
const GITHUB_REPOSITORY = process.env.GITHUB_REPOSITORY;
const GITHUB_RUN_ID = process.env.GITHUB_RUN_ID;
const GITHUB_SHA = process.env.GITHUB_SHA;
const {
GITHUB_SERVER_URL,
GITHUB_REPOSITORY,
GITHUB_RUN_ID,
GITHUB_SHA,
} = process.env;

module.exports = CustomizeCard;
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const CustomizeCard = require('../CustomizeCard');
const expectedPayLoad = require('./expectedPayLoadObject');

describe('Verify JSON payload', () => {

test('Only message', () => {
const message = 'Only message';

const payload = new CustomizeCard(message, { status: '' }).constructCard();

expect(payload).toMatchObject(expectedPayLoad({message, statusText: '', statusColour: 'default',}));
});

test('Message with status success', () => {
const message = 'Message with success status';
const statusText = 'Success';
const statusColour = 'good';
const payload = new CustomizeCard(message, {status: statusText,}).constructCard();

expect(payload).toMatchObject(expectedPayLoad({message, statusText, statusColour,}));
});

test('Message with custom status', () => {
const message = 'Message with custom status';
const statusText = 'Custom status';
const statusColour = 'default';
const payload = new CustomizeCard(message, {status: statusText,}).constructCard();

expect(payload).toMatchObject(expectedPayLoad({message, statusText, statusColour,}));
});

describe('With env variables', () => {
const GITHUB_SERVER_URL = "GITHUB_SERVER_URL";
const GITHUB_REPOSITORY = "GITHUB_REPOSITORY";
const GITHUB_RUN_ID = "GITHUB_RUN_ID";
const GITHUB_SHA = "GITHUB_SHA";
const SHOULD_DISPLAY_VIEW_RUN_BUTTON = "SHOULD_DISPLAY_VIEW_RUN_BUTTON";

const githubServerUrl = 'https://foo';
const githubRepository = 'notify';
const githubRunId = 1234;

beforeEach(() => {
delete process.env[GITHUB_SERVER_URL];
delete process.env[GITHUB_REPOSITORY];
delete process.env[GITHUB_RUN_ID];
delete process.env[GITHUB_SHA];
});

test('Payload should not have run URL when view run env variable is set to false', () => {
process.env = Object.assign(process.env, {
[GITHUB_SERVER_URL]: githubServerUrl, [GITHUB_REPOSITORY]: githubRepository, [GITHUB_RUN_ID]: githubRunId,
[SHOULD_DISPLAY_VIEW_RUN_BUTTON]: 'false'
});
const message = 'Message with cancelled status';
const statusText = 'Cancelled';
const statusColour = 'warning';
const payload = new CustomizeCard(message, {status: statusText,}).constructCard();

expect(payload).toMatchObject(expectedPayLoad({message, statusText, statusColour,}));
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const expectedPayLoadObject = ({
message, statusText, statusColour
}) => ({
"type": "message", "attachments": [{
"contentType": "application/vnd.microsoft.card.adaptive", "contentUrl": null, "content": {
"$schema": "https://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.2",
"msteams": {"width": "Full"},
"body": [{
"type": "RichTextBlock", "isVisible": statusText !== '', "inlines": [{
"type": "TextRun", "text": "Status: ", "wrap": true, "fontType": "monospace"
}, {
"type": "TextRun",
"text": statusText,
"wrap": true,
"color": statusColour,
"weight": "bolder",
"fontType": "monospace"
}]
}, {
"type": "ColumnSet", "columns": [{
"type": "Column",
"width": "stretch",
"style": "emphasis",
"items": [{"type": "TextBlock", "text": message, "wrap": true}]
}, {
"type": "Column",
"width": "auto",
"verticalContentAlignment": "center",
"isVisible": false,
"items": [{"type": "ActionSet", "actions": []}, {"type": "ActionSet", "actions": []}]
}]
}]
}
}]
});

module.exports = expectedPayLoadObject;
4 changes: 2 additions & 2 deletions src/payload/payload.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const CustomizeCard = require("./customize-card/CustomizeCard");

let payLoad = function constructPayload(message, options) {
return new CustomizeCard(message, options).constructCard();
let payLoad = function constructPayload(message, { status, }) {
return new CustomizeCard(message, { status, }).constructCard();
};

module.exports = payLoad;
4 changes: 2 additions & 2 deletions test/http-request-tests/message-with-job-status.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ describe('Post message with job status', () => {

test('Send a long message with no status', async () => {
const messageToSend = 'Long message with no status. With label message published SDK version of container 0.1.1 (major) version. Pushed the container to docker registry and artifactory';
let response = await main(_teamsIncomingHookUrl, messageToSend, {});
let response = await main(_teamsIncomingHookUrl, messageToSend, {status: ''});
expect(response).toBe(responseBody);
});

test('Send a short message with no status', async () => {
const messageToSend = 'Short message with no status';
let response = await main(_teamsIncomingHookUrl, messageToSend, {});
let response = await main(_teamsIncomingHookUrl, messageToSend, {status: ''});
expect(response).toBe(responseBody);
});
});
4 changes: 2 additions & 2 deletions test/http-request-tests/url-buttons.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Post message with job status', () => {
test('Both buttons to be visible with no status', async () => {
process.env = Object.assign(process.env, { [SHOULD_DISPLAY_VIEW_COMMIT_BUTTON]: 'true', [SHOULD_DISPLAY_VIEW_RUN_BUTTON]: 'true' });
const messageToSend = 'Both buttons to be visible with no status';
let response = await main(_teamsIncomingHookUrl, messageToSend, {});
let response = await main(_teamsIncomingHookUrl, messageToSend, { status: '' });
expect(response).toBe(responseBody);
});

Expand Down Expand Up @@ -61,7 +61,7 @@ describe('Post message with job status', () => {
test('Send a long message with one of the buttons', async () => {
process.env = Object.assign(process.env, { [SHOULD_DISPLAY_VIEW_RUN_BUTTON]: 'true', });
const messageToSend = 'Long message with no status status along with view run button. With status message published SDK version of container 14.1.1 (major) version. Pushed the container to docker registry and artifactory';
let response = await main(_teamsIncomingHookUrl, messageToSend, {});
let response = await main(_teamsIncomingHookUrl, messageToSend, { status: '' });
expect(response).toBe(responseBody);
});

Expand Down

0 comments on commit b8fc57f

Please sign in to comment.