Use listMatchingRefs to get github Tags

This commit is contained in:
liamnv 2025-03-14 13:17:52 +07:00
parent 9d35d648e8
commit 13fdc94c0e
No known key found for this signature in database
GPG Key ID: 9E72A0478BF39054
4 changed files with 44 additions and 8 deletions

View File

@ -70,8 +70,8 @@ export default async function main() {
const prefixRegex = new RegExp(`^${tagPrefix}`);
const validTags = await getValidTags(
prefixRegex,
/true/i.test(shouldFetchAllTags)
tagPrefix,
prefixRegex
);
const latestTag = getLatestTag(validTags, prefixRegex, tagPrefix);
const latestPrereleaseTag = getLatestPrereleaseTag(

View File

@ -14,6 +14,16 @@ type Tag = {
tarball_url: string;
node_id: string;
};
type Ref = {
ref: string;
node_id: string;
url: string;
object: {
sha: string;
type: string;
url: string;
};
};
export function getOctokitSingleton() {
if (octokitSingleton) {
@ -47,6 +57,31 @@ export async function listTags(
return listTags(shouldFetchAllTags, [...fetchedTags, ...tags.data], page + 1);
}
// Github API does not support filtering tags by prefix and it only allows getting 100 tags at a time.
// We need to use listMatchingRefs to get all tags.
export async function listRefs(
tagPrefix: string,
): Promise<Tag[]> {
const octokit = getOctokitSingleton();
const refs = await octokit.git.listMatchingRefs({
...context.repo,
ref: `tags/${tagPrefix}`,
});
console.log(refs.data);
return refs.data.map((ref: Ref) => ({
name: ref.ref.replace('refs/tags/', ''),
commit: {
sha: ref.object.sha,
url: ref.object.url,
},
zipball_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/zipball/${ref.ref.replace('refs/tags/', '')}`,
tarball_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/tarball/${ref.ref.replace('refs/tags/', '')}`,
node_id: ref.node_id,
}));
}
/**
* Compare `headRef` to `baseRef` (i.e. baseRef...headRef)
* @param baseRef - old commit

View File

@ -2,17 +2,17 @@ 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, listRefs, listTags } from './github';
import { defaultChangelogRules } from './defaults';
import { Await } from './ts';
type Tags = Await<ReturnType<typeof listTags>>;
export async function getValidTags(
tagPrefix: string,
prefixRegex: RegExp,
shouldFetchAllTags: boolean
) {
const tags = await listTags(shouldFetchAllTags);
const tags = await listRefs(tagPrefix);
const invalidTags = tags.filter(
(tag) =>

View File

@ -8,6 +8,7 @@ jest.spyOn(core, 'debug').mockImplementation(() => {});
jest.spyOn(core, 'warning').mockImplementation(() => {});
const regex = /^v/;
const tagPrefix = 'v';
describe('utils', () => {
it('extracts branch from ref', () => {
@ -71,7 +72,7 @@ describe('utils', () => {
/*
* When
*/
const validTags = await getValidTags(regex, false);
const validTags = await getValidTags(tagPrefix, regex);
/*
* Then
@ -121,7 +122,7 @@ describe('utils', () => {
/*
* When
*/
const validTags = await getValidTags(regex, false);
const validTags = await getValidTags(tagPrefix, regex);
/*
* Then
@ -169,7 +170,7 @@ describe('utils', () => {
/*
* When
*/
const validTags = await getValidTags(/^app1\//, false);
const validTags = await getValidTags(tagPrefix, regex);
/*
* Then
*/