Skip to content

Commit

Permalink
build(release): compiled action for 1.2.2
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
semantic-release-bot committed Oct 8, 2019
1 parent a469646 commit 81e887a
Showing 1 changed file with 96 additions and 68 deletions.
164 changes: 96 additions & 68 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ const { command } = __webpack_require__(955);
const core = __webpack_require__(470);
const { request } = __webpack_require__(753);

const TEMPORARY_BRANCH_NAME = `tmp-create-or-update-pull-request-action-${Math.random()
.toString(36)
.substr(2)}`;

main();

async function main() {
Expand Down Expand Up @@ -485,62 +489,81 @@ async function main() {

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

const { hasChanges, hasUncommitedChanges } = await getLocalChanges(
inputs.path
);
const {
data: { default_branch }
} = await request(`GET /repos/${process.env.GITHUB_REPOSITORY}`, {
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`
}
});
const DEFAULT_BRANCH = default_branch;
core.debug(`DEFAULT_BRANCH: ${DEFAULT_BRANCH}`);

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

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

if (hasUncommitedChanges) {
core.debug(`Uncommited changes found`);

const gitUser = await getGitUser();
if (gitUser) {
core.debug(`Git User already configured as: ${inspect(gitUser)}`);
} else {
const matches = inputs.author.match(/^([^<]+)\s*<([^>]+)>$/);
assert(
matches,
`The "author" input "${inputs.author}" does conform to the "Name <email@domain.test>" format`
);
const [, name, email] = matches;

await setGitUser({
name,
email
});
}
core.debug(`Local changes found`);

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 runShellCommand(`git checkout -b "${TEMPORARY_BRANCH_NAME}"`);

await command(
`git commit -m "${inputs.commitMessage}" --author "${inputs.author}"`,
{ shell: true }
const gitUser = await getGitUser();
if (gitUser) {
core.debug(`Git User already configured as: ${inspect(gitUser)}`);
} else {
const matches = inputs.author.match(/^([^<]+)\s*<([^>]+)>$/);
assert(
matches,
`The "author" input "${inputs.author}" does conform to the "Name <email@domain.test>" format`
);
const [, name, email] = matches;

await setGitUser({
name,
email
});
}

if (inputs.path) {
core.debug(`Committing local changes matching "${inputs.path}"`);
await runShellCommand(`git add "${inputs.path}"`);
} else {
core.debug(`No uncommited changes found`);
core.debug(`Committing all local changes`);
await runShellCommand("git add .");
}

core.debug(`Try to fetch and checkout remote branch`);
await runShellCommand(
`git commit -m "${inputs.commitMessage}" --author "${inputs.author}"`
);

const currentBranch = await runShellCommand(
`git rev-parse --abbrev-ref HEAD`
);

if (currentBranch === DEFAULT_BRANCH) {
core.info(`Already in basen branch "${branch}".`);
} else {
core.debug(`rebase all local changes on base branch`);
await runShellCommand(
`git fetch https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git ${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`
);
await runShellCommand(`git stash --include-untracked`);
await runShellCommand(`git rebase -X theirs "${DEFAULT_BRANCH}"`);
}

core.debug(`Try to fetch and checkout remote branch "${inputs.branch}"`);
const remoteBranchExists = await checkOutRemoteBranch(inputs.branch);

core.debug(`Pushing local changes`);
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 }
await runShellCommand(
`git push -f https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git HEAD:refs/heads/${inputs.branch}`
);

if (remoteBranchExists) {
Expand All @@ -558,45 +581,38 @@ async function main() {
title: inputs.title,
body: inputs.body,
head: inputs.branch,
base: process.env.GITHUB_REF.substr("refs/heads/".length)
base: DEFAULT_BRANCH
});

core.info(`Pull request created: ${html_url}`);
await runShellCommand(`git stash pop || true`);
} catch (error) {
core.debug(inspect(error));
core.setFailed(error.message);
}
}

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

if (/Your branch is up to date/.test(stdout)) {
if (/Your branch is up to date/.test(output)) {
return;
}

const hasCommits = /Your branch is ahead/.test(stdout);
const hasUncommitedChanges = /(Changes to be committed|Changes not staged|Untracked files)/.test(
stdout
output
);

return {
hasCommits,
hasUncommitedChanges,
hasChanges: hasCommits || hasUncommitedChanges
hasChanges: hasUncommitedChanges
};
}

async function getGitUser() {
try {
const { stdout: name } = await command("git config --get user.name", {
shell: true
});
const { stdout: email } = await command("git config --get user.email", {
shell: true
});
const name = await runShellCommand("git config --get user.name");
const email = await runShellCommand("git config --get user.email");

return {
name,
Expand All @@ -609,45 +625,57 @@ async function getGitUser() {

async function setGitUser({ name, email }) {
core.debug(`Configuring user.name as "${name}"`);
await command(`git config --global user.name "${name}"`, { shell: true });
await runShellCommand(`git config --global user.name "${name}"`);

core.debug(`Configuring user.email as "${email}"`);
await command(`git config --global user.email "${email}"`, { shell: true });
await runShellCommand(`git config --global user.email "${email}"`);
}

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

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

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

await runShellCommand(`git branch`);

core.debug(`Checking out "${branch}" branch locally`);
await command(`git checkout ${branch}`, { shell: true });
await runShellCommand(`git checkout ${branch}`);
core.info(`Remote branch "${branch}" checked out locally.`);

core.debug(
`Rebasing "${branch}" locally using "git rebase -Xtheirs --autostash -"`
);
await command(`git rebase -Xtheirs --autostash -`, { shell: true });
await runShellCommand(`git rebase -X theirs "${TEMPORARY_BRANCH_NAME}"`);

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

async function runShellCommand(commandString) {
core.debug(`$ ${commandString}`);
try {
const { stdout, stderr } = await command(commandString, { shell: true });
const output = [stdout, stderr].filter(Boolean).join("\n");
core.debug(output);
return output;
} catch (error) {
core.debug(inspect(error));
throw error;
}
}


/***/ }),

Expand Down

0 comments on commit 81e887a

Please sign in to comment.