Skip to content

Commit 216b8b2

Browse files
authored
feat: add retry mechanism for Microsoft Teams notifications and update documentation (#199)
1 parent 96f8e0d commit 216b8b2

5 files changed

Lines changed: 318 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ You can customize the following parameters:
3939
| steps | optional | {} | JSON parsed steps context |
4040
| needs | optional | {} | JSON parsed needs context |
4141
| dry_run | optional | False | Do not actually send the message |
42+
| retries | optional | 0 | Number of retries on Teams notification errors (non-negative integer) |
4243
| raw | optional | '' | JSON object to send to Microsoft Teams |
4344
| title | optional | '' | Overwrite default title |
4445
| actions | optional | '' | JSON array of Adaptive Card Action objects to replace the default Repository/Compare buttons |
4546
| msteams_emails | optional | '' | Microsoft teams email ids in CSV to tag in the message |
4647

48+
When debug logging is enabled for GitHub Actions, this action emits additional debug details for notification attempts and failures.
49+
Debug mode is enabled when either `ACTIONS_STEP_DEBUG=true` or `RUNNER_DEBUG=1` is set.
50+
4751
Please refer [action.yml](./action.yml) for more details.
4852

4953
## Examples
@@ -69,6 +73,7 @@ jobs:
6973
needs: ${{ toJson(needs) }}
7074
job: ${{ toJson(job) }}
7175
steps: ${{ toJson(steps) }}
76+
retries: 2
7277
dry_run: True
7378

7479
with_emails:

‎action.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ inputs:
3535
dry_run:
3636
description: 'Do not actually send the message'
3737
required: false
38+
retries:
39+
description: 'Number of retries on notification errors (non-negative integer)'
40+
required: false
3841
runs:
3942
using: 'node24'
4043
main: 'dist/index.js'

‎dist/index.js‎

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110697,6 +110697,49 @@ const missing_functionality_warning = objective =>
110697110697
`Missing ${objective} parameter will result in reduced functionality.`
110698110698
) || {}
110699110699

