From 55dfa28359fa46aa9b63c616cd6ed40d5cf85e62 Mon Sep 17 00:00:00 2001 From: "Nicholas Carrigan (he/him)" Date: Fri, 5 Mar 2021 12:00:02 -0800 Subject: [PATCH] fix(tools): implement better error handling (#41379) * fix(tools): implement better error handling Modifies the workflow to validate the branch exists and the PR does not. This was not blocking any issues, but caused the action to report a failure. * fix: apply Randy's review suggestions Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> --- .../crowdin-i18n-client-ui-download.yml | 25 +++++++++++++++++++ .../crowdin-i18n-curriculum-download.yml | 25 +++++++++++++++++++ .../workflows/crowdin-i18n-docs-download..yml | 25 +++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/.github/workflows/crowdin-i18n-client-ui-download.yml b/.github/workflows/crowdin-i18n-client-ui-download.yml index d53cd6df35..2063451043 100644 --- a/.github/workflows/crowdin-i18n-client-ui-download.yml +++ b/.github/workflows/crowdin-i18n-client-ui-download.yml @@ -90,6 +90,25 @@ jobs: with: github-token: ${{ secrets.CROWDIN_CAMPERBOT_PAT }} script: | + const branchExists = await github.repos.getBranch({ + owner: 'freeCodeCamp', + repo: 'freeCodeCamp', + branch: 'i18n-sync-client' + }).catch(err => { + console.info("Branch does not exist. Likely no changes in download?"); + }); + if (!branchExists || branchExists.status !== 200) { + return; + } + const pullRequestExists = await github.pulls.list({ + owner: 'freeCodeCamp', + repo: 'freeCodeCamp', + head: 'freeCodeCamp:i18n-sync-client' + }); + if (pullRequestExists.data.length) { + console.info("It looks like a pull request already exists for this branch."); + return; + } const PR = await github.pulls.create({ owner: 'freeCodeCamp', repo: 'freeCodeCamp', @@ -97,7 +116,13 @@ jobs: base: 'main', title: 'chore(i18n,client): processed translations', body: 'This PR was opened auto-magically by Crowdin.' + }).catch(err => { + console.info("Unpredicted error occurred when trying to create the PR."); + console.error(err); }); + if (!PR || PR.status !== 201) { + return; + } const PRNumber = PR.data.number; await github.issues.addLabels({ owner: 'freeCodeCamp', diff --git a/.github/workflows/crowdin-i18n-curriculum-download.yml b/.github/workflows/crowdin-i18n-curriculum-download.yml index efaec0ad09..75f9792db8 100644 --- a/.github/workflows/crowdin-i18n-curriculum-download.yml +++ b/.github/workflows/crowdin-i18n-curriculum-download.yml @@ -91,6 +91,25 @@ jobs: with: github-token: ${{ secrets.CROWDIN_CAMPERBOT_PAT }} script: | + const branchExists = await github.repos.getBranch({ + owner: 'freeCodeCamp', + repo: 'freeCodeCamp', + branch: 'i18n-sync-learn' + }).catch(err => { + console.info("Branch does not exist. Likely no changes in download?"); + }); + if (!branchExists || branchExists.status !== 200) { + return; + } + const pullRequestExists = await github.pulls.list({ + owner: 'freeCodeCamp', + repo: 'freeCodeCamp', + head: 'freeCodeCamp:i18n-sync-learn' + }); + if (pullRequestExists.data.length) { + console.info("It looks like a pull request already exists for this branch."); + return; + } const PR = await github.pulls.create({ owner: 'freeCodeCamp', repo: 'freeCodeCamp', @@ -98,7 +117,13 @@ jobs: base: 'main', title: 'chore(i18n,learn): processed translations', body: 'This PR was opened auto-magically by Crowdin.' + }).catch(err => { + console.info("Unpredicted error occurred when trying to create the PR."); + console.error(err); }); + if (!PR || PR.status !== 201) { + return; + } const PRNumber = PR.data.number; await github.issues.addLabels({ owner: 'freeCodeCamp', diff --git a/.github/workflows/crowdin-i18n-docs-download..yml b/.github/workflows/crowdin-i18n-docs-download..yml index 47cf8ea880..2fa68eae10 100644 --- a/.github/workflows/crowdin-i18n-docs-download..yml +++ b/.github/workflows/crowdin-i18n-docs-download..yml @@ -124,6 +124,25 @@ jobs: with: github-token: ${{ secrets.CROWDIN_CAMPERBOT_PAT }} script: | + const branchExists = await github.repos.getBranch({ + owner: 'freeCodeCamp', + repo: 'freeCodeCamp', + branch: 'i18n-sync-docs' + }).catch(err => { + console.info("Branch does not exist. Likely no changes in download?"); + }); + if (!branchExists || branchExists.status !== 200) { + return; + } + const pullRequestExists = await github.pulls.list({ + owner: 'freeCodeCamp', + repo: 'freeCodeCamp', + head: 'freeCodeCamp:i18n-sync-docs' + }); + if (pullRequestExists.data.length) { + console.info("It looks like a pull request already exists for this branch."); + return; + } const PR = await github.pulls.create({ owner: 'freeCodeCamp', repo: 'freeCodeCamp', @@ -131,7 +150,13 @@ jobs: base: 'main', title: 'chore(i18n,docs): processed translations', body: 'This PR was opened auto-magically by Crowdin.' + }).catch(err => { + console.info("Unpredicted error occurred when trying to create the PR."); + console.error(err); }); + if (!PR || PR.status !== 201) { + return; + } const PRNumber = PR.data.number; await github.issues.addLabels({ owner: 'freeCodeCamp',