Skip to content

Commit

Permalink
fix: Override release branch and tag if they already exist (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzypixelz authored Apr 11, 2024
1 parent 8532b05 commit 37fd8ef
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 50 deletions.
2 changes: 1 addition & 1 deletion dist/build-crates-debian-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -128158,7 +128158,7 @@ function command_sh(cmd, options) {
// important
env: {
...process.env,
...options.env
...options.env,
},
stdio: "pipe",
shell: true,
Expand Down
2 changes: 1 addition & 1 deletion dist/build-crates-standalone-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -128157,7 +128157,7 @@ function command_sh(cmd, options) {
// important
env: {
...process.env,
...options.env
...options.env,
},
stdio: "pipe",
shell: true,
Expand Down
21 changes: 12 additions & 9 deletions dist/bump-crates-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -80895,7 +80895,7 @@ function command_sh(cmd, options) {
// important
env: {
...process.env,
...options.env
...options.env,
},
stdio: "pipe",
shell: true,
Expand Down Expand Up @@ -82339,12 +82339,6 @@ async function main(input) {
const remote = `https://${input.githubToken}@github.com/${input.repo}.git`;
command_sh(`git clone --recursive --single-branch --branch ${input.branch} ${remote}`);
command_sh(`ls ${workspace}`);
const tags = command_sh("git tag", { cwd: repo }).split("\n");
if (tags.includes(input.version)) {
lib_core.info(`Tag ${input.version} has already been created`);
await cleanup(input);
return;
}
await bump(workspace, input.version);
command_sh("git add .", { cwd: repo });
command_sh(`git commit --message 'chore: Bump version to \`${input.version}\`'`, { cwd: repo, env: gitEnv });
Expand All @@ -82363,10 +82357,19 @@ async function main(input) {
check: false,
});
}
command_sh(`git tag ${input.version} --message v${input.version}`, { cwd: repo, env: gitEnv });
command_sh(`git push ${remote} ${input.branch}`, { cwd: repo });
const tagExists = command_sh("git tag", { cwd: repo }).split("\n").includes(input.version);
if (tagExists) {
lib_core.info(`Tag ${input.version} already exists and will be replaced`);
command_sh(`git tag --force ${input.version} --message v${input.version}`, { cwd: repo, env: gitEnv });
command_sh(`git push --force ${remote} ${input.version}`, { cwd: repo });
}
else {
command_sh(`git tag ${input.version} --message v${input.version}`, { cwd: repo, env: gitEnv });
command_sh(`git push ${remote} ${input.version}`, { cwd: repo });
}
command_sh("git log -10", { cwd: repo });
command_sh("git show-ref --tags", { cwd: repo });
command_sh(`git push ${remote} ${input.branch} ${input.version}`, { cwd: repo });
await cleanup(input);
}
catch (error) {
Expand Down
25 changes: 14 additions & 11 deletions dist/create-release-branch-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24756,7 +24756,7 @@ function sh(cmd, options) {
// important
env: {
...process.env,
...options.env
...options.env,
},
stdio: "pipe",
shell: true,
Expand Down Expand Up @@ -24806,6 +24806,9 @@ async function main(input) {
sh(`git clone --recursive ${remote}`);
const version = input.version ?? sh("git describe", { cwd: repo }).trimEnd();
core.setOutput("version", version);
const refsPattern = "refs/remotes/origin/release/dry-run";
const refsRaw = sh(`git for-each-ref --format='%(refname)' --sort=authordate ${refsPattern}`, { cwd: repo });
const refs = refsRaw.split("\n");
let branch;
if (input.liveRun) {
branch = `release/${version}`;
Expand All @@ -24814,20 +24817,20 @@ async function main(input) {
else {
branch = `release/dry-run/${version}`;
core.setOutput("branch", branch);
const refsPattern = "refs/remotes/origin/release/dry-run";
const refsRaw = sh(`git for-each-ref --format='%(refname)' --sort=authordate ${refsPattern}`, { cwd: repo });
const refs = refsRaw.split("\n");
if (refs.includes(`refs/remotes/origin/${branch}`)) {
core.info(`Release branch for ${version} has already been created`);
await cleanup(input);
return;
}
if (refs.length >= input.dryRunHistorySize) {
sh(`git push origin --delete ${refs.at(0)}`, { cwd: repo });
}
}
sh(`git switch --create ${branch}`, { cwd: repo });
sh(`git push ${remote} ${branch}`, { cwd: repo });
const branchExists = refs.includes(`refs/remotes/origin/${branch}`);
if (branchExists) {
core.info(`Release branch for ${version} already exists and will be updated`);
sh(`git switch ${branch}`, { cwd: repo });
sh(`git push --force ${remote} ${branch}`, { cwd: repo });
}
else {
sh(`git switch --create ${branch}`, { cwd: repo });
sh(`git push ${remote} ${branch}`, { cwd: repo });
}
await cleanup(input);
}
catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion dist/publish-crates-cargo-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -82016,7 +82016,7 @@ function command_sh(cmd, options) {
// important
env: {
...process.env,
...options.env
...options.env,
},
stdio: "pipe",
shell: true,
Expand Down
2 changes: 1 addition & 1 deletion dist/publish-crates-debian-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -127190,7 +127190,7 @@ function command_sh(cmd, options) {
// important
env: {
...process.env,
...options.env
...options.env,
},
stdio: "pipe",
shell: true,
Expand Down
2 changes: 1 addition & 1 deletion dist/publish-crates-eclipse-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -127190,7 +127190,7 @@ function command_sh(cmd, options) {
// important
env: {
...process.env,
...options.env
...options.env,
},
stdio: "pipe",
shell: true,
Expand Down
2 changes: 1 addition & 1 deletion dist/publish-crates-github-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -127186,7 +127186,7 @@ function command_sh(cmd, options) {
// important
env: {
...process.env,
...options.env
...options.env,
},
stdio: "pipe",
shell: true,
Expand Down
2 changes: 1 addition & 1 deletion dist/publish-crates-homebrew-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -127190,7 +127190,7 @@ function command_sh(cmd, options) {
// important
env: {
...process.env,
...options.env
...options.env,
},
stdio: "pipe",
shell: true,
Expand Down
21 changes: 12 additions & 9 deletions src/bump-crates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ export async function main(input: Input) {
sh(`git clone --recursive --single-branch --branch ${input.branch} ${remote}`);
sh(`ls ${workspace}`);

const tags = sh("git tag", { cwd: repo }).split("\n");
if (tags.includes(input.version)) {
core.info(`Tag ${input.version} has already been created`);
await cleanup(input);
return;
}

await cargo.bump(workspace, input.version);
sh("git add .", { cwd: repo });
sh(`git commit --message 'chore: Bump version to \`${input.version}\`'`, { cwd: repo, env: gitEnv });
Expand All @@ -77,10 +70,20 @@ export async function main(input: Input) {
});
}

sh(`git tag ${input.version} --message v${input.version}`, { cwd: repo, env: gitEnv });
sh(`git push ${remote} ${input.branch}`, { cwd: repo });

const tagExists = sh("git tag", { cwd: repo }).split("\n").includes(input.version);
if (tagExists) {
core.info(`Tag ${input.version} already exists and will be replaced`);
sh(`git tag --force ${input.version} --message v${input.version}`, { cwd: repo, env: gitEnv });
sh(`git push --force ${remote} ${input.version}`, { cwd: repo });
} else {
sh(`git tag ${input.version} --message v${input.version}`, { cwd: repo, env: gitEnv });
sh(`git push ${remote} ${input.version}`, { cwd: repo });
}

sh("git log -10", { cwd: repo });
sh("git show-ref --tags", { cwd: repo });
sh(`git push ${remote} ${input.branch} ${input.version}`, { cwd: repo });

await cleanup(input);
} catch (error) {
Expand Down
3 changes: 1 addition & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { spawnSync } from "child_process";
import * as core from "@actions/core";
import * as os from "os";

const MAX_BUFFER = 10 * 1024 * 1024;

Expand All @@ -26,7 +25,7 @@ export function sh(cmd: string, options?: CommandOptions): string {
// important
env: {
...process.env,
...options.env
...options.env,
},
stdio: "pipe",
shell: true,
Expand Down
25 changes: 13 additions & 12 deletions src/create-release-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export async function main(input: Input) {
const version = input.version ?? sh("git describe", { cwd: repo }).trimEnd();
core.setOutput("version", version);

const refsPattern = "refs/remotes/origin/release/dry-run";
const refsRaw = sh(`git for-each-ref --format='%(refname)' --sort=authordate ${refsPattern}`, { cwd: repo });
const refs = refsRaw.split("\n");

let branch: string;
if (input.liveRun) {
branch = `release/${version}`;
Expand All @@ -48,23 +52,20 @@ export async function main(input: Input) {
branch = `release/dry-run/${version}`;
core.setOutput("branch", branch);

const refsPattern = "refs/remotes/origin/release/dry-run";
const refsRaw = sh(`git for-each-ref --format='%(refname)' --sort=authordate ${refsPattern}`, { cwd: repo });
const refs = refsRaw.split("\n");

if (refs.includes(`refs/remotes/origin/${branch}`)) {
core.info(`Release branch for ${version} has already been created`);
await cleanup(input);
return;
}

if (refs.length >= input.dryRunHistorySize) {
sh(`git push origin --delete ${refs.at(0)}`, { cwd: repo });
}
}

sh(`git switch --create ${branch}`, { cwd: repo });
sh(`git push ${remote} ${branch}`, { cwd: repo });
const branchExists = refs.includes(`refs/remotes/origin/${branch}`);
if (branchExists) {
core.info(`Release branch for ${version} already exists and will be updated`);
sh(`git switch ${branch}`, { cwd: repo });
sh(`git push --force ${remote} ${branch}`, { cwd: repo });
} else {
sh(`git switch --create ${branch}`, { cwd: repo });
sh(`git push ${remote} ${branch}`, { cwd: repo });
}

await cleanup(input);
} catch (error) {
Expand Down

0 comments on commit 37fd8ef

Please sign in to comment.