-
Notifications
You must be signed in to change notification settings - Fork 1k
105 lines (95 loc) · 3.38 KB
/
issue_on_error.yml
File metadata and controls
105 lines (95 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
name: Issue On Error
# This workflow can be called from other workflows in order to generate
# an issue on failure.
#
# With only the required inputs issue will have the title of the failing workflow
# and a link to it.
#
# If the optional pr_number and link are provided the title and link will be for
# the PR.
#
# Either type of issue will be tagged (by default with bot:issue).
# If an issue already exists, a comment will be added with a timestamp of the
# new failure and a link.
on:
workflow_call:
inputs:
repo:
type: string
required: true
workflow:
type: string
required: true
run_id:
type: string
required: true
run_number:
type: string
required: true
flag_label:
type: string
default: 'bot:issue'
pr_number:
type: string
pr_link:
type: string
secrets:
token:
required: true
jobs:
error-trap:
runs-on: ubuntu-latest
permissions:
issues: write
name: Error Trap
steps:
- name: Report Error
uses: actions/github-script@v8
env:
WORKFLOW: ${{ inputs.workflow }}
LABEL: ${{ inputs.flag_label }}
RUN_ID: ${{ inputs.run_id }}
RUN_NUMBER: ${{ inputs.run_number }}
PR_NUMBER: ${{ inputs.pr_number }}
PR_LINK: ${{ inputs.pr_link }}
with:
github-token: ${{ secrets.token }}
script: |
const { WORKFLOW, LABEL, RUN_ID, RUN_NUMBER, PR_NUMBER, PR_LINK } = process.env;
const repo = context.repo;
const isPR = PR_NUMBER && PR_NUMBER.trim() !== "";
const titlePrefix = isPR ? `PR #${PR_NUMBER} CI Failure:` : `CI Failure:`;
const issueTitle = `${titlePrefix} ${WORKFLOW}`;
const runUrl = `https://github.com/${repo.owner}/${repo.repo}/actions/runs/${RUN_ID}`;
// 1. Search for an existing open issue with this label and title
const { data: issues } = await github.rest.issues.listForRepo({
...repo,
state: 'open',
labels: [LABEL],
per_page: 100
});
const existingIssue = issues.find(i => i.title === issueTitle);
if (existingIssue) {
// 2. Add a comment if it already exists (The "Heartbeat")
await github.rest.issues.createComment({
...repo,
issue_number: existingIssue.number,
body: `**Error reoccurred** at ${new Date().toUTCString()}\n[Run #${RUN_NUMBER}](${runUrl})`
});
console.log(`Updated issue #${existingIssue.number}`);
} else {
// 3. Create a new issue if it doesn't
const body = [
`### ❌ ${WORKFLOW} CI Failure`,
isPR ? `**Source:** Pull Request #${PR_NUMBER} ([Link](${PR_LINK}))` : `**Source:** Main Branch (HEAD)`,
`**Latest Failure:** [Run #${RUN_NUMBER}](${runUrl})`,
`_Automatically generated for notification purposes._`
].join('\n\n');
const { data: newIssue } = await github.rest.issues.create({
...repo,
title: issueTitle,
body: body,
labels: [LABEL]
});
console.log(`Created new issue #${newIssue.number}`);
}