Skip to content

Commit

Permalink
feat: path input
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Oct 4, 2019
1 parent 04911be commit db4aa16
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,23 @@ async function main() {
title: core.getInput("title"),
body: core.getInput("body"),
branch: core.getInput("branch"),
path: core.getInput("path"),
commitMessage: core.getInput("commit-message"),
author: core.getInput("author")
};

core.debug(`Inputs: ${inspect(inputs)}`);

const { hasChanges, hasUncommitedChanges } = await getLocalChanges();
const { hasChanges, hasUncommitedChanges } = await getLocalChanges(
inputs.path
);

if (!hasChanges) {
core.info("No local changes");
if (inputs.path) {
core.info(`No local changes matchin "${inputs.path}"`);
} else {
core.info("No local changes");
}
process.exit(0); // there is currently no neutral exit code
}

Expand All @@ -70,10 +77,16 @@ async function main() {
});
}

core.debug(`Comitting local changes`);
await command("git add .", { shell: true });
if (inputs.path) {
core.debug(`Committing local changes matching "${inputs.path}"`);
await command(`git add "${inputs.path}"`, { shell: true });
} else {
core.debug(`Committing all local changes`);
await command("git add .", { shell: true });
}

await command(
`git commit -a -m "${inputs.commitMessage}" --author "${inputs.author}"`,
`git commit -m "${inputs.commitMessage}" --author "${inputs.author}"`,
{ shell: true }
);
} else {
Expand All @@ -84,12 +97,11 @@ async function main() {
const remoteBranchExists = await checkOutRemoteBranch(inputs.branch);

core.debug(`Pushing local changes`);
const { stdout: pushStdOut, stderr: pushStdErr } = await command(
await command(
`git push -f https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git HEAD:refs/heads/${inputs.branch}`,
{ shell: true }
);

// no idea why the `git push` output goes into stderr. Checking in both just in case.
if (remoteBranchExists) {
core.info(`Existing pull request for "${inputs.branch}" updated`);
return;
Expand All @@ -115,8 +127,10 @@ async function main() {
}
}

async function getLocalChanges() {
const { stdout } = await command("git status", { shell: true });
async function getLocalChanges(path) {
const { stdout } = await command(`git status ${path || "*"}`, {
shell: true
});

if (/Your branch is up to date/.test(stdout)) {
return;
Expand Down Expand Up @@ -162,16 +176,27 @@ async function setGitUser({ name, email }) {

async function checkOutRemoteBranch(branch) {
try {
const { stdout } = await command(`git rev-parse --abbrev-ref HEAD`, {
shell: true
});

if (stdout === branch) {
core.info(`Already in "${branch}".`);
return true;
}

await command(
`git fetch https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git ${branch}:${branch}`,
{ shell: true }
);

await command(`git checkout ${branch}`, { shell: true });
core.info(`Remote branch "${branch}" checked out locally.`);
await command(`git rebase -Xtheirs -`, { shell: true });
await command(`git rebase -Xtheirs --autostash -`, { shell: true });
return true;
} catch (error) {
core.info(`Branch "${branch}" does not yet exist on remote.`);
await command(`git checkout -b ${branch}`, { shell: true });
return false;
}
}

0 comments on commit db4aa16

Please sign in to comment.