Skip to content

Commit 5c0c8f6

Browse files
committed
add try-catch for check-sample workflow
1 parent 4ac57ac commit 5c0c8f6

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

.github/workflows/check-sample.yml

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,48 +45,64 @@ jobs:
4545
const fs = require('fs');
4646
const pr_number = context.issue.number;
4747
const marker = '## Samples Checklist';
48-
49-
// Read the checklist from the file
50-
let checklist = fs.readFileSync('checklist.txt', 'utf8').trim();
48+
49+
let checklist;
5150
let error = false;
5251
53-
if(!checklist) {
54-
checklist = "✅ All good!"
55-
}
56-
else {
57-
error = true;
52+
try {
53+
// Check if the checklist file exists
54+
if (!fs.existsSync('checklist.txt')) {
55+
throw new Error("Samples checklist file does not exist. Please fix the issues and try again.");
56+
}
57+
58+
// Read the checklist from the file
59+
checklist = fs.readFileSync('checklist.txt', 'utf8');
60+
checklist = checklist.trim();
61+
if(!checklist) {
62+
checklist = "✅ All good!"
63+
}
64+
else {
65+
error = true;
66+
}
67+
} catch (readFileError) {
68+
throw new Error(Could not read samples checklist from file. Please fix the issues and try again.")
5869
}
5970
60-
// Get the current PR
61-
const { data: pullRequest } = await github.rest.pulls.get({
62-
owner: context.repo.owner,
63-
repo: context.repo.repo,
64-
pull_number: pr_number
65-
});
71+
try {
72+
// Get the current PR
73+
const { data: pullRequest } = await github.rest.pulls.get({
74+
owner: context.repo.owner,
75+
repo: context.repo.repo,
76+
pull_number: pr_number
77+
});
6678
67-
let newBody;
68-
const body = pullRequest.body || "";
69-
const markerIndex = body.indexOf(marker);
79+
let newBody;
80+
const body = pullRequest.body || "";
81+
const markerIndex = body.indexOf(marker);
7082
71-
if (markerIndex !== -1) {
72-
// Replace the content below the marker
73-
newBody = body.substring(0, markerIndex + marker.length) + "\n" + checklist;
74-
} else {
75-
// Append the checklist if the marker doesn't exist
76-
newBody = body + "\n" + marker + "\n" + checklist;
77-
}
83+
if (markerIndex !== -1) {
84+
// Replace the content below the marker
85+
newBody = body.substring(0, markerIndex + marker.length) + "\n" + checklist;
86+
} else {
87+
// Append the checklist if the marker doesn't exist
88+
newBody = body + "\n" + marker + "\n" + checklist;
89+
}
7890
79-
// Update the PR description
80-
await github.rest.pulls.update({
81-
owner: context.repo.owner,
82-
repo: context.repo.repo,
83-
pull_number: pr_number,
84-
body: newBody
85-
});
91+
// Update the PR description
92+
await github.rest.pulls.update({
93+
owner: context.repo.owner,
94+
repo: context.repo.repo,
95+
pull_number: pr_number,
96+
body: newBody
97+
});
98+
} catch (updatePrError) {
99+
throw new Error("Could not update PR description based on samples checklist. Please fix the issues and try again.")
100+
}
86101
87-
if(error) {
102+
if (error) {
88103
throw new Error("Incomplete samples checklist. Please fix the issues and try again.");
89104
}
105+
90106
91107
- name: Create / Update Template Repo
92108
uses: actions/github-script@v7

0 commit comments

Comments
 (0)