Skip to content

Commit 082cf8b

Browse files
committed
separate js script from Add Checklist to PR Desc.
1 parent 0f62439 commit 082cf8b

File tree

2 files changed

+68
-57
lines changed

2 files changed

+68
-57
lines changed

.github/workflows/check-sample.yml

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -42,64 +42,12 @@ jobs:
4242
uses: actions/github-script@v5
4343
with:
4444
script: |
45-
const fs = require('fs');
46-
const pr_number = context.issue.number;
47-
const marker = '## Samples Checklist';
48-
49-
let checklist;
50-
let error = false;
51-
52-
try {
53-
// Read the checklist from the file
54-
checklist = fs.readFileSync('checklist.txt', 'utf8');
55-
checklist = checklist.trim();
56-
if(!checklist) {
57-
checklist = "✅ All good!"
58-
}
59-
else {
60-
error = true;
61-
}
62-
} catch (readFileError) {
63-
throw new Error("Could not read samples checklist from file. Please fix the issues and try again.")
64-
}
65-
45+
const script = require('./scripts/add-checklist-to-pr.js');
6646
try {
67-
// Get the current PR
68-
const { data: pullRequest } = await github.rest.pulls.get({
69-
owner: context.repo.owner,
70-
repo: context.repo.repo,
71-
pull_number: pr_number
72-
});
73-
} catch (getPrError) {
74-
throw new Error ("Could not get current PR from source. Please fix the issues and try again.")
75-
}
76-
77-
try {
78-
let newBody;
79-
const body = pullRequest.body || "";
80-
const markerIndex = body.indexOf(marker);
81-
82-
if (markerIndex !== -1) {
83-
// Replace the content below the marker
84-
newBody = body.substring(0, markerIndex + marker.length) + "\n" + checklist;
85-
} else {
86-
// Append the checklist if the marker doesn't exist
87-
newBody = body + "\n" + marker + "\n" + checklist;
88-
}
89-
90-
// Update the PR description
91-
await github.rest.pulls.update({
92-
owner: context.repo.owner,
93-
repo: context.repo.repo,
94-
pull_number: pr_number,
95-
body: newBody
96-
});
97-
} catch (updatePrError) {
98-
throw new Error("Could not update PR description based on samples checklist. Please fix the issues and try again.")
99-
}
100-
101-
if (error) {
102-
throw new Error("Incomplete samples checklist. Please fix the issues and try again.");
47+
await script({github, context});
48+
core.info(`Checklist successfully added to PR description`);
49+
} catch (error) {
50+
core.setFailed(`Script execution failed: ${error.message}`);
10351
}
10452
10553
- name: Create / Update Template Repo

scripts/add-checklist-to-pr.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const fs = require('fs');
2+
3+
module.exports = async ({ github, context }) => {
4+
const pr_number = context.issue.number;
5+
const marker = '## Samples Checklist';
6+
7+
let checklist;
8+
let error = false;
9+
10+
try {
11+
// Read the checklist from the file
12+
checklist = fs.readFileSync('checklist.txt', 'utf8');
13+
checklist = checklist.trim();
14+
if(!checklist) {
15+
checklist = "✅ All good!"
16+
}
17+
else {
18+
error = true;
19+
}
20+
} catch (readFileError) {
21+
throw new Error("Could not read samples checklist from file. Please fix the issues and try again.")
22+
}
23+
24+
try {
25+
// Get the current PR
26+
const { data: pullRequest } = await github.rest.pulls.get({
27+
owner: context.repo.owner,
28+
repo: context.repo.repo,
29+
pull_number: pr_number
30+
});
31+
} catch (getPrError) {
32+
throw new Error ("Could not get current PR from source. Please fix the issues and try again.")
33+
}
34+
35+
try {
36+
let newBody;
37+
const body = pullRequest.body || "";
38+
const markerIndex = body.indexOf(marker);
39+
40+
if (markerIndex !== -1) {
41+
// Replace the content below the marker
42+
newBody = body.substring(0, markerIndex + marker.length) + "\n" + checklist;
43+
} else {
44+
// Append the checklist if the marker doesn't exist
45+
newBody = body + "\n" + marker + "\n" + checklist;
46+
}
47+
48+
// Update the PR description
49+
await github.rest.pulls.update({
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
pull_number: pr_number,
53+
body: newBody
54+
});
55+
} catch (updatePrError) {
56+
throw new Error("Could not update PR description based on samples checklist. Please fix the issues and try again.")
57+
}
58+
59+
if (error) {
60+
throw new Error("Incomplete samples checklist. Please fix the issues and try again.");
61+
}
62+
63+
}

0 commit comments

Comments
 (0)