From d0f04a925256e1e693c5d5077d75079b427046bc Mon Sep 17 00:00:00 2001 From: Carlos Pereira Date: Tue, 27 Sep 2022 00:16:18 +0100 Subject: [PATCH] feat: receive branch name from input parameter --- action.yml | 3 +++ index.js | 30 ++++++++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/action.yml b/action.yml index 407ee60..7e179c9 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,9 @@ inputs: githubToken: description: "The github token that will be used to comment the results on available pull requests." required: true + branchName: + description: "The name of the branch that triggered the workflow." + required: true cloudflareProjectName: description: "The name of the project on Cloudflare that will receive the coverage report." required: true diff --git a/index.js b/index.js index 2391729..564be48 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,7 @@ const COVERAGE_OUTPUT_FOLDER = "./coverage"; const input = { framework: core.getInput("framework"), githubToken: core.getInput("githubToken", { required: true }), + branchName: core.getInput("branchName", { required: true }), cloudflareProjectName: core.getInput("cloudflareProjectName", { required: true, }), @@ -114,14 +115,14 @@ const COVERAGE_OUTPUT_FOLDER = "./coverage"; * https://octokit.github.io/rest.js/v19 **/ core.startGroup("Comment on available pull requests..."); - const octokit = new github.getOctokit(input.githubToken); + const octokit = github.getOctokit(input.githubToken); const BRANCH_NAME = github.context.ref; const BRANCH_COMMIT = commitShortHash; const UPLOAD_URL = `https://${commitShortHash}.${input.baseCloudflareDeploymentUrl}`; - core.info("BRANCH_NAME: ", BRANCH_NAME); - core.info("BRANCH_COMMIT: ", BRANCH_COMMIT); - core.info("UPLOAD_URL: ", UPLOAD_URL); + core.info(`BRANCH_NAME: ${input.branchName}`); + core.info(`BRANCH_COMMIT: ${BRANCH_COMMIT}`); + core.info(`UPLOAD_URL: ${UPLOAD_URL}`); // Get coverage summary with retries const getCoverageSummary = async ( @@ -163,12 +164,11 @@ const COVERAGE_OUTPUT_FOLDER = "./coverage"; } }; - // Get open pull requests for $BRANCH_NAME const { data: pulls } = await octokit.rest.search.issuesAndPullRequests({ - q: `is:pr state:open repo:${github.context.repo.owner}/${github.context.repo.repo} head:${BRANCH_NAME}`, + q: `is:pr state:open repo:${github.context.repo.owner}/${github.context.repo.repo} head:${input.branchName}`, }); - core.info("Pull requests available: ", pulls); + core.info(`Pull requests available: ${JSON.stringify(pulls)}`); if (pulls.total_count > 0) { pulls.items.forEach(async (pull) => { @@ -240,19 +240,17 @@ const COVERAGE_OUTPUT_FOLDER = "./coverage"; if (headAvgPercentage > baseAvgPercentage) { coverageMessage = `\n> Wooo šŸŽ‰, the tests are passing and the coverage percentage **increased** with this pull request, well done! šŸ‘\n> ${ pullRequest.base.ref - }: **${Math.round( - baseAvgPercentage, - -1 - )}%** | ${BRANCH_NAME}: **${Math.round(headAvgPercentage, -1)}%**`; + }: **${Math.round(baseAvgPercentage, -1)}%** | ${ + input.branchName + }: **${Math.round(headAvgPercentage, -1)}%**`; } else if (headAvgPercentage === baseAvgPercentage) { coverageMessage = `\n> Good job šŸ‘Œ, the tests are passing and the coverage percentage remained intact.`; } else { coverageMessage = `\n> Tests are passing but the coverage percentage **is decreased** šŸ˜± by this pull request, read coverage report below for more details.\n\nšŸ”» ${ pullRequest.base.ref - }: **${Math.round( - baseAvgPercentage, - -1 - )}%** | ${BRANCH_NAME}: **${Math.round(headAvgPercentage, -1)}%** šŸ”»`; + }: **${Math.round(baseAvgPercentage, -1)}%** | ${ + input.branchName + }: **${Math.round(headAvgPercentage, -1)}%** šŸ”»`; } let coverageSummaryTable = ` @@ -328,7 +326,7 @@ const COVERAGE_OUTPUT_FOLDER = "./coverage"; - Time: **${timeTaken}** - > Coverage data is based on head **${BRANCH_NAME}** (\`${BRANCH_COMMIT}\`) compared to base **${pullRequest.base.ref}** (\`${shortBaseSha}\`). + > Coverage data is based on head **${input.branchName}** (\`${BRANCH_COMMIT}\`) compared to base **${pullRequest.base.ref}** (\`${shortBaseSha}\`). [View full coverage report šŸ”—](${UPLOAD_URL})`;