Skip to content

Commit

Permalink
Merge pull request #21 from toko-bifrost/release/v3
Browse files Browse the repository at this point in the history
Release 3.1.2
  • Loading branch information
aldwyn-acn authored Jun 30, 2020
2 parents 241ee23 + 84423e9 commit dcc94e4
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 60 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ jobs:
github-token: ${{ github.token }}
webhook-uri: ${{ secrets.MS_TEAMS_WEBHOOK_URI }}
timezone: Australia/Sydney
card-layout-start: cozy

- run: exit 0
54 changes: 38 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,37 @@ jobs:
### Configurations
#### Main Configurations
| Name | Required | Default | Description |
| ------------------- | -------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `github-token` | `true` | None | This can be set to the following:<br/>- `${{ github.token }}`<br/>- `${{ secrets.GITHUB_TOKEN }}`<br/>- `${{ secrets.CUSTOM_TOKEN }}` |
| `webhook-uri` | `true` | None | The value of `MS_TEAMS_WEBHOOK_URI` |
| `environment` | `false` | None | Name of the environment, e.g. `development`, `production` (won't be included in the card if none) |
| `timezone` | `false` | `UTC` | A [valid database timezone name](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones), e.g. `Australia/Sydney` |
| `card-layout-start` | `false` | `complete` | Card layout on **_start_** (i.e. `complete`, `cozy`, `compact`) |
| `card-layout-exit` | `false` | `compact` | Card layout on **_exit_** (i.e. `complete`, `cozy`, `compact`) |
| `show-on-start` | `false` | `true` | Show an MS Teams card upon **_starting_** this Github Actions job |
| `show-on-exit` | `false` | `true` | Show an MS Teams card upon **_exiting_** this Github Actions job, regardless if it's a successful or a failed exit |
| `show-on-failure` | `false` | `false` | Show an MS Teams card upon **_exiting_** this Github Actions job and status is `FAILURE`; **This will override any boolean value of `show-on-exit`**, e.g. with `show-on-exit=false` and `show-on-failure=true`, the notification card will still proceed upon job failure |
| `custom-facts` | `false` | `null` | YAML-parseable string defining a list of objects with `name` and `value` to display alongside existing facts. Only for `complete` layout. |
| `include-files` | `false` | `true` | Include the list of files when `layout` is set to `complete` |
| `allowed-file-len` | `false` | `7` | Allowed number of changed files to display, when `include-files` is set to `true` |

#### Cozy and Complete Layout-specific Configurations

| Name | Required | Default | Description |
| -------------------------- | -------- | ---------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| `environment` | `false` | None | Name of the environment, e.g. `development`, `production` (won't be included in the card if none) |
| `timezone` | `false` | `"UTC"` | A [valid database timezone name](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones), e.g. `Australia/Sydney` |
| `enable-view-status` | `false` | `true` | Enable an action to view the deployment status |
| `enable-review-diffs` | `false` | `true` | Enable an action to review commit diffs |
| `view-status-action-text` | `false` | `"View build/deploy status"` | Customize action text in viewing the deployment status |
| `review-diffs-action-text` | `false` | `"Review commit diffs"` | Customize action text in reviewing commit diffs |
| `custom-actions` | `false` | `null` | Add more actions; must be a YAML-parseable multiline string with `text` and `url` pairs |

#### Complete Layout-specific Configurations

| Name | Required | Default | Description |
| ------------------ | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `custom-facts` | `false` | `null` | YAML-parseable multiline string defining a list of objects with `name` and `value` to display alongside existing facts. Only for `complete` layout. |
| `include-files` | `false` | `true` | Include the list of files when `layout` is set to `complete` |
| `allowed-file-len` | `false` | `7` | Allowed number of changed files to display, when `include-files` is set to `true` |

### Local Set-up

Expand All @@ -74,18 +91,23 @@ jobs:

- As this is still in development, always use the working latest version from the `Releases`, as they have more bug fixes and added features.

- If you are going to set `custom-facts` with the `complete` layout, look out for the pipe symbol (`|`). Github Actions only accept `string`, `number`, and `boolean` as parameters on job steps. To bypass this restriction and to instill YAML list formatting, we have to set it with a YAML-parseable multiline string containing a YAML list of objects with `name` and `value`. If there is an error in YAML parsing, no custom facts will be appended to the table.
- If you are going to set `custom-facts` (only for `complete` layout) and `custom-actions` (on both `cozy` and `complete` layout), look out for the pipe symbol (`|`). Github Actions only accept `string`, `number`, and `boolean` as parameters on job steps. To bypass this restriction and to instill YAML list formatting, we have to set it with a YAML-parseable multiline string containing a YAML list of objects with `name` and `value` on `custom-facts`, and `text` and `url` (i.e. must be a valid URI format) on `custom-actions`. If there is an error in YAML parsing, no custom facts/actions will be appended.

```
- uses: toko-bifrost/ms-teams-deploy-card@master
with:
github-token: ${{ github.token }}
webhook-uri: ${{ secrets.MS_TEAMS_WEBHOOK_URI }}
custom-facts: |
- name: Custom Fact 1
value: Random URL maybe
- name: Pull Request
value: http://example.com/${{ github.run_id }}
```yaml
- uses: toko-bifrost/ms-teams-deploy-card@master
with:
github-token: ${{ github.token }}
webhook-uri: ${{ secrets.MS_TEAMS_WEBHOOK_URI }}
custom-facts: |
- name: Custom Fact Name 1
value: Custom Fact Value 1
- name: Pull Request
value: "http://example.com/${{ github.run_id }}"
custom-actions: |
- text: View PR
url: "http://valid.uri.com"
- text: View CI
url: "http://example.com/${{ github.run_id }}"
```

- Always set this job with `if: always()` when there are steps between `actions/checkout@v2` and this job.
Expand Down
20 changes: 20 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ inputs:
description: YAML-parseable multiline string defining the list of object with name and value to display on the facts table
required: false
default: null
enable-view-status-action:
description: Enable an action to view the deployment status
required: false
default: true
enable-review-diffs-action:
description: Enable an action to review commit diffs
required: false
default: true
view-status-action-text:
description: Customize action text in viewing the deployment status
required: false
default: View build/deploy status
review-diffs-action-text:
description: Customize action text in reviewing commit diffs
required: false
default: Review commit diffs
custom-actions:
description: Add more actions and links on cozy and complete layout
required: false
default: null
include-files:
description: Only for complete layout; Set false to exclude list of files
required: false
Expand Down
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { setFailed, getInput, info } from "@actions/core";
import { formatAndNotify } from "./utils";

try {
const showCardOnStart =
getInput(`show-on-start`).trim().toLowerCase() == "true";
const showCardOnStart = getInput(`show-on-start`).toLowerCase() == "true";
if (showCardOnStart) {
formatAndNotify("start");
} else {
info(`Configured to not show card upon job start.`);
info("Configured to not show card upon job start.");
}
} catch (error) {
setFailed(error.message);
Expand Down
3 changes: 2 additions & 1 deletion src/layouts/compact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function formatCompactLayout(

// Set environment name
const environment = getInput("environment");
if (environment.trim() !== "") {
if (environment !== "") {
labels += ` \`ENV:${environment.toUpperCase()}\``;
}

Expand All @@ -33,5 +33,6 @@ export function formatCompactLayout(
`${labels} &nbsp; CI [#${process.env.GITHUB_RUN_NUMBER}](${runLink}) ` +
`(commit [${shortSha}](${commit.data.html_url})) on [${process.env.GITHUB_REPOSITORY}](${repoUrl}) ` +
`by [@${author.login}](${author.html_url})`;

return webhookBody;
}
20 changes: 8 additions & 12 deletions src/layouts/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Octokit } from "@octokit/rest";
import { getInput, warning, info } from "@actions/core";
import yaml from "yaml";

