3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2025-02-23 05:54:33 +00:00

fix: bug detecting previous tag when workflow is rerun (#2107)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Tonye Jack 2024-05-23 22:46:34 -06:00 committed by GitHub
parent 8a0655f075
commit 03334d095e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 128 additions and 171 deletions

61
dist/index.js generated vendored
View file

@ -1058,7 +1058,8 @@ const getSHAForNonPullRequestEvent = (_j) => __awaiter(void 0, [_j], void 0, fun
const { sha, tag } = yield (0, utils_1.getPreviousGitTag)({
cwd: workingDirectory,
tagsPattern: inputs.tagsPattern,
tagsIgnorePattern: inputs.tagsIgnorePattern
tagsIgnorePattern: inputs.tagsIgnorePattern,
currentBranch
});
previousSha = sha;
targetBranch = tag;
@ -2620,29 +2621,55 @@ const cleanShaInput = (_5) => __awaiter(void 0, [_5], void 0, function* ({ sha,
return stdout.trim();
});
exports.cleanShaInput = cleanShaInput;
const getPreviousGitTag = (_6) => __awaiter(void 0, [_6], void 0, function* ({ cwd, tagsPattern, tagsIgnorePattern }) {
const { stdout } = yield exec.getExecOutput('git', ['tag', '--sort=-creatordate'], {
const getPreviousGitTag = (_6) => __awaiter(void 0, [_6], void 0, function* ({ cwd, tagsPattern, currentBranch, tagsIgnorePattern }) {
const ignorePatterns = [];
let currentShaDate = null;
const { stdout } = yield exec.getExecOutput('git', [
'tag',
'--sort=-creatordate',
'--format=%(refname:short)|%(objectname)|%(creatordate:iso)'
], {
cwd,
silent: !core.isDebug()
});
let tags = stdout.trim().split('\n');
if (tagsPattern) {
tags = tags.filter(tag => micromatch_1.default.isMatch(tag, tagsPattern));
}
if (tagsIgnorePattern) {
tags = tags.filter(tag => !micromatch_1.default.isMatch(tag, tagsIgnorePattern));
ignorePatterns.push(tagsIgnorePattern);
}
if (tags.length < 2) {
if (currentBranch) {
ignorePatterns.push(currentBranch);
try {
const { stdout: currentShaDateOutput } = yield exec.getExecOutput('git', ['show', '-s', '--format=%ai', currentBranch], {
cwd,
silent: !core.isDebug()
});
currentShaDate = new Date(currentShaDateOutput.trim());
}
catch (error) {
// Handle the case where the current branch doesn't exist
// This might happen in detached head state
core.warning(`Failed to get date for current branch ${currentBranch}`);
}
}
const previousTag = { tag: '', sha: '' };
const tags = stdout.trim().split('\n');
for (const tagData of tags) {
const [tag, sha, dateString] = tagData.split('|');
if (!micromatch_1.default.isMatch(tag, tagsPattern) || micromatch_1.default.isMatch(tag, ignorePatterns)) {
continue;
}
const date = new Date(dateString);
if (currentShaDate && date >= currentShaDate) {
continue;
}
// Found a suitable tag, no need to continue
previousTag.tag = tag;
previousTag.sha = sha;
break;
}
if (!previousTag.tag) {
core.warning('No previous tag found');
return { tag: '', sha: '' };
}
const previousTag = tags[1];
const { stdout: stdout2 } = yield exec.getExecOutput('git', ['rev-parse', previousTag], {
cwd,
silent: !core.isDebug()
});
const sha = stdout2.trim();
return { tag: previousTag, sha };
return previousTag;
});
exports.getPreviousGitTag = getPreviousGitTag;
const canDiffCommits = (_7) => __awaiter(void 0, [_7], void 0, function* ({ cwd, sha1, sha2, diff }) {

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -659,95 +659,45 @@ describe('utils test', () => {
})
})
describe('getPreviousGitTag', () => {
// Function returns the second latest tag and its SHA
// 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: ''
tagsIgnorePattern: '',
currentBranch: 'v1.0.1'
})
expect(result).toEqual({
tag: 'v1.0.0',
sha: 'f0751de6af436d4e79016e2041cf6400e0833653'
})
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: ''
tagsIgnorePattern: '',
currentBranch: 'v1.0.1'
})
expect(result).toEqual({
tag: 'v1.0.0',
sha: 'f0751de6af436d4e79016e2041cf6400e0833653'
})
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.*.*'
tagsIgnorePattern: 'v0.*.*',
currentBranch: 'v1.0.1'
})
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
expect(result).toEqual({
tag: 'v1.0.0',
sha: 'f0751de6af436d4e79016e2041cf6400e0833653'
})
})
@ -761,7 +711,8 @@ describe('utils test', () => {
const result = await getPreviousGitTag({
cwd: '.',
tagsPattern: '*',
tagsIgnorePattern: ''
tagsIgnorePattern: '',
currentBranch: ''
})
expect(result).toEqual({tag: '', sha: ''})
})
@ -769,44 +720,16 @@ describe('utils test', () => {
// 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',
stdout:
'v1.0.1|f0751de6af436d4e79016e2041cf6400e0833653|2021-01-01T00:00:00Z',
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*'
tagsIgnorePattern: '',
currentBranch: 'v1.0.1'
})
expect(result).toEqual({tag: '', sha: ''})
})
@ -817,34 +740,13 @@ describe('utils test', () => {
.spyOn(exec, 'getExecOutput')
.mockRejectedValue(new Error('git command failed'))
await expect(
getPreviousGitTag({cwd: '.', tagsPattern: '*', tagsIgnorePattern: ''})
getPreviousGitTag({
cwd: '.',
tagsPattern: '*',
tagsIgnorePattern: '',
currentBranch: 'v1.0.1'
})
).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
})
})
})
})

