From 0f62439bd5ba4e87d933c9950663308d1e5d4a43 Mon Sep 17 00:00:00 2001 From: commit111 Date: Thu, 19 Dec 2024 11:47:10 -0800 Subject: [PATCH 1/3] add try-catch for get-pr --- .github/workflows/check-sample.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check-sample.yml b/.github/workflows/check-sample.yml index 736c492f..0712f8b8 100644 --- a/.github/workflows/check-sample.yml +++ b/.github/workflows/check-sample.yml @@ -70,7 +70,11 @@ jobs: repo: context.repo.repo, pull_number: pr_number }); + } catch (getPrError) { + throw new Error ("Could not get current PR from source. Please fix the issues and try again.") + } + try { let newBody; const body = pullRequest.body || ""; const markerIndex = body.indexOf(marker); @@ -97,7 +101,6 @@ jobs: if (error) { throw new Error("Incomplete samples checklist. Please fix the issues and try again."); } - - name: Create / Update Template Repo uses: actions/github-script@v7 From 082cf8b7968d22b551d483b666e5b4da76b1c6b1 Mon Sep 17 00:00:00 2001 From: commit111 Date: Thu, 19 Dec 2024 11:48:39 -0800 Subject: [PATCH 2/3] separate js script from Add Checklist to PR Desc. --- .github/workflows/check-sample.yml | 62 +++-------------------------- scripts/add-checklist-to-pr.js | 63 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 57 deletions(-) create mode 100644 scripts/add-checklist-to-pr.js diff --git a/.github/workflows/check-sample.yml b/.github/workflows/check-sample.yml index 0712f8b8..c56ab068 100644 --- a/.github/workflows/check-sample.yml +++ b/.github/workflows/check-sample.yml @@ -42,64 +42,12 @@ jobs: uses: actions/github-script@v5 with: script: | - const fs = require('fs'); - const pr_number = context.issue.number; - const marker = '## Samples Checklist'; - - let checklist; - let error = false; - - try { - // Read the checklist from the file - checklist = fs.readFileSync('checklist.txt', 'utf8'); - checklist = checklist.trim(); - if(!checklist) { - checklist = "✅ All good!" - } - else { - error = true; - } - } catch (readFileError) { - throw new Error("Could not read samples checklist from file. Please fix the issues and try again.") - } - + const script = require('./scripts/add-checklist-to-pr.js'); try { - // Get the current PR - const { data: pullRequest } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pr_number - }); - } catch (getPrError) { - throw new Error ("Could not get current PR from source. Please fix the issues and try again.") - } - - try { - let newBody; - const body = pullRequest.body || ""; - const markerIndex = body.indexOf(marker); - - if (markerIndex !== -1) { - // Replace the content below the marker - newBody = body.substring(0, markerIndex + marker.length) + "\n" + checklist; - } else { - // Append the checklist if the marker doesn't exist - newBody = body + "\n" + marker + "\n" + checklist; - } - - // Update the PR description - await github.rest.pulls.update({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pr_number, - body: newBody - }); - } catch (updatePrError) { - throw new Error("Could not update PR description based on samples checklist. Please fix the issues and try again.") - } - - if (error) { - throw new Error("Incomplete samples checklist. Please fix the issues and try again."); + await script({github, context}); + core.info(`Checklist successfully added to PR description`); + } catch (error) { + core.setFailed(`Script execution failed: ${error.message}`); } - name: Create / Update Template Repo diff --git a/scripts/add-checklist-to-pr.js b/scripts/add-checklist-to-pr.js new file mode 100644 index 00000000..62fc076a --- /dev/null +++ b/scripts/add-checklist-to-pr.js @@ -0,0 +1,63 @@ +const fs = require('fs'); + +module.exports = async ({ github, context }) => { + const pr_number = context.issue.number; + const marker = '## Samples Checklist'; + + let checklist; + let error = false; + + try { + // Read the checklist from the file + checklist = fs.readFileSync('checklist.txt', 'utf8'); + checklist = checklist.trim(); + if(!checklist) { + checklist = "✅ All good!" + } + else { + error = true; + } + } catch (readFileError) { + throw new Error("Could not read samples checklist from file. Please fix the issues and try again.") + } + + try { + // Get the current PR + const { data: pullRequest } = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pr_number + }); + } catch (getPrError) { + throw new Error ("Could not get current PR from source. Please fix the issues and try again.") + } + + try { + let newBody; + const body = pullRequest.body || ""; + const markerIndex = body.indexOf(marker); + + if (markerIndex !== -1) { + // Replace the content below the marker + newBody = body.substring(0, markerIndex + marker.length) + "\n" + checklist; + } else { + // Append the checklist if the marker doesn't exist + newBody = body + "\n" + marker + "\n" + checklist; + } + + // Update the PR description + await github.rest.pulls.update({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pr_number, + body: newBody + }); + } catch (updatePrError) { + throw new Error("Could not update PR description based on samples checklist. Please fix the issues and try again.") + } + + if (error) { + throw new Error("Incomplete samples checklist. Please fix the issues and try again."); + } + +} \ No newline at end of file From a6e18f8687386e309c4793c763d16d7f31a6b8dd Mon Sep 17 00:00:00 2001 From: Linda Lee Date: Fri, 20 Dec 2024 09:54:35 -0800 Subject: [PATCH 3/3] Update scripts/add-checklist-to-pr.js Co-authored-by: Jordan Stephens --- scripts/add-checklist-to-pr.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/add-checklist-to-pr.js b/scripts/add-checklist-to-pr.js index 62fc076a..cfd26818 100644 --- a/scripts/add-checklist-to-pr.js +++ b/scripts/add-checklist-to-pr.js @@ -60,4 +60,4 @@ module.exports = async ({ github, context }) => { throw new Error("Incomplete samples checklist. Please fix the issues and try again."); } -} \ No newline at end of file +}