diff --git a/plugins/git-release-manager/src/features/Stats/helpers/getTagDates.test.ts b/plugins/git-release-manager/src/features/Stats/helpers/getTagDates.test.ts index e02aac9551..3274ecc31f 100644 --- a/plugins/git-release-manager/src/features/Stats/helpers/getTagDates.test.ts +++ b/plugins/git-release-manager/src/features/Stats/helpers/getTagDates.test.ts @@ -23,7 +23,54 @@ import { import { getTagDates } from './getTagDates'; describe('getTagDates', () => { - beforeEach(jest.clearAllMocks); + beforeEach(() => { + jest.clearAllMocks(); + jest.resetAllMocks(); + }); + + it('should get tag dates for startTag when is tag and endTag is undefined', async () => { + (mockApiClient.getTag as jest.Mock).mockResolvedValueOnce( + createMockTag({ date: 'TAG-START' }), + ); + + const result = await getTagDates({ + pluginApiClient: mockApiClient, + project: mockSemverProject, + startTag: { + tagSha: 'sha-start', + tagType: 'tag', + }, + }); + + expect(result).toMatchInlineSnapshot(` + Object { + "endDate": undefined, + "startDate": "TAG-START", + } + `); + }); + + it('should get tag dates for startTag when startTag is commit and endTag is undefined', async () => { + (mockApiClient.getCommit as jest.Mock).mockResolvedValueOnce( + createMockCommit({ createdAt: 'COMMIT_START' }), + ); + + const result = await getTagDates({ + pluginApiClient: mockApiClient, + project: mockSemverProject, + startTag: { + tagSha: 'sha-start', + tagType: 'commit', + }, + }); + + expect(result).toMatchInlineSnapshot(` + Object { + "endDate": undefined, + "startDate": "COMMIT_START", + } + `); + }); it('should get tag dates when startTag & endTag both are of type tag', async () => { (mockApiClient.getTag as jest.Mock) diff --git a/plugins/git-release-manager/src/features/Stats/helpers/getTagDates.ts b/plugins/git-release-manager/src/features/Stats/helpers/getTagDates.ts index d4803be4be..d79d7489fd 100644 --- a/plugins/git-release-manager/src/features/Stats/helpers/getTagDates.ts +++ b/plugins/git-release-manager/src/features/Stats/helpers/getTagDates.ts @@ -25,7 +25,7 @@ interface GetTagDates { tagSha: string; tagType: 'tag' | 'commit'; }; - endTag: { + endTag?: { tagSha: string; tagType: 'tag' | 'commit'; }; @@ -37,6 +37,33 @@ export const getTagDates = async ({ startTag, endTag, }: GetTagDates) => { + if (!endTag) { + if (startTag.tagType === 'tag') { + const { tag: startTagResponse } = await pluginApiClient.getTag({ + owner: project.owner, + repo: project.repo, + tagSha: startTag.tagSha, + }); + + return { + startDate: startTagResponse.date, + endDate: undefined, + }; + } + + // If tagType is not a 'tag', it has to be a commit + const { commit: startCommit } = await pluginApiClient.getCommit({ + owner: project.owner, + repo: project.repo, + ref: startTag.tagSha, + }); + + return { + startDate: startCommit.createdAt, + endDate: undefined, + }; + } + if (startTag.tagType === 'tag' && endTag.tagType === 'tag') { const [ { tag: startTagResponse }, @@ -124,7 +151,7 @@ async function getCommitFromTag({ }: { pluginApiClient: GetTagDates['pluginApiClient']; project: GetTagDates['project']; - tag: GetTagDates['startTag'] | GetTagDates['endTag']; + tag: GetTagDates['startTag'] | NonNullable; }) { const { tag: tagResponse } = await pluginApiClient.getTag({ owner: project.owner,