Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support updating existing tag ref #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 7 additions & 7 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,7 @@ 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 +70,8 @@ export default async function main() {

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

const validTags = await getValidTags(
prefixRegex,
/true/i.test(shouldFetchAllTags)
);
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 +217,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 +228,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,
});
}
}
14 changes: 3 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@ 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
) {
const tags = await listTags(shouldFetchAllTags);

export async function getValidTags(tags: Tags, prefixRegex: RegExp) {
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
Loading