View file

@ -243,7 +243,8 @@ export const getSHAForNonPullRequestEvent = async ({
const {sha, tag} = await getPreviousGitTag({
cwd: workingDirectory,
tagsPattern: inputs.tagsPattern,
tagsIgnorePattern: inputs.tagsIgnorePattern
tagsIgnorePattern: inputs.tagsIgnorePattern,
currentBranch
})
previousSha = sha
targetBranch = tag

View file

@ -831,53 +831,80 @@ export const cleanShaInput = async ({
return stdout.trim()
}
export const getPreviousGitTag = async ({
cwd,
tagsPattern,
currentBranch,
tagsIgnorePattern
}: {
cwd: string
tagsPattern: string
currentBranch: string
tagsIgnorePattern?: string
}): Promise<{tag: string; sha: string}> => {
const ignorePatterns: string[] = []
let currentShaDate: Date | null = null
const {stdout} = await exec.getExecOutput(
'git',
['tag', '--sort=-creatordate'],
[
'tag',
'--sort=-creatordate',
'--format=%(refname:short)|%(objectname)|%(creatordate:iso)'
],
{
cwd,
silent: !core.isDebug()
}
)
let tags = stdout.trim().split('\n')
if (tagsPattern) {
tags = tags.filter(tag => mm.isMatch(tag, tagsPattern))
}
if (tagsIgnorePattern) {
tags = tags.filter(tag => !mm.isMatch(tag, tagsIgnorePattern))
ignorePatterns.push(tagsIgnorePattern)
}
if (tags.length < 2) {
core.warning('No previous tag found')
return {tag: '', sha: ''}
}
const previousTag = tags[1]
const {stdout: stdout2} = await exec.getExecOutput(
'git',
['rev-parse', previousTag],
{
cwd,
silent: !core.isDebug()
if (currentBranch) {
ignorePatterns.push(currentBranch)
try {
const {stdout: currentShaDateOutput} = await exec.getExecOutput(
'git',
['show', '-s', '--format=%ai', currentBranch],
{
cwd,
silent: !core.isDebug()
}
)
currentShaDate = new Date(currentShaDateOutput.trim())
} catch (error) {
// Handle the case where the current branch doesn't exist
// This might happen in detached head state
core.warning(`Failed to get date for current branch ${currentBranch}`)
}
)
}
const sha = stdout2.trim()
const previousTag: {tag: string; sha: string} = {tag: '', sha: ''}
return {tag: previousTag, sha}
const tags = stdout.trim().split('\n')
for (const tagData of tags) {
const [tag, sha, dateString] = tagData.split('|')
if (!mm.isMatch(tag, tagsPattern) || mm.isMatch(tag, ignorePatterns)) {
continue
}
const date = new Date(dateString)
if (currentShaDate && date >= currentShaDate) {
continue
}
// Found a suitable tag, no need to continue
previousTag.tag = tag
previousTag.sha = sha
break
}
if (!previousTag.tag) {
core.warning('No previous tag found')
}
return previousTag
}
export const canDiffCommits = async ({