Skip to content

Commit 654a379

Browse files
committed
Add a step which opens an issue when the update workflow fails
fixes #231
1 parent dbbdd05 commit 654a379

1 file changed

Lines changed: 69 additions & 4 deletions

File tree

{{ cookiecutter.package_name }}/.github/workflows/sub_package_update.yml

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# This template is taken from the cruft example code, for further information please see:
22
# https://cruft.github.io/cruft/#automating-updates-with-github-actions
33
name: Automatic Update from package template
4-
permissions:
5-
contents: write
6-
pull-requests: write
74

85
on:
96
# Allow manual runs through the web UI
@@ -19,14 +16,17 @@ on:
1916
jobs:
2017
update:
2118
runs-on: ubuntu-latest
19+
permissions:
20+
contents: write
21+
pull-requests: write
2222
strategy:
2323
fail-fast: true
2424
steps:
2525
- uses: actions/checkout@v6
2626

2727
- uses: actions/setup-python@v6
2828
with:
29-
python-version: "3.11"
29+
python-version: "3.14"
3030

3131
- name: Install Cruft
3232
run: python -m pip install git+https://github.com/Cadair/cruft@patch-p1
@@ -93,3 +93,68 @@ jobs:
9393
If this pull request has been opened as a draft there are conflicts which need fixing.
9494
9595
**To run the CI on this pull request you will need to close it and reopen it.**
96+
97+
report-fail:
98+
if: failure()
99+
needs: [update]
100+
runs-on: ubuntu-latest
101+
permissions:
102+
issues: write
103+
steps:
104+
- name: Open an issue if workflow fails
105+
uses: actions/github-script@v7
106+
with:
107+
github-token: ${{ github.token }}
108+
script: |
109+
const fs = require('fs');
110+
111+
// Edit these if needed for your repo
112+
const variables = {
113+
owner: context.repo.owner,
114+
name: context.repo.repo,
115+
label: "Infrastructure",
116+
creator: "app/github-actions",
117+
title: "SunPy Package Template auto-update failed."
118+
};
119+
120+
const logs = 'The package update workflow failed.'
121+
const workflow_url = `https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`;
122+
const issue_body = `[Workflow Run URL](${workflow_url})\n${logs}`;
123+
124+
const query_string = `repo:${variables.owner}/${variables.name} author:${variables.creator} label:${variables.label} is:open in:title ${variables.title}`;
125+
126+
// Run GraphQL query against GitHub API to find the most recent open issue used for reporting failures
127+
const query = `query {
128+
search(query: "${query_string}", type:ISSUE, first: 1) {
129+
edges {
130+
node {
131+
... on Issue {
132+
body
133+
id
134+
number
135+
}
136+
}
137+
}
138+
}
139+
}`;
140+
141+
const result = await github.graphql(query);
142+
143+
// If no issue is open, create a new issue,
144+
// else update the body of the existing issue.
145+
if (result.search.edges.length === 0) {
146+
github.rest.issues.create({
147+
owner: variables.owner,
148+
repo: variables.n ame,
149+
body: issue_body,
150+
title: variables.title,
151+
labels: [variables.label],
152+
});
153+
} else {
154+
github.rest.issues.update({
155+
owner: variables.owner,
156+
repo: variables.name,
157+
issue_number: result.search.edges[0].node.number,
158+
body: issue_body
159+
});
160+
}

0 commit comments

Comments
 (0)