Skip to content

Commit

Permalink
Merge pull request #20 from gruntwork-io/validate-changes-before-commit
Browse files Browse the repository at this point in the history
bug fix: Validate code changes before committing
  • Loading branch information
marinalimeira authored Aug 24, 2023
2 parents 4b6f915 + dba0013 commit fccda1b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
27 changes: 21 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 19 additions & 7 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit fccda1b

Please sign in to comment.