110700+
const isDebugFlagEnabled = value => {
110701+
if (!value) return false
110702+
110703+
const normalizedValue = String(value).trim().toLowerCase()
110704+
return normalizedValue === '1' || normalizedValue === 'true'
110705+
}
110706+
110707+
const isDebugEnabled = () =>
110708+
isDebugFlagEnabled(process.env.RUNNER_DEBUG) ||
110709+
isDebugFlagEnabled(process.env.ACTIONS_STEP_DEBUG)
110710+
110711+
const serializeError = err => {
110712+
if (!err) return 'Unknown error'
110713+
if (err.stack) return err.stack
110714+
if (err.message) return err.message
110715+
110716+
try {
110717+
return JSON.stringify(err, null, 2)
110718+
} catch {
110719+
return String(err)
110720+
}
110721+
}
110722+
110723+
const logError = (message, err) => {
110724+
core.error(`${message}: ${err && err.message ? err.message : String(err)}`)
110725+
if (isDebugEnabled()) {
110726+
core.debug(`Error details:\n${serializeError(err)}`)
110727+
}
110728+
}
110729+
110730+
const parseRetryInput = value => {
110731+
if (!value) return 0
110732+
110733+
const retries = Number.parseInt(value, 10)
110734+
if (Number.isNaN(retries) || retries < 0) {
110735+
throw new Error(
110736+
`Invalid "retries" input: "${value}". Please provide a non-negative integer.`
110737+
)
110738+
}
110739+
110740+
return retries
110741+
}
110742+
110700110743
const access_context = context_name => {
110701110744
const context = core.getInput(context_name)
110702110745
if (!context) missing_functionality_warning(context_name)
@@ -110724,6 +110767,7 @@ async function run () {
110724110767
const msteams_emails = core.getInput('msteams_emails')
110725110768
let raw = core.getInput('raw')
110726110769
const dry_run = core.getInput('dry_run')
110770+
const retries = parseRetryInput(core.getInput('retries'))
110727110771

110728110772
const overwrite = core.getInput('overwrite')
110729110773
if (overwrite) {
@@ -110743,10 +110787,15 @@ async function run () {
110743110787
title,
110744110788
actions,
110745110789
msteams_emails,
110746-
dry_run
110790+
dry_run,
110791+
retries
110747110792
})}`
110748110793
)
110749110794

110795+
if (isDebugEnabled()) {
110796+
core.debug('GitHub Actions step debug logging is enabled.')
110797+
}
110798+
110750110799
const msteams = new MSTeams()
110751110800
let payload
110752110801
if (raw === '') {
@@ -110788,13 +110837,47 @@ async function run () {
110788110837
}
110789110838

110790110839
if (dry_run === '' || dry_run === 'false') {
110791-
await msteams.notify(webhook_url, payload)
110792-
core.info('Sent message to Microsoft Teams')
110840+
const attempts = retries + 1
110841+
let sent = false
110842+
let lastError
110843+
110844+
for (let attempt = 1; attempt <= attempts; attempt++) {
110845+
if (isDebugEnabled()) {
110846+
core.debug(`Sending notification attempt ${attempt}/${attempts}`)
110847+
}
110848+
110849+
try {
110850+
await msteams.notify(webhook_url, payload)
110851+
sent = true
110852+
core.info('Sent message to Microsoft Teams')
110853+
break
110854+
} catch (err) {
110855+
lastError = err
110856+
logError(`Notification attempt ${attempt}/${attempts} failed`, err)
110857+
110858+
if (attempt < attempts) {
110859+
core.warning(`Retrying Microsoft Teams notification (${attempt + 1}/${attempts})`)
110860+
}
110861+
}
110862+
}
110863+
110864+
if (!sent) {
110865+
const lastErrorMessage =
110866+
lastError && lastError.message
110867+
? lastError.message
110868+
: serializeError(lastError)
110869+
110870+
throw new Error(
110871+
`Failed to send notification to Microsoft Teams after ${attempts} attempt(s): ${lastErrorMessage}`
110872+
)
110873+
}
110793110874
} else {
110794110875
core.info('Dry run - skipping notification send. Done.')
110795110876
}
110796110877
} catch (err) {
110797-
core.setFailed(err.message)
110878+
logError('Action execution failed', err)
110879+
const failureMessage = err && err.message ? err.message : serializeError(err)
110880+
core.setFailed(failureMessage)
110798110881
}
110799110882
}
110800110883

‎src/index.js‎

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,49 @@ const missing_functionality_warning = objective =>
66
`Missing ${objective} parameter will result in reduced functionality.`
77
) || {}
88

9+
const isDebugFlagEnabled = value => {
10+
if (!value) return false
11+
12+
const normalizedValue = String(value).trim().toLowerCase()
13+
return normalizedValue === '1' || normalizedValue === 'true'
14+
}
15+
16+
const isDebugEnabled = () =>
17+
isDebugFlagEnabled(process.env.RUNNER_DEBUG) ||
18+
isDebugFlagEnabled(process.env.ACTIONS_STEP_DEBUG)
19+
20+
const serializeError = err => {
21+
if (!err) return 'Unknown error'
22+
if (err.stack) return err.stack
23+
if (err.message) return err.message
24+
25+
try {
26+
return JSON.stringify(err, null, 2)
27+
} catch {
28+
return String(err)
29+
}
30+
}
31+
32+
const logError = (message, err) => {
33+
core.error(`${message}: ${err && err.message ? err.message : String(err)}`)
34+
if (isDebugEnabled()) {
35+
core.debug(`Error details:\n${serializeError(err)}`)
36+
}
37+
}
38+
39+
const parseRetryInput = value => {
40+
if (!value) return 0
41+
42+
const retries = Number.parseInt(value, 10)
43+
if (Number.isNaN(retries) || retries < 0) {
44+
throw new Error(
45+
`Invalid "retries" input: "${value}". Please provide a non-negative integer.`
46+
)
47+
}
48+
49+
return retries
50+
}
51+
952
const access_context = context_name => {
1053
const context = core.getInput(context_name)
1154
if (!context) missing_functionality_warning(context_name)
@@ -33,6 +76,7 @@ async function run () {
3376
const msteams_emails = core.getInput('msteams_emails')
3477
let raw = core.getInput('raw')
3578
const dry_run = core.getInput('dry_run')
79+
const retries = parseRetryInput(core.getInput('retries'))
3680

3781
const overwrite = core.getInput('overwrite')
3882
if (overwrite) {
@@ -52,10 +96,15 @@ async function run () {
5296
title,
5397
actions,
5498
msteams_emails,
55-
dry_run
99+
dry_run,
100+
retries
56101
})}`
57102
)
58103

104+
if (isDebugEnabled()) {
105+
core.debug('GitHub Actions step debug logging is enabled.')
106+
}
107+
59108
const msteams = new MSTeams()
60109
let payload
61110
if (raw === '') {
@@ -97,13 +146,47 @@ async function run () {
97146
}
98147

99148
if (dry_run === '' || dry_run === 'false') {
100-
await msteams.notify(webhook_url, payload)
101-
core.info('Sent message to Microsoft Teams')
149+
const attempts = retries + 1
150+
let sent = false
151+
let lastError
152+
153+
for (let attempt = 1; attempt <= attempts; attempt++) {
154+
if (isDebugEnabled()) {
155+
core.debug(`Sending notification attempt ${attempt}/${attempts}`)
156+
}
157+
158+
try {
159+
await msteams.notify(webhook_url, payload)
160+
sent = true
161+
core.info('Sent message to Microsoft Teams')
162+
break
163+
} catch (err) {
164+
lastError = err
165+
logError(`Notification attempt ${attempt}/${attempts} failed`, err)
166+
167+
if (attempt < attempts) {
168+
core.warning(`Retrying Microsoft Teams notification (${attempt + 1}/${attempts})`)
169+
}
170+
}
171+
}
172+
173+
if (!sent) {
174+
const lastErrorMessage =
175+
lastError && lastError.message
176+
? lastError.message
177+
: serializeError(lastError)
178+
179+
throw new Error(
180+
`Failed to send notification to Microsoft Teams after ${attempts} attempt(s): ${lastErrorMessage}`
181+
)
182+
}
102183
} else {
103184
core.info('Dry run - skipping notification send. Done.')
104185
}
105186
} catch (err) {
106-
core.setFailed(err.message)
187+
logError('Action execution failed', err)
188+
const failureMessage = err && err.message ? err.message : serializeError(err)
189+
core.setFailed(failureMessage)
107190
}
108191
}
109192

0 commit comments

Comments
 (0)