Skip to content

Commit fe8fefd

Browse files
CopilotSkitionek
andauthored
feat: add actions input to override default notification action buttons (#148)
* Initial plan * feat: add actions parameter to allow overwriting potentialAction buttons Agent-Logs-Url: https://github.com/Skitionek/notify-microsoft-teams/sessions/e415b603-9228-4bcb-8943-2c75a2795840 Co-authored-by: Skitionek <[email protected]> * fix: improve actions input validation with try-catch and better docs Agent-Logs-Url: https://github.com/Skitionek/notify-microsoft-teams/sessions/e415b603-9228-4bcb-8943-2c75a2795840 Co-authored-by: Skitionek <[email protected]> * fix: improve action.yml description formatting and error message clarity Agent-Logs-Url: https://github.com/Skitionek/notify-microsoft-teams/sessions/e415b603-9228-4bcb-8943-2c75a2795840 Co-authored-by: Skitionek <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Skitionek <[email protected]>
1 parent 681fce1 commit fe8fefd

7 files changed

Lines changed: 191 additions & 32 deletions

File tree

‎README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ You can customize the following parameters:
4141
| dry_run | optional | False | Do not actually send the message |
4242
| raw | optional | '' | JSON object to send to Microsoft Teams |
4343
| title | optional | '' | Overwrite default title |
44+
| actions | optional | '' | JSON array of Adaptive Card Action objects to replace the default Repository/Compare buttons |
4445
| msteams_emails | optional | '' | Microsoft teams email ids in CSV to tag in the message |
4546

4647
Please refer [action.yml](./action.yml) for more details.
@@ -109,6 +110,31 @@ jobs:
109110
webhook_url: ${{ secrets.MSTEAMS_WEBHOOK }}
110111
title: '`Overwrote title in ${workflow_link}`'
111112

113+
with_custom_actions:
114+
name: One with custom action buttons
115+
runs-on: ubuntu-latest
116+
steps:
117+
- uses: actions/checkout@master
118+
- name: Microsoft Teams Notification
119+
uses: skitionek/notify-microsoft-teams@master
120+
if: always()
121+
with:
122+
webhook_url: ${{ secrets.MSTEAMS_WEBHOOK }}
123+
job: ${{ toJson(job) }}
124+
actions: >-
125+
[
126+
{
127+
"type": "Action.OpenUrl",
128+
"title": "View Run",
129+
"url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
130+
},
131+
{
132+
"type": "Action.OpenUrl",
133+
"title": "Repository",
134+
"url": "https://github.com/${{ github.repository }}"
135+
}
136+
]
137+
112138
with_raw:
113139
name: One with raw data
114140
runs-on: ubuntu-latest

‎action.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ inputs:
1414
title:
1515
description: 'Text to show as message title'
1616
required: false
17+
actions:
18+
description: >-
19+
JSON array of Adaptive Card Action objects to replace the default action buttons.
20+
Leave empty to use the default Repository/Compare buttons.
21+
See https://adaptivecards.io/explorer/Action.OpenUrl.html for schema.
22+
required: false
1723
msteams_emails:
1824
description: 'Microsoft teams emails in CSV to tag in the message'
1925
required: false

‎dist/index.js‎

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104806,6 +104806,7 @@ class MSTeams {
104806104806
* @param steps
104807104807
* @param needs
104808104808
* @param title {string} msteams message title
104809+
* @param actions {Array} optional array of Adaptive Card Action objects to replace default buttons
104809104810
* @param msteams_emails {string} msteams emails in CSV
104810104811
* @return
104811104812
*/
@@ -104814,6 +104815,7 @@ class MSTeams {
104814104815
steps = {},
104815104816
needs = {},
104816104817
title = '',
104818+
actions = null,
104817104819
msteams_emails = ''
104818104820
}) {
104819104821
const steps_summary = summary_generator(steps, 'outcome')
@@ -104863,22 +104865,24 @@ class MSTeams {
104863104865

104864104866
const actionLinks = {
104865104867
type: 'ActionSet',
104866-
actions: [
104867-
{
104868-
type: 'Action.OpenUrl',
104869-
title: 'Repository',
104870-
url: repository.html_url
104871-
},
104872-
...(compare
104873-
? [
104874-
{
104875-
type: 'Action.OpenUrl',
104876-
title: 'Compare',
104877-
url: compare
104878-
}
104879-
]
104880-
: [])
104881-
]
104868+
actions: actions !== null
104869+
? actions
104870+
: [
104871+
{
104872+
type: 'Action.OpenUrl',
104873+
title: 'Repository',
104874+
url: repository.html_url
104875+
},
104876+
...(compare
104877+
? [
104878+
{
104879+
type: 'Action.OpenUrl',
104880+
title: 'Compare',
104881+
url: compare
104882+
}
104883+
]
104884+
: [])
104885+
]
104882104886
}
104883104887

104884104888
const entities =
@@ -110716,6 +110720,7 @@ async function run () {
110716110720
const needs = access_context('needs')
110717110721

110718110722
const title = core.getInput('title')
110723+
const actions = core.getInput('actions')
110719110724
const msteams_emails = core.getInput('msteams_emails')
110720110725
let raw = core.getInput('raw')
110721110726
const dry_run = core.getInput('dry_run')
@@ -110736,6 +110741,7 @@ async function run () {
110736110741
needs,
110737110742
raw,
110738110743
title,
110744+
actions,
110739110745
msteams_emails,
110740110746
dry_run
110741110747
})}`
@@ -110744,11 +110750,22 @@ async function run () {
110744110750
const msteams = new MSTeams()
110745110751
let payload
110746110752
if (raw === '') {
110753+
let parsedActions = null
110754+
if (actions) {
110755+
try {
110756+
parsedActions = JSON.parse(actions)
110757+
} catch (e) {
110758+
throw new Error(
110759+
`Invalid JSON provided for "actions" input: ${e.message}. Please ensure the "actions" input is a valid JSON array of Adaptive Card Action objects (see https://adaptivecards.io/explorer/Action.OpenUrl.html).`
110760+
)
110761+
}
110762+
}
110747110763
payload = await msteams.generatePayload({
110748110764
job,
110749110765
steps,
110750110766
needs,
110751110767
title,
110768+
actions: parsedActions,
110752110769
msteams_emails
110753110770
})
110754110771
} else {

‎src/MSTeams.js‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ class MSTeams {
194194
* @param steps
195195
* @param needs
196196
* @param title {string} msteams message title
197+
* @param actions {Array} optional array of Adaptive Card Action objects to replace default buttons
197198
* @param msteams_emails {string} msteams emails in CSV
198199
* @return
199200
*/
@@ -202,6 +203,7 @@ class MSTeams {
202203
steps = {},
203204
needs = {},
204205
title = '',
206+
actions = null,
205207
msteams_emails = ''
206208
}) {
207209
const steps_summary = summary_generator(steps, 'outcome')
@@ -251,22 +253,24 @@ class MSTeams {
251253

252254
const actionLinks = {
253255
type: 'ActionSet',
254-
actions: [
255-
{
256-
type: 'Action.OpenUrl',
257-
title: 'Repository',
258-
url: repository.html_url
259-
},
260-
...(compare
261-
? [
262-
{
263-
type: 'Action.OpenUrl',
264-
title: 'Compare',
265-
url: compare
266-
}
267-
]
268-
: [])
269-
]
256+
actions: actions !== null
257+
? actions
258+
: [
259+
{
260+
type: 'Action.OpenUrl',
261+
title: 'Repository',
262+
url: repository.html_url
263+
},
264+
...(compare
265+
? [
266+
{
267+
type: 'Action.OpenUrl',
268+
title: 'Compare',
269+
url: compare
270+
}
271+
]
272+
: [])
273+
]
270274
}
271275

272276
const entities =

‎src/MSTeams.test.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,43 @@ jest.mock('@actions/github', () => ({
2727

2828
jest.mock('ms-teams-webhook')
2929

30+
describe('MSTeams.generatePayload', () => {
31+
it('should use custom actions when provided', async () => {
32+
const customActions = [
33+
{ type: 'Action.OpenUrl', title: 'Custom Link', url: 'https://example.com' }
34+
]
35+
const msTeams = new MSTeams()
36+
const payload = await msTeams.generatePayload({ actions: customActions })
37+
38+
const actionSet = payload.attachments[0].content.body.find(b => b.type === 'ActionSet')
39+
expect(actionSet).toBeDefined()
40+
expect(actionSet.actions).toEqual(customActions)
41+
})
42+
43+
it('should use default Repository/Compare actions when actions is null', async () => {
44+
const msTeams = new MSTeams()
45+
const payload = await msTeams.generatePayload({ actions: null })
46+
47+
const actionSet = payload.attachments[0].content.body.find(b => b.type === 'ActionSet')
48+
expect(actionSet).toBeDefined()
49+
expect(actionSet.actions).toEqual(
50+
expect.arrayContaining([
51+
expect.objectContaining({ title: 'Repository' }),
52+
expect.objectContaining({ title: 'Compare' })
53+
])
54+
)
55+
})
56+
57+
it('should use default Repository/Compare actions when actions is not provided', async () => {
58+
const msTeams = new MSTeams()
59+
const payload = await msTeams.generatePayload({})
60+
61+
const actionSet = payload.attachments[0].content.body.find(b => b.type === 'ActionSet')
62+
expect(actionSet).toBeDefined()
63+
expect(actionSet.actions.some(a => a.title === 'Repository')).toBe(true)
64+
})
65+
})
66+
3067
describe('MSTeams.notify', () => {
3168
const webhookUrl = 'test-webhook-url'
3269
const payload = { message: 'Test Payload' }

‎src/index.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ async function run () {
2929
const needs = access_context('needs')
3030

3131
const title = core.getInput('title')
32+
const actions = core.getInput('actions')
3233
const msteams_emails = core.getInput('msteams_emails')
3334
let raw = core.getInput('raw')
3435
const dry_run = core.getInput('dry_run')
@@ -49,6 +50,7 @@ async function run () {
4950
needs,
5051
raw,
5152
title,
53+
actions,
5254
msteams_emails,
5355
dry_run
5456
})}`
@@ -57,11 +59,22 @@ async function run () {
5759
const msteams = new MSTeams()
5860
let payload
5961
if (raw === '') {
62+
let parsedActions = null
63+
if (actions) {
64+
try {
65+
parsedActions = JSON.parse(actions)
66+
} catch (e) {
67+
throw new Error(
68+
`Invalid JSON provided for "actions" input: ${e.message}. Please ensure the "actions" input is a valid JSON array of Adaptive Card Action objects (see https://adaptivecards.io/explorer/Action.OpenUrl.html).`
69+
)
70+
}
71+
}
6072
payload = await msteams.generatePayload({
6173
job,
6274
steps,
6375
needs,
6476
title,
77+
actions: parsedActions,
6578
msteams_emails
6679
})
6780
} else {

‎src/index.test.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,62 @@ describe('run function', () => {
203203
custom: 'raw_payload'
204204
})
205205
})
206+
207+
it('should pass custom actions to generatePayload', async () => {
208+
const customActions = [
209+
{ type: 'Action.OpenUrl', title: 'Custom Link', url: 'https://example.com' }
210+
]
211+
const params = {
212+
...defaultParams,
213+
actions: JSON.stringify(customActions)
214+
}
215+
core.getInput.mockImplementation(name =>
216+
params[name] !== undefined ? params[name] : ''
217+
)
218+
const mockGeneratePayload = jest.spyOn(MSTeams.prototype, 'generatePayload')
219+
jest.spyOn(MSTeams.prototype, 'notify').mockImplementation(jest.fn())
220+
221+
await run()
222+
223+
expect(mockGeneratePayload).toHaveBeenCalledWith(
224+
expect.objectContaining({ actions: customActions })
225+
)
226+
})
227+
228+
it('should pass null actions to generatePayload when actions input is empty', async () => {
229+
const params = {
230+
...defaultParams,
231+
actions: ''
232+
}
233+
core.getInput.mockImplementation(name =>
234+
params[name] !== undefined ? params[name] : ''
235+
)
236+
const mockGeneratePayload = jest.spyOn(MSTeams.prototype, 'generatePayload')
237+
jest.spyOn(MSTeams.prototype, 'notify').mockImplementation(jest.fn())
238+
239+
await run()
240+
241+
expect(mockGeneratePayload).toHaveBeenCalledWith(
242+
expect.objectContaining({ actions: null })
243+
)
244+
})
245+
246+
it('should fail with a clear error when actions input contains invalid JSON', async () => {
247+
const params = {
248+
...defaultParams,
249+
actions: '{invalid_json'
250+
}
251+
core.getInput.mockImplementation(name =>
252+
params[name] !== undefined ? params[name] : ''
253+
)
254+
const mockSetFailed = jest.spyOn(core, 'setFailed')
255+
256+
await run()
257+
258+
expect(mockSetFailed).toHaveBeenCalledWith(
259+
expect.stringContaining('Invalid JSON provided for "actions" input')
260+
)
261+
})
206262
})
207263

208264
describe('run function with dry_run', () => {

0 commit comments

Comments
 (0)