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

feat: add support for providing patterns to match tags #2098

Merged
merged 6 commits into from
May 21, 2024
Merged
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
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ inputs:
description: "Use POSIX path separator `/` for output file paths on Windows."
required: false
default: "false"
tags_pattern:
description: "Tags pattern to include."
required: false
default: "*"
tags_ignore_pattern:
description: "Tags pattern to ignore."
required: false
default: ""


outputs:
added_files:
Expand Down
30 changes: 26 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/inputs.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ exports[`getInputs should correctly parse boolean inputs 1`] = `
"since": "",
"sinceLastRemoteCommit": "false",
"skipInitialFetch": "true",
"tagsIgnorePattern": "",
"tagsPattern": "*",
"token": "",
"until": "",
"usePosixPathSeparator": "false",
Expand Down Expand Up @@ -113,6 +115,8 @@ exports[`getInputs should correctly parse numeric inputs 1`] = `
"since": "",
"sinceLastRemoteCommit": false,
"skipInitialFetch": false,
"tagsIgnorePattern": "",
"tagsPattern": "",
"token": "",
"until": "",
"usePosixPathSeparator": false,
Expand Down Expand Up @@ -171,6 +175,8 @@ exports[`getInputs should correctly parse string inputs 1`] = `
"since": "",
"sinceLastRemoteCommit": false,
"skipInitialFetch": false,
"tagsIgnorePattern": "",
"tagsPattern": "",
"token": "token",
"until": "",
"usePosixPathSeparator": false,
Expand Down Expand Up @@ -231,6 +237,8 @@ exports[`getInputs should handle invalid numeric inputs correctly 1`] = `
"since": "",
"sinceLastRemoteCommit": false,
"skipInitialFetch": false,
"tagsIgnorePattern": "",
"tagsPattern": "",
"token": "",
"until": "",
"usePosixPathSeparator": false,
Expand Down Expand Up @@ -291,6 +299,8 @@ exports[`getInputs should handle negative numeric inputs correctly 1`] = `
"since": "",
"sinceLastRemoteCommit": false,
"skipInitialFetch": false,
"tagsIgnorePattern": "",
"tagsPattern": "",
"token": "",
"until": "",
"usePosixPathSeparator": false,
Expand Down Expand Up @@ -352,6 +362,8 @@ exports[`getInputs should return default values when no inputs are provided 1`]
"since": "",
"sinceLastRemoteCommit": false,
"skipInitialFetch": false,
"tagsIgnorePattern": "",
"tagsPattern": "*",
"token": "",
"until": "",
"usePosixPathSeparator": false,
Expand Down
195 changes: 194 additions & 1 deletion src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import {ChangeTypeEnum} from '../changedFiles'
import {Inputs} from '../inputs'
import {
getDirname,
getDirnameMaxDepth,
getFilteredChangedFiles,
getPreviousGitTag,
normalizeSeparators,
warnUnsupportedRESTAPIInputs
} from '../utils'
Expand Down Expand Up @@ -638,7 +640,9 @@ describe('utils test', () => {
useRestApi: false,
excludeSubmodules: false,
fetchMissingHistoryMaxRetries: 10,
usePosixPathSeparator: false
usePosixPathSeparator: false,
tagsPattern: '*',
tagsIgnorePattern: ''
}

const coreWarningSpy = jest.spyOn(core, 'warning')
Expand All @@ -654,4 +658,193 @@ describe('utils test', () => {
expect(coreWarningSpy).toHaveBeenCalledTimes(1)
})
})
describe('getPreviousGitTag', () => {
// Function returns the second latest tag and its SHA
it('should return the second latest tag and its SHA when multiple tags are present', async () => {
jest
.spyOn(exec, 'getExecOutput')
.mockResolvedValueOnce({
stdout: 'v1.0.1\nv1.0.0\nv0.9.9',
stderr: '',
exitCode: 0
})
.mockResolvedValueOnce({
stdout: 'abc123',
stderr: '',
exitCode: 0
})
const result = await getPreviousGitTag({
cwd: '.',
tagsPattern: '*',
tagsIgnorePattern: ''
})
expect(result).toEqual({tag: 'v1.0.0', sha: 'abc123'})
})

// Tags are filtered by a specified pattern when 'tagsPattern' is provided
it('should filter tags by the specified pattern', async () => {
jest
.spyOn(exec, 'getExecOutput')
.mockResolvedValueOnce({
stdout: 'v1.0.1\nv1.0.0\nv0.9.9',
stderr: '',
exitCode: 0
})
.mockResolvedValueOnce({
stdout: 'def456',
stderr: '',
exitCode: 0
})
const result = await getPreviousGitTag({
cwd: '.',
tagsPattern: 'v1.*',
tagsIgnorePattern: ''
})
expect(result).toEqual({tag: 'v1.0.0', sha: 'def456'})
})

// Tags are excluded by a specified ignore pattern when 'tagsIgnorePattern' is provided
it('should exclude tags by the specified ignore pattern', async () => {
jest
.spyOn(exec, 'getExecOutput')
.mockResolvedValueOnce({
stdout: 'v1.0.1\nv1.0.0\nv0.9.9',
stderr: '',
exitCode: 0
})
.mockResolvedValueOnce({
stdout: 'ghi789',
stderr: '',
exitCode: 0
})
const result = await getPreviousGitTag({
cwd: '.',
tagsPattern: '*',
tagsIgnorePattern: 'v0.*.*'
})
expect(result).toEqual({tag: 'v1.0.0', sha: 'ghi789'})
})

// Function executes silently when debug mode is not active
it('should execute silently when debug mode is not active', async () => {
jest.spyOn(core, 'isDebug').mockReturnValue(false)
const spy = jest
.spyOn(exec, 'getExecOutput')
.mockResolvedValueOnce({
stdout: 'v1.0.1\nv1.0.0',
stderr: '',
exitCode: 0
})
.mockResolvedValueOnce({
stdout: 'jkl012',
stderr: '',
exitCode: 0
})
await getPreviousGitTag({
cwd: '.',
tagsPattern: '*',
tagsIgnorePattern: ''
})
expect(spy).toHaveBeenCalledWith('git', ['tag', '--sort=-creatordate'], {
cwd: '.',
silent: true
})
})

// No tags are available in the repository
it('should return empty values when no tags are available in the repository', async () => {
jest.spyOn(exec, 'getExecOutput').mockResolvedValueOnce({
stdout: '',
stderr: '',
exitCode: 0
})
const result = await getPreviousGitTag({
cwd: '.',
tagsPattern: '*',
tagsIgnorePattern: ''
})
expect(result).toEqual({tag: '', sha: ''})
})

// Only one tag is available, making it impossible to find a previous tag
it('should return empty values when only one tag is available', async () => {
jest.spyOn(exec, 'getExecOutput').mockResolvedValueOnce({
stdout: 'v1.0.1',
stderr: '',
exitCode: 0
})
const result = await getPreviousGitTag({
cwd: '.',
tagsPattern: '*',
tagsIgnorePattern: ''
})
expect(result).toEqual({tag: '', sha: ''})
})

// Provided 'tagsPattern' matches no tags
it('should return empty values when provided tagsPattern matches no tags', async () => {
jest.spyOn(exec, 'getExecOutput').mockResolvedValueOnce({
stdout: 'v1.0.1\nv1.0.0',
stderr: '',
exitCode: 0
})
const result = await getPreviousGitTag({
cwd: '.',
tagsPattern: 'nonexistent*',
tagsIgnorePattern: ''
})
expect(result).toEqual({tag: '', sha: ''})
})

// Provided 'tagsIgnorePattern' excludes all tags
it('should return empty values when provided tagsIgnorePattern excludes all tags', async () => {
jest.spyOn(exec, 'getExecOutput').mockResolvedValueOnce({
stdout: 'v1.0.1\nv1.0.0',
stderr: '',
exitCode: 0
})
const result = await getPreviousGitTag({
cwd: '.',
tagsPattern: '*',
tagsIgnorePattern: 'v*'
})
expect(result).toEqual({tag: '', sha: ''})
})

// Git commands fail and throw errors
it('should throw an error when git commands fail', async () => {
jest
.spyOn(exec, 'getExecOutput')
.mockRejectedValue(new Error('git command failed'))
await expect(
getPreviousGitTag({cwd: '.', tagsPattern: '*', tagsIgnorePattern: ''})
).rejects.toThrow('git command failed')
})

// Debug mode logs additional information
it('should log additional information when debug mode is active', async () => {
jest.spyOn(core, 'isDebug').mockReturnValue(true)
const spy = jest
.spyOn(exec, 'getExecOutput')
.mockResolvedValueOnce({
stdout: 'v1.0.1\nv1.0.0',
stderr: '',
exitCode: 0
})
.mockResolvedValueOnce({
stdout: 'mno345',
stderr: '',
exitCode: 0
})
await getPreviousGitTag({
cwd: '.',
tagsPattern: '*',
tagsIgnorePattern: ''
})
expect(spy).toHaveBeenCalledWith('git', ['tag', '--sort=-creatordate'], {
cwd: '.',
silent: false
})
})
})
})
6 changes: 5 additions & 1 deletion src/commitSha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ export const getSHAForNonPullRequestEvent = async ({
}
} else if (isTag) {
core.debug('Getting previous SHA for tag...')
const {sha, tag} = await getPreviousGitTag({cwd: workingDirectory})
const {sha, tag} = await getPreviousGitTag({
cwd: workingDirectory,
tagsPattern: inputs.tagsPattern,
tagsIgnorePattern: inputs.tagsIgnorePattern
})
previousSha = sha
targetBranch = tag
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ export const DEFAULT_VALUES_OF_UNSUPPORTED_API_INPUTS: Partial<Inputs> = {
dirNamesDeletedFilesIncludeOnlyDeletedDirs: false,
excludeSubmodules: false,
fetchMissingHistoryMaxRetries: 10,
usePosixPathSeparator: false
usePosixPathSeparator: false,
tagsPattern: '*',
tagsIgnorePattern: ''
}
Loading
Loading