From dba0013cde8087c101580730e1bbafd2703c673b Mon Sep 17 00:00:00 2001 From: Marina Limeira Date: Thu, 24 Aug 2023 12:36:01 +0200 Subject: [PATCH] Validate code changes before committing --- dist/index.js | 27 +++++++++++++++++++++------ src/action.ts | 26 +++++++++++++++++++------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/dist/index.js b/dist/index.js index fbb0cf7..a551497 100644 --- a/dist/index.js +++ b/dist/index.js @@ -13552,6 +13552,8 @@ function osPlatform() { throw new Error("Unsupported operating system - the Patcher action is only released for Darwin and Linux"); } } +// pullRequestBranch formats the branch name. When dependency and workingDir are provided, the branch format will be +// patcher-dev-updates-gruntwork-io/terraform-aws-vpc/vpc-app`. function pullRequestBranch(dependency, workingDir) { let branch = "patcher"; if (workingDir) { @@ -13563,6 +13565,8 @@ function pullRequestBranch(dependency, workingDir) { } return branch; } +// pullRequestTitle formats the Pull Request title. When dependency and workingDir are provided, the title will be +// [Patcher] [dev] Update gruntwork-io/terraform-aws-vpc/vpc-app dependency function pullRequestTitle(dependency, workingDir) { let title = "[Patcher]"; if (workingDir) { @@ -13576,6 +13580,11 @@ function pullRequestTitle(dependency, workingDir) { } return title; } +async function wasCodeUpdated() { + const output = await exec.getExecOutput("git", ["status", "--porcelain"]); + // If there are changes, they will appear in the stdout. Otherwise, it returns blank. + return !!output.stdout; +} async function commitAndPushChanges(gitCommiter, dependency, workingDir, token) { const { owner, repo } = github.context.repo; const head = pullRequestBranch(dependency, workingDir); @@ -13673,12 +13682,17 @@ async function runPatcher(octokit, gitCommiter, binaryPath, command, { updateStr core.startGroup("Running 'patcher update'"); const updateOutput = await exec.getExecOutput(binaryPath, updateArgs(updateStrategy, dependency, workingDir), { env: getPatcherEnvVars(token) }); core.endGroup(); - core.startGroup("Commit and push changes"); - await commitAndPushChanges(gitCommiter, dependency, workingDir, token); - core.endGroup(); - core.startGroup("Opening pull request"); - await openPullRequest(octokit, gitCommiter, updateOutput.stdout, dependency, workingDir, token); - core.endGroup(); + if (await wasCodeUpdated()) { + core.startGroup("Commit and push changes"); + await commitAndPushChanges(gitCommiter, dependency, workingDir, token); + core.endGroup(); + core.startGroup("Opening pull request"); + // await openPullRequest(octokit, gitCommiter, updateOutput.stdout, dependency, workingDir, token) + core.endGroup(); + } + else { + core.info(`No changes in ${dependency} after running Patcher. No further action is necessary.`); + } return; } } @@ -13717,6 +13731,7 @@ async function run() { const dependency = core.getInput("dependency"); const workingDir = core.getInput("working_dir"); const commitAuthor = core.getInput("commit_author"); + // Always mask the `token` string in the logs. core.setSecret(token); // Only run the action if the user has access to Patcher. Otherwise, the download won't work. const octokit = github.getOctokit(token); diff --git a/src/action.ts b/src/action.ts index dcb83f3..4b9497d 100644 --- a/src/action.ts +++ b/src/action.ts @@ -81,6 +81,12 @@ function pullRequestTitle(dependency: string, workingDir: string): string { return title } +async function wasCodeUpdated() { + const output = await exec.getExecOutput("git", ["status", "--porcelain"]) + // If there are changes, they will appear in the stdout. Otherwise, it returns blank. + return !!output.stdout; +} + async function commitAndPushChanges(gitCommiter: GitCommitter, dependency: string, workingDir: string, token: string) { const { owner, repo } = github.context.repo; const head = pullRequestBranch(dependency, workingDir) @@ -95,11 +101,13 @@ async function commitAndPushChanges(gitCommiter: GitCommitter, dependency: strin // Checkout to new branch and commit await exec.exec("git", ["checkout", "-b", head]) await exec.exec("git", ["add", "."]) + const commitMessage = "Update dependencies using Patcher by Gruntwork" await exec.exec("git", ["commit", "-m", commitMessage]) // Push changes to head branch await exec.exec("git", ["push", "--force", "origin", `${head}:refs/heads/${head}`]) + } async function openPullRequest(octokit: GitHub, gitCommiter: GitCommitter, patcherRawOutput: string, dependency: string, workingDir: string, token: string) { @@ -214,13 +222,17 @@ async function runPatcher(octokit: GitHub, gitCommiter: GitCommitter, binaryPath {env: getPatcherEnvVars(token)}); core.endGroup() - core.startGroup("Commit and push changes") - await commitAndPushChanges(gitCommiter, dependency, workingDir, token) - core.endGroup() - - core.startGroup("Opening pull request") - await openPullRequest(octokit, gitCommiter, updateOutput.stdout, dependency, workingDir, token) - core.endGroup() + if (await wasCodeUpdated()) { + core.startGroup("Commit and push changes") + await commitAndPushChanges(gitCommiter, dependency, workingDir, token) + core.endGroup() + + core.startGroup("Opening pull request") + await openPullRequest(octokit, gitCommiter, updateOutput.stdout, dependency, workingDir, token) + core.endGroup() + } else { + core.info(`No changes in ${dependency} after running Patcher. No further action is necessary.`) + } return }