Skip to content

Commit 307a257

Browse files
authored
ref: Log descriptive error message when no tags are found during release preparation (#617)
1 parent 98fff70 commit 307a257

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/utils/__tests__/git.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { getLatestTag } from '../git';
2+
import * as loggerModule from '../../logger';
3+
4+
describe('getLatestTag', () => {
5+
it('returns latest tag in the repo by calling `git describe`', async () => {
6+
const git = {
7+
raw: jest.fn().mockResolvedValue('1.0.0'),
8+
} as any;
9+
10+
const latestTag = await getLatestTag(git);
11+
expect(latestTag).toBe('1.0.0');
12+
13+
expect(git.raw).toHaveBeenCalledWith('describe', '--tags', '--abbrev=0');
14+
});
15+
16+
it('logs a helpful error message if the git call throws', async () => {
17+
loggerModule.setLevel(loggerModule.LogLevel.Debug);
18+
const loggerErrorSpy = jest
19+
.spyOn(loggerModule.logger, 'error')
20+
.mockImplementation(() => {
21+
// just to avoid spamming the test output
22+
});
23+
24+
const error = new Error('Nothing to describe');
25+
const git = {
26+
raw: jest.fn().mockRejectedValue(error),
27+
} as any;
28+
29+
try {
30+
await getLatestTag(git);
31+
} catch (e) {
32+
expect(e).toBe(error);
33+
}
34+
35+
expect(loggerErrorSpy).toHaveBeenCalledWith(
36+
expect.stringContaining(
37+
"If you're releasing for the first time, check if your repo contains any tags"
38+
)
39+
);
40+
});
41+
});

src/utils/git.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ export async function getDefaultBranch(
3535

3636
export async function getLatestTag(git: SimpleGit): Promise<string> {
3737
// This part is courtesy of https://stackoverflow.com/a/7261049/90297
38-
return (await git.raw('describe', '--tags', '--abbrev=0')).trim();
38+
try {
39+
return (await git.raw('describe', '--tags', '--abbrev=0')).trim();
40+
} catch (e) {
41+
logger.error(
42+
'Couldn\'t get the latest tag! If you\'re releasing for the first time, check if your repo contains any tags. If not, add one manually and try again: `git tag 0.0.0 "$(git log -1 --reverse --format=%h)"'
43+
);
44+
// handle this error in the global error handler
45+
throw e;
46+
}
3947
}
4048

4149
export async function getChangesSince(

0 commit comments

Comments
 (0)