Skip to content

Commit

Permalink
Support updating existing tag ref
Browse files Browse the repository at this point in the history
  • Loading branch information
erodozer committed Dec 27, 2024
1 parent 9d35d64 commit fc68960
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 48 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ inputs:
custom_tag:
description: "Custom tag name. If specified, it overrides bump settings."
required: false
force_update:
description: "Updates the sha of a tag if it already exists"
required: false
default: "false"
custom_release_rules:
description: "Comma separated list of release rules. Format: `<keyword>:<release_type>`. Example: `hotfix:patch,pre-feat:preminor`."
required: false
Expand Down
17 changes: 12 additions & 5 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
mapCustomReleaseRules,
mergeWithDefaultChangelogRules,
} from './utils';
import { createTag } from './github';
import { createTag, listTags } from './github';
import { Await } from './ts';

export default async function main() {
Expand All @@ -22,6 +22,9 @@ export default async function main() {
| 'false';
const tagPrefix = core.getInput('tag_prefix');
const customTag = core.getInput('custom_tag');
const forceUpdate = /true/i.test(
core.getInput('force_update')
);
const releaseBranches = core.getInput('release_branches');
const preReleaseBranches = core.getInput('pre_release_branches');
const appendToPreReleaseTag = core.getInput('append_to_pre_release_tag');
Expand Down Expand Up @@ -69,10 +72,13 @@ export default async function main() {

const prefixRegex = new RegExp(`^${tagPrefix}`);

const validTags = await getValidTags(
prefixRegex,
const tags = await listTags(
/true/i.test(shouldFetchAllTags)
);
const validTags = await getValidTags(
tags,
prefixRegex
);
const latestTag = getLatestTag(validTags, prefixRegex, tagPrefix);
const latestPrereleaseTag = getLatestPrereleaseTag(
validTags,
Expand Down Expand Up @@ -218,7 +224,8 @@ export default async function main() {
return;
}

if (validTags.map((tag) => tag.name).includes(newTag)) {
const tagExists = tags.map((tag) => tag.name).includes(newTag);
if (tagExists && !forceUpdate) {
core.info('This tag already exists. Skipping the tag creation.');
return;
}
Expand All @@ -228,5 +235,5 @@ export default async function main() {
return;
}

await createTag(newTag, createAnnotatedTag, commitRef);
await createTag(newTag, createAnnotatedTag, tagExists, commitRef);
}
27 changes: 20 additions & 7 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Await } from './ts';

let octokitSingleton: ReturnType<typeof getOctokit>;

type Tag = {
export type Tag = {
name: string;
commit: {
sha: string;
Expand All @@ -15,6 +15,8 @@ type Tag = {
node_id: string;
};

export type Tags = Await<ReturnType<typeof listTags>>;

export function getOctokitSingleton() {
if (octokitSingleton) {
return octokitSingleton;
Expand Down Expand Up @@ -68,6 +70,7 @@ export async function compareCommits(baseRef: string, headRef: string) {
export async function createTag(
newTag: string,
createAnnotatedTag: boolean,
update: boolean,
GITHUB_SHA: string
) {
const octokit = getOctokitSingleton();
Expand All @@ -85,10 +88,20 @@ export async function createTag(
});
}

core.debug(`Pushing new tag to the repo.`);
await octokit.git.createRef({
...context.repo,
ref: `refs/tags/${newTag}`,
sha: annotatedTag ? annotatedTag.data.sha : GITHUB_SHA,
});
if (update) {
core.info(`Updating existing tag ${newTag} on the repo.`);
await octokit.git.updateRef({
...context.repo,
ref: `tags/${newTag}`,
sha: annotatedTag ? annotatedTag.data.sha : GITHUB_SHA,
force: true,
});
} else {
core.info(`Pushing new tag to the repo.`);
await octokit.git.createRef({
...context.repo,
ref: `refs/tags/${newTag}`,
sha: annotatedTag ? annotatedTag.data.sha : GITHUB_SHA,
});
}
}
13 changes: 4 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@ import * as core from '@actions/core';
import { prerelease, rcompare, valid } from 'semver';
// @ts-ignore
import DEFAULT_RELEASE_TYPES from '@semantic-release/commit-analyzer/lib/default-release-types';
import { compareCommits, listTags } from './github';
import { compareCommits, Tags } from './github';
import { defaultChangelogRules } from './defaults';
import { Await } from './ts';

type Tags = Await<ReturnType<typeof listTags>>;

export async function getValidTags(
prefixRegex: RegExp,
shouldFetchAllTags: boolean
tags: Tags,
prefixRegex: RegExp
) {
const tags = await listTags(shouldFetchAllTags);

const invalidTags = tags.filter(
(tag) =>
!prefixRegex.test(tag.name) || !valid(tag.name.replace(prefixRegex, ''))
);

invalidTags.forEach((name) => core.debug(`Found Invalid Tag: ${name}.`));
invalidTags.forEach((tag) => core.debug(`Found Invalid Tag: ${tag.name}.`));

const validTags = tags
.filter(
Expand Down
Loading

0 comments on commit fc68960

Please sign in to comment.