Skip to content

Commit 08b6fa9

Browse files
authored
converting to an adaptive card and adding link (#9)
<!-- markdownlint-disable-next-line first-line-heading --> ## Description Adding ability to display a link button through the notification in teams ## Context Enables users to optionally provide a link and to display this in the notification. Also switched to using AdaptiveCards for the incoming webhook format. ## Type of changes - [X] Refactoring (non-breaking change) - [X] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would change existing functionality) - [ ] Bug fix (non-breaking change which fixes an issue) ## Checklist - [X] I am familiar with the [contributing guidelines](../docs/CONTRIBUTING.md) - [X] I have followed the code style of the project - [X] I have added tests to cover my changes - [X] I have updated the documentation accordingly - [ ] This PR is a result of pair or mob programming --- ## Sensitive Information Declaration To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including [PII (Personal Identifiable Information) / PID (Personal Identifiable Data)](https://digital.nhs.uk/data-and-information/keeping-data-safe-and-benefitting-the-public) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter. - [X] I confirm that neither PII/PID nor sensitive data are included in this PR and the codebase changes.
1 parent 9e5cb95 commit 08b6fa9

8 files changed

Lines changed: 148 additions & 31 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ jobs:
2222
teams-webhook-url: ${{ secrets.TEAMS_WEBHOOK_URL }}
2323
message-title: Title
2424
message-text: Text
25+
link: ${{ github.event.pull_request.html_url }}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ Add the following section to your existing workflow file:
2525

2626
```yml
2727
- name: Testing action to notify Teams
28-
uses: nhs-england-tools/notify-msteams-action@v0.0.2
28+
uses: nhs-england-tools/notify-msteams-action@v0.0.3
2929
with:
3030
github-token: ${{ secrets.GITHUB_TOKEN }}
3131
teams-webhook-url: ${{ secrets.TEAMS_WEBHOOK_URL }}
3232
message-title: "Replace with an appropriate title"
3333
message-text: "Replace with appropriate text"
34+
link: https://google.com
3435
```
3536
3637
Follow the instructions [to add an Incoming Webhook](https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook?tabs=dotnet) to the Teams channel of your choice.
@@ -71,6 +72,7 @@ The steps above detail how to quickly use this action within your repository. Th
7172
- message-title - the title for your message - this will be displayed in the notification
7273
- message-text - the text for your message - this will be displayed in the notification
7374
- message-colour - The colour to use for the header line in the notification
75+
- link - optional if required provide a link to be presented in the notification
7476

7577
## Architecture
7678

__tests__/main.test.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ test('test building a message card', async () => {
1313
const repoName = 'XXXXXX'
1414
const repoUrl = 'XXXXXX'
1515
const repoBranch = 'XXXXXX'
16-
const avatar_url = 'XXXXXX'
16+
const avatar_url = 'https://avatars.githubusercontent.com/u/105098969'
1717
const login = 'XXXXXX'
1818
const author_url = 'XXXXXX'
19+
const link = 'https://google.com'
1920
const message = buildMessageCard(
2021
messageTitle,
2122
messageBody,
@@ -27,8 +28,44 @@ test('test building a message card', async () => {
2728
repoBranch,
2829
avatar_url,
2930
login,
30-
author_url
31+
author_url,
32+
link
3133
)
3234
const messageJson = JSON.parse(message)
33-
expect(messageJson.summary).toBe(messageTitle)
35+
expect(messageJson.type).toBe('message')
36+
expect(messageJson.attachments[0].content.body[0].text).toBe(messageTitle)
37+
expect(messageJson.attachments[0].content.actions[0].url).toBe(link)
38+
})
39+
40+
test('test building a message card with no link', async () => {
41+
const messageTitle = 'This is a title'
42+
const messageBody = 'This is some text'
43+
const messageColour = 'HEXCOL'
44+
const runNumber = 'XXXXXX'
45+
const runId = 'XXXXXX'
46+
const repoName = 'XXXXXX'
47+
const repoUrl = 'XXXXXX'
48+
const repoBranch = 'XXXXXX'
49+
const avatar_url = 'https://avatars.githubusercontent.com/u/105098969'
50+
const login = 'XXXXXX'
51+
const author_url = 'XXXXXX'
52+
const link = ''
53+
const message = buildMessageCard(
54+
messageTitle,
55+
messageBody,
56+
messageColour,
57+
runNumber,
58+
runId,
59+
repoName,
60+
repoUrl,
61+
repoBranch,
62+
avatar_url,
63+
login,
64+
author_url,
65+
link
66+
)
67+
const messageJson = JSON.parse(message)
68+
expect(messageJson.type).toBe('message')
69+
expect(messageJson.attachments[0].content.body[0].text).toBe(messageTitle)
70+
expect(messageJson.attachments[0].content.actions).toStrictEqual([])
3471
})

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ inputs:
1818
message-colour:
1919
required: false
2020
description: 'The colour to use for the header line in the notification'
21+
link:
22+
required: false
23+
description: 'A link to include in the message'
2124
runs:
2225
using: 'node16'
2326
main: 'dist/index.js'

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "notify-msteams-action",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"private": true,
55
"description": "GitHub Action to notify an MS Teams channel",
66
"main": "lib/main.js",

src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ async function run(): Promise<void> {
1212
const messageBody = core.getInput('message-text', {required: true})
1313
const messageColour =
1414
core.getInput('message-colour', {required: false}) || '00cbff'
15+
const link = core.getInput('link', {required: false}) || ''
1516

1617
const [owner, repoName] = (process.env.GITHUB_REPOSITORY || '').split('/') // https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
1718
const sha = process.env.GITHUB_SHA || ''
@@ -46,7 +47,8 @@ async function run(): Promise<void> {
4647
repoBranch,
4748
avatar_url,
4849
login,
49-
author_url
50+
author_url,
51+
link
5052
)
5153

5254
const response = await axios.post(teamsWebhookUrl, messageCard, {

src/messagecard.ts

Lines changed: 95 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,110 @@ export function buildMessageCard(
99
repoBranch: string,
1010
avatar_url: string,
1111
login: string,
12-
author_url: string
12+
author_url: string,
13+
link: string
1314
): string {
14-
const card = {
15-
'@type': 'MessageCard',
16-
'@context': 'https://schema.org/extensions',
17-
$schema: 'https://adaptivecards.io/schemas/adaptive-card.json',
18-
version: '1.0',
19-
summary: messageTitle,
20-
themeColor: messageColour,
21-
title: messageTitle,
22-
sections: [
15+
let actions: Record<string, string>[] = []
16+
if (link !== '') {
17+
actions = [
18+
{
19+
type: 'Action.OpenUrl',
20+
title: 'View',
21+
url: link,
22+
role: 'button'
23+
}
24+
]
25+
}
26+
27+
const title = {
28+
type: 'TextBlock',
29+
size: 'Large',
30+
weight: 'Bolder',
31+
text: messageTitle,
32+
wrap: true
33+
}
34+
35+
const subtitle = {
36+
type: 'ColumnSet',
37+
columns: [
2338
{
24-
activityTitle: `[${repoName}](${repoUrl})`,
25-
activitySubtitle: `by [${login}](${author_url})`,
26-
activityImage: avatar_url,
27-
facts: [
39+
type: 'Column',
40+
width: 'auto',
41+
items: [
2842
{
29-
name: 'Run Number',
30-
value: runNumber
31-
},
43+
type: 'Image',
44+
url: avatar_url,
45+
size: 'Small',
46+
style: 'Person',
47+
altText: login
48+
}
49+
]
50+
},
51+
{
52+
type: 'Column',
53+
width: 'stretch',
54+
items: [
3255
{
33-
name: 'Run ID',
34-
value: runId
56+
type: 'TextBlock',
57+
text: `[${repoName}](${repoUrl})`,
58+
weight: 'Bolder',
59+
size: 'Medium',
60+
wrap: true
3561
},
3662
{
37-
name: 'Branch',
38-
value: repoBranch
63+
type: 'TextBlock',
64+
text: `by [${login}](${author_url})`,
65+
weight: 'Bolder',
66+
size: 'Small',
67+
wrap: true
3968
}
40-
],
41-
text: messageBody
69+
]
70+
}
71+
]
72+
}
73+
74+
const message = {
75+
type: 'TextBlock',
76+
text: messageBody,
77+
wrap: true
78+
}
79+
80+
const facts = {
81+
type: 'FactSet',
82+
facts: [
83+
{
84+
title: 'Run Number',
85+
value: runNumber
86+
},
87+
{
88+
title: 'Run ID',
89+
value: runId
90+
},
91+
{
92+
title: 'Branch',
93+
value: repoBranch
94+
}
95+
]
96+
}
97+
98+
const adaptiveCard = {
99+
type: 'AdaptiveCard',
100+
$schema: 'http://adaptivecards.io/schemas/adaptive-card.json',
101+
version: '1.4',
102+
body: [title, subtitle, message, facts],
103+
actions
104+
}
105+
106+
const card = {
107+
type: 'message',
108+
attachments: [
109+
{
110+
contentType: 'application/vnd.microsoft.card.adaptive',
111+
contentUrl: null,
112+
content: adaptiveCard
42113
}
43114
]
44115
}
116+
45117
return JSON.stringify(card)
46118
}

0 commit comments

Comments
 (0)