Skip to content

Commit 623c605

Browse files
committed
refactor: move common operations out of checkRun
1 parent 858f39e commit 623c605

File tree

4 files changed

+91
-51
lines changed

4 files changed

+91
-51
lines changed

src/api/github/checkRun.test.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { Octokit } from '@octokit/rest';
22
import { Endpoints } from '@octokit/types';
3-
import git, { ReadCommitResult } from 'isomorphic-git';
43
import { mocked } from 'ts-jest/utils';
54

65
import * as GitHub from '../github';
76

87
import { createCheckRun } from './checkRun';
8+
import { getHeadSha, getOwnerRepo } from './util';
99

1010
type CreateCheckRunResponse =
1111
Endpoints['POST /repos/{owner}/{repo}/check-runs']['response'];
1212

1313
jest.mock('@octokit/rest');
1414
jest.mock('isomorphic-git');
15+
jest.mock('./util');
1516

1617
const mockClient = {
1718
checks: {
@@ -50,12 +51,13 @@ describe('createCheckRun', () => {
5051

5152
beforeEach(() => {
5253
mocked(Octokit).mockReturnValue(mockClient as unknown as Octokit);
53-
mocked(git.listRemotes).mockResolvedValue([
54-
{ remote: 'origin', url: '[email protected]:seek-oss/skuba.git' },
55-
]);
56-
mocked(git.log).mockResolvedValue([
57-
{ oid: 'cdd335a418c3dc6804be1c642b19bb63437e2cad' } as ReadCommitResult,
58-
]);
54+
mocked(getHeadSha).mockResolvedValue(
55+
'cdd335a418c3dc6804be1c642b19bb63437e2cad',
56+
);
57+
mocked(getOwnerRepo).mockResolvedValue({
58+
owner: 'seek-oss',
59+
repo: 'skuba',
60+
});
5961
mockClient.checks.create.mockReturnValue(createResponse);
6062
});
6163

src/api/github/checkRun.ts

+2-44
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Octokit } from '@octokit/rest';
22
import { Endpoints } from '@octokit/types';
3-
import fs from 'fs-extra';
4-
import git from 'isomorphic-git';
53

64
import { pluralise } from '../../utils/logging';
75

6+
import { getHeadSha, getOwnerRepo } from './util';
7+
88
type Output = NonNullable<
99
Endpoints['POST /repos/{owner}/{repo}/check-runs']['parameters']['output']
1010
>;
@@ -13,48 +13,6 @@ export type Annotation = NonNullable<Output['annotations']>[number];
1313

1414
const GITHUB_MAX_ANNOTATIONS = 50;
1515

16-
/**
17-
* Matches the owner and repository names in a GitHub repository URL.
18-
*
19-
* For example, given the following input strings:
20-
*
21-
* ```console
22-
* [email protected]:seek-oss/skuba.git
23-
* https://github.com/seek-oss/skuba.git
24-
* ```
25-
*
26-
* This pattern will produce the following matches:
27-
*
28-
* 1. seek-oss
29-
* 2. skuba
30-
*/
31-
const ownerRepoRegex = /github.com(?::|\/)(.+)\/(.+).git$/;
32-
33-
const getOwnerRepo = async (
34-
dir: string,
35-
): Promise<{ owner: string; repo: string }> => {
36-
const remotes = await git.listRemotes({ dir, fs });
37-
38-
for (const { url } of remotes) {
39-
const match = ownerRepoRegex.exec(url);
40-
41-
const owner = match?.[1];
42-
const repo = match?.[2];
43-
44-
if (owner && repo) {
45-
return { owner, repo };
46-
}
47-
}
48-
49-
throw new Error('Could not find a GitHub remote');
50-
};
51-
52-
const getHeadSha = async (dir: string): Promise<string> => {
53-
const [commit] = await git.log({ depth: 1, dir, fs });
54-
55-
return commit.oid;
56-
};
57-
5816
/**
5917
* Suffixes the title with the number of annotations added, e.g.
6018
*

src/api/github/util.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import git, { ReadCommitResult } from 'isomorphic-git';
2+
import { mocked } from 'ts-jest/utils';
3+
4+
import { getHeadSha, getOwnerRepo } from './util';
5+
6+
const dir = process.cwd();
7+
8+
beforeEach(() => {
9+
mocked(git.listRemotes).mockResolvedValue([
10+
{ remote: 'origin', url: '[email protected]:seek-oss/skuba.git' },
11+
]);
12+
mocked(git.log).mockResolvedValue([
13+
{ oid: 'cdd335a418c3dc6804be1c642b19bb63437e2cad' } as ReadCommitResult,
14+
]);
15+
});
16+
17+
afterEach(() => {
18+
jest.resetAllMocks();
19+
});
20+
21+
describe('getOwnerRepo', () => {
22+
it('should extract a GitHub owner and repo from Git remotes', () => {
23+
expect(getOwnerRepo(dir)).toStrictEqual({
24+
owner: 'seek-oss',
25+
repo: 'skuba',
26+
});
27+
});
28+
});
29+
30+
describe('getHeadSha', () => {
31+
it('should extract a GitHub owner and repo from Git remotes', () => {
32+
expect(getHeadSha(dir)).toBe('cdd335a418c3dc6804be1c642b19bb63437e2cad');
33+
});
34+
});

src/api/github/util.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import fs from 'fs-extra';
2+
import git from 'isomorphic-git';
3+
4+
const getHeadSha = async (dir: string): Promise<string> => {
5+
const [commit] = await git.log({ depth: 1, dir, fs });
6+
7+
return commit.oid;
8+
};
9+
10+
/**
11+
* Matches the owner and repository names in a GitHub repository URL.
12+
*
13+
* For example, given the following input strings:
14+
*
15+
* ```console
16+
* [email protected]:seek-oss/skuba.git
17+
* https://github.com/seek-oss/skuba.git
18+
* ```
19+
*
20+
* This pattern will produce the following matches:
21+
*
22+
* 1. seek-oss
23+
* 2. skuba
24+
*/
25+
const ownerRepoRegex = /github.com(?::|\/)(.+)\/(.+).git$/;
26+
27+
const getOwnerRepo = async (
28+
dir: string,
29+
): Promise<{ owner: string; repo: string }> => {
30+
const remotes = await git.listRemotes({ dir, fs });
31+
32+
for (const { url } of remotes) {
33+
const match = ownerRepoRegex.exec(url);
34+
35+
const owner = match?.[1];
36+
const repo = match?.[2];
37+
38+
if (owner && repo) {
39+
return { owner, repo };
40+
}
41+
}
42+
43+
throw new Error('Could not find a GitHub remote');
44+
};
45+
46+
export { getOwnerRepo, getHeadSha };

0 commit comments

Comments
 (0)