Merge branch 'master' into feature/commit_sha

# Conflicts:
#	src/action.ts
This commit is contained in:
Yann Savary 2021-10-28 14:07:53 +02:00
commit 2fca5fd609
9 changed files with 107 additions and 16 deletions

View File

@ -17,16 +17,14 @@ jobs:
- uses: actions/checkout@v2
- name: Bump version and push tag
id: tag_version
uses: mathieudutour/github-tag-action@v5.5
uses: mathieudutour/github-tag-action@v5.6
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Create a GitHub release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
uses: ncipollo/release-action@v1
with:
tag_name: ${{ steps.tag_version.outputs.new_tag }}
release_name: Release ${{ steps.tag_version.outputs.new_tag }}
tag: ${{ steps.tag_version.outputs.new_tag }}
name: Release ${{ steps.tag_version.outputs.new_tag }}
body: ${{ steps.tag_version.outputs.changelog }}
```
@ -35,6 +33,10 @@ jobs:
- **github_token** _(required)_ - Required for permission to tag the repo. Usually `${{ secrets.GITHUB_TOKEN }}`.
- **commit_sha** _(optional)_ - Use this commit SHA instead GITHUB_SHA.
#### Fetch all tags
- **fetch_all_tags** _(optional)_ - By default, this action fetch the last 100 tags from Github. Sometimes, this is not enough and using this action will fetch all tags recursively (default: `false`).
#### Filter branches
- **release_branches** _(optional)_ - Comma separated list of branches (JavaScript regular expression accepted) that will generate the release tags. Other branches and pull-requests generate versions postfixed with the commit hash and do not generate any repository tag. Examples: `master` or `.*` or `release.*,hotfix.*,master`... (default: `master,main`).

View File

@ -47,6 +47,10 @@ inputs:
description: "Boolean to create an annotated tag rather than lightweight."
required: false
default: "false"
fetch_all_tags:
description: "Boolean to fetch all tags for a repo (if false, only the last 100 will be fetched)."
required: false
default: "false"
dry_run:
description: "Do not perform tagging, just calculate next version and changelog, then exit."
required: false

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "github-tag-action",
"version": "5.5.0",
"version": "5.6.0",
"lockfileVersion": 2,
"requires": true,
"packages": {

View File

@ -1,6 +1,6 @@
{
"name": "github-tag-action",
"version": "5.5.0",
"version": "5.6.0",
"private": true,
"description": "A GitHub Action to automatically bump and tag master, on merge, with the latest SemVer formatted version.",
"main": "lib/main.js",

View File

@ -25,6 +25,7 @@ export default async function main() {
const createAnnotatedTag = !!core.getInput('create_annotated_tag');
const dryRun = core.getInput('dry_run');
const customReleaseRules = core.getInput('custom_release_rules');
const shouldFetchAllTags = core.getInput('fetch_all_tags');
const commit_sha = core.getInput('commit_sha');
let mappedReleaseRules;
@ -61,7 +62,10 @@ export default async function main() {
const prefixRegex = new RegExp(`^${tagPrefix}`);
const validTags = await getValidTags(prefixRegex);
const validTags = await getValidTags(
prefixRegex,
/true/i.test(shouldFetchAllTags)
);
const latestTag = getLatestTag(validTags, prefixRegex, tagPrefix);
const latestPrereleaseTag = getLatestPrereleaseTag(
validTags,
@ -173,7 +177,7 @@ export default async function main() {
commits,
logger: { log: console.info.bind(console) },
options: {
repositoryUrl: `https://github.com/${process.env.GITHUB_REPOSITORY}`,
repositoryUrl: `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}`,
},
lastRelease: { gitTag: latestTag.name },
nextRelease: { gitTag: newTag, version: newVersion },

View File

@ -4,6 +4,17 @@ import { Await } from './ts';
let octokitSingleton: ReturnType<typeof getOctokit>;
type Tag = {
name: string;
commit: {
sha: string;
url: string;
};
zipball_url: string;
tarball_url: string;
node_id: string;
};
export function getOctokitSingleton() {
if (octokitSingleton) {
return octokitSingleton;
@ -13,15 +24,27 @@ export function getOctokitSingleton() {
return octokitSingleton;
}
export async function listTags() {
/**
* Fetch all tags for a given repository recursively
*/
export async function listTags(
shouldFetchAllTags = false,
fetchedTags: Tag[] = [],
page = 1
): Promise<Tag[]> {
const octokit = getOctokitSingleton();
const tags = await octokit.repos.listTags({
...context.repo,
per_page: 100,
page,
});
return tags.data;
if (tags.data.length < 100 || shouldFetchAllTags === false) {
return [...fetchedTags, ...tags.data];
}
return listTags(shouldFetchAllTags, [...fetchedTags, ...tags.data], page + 1);
}
/**

View File

@ -8,8 +8,11 @@ import { Await } from './ts';
type Tags = Await<ReturnType<typeof listTags>>;
export async function getValidTags(prefixRegex: RegExp) {
const tags = await listTags();
export async function getValidTags(
prefixRegex: RegExp,
shouldFetchAllTags: boolean
) {
const tags = await listTags(shouldFetchAllTags);
const invalidTags = tags.filter(
(tag) => !valid(tag.name.replace(prefixRegex, ''))

55
tests/github.test.ts Normal file
View File

@ -0,0 +1,55 @@
import { listTags } from '../src/github';
jest.mock(
'@actions/github',
jest.fn().mockImplementation(() => ({
context: { repo: { owner: 'mock-owner', repo: 'mock-repo' } },
getOctokit: jest.fn().mockReturnValue({
repos: {
listTags: jest.fn().mockImplementation(({ page }: { page: number }) => {
if (page === 6) {
return { data: [] };
}
const res = [...new Array(100).keys()].map((_) => ({
name: `v0.0.${_ + (page - 1) * 100}`,
commit: { sha: 'string', url: 'string' },
zipball_url: 'string',
tarball_url: 'string',
node_id: 'string',
}));
return { data: res };
}),
},
}),
}))
);
describe('github', () => {
it('returns all tags', async () => {
const tags = await listTags(true);
expect(tags.length).toEqual(500);
expect(tags[499]).toEqual({
name: 'v0.0.499',
commit: { sha: 'string', url: 'string' },
zipball_url: 'string',
tarball_url: 'string',
node_id: 'string',
});
});
it('returns only the last 100 tags', async () => {
const tags = await listTags(true);
expect(tags.length).toEqual(500);
expect(tags[99]).toEqual({
name: 'v0.0.99',
commit: { sha: 'string', url: 'string' },
zipball_url: 'string',
tarball_url: 'string',
node_id: 'string',
});
});
});

View File

@ -71,7 +71,7 @@ describe('utils', () => {
/*
* When
*/
const validTags = await getValidTags(regex);
const validTags = await getValidTags(regex, false);
/*
* Then
@ -121,7 +121,7 @@ describe('utils', () => {
/*
* When
*/
const validTags = await getValidTags(regex);
const validTags = await getValidTags(regex, false);
/*
* Then