import { escapeMarkdownTokens } from "../utils";
import { escapeMarkdownTokens, renderActions } from "../utils";
import { Fact, PotentialAction } from "../models";
import { formatCozyLayout } from "./cozy";

Expand Down Expand Up @@ -46,12 +46,10 @@ export function formatCompleteLayout(

// for complete layout, just replace activityText with potentialAction
section.activityText = undefined;
section.potentialAction = [
new PotentialAction("View build/deploy status", [
`${repoUrl}/actions/runs/${process.env.GITHUB_RUN_ID}`,
]),
new PotentialAction("Review commit diffs", [commit.data.html_url]),
];
section.potentialAction = renderActions(
`${repoUrl}/actions/runs/${process.env.GITHUB_RUN_ID}`,
commit.data.html_url
);

// Set status and elapsedSeconds
let labels = `\`${conclusion.toUpperCase()}\``;
Expand All @@ -75,7 +73,7 @@ export function formatCompleteLayout(

// Set custom facts
const customFacts = getInput("custom-facts");
if (customFacts) {
if (customFacts && customFacts.toLowerCase() !== "null") {
try {
let customFactsCounter = 0;
const customFactsList = yaml.parse(customFacts);
Expand All @@ -95,7 +93,7 @@ export function formatCompleteLayout(

// Set environment name
const environment = getInput("environment");
if (environment.trim() !== "") {
if (environment !== "") {
section.facts.splice(
1,
0,
Expand All @@ -104,9 +102,7 @@ export function formatCompleteLayout(
}

// Set list of files
const includeFiles =
getInput("include-files").trim().toLowerCase() === "true";
if (includeFiles) {
if (getInput("include-files").toLowerCase() === "true") {
const allowedFileLen = getInput("allowed-file-len").toLowerCase();
const allowedFileLenParsed = parseInt(
allowedFileLen === "" ? "7" : allowedFileLen
Expand Down
20 changes: 16 additions & 4 deletions src/layouts/cozy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Octokit } from "@octokit/rest";
import moment from "moment-timezone";
import { WebhookBody } from "../models";
import yaml from "yaml";
import { getInput } from "@actions/core";

import { WebhookBody } from "../models";
import { CONCLUSION_THEMES } from "../constants";
import { renderActions } from "../utils";

export const OCTOCAT_LOGO_URL =
"https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png";
Expand All @@ -19,7 +22,6 @@ export function formatCozyLayout(
const webhookBody = new WebhookBody();
const repoUrl = `https://github.com/${process.env.GITHUB_REPOSITORY}`;
const shortSha = process.env.GITHUB_SHA?.substr(0, 7);
const statusUrl = `${repoUrl}/actions/runs/${process.env.GITHUB_RUN_ID}`;

// Set status and elapsedSeconds
let labels = `\`${conclusion.toUpperCase()}\``;
Expand All @@ -29,13 +31,22 @@ export function formatCozyLayout(

// Set environment name
const environment = getInput("environment");
if (environment.trim() !== "") {
if (environment !== "") {
labels += ` \`ENV:${environment.toUpperCase()}\``;
}

// Set themeColor
webhookBody.themeColor = CONCLUSION_THEMES[conclusion] || "957DAD";

// Get potential actions
const actions = renderActions(
`${repoUrl}/actions/runs/${process.env.GITHUB_RUN_ID}`,
commit.data.html_url
);
const actionsConcat = actions
.map((action) => ` &nbsp; &nbsp; [${action.name}](${action.target})`)
.join("");

const author = commit.data.author;
// Set sections
webhookBody.sections = [
Expand All @@ -45,8 +56,9 @@ export function formatCozyLayout(
activitySubtitle: author
? `by [@${author.login}](${author.html_url}) on ${nowFmt}`
: nowFmt,
activityText: `${labels} &nbsp; &nbsp; [View status](${statusUrl}) &nbsp; &nbsp; [Review diffs](${commit.data.html_url})`,
activityText: `${labels}${actionsConcat}`,
},
];

return webhookBody;
}
8 changes: 4 additions & 4 deletions src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { setFailed, info, getInput } from "@actions/core";
import { formatAndNotify, getWorkflowRunStatus } from "./utils";

try {
// setTimeout to give time for Github API to show up the final conclusion
setTimeout(async () => {
const showCardOnExit =
getInput(`show-on-exit`).trim().toLowerCase() === "true";
const showCardOnExit = getInput(`show-on-exit`).toLowerCase() === "true";
const showCardOnFailure =
getInput(`show-on-failure`).trim().toLowerCase() === "true";
getInput(`show-on-failure`).toLowerCase() === "true";

const workflowRunStatus = await getWorkflowRunStatus();
if (
Expand All @@ -19,7 +19,7 @@ try {
workflowRunStatus.elapsedSeconds
);
} else {
info(`Configured to not show card upon job exit.`);
info("Configured to not show card upon job exit.");
}
}, 2000);
} catch (error) {
Expand Down
44 changes: 42 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Octokit } from "@octokit/rest";
import { setOutput, info, getInput } from "@actions/core";
import { setOutput, info, getInput, warning } from "@actions/core";
import fetch, { Response } from "node-fetch";
import moment from "moment";
import yaml from "yaml";

import { WebhookBody } from "./models";
import { WebhookBody, PotentialAction } from "./models";
import { formatCompactLayout } from "./layouts/compact";
import { formatCozyLayout } from "./layouts/cozy";
import { formatCompleteLayout } from "./layouts/complete";
Expand Down Expand Up @@ -128,3 +129,42 @@ export async function getWorkflowRunStatus() {
conclusion: lastStep?.conclusion,
};
}

export function renderActions(statusUrl: string, diffUrl: string) {
const actions: PotentialAction[] = [];
if (getInput("enable-view-status-action").toLowerCase() === "true") {
actions.push(
new PotentialAction(getInput("view-status-action-text"), [statusUrl])
);
}
if (getInput("enable-review-diffs-action").toLowerCase() === "true") {
actions.push(
new PotentialAction(getInput("review-diffs-action-text"), [diffUrl])
);
}

// Set custom actions
const customActions = getInput("custom-actions");
if (customActions && customActions.toLowerCase() !== "null") {
try {
let customActionsCounter = 0;
const customActionsList = yaml.parse(customActions);
if (Array.isArray(customActionsList)) {
(customActionsList as any[]).forEach((action) => {
if (
action.text !== undefined &&
action.url !== undefined &&
(action.url as string).match(/https?:\/\/\S+/g)
) {
actions.push(new PotentialAction(action.text, [action.url]));
customActionsCounter++;
}
});
}
info(`Added ${customActionsCounter} custom facts.`);
} catch {
warning("Invalid custom-actions value.");
}
}
return actions;
}
Loading

0 comments on commit dcc94e4

Please sign in to comment.