@ffflorian/gh-open 3.10.1 → 3.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/GitHubClient.d.ts
CHANGED
|
@@ -9,7 +9,8 @@ export interface PullRequest {
|
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
11
|
export declare class GitHubClient {
|
|
12
|
-
private readonly
|
|
12
|
+
private readonly baseURL;
|
|
13
|
+
private readonly timeout;
|
|
13
14
|
constructor(timeout?: number);
|
|
14
15
|
getPullRequestByBranch(user: string, repository: string, branch: string): Promise<PullRequest | undefined>;
|
|
15
16
|
/**
|
package/dist/GitHubClient.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
1
|
const TWO_SECONDS_IN_MILLIS = 2000;
|
|
3
2
|
export class GitHubClient {
|
|
4
3
|
constructor(timeout = TWO_SECONDS_IN_MILLIS) {
|
|
5
|
-
this.
|
|
4
|
+
this.baseURL = 'https://api.github.com';
|
|
5
|
+
this.timeout = timeout;
|
|
6
6
|
}
|
|
7
7
|
async getPullRequestByBranch(user, repository, branch) {
|
|
8
8
|
const pullRequests = await this.getPullRequests(user, repository);
|
|
@@ -12,12 +12,12 @@ export class GitHubClient {
|
|
|
12
12
|
* @see https://developer.github.com/v3/pulls/#list-pull-requests
|
|
13
13
|
*/
|
|
14
14
|
async getPullRequests(user, repository) {
|
|
15
|
-
const resourceUrl = `repos/${user}/${repository}/pulls
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return response.
|
|
15
|
+
const resourceUrl = new URL(`repos/${user}/${repository}/pulls`, this.baseURL);
|
|
16
|
+
resourceUrl.search = new URLSearchParams({ per_page: '100', state: 'open' }).toString();
|
|
17
|
+
const response = await fetch(resourceUrl, { signal: AbortSignal.timeout(this.timeout) });
|
|
18
|
+
if (!response.ok) {
|
|
19
|
+
throw new Error(`Error while fetching pull requests: ${response.statusText}`);
|
|
20
|
+
}
|
|
21
|
+
return response.json();
|
|
22
22
|
}
|
|
23
23
|
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
"author": "Florian Imdahl <git@ffflorian.de>",
|
|
3
3
|
"bin": "dist/cli.js",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"axios": "1.13.2",
|
|
6
5
|
"commander": "14.0.2",
|
|
7
6
|
"find-up": "8.0.0",
|
|
8
7
|
"logdown": "3.3.1",
|
|
@@ -36,13 +35,13 @@
|
|
|
36
35
|
"name": "@ffflorian/gh-open",
|
|
37
36
|
"repository": "https://github.com/ffflorian/node-packages/tree/main/packages/gh-open",
|
|
38
37
|
"scripts": {
|
|
39
|
-
"build": "tsc -p tsconfig.json",
|
|
38
|
+
"build": "tsc -p tsconfig.build.json",
|
|
40
39
|
"clean": "rimraf dist",
|
|
41
40
|
"dist": "yarn clean && yarn build",
|
|
42
41
|
"start": "tsx src/cli.ts -d",
|
|
43
42
|
"test": "vitest run"
|
|
44
43
|
},
|
|
45
44
|
"type": "module",
|
|
46
|
-
"version": "3.
|
|
47
|
-
"gitHead": "
|
|
45
|
+
"version": "3.11.1",
|
|
46
|
+
"gitHead": "624bfac36e769ed7157fe9a9ada1c3e5443745ff"
|
|
48
47
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { assert, expect, describe, test } from 'vitest';
|
|
2
|
-
import nock from 'nock';
|
|
3
|
-
import { StatusCodes as HTTP_STATUS } from 'http-status-codes';
|
|
4
|
-
import { GitHubClient } from './GitHubClient.js';
|
|
5
|
-
const TEN_SECONDS_IN_MILLIS = 10000;
|
|
6
|
-
const HALF_SECOND_IN_MILLIS = 500;
|
|
7
|
-
describe('GitHubClient', () => {
|
|
8
|
-
describe('getPullRequests', () => {
|
|
9
|
-
test('cancels the request after a given time', async () => {
|
|
10
|
-
nock('https://api.github.com')
|
|
11
|
-
.get(/repos\/.*\/.*\/pulls/)
|
|
12
|
-
.query(true)
|
|
13
|
-
.delay(TEN_SECONDS_IN_MILLIS)
|
|
14
|
-
.reply(HTTP_STATUS.OK);
|
|
15
|
-
const gitHubClient = new GitHubClient(HALF_SECOND_IN_MILLIS);
|
|
16
|
-
try {
|
|
17
|
-
await gitHubClient.getPullRequests('user', 'repository');
|
|
18
|
-
assert.fail('Should not have resolved');
|
|
19
|
-
}
|
|
20
|
-
catch (error) {
|
|
21
|
-
expect(error.message).toBe('timeout of 500ms exceeded');
|
|
22
|
-
}
|
|
23
|
-
finally {
|
|
24
|
-
nock.cleanAll();
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
describe('getPullRequestsByBranch', () => {
|
|
29
|
-
test('correctly parses pull requests', async () => {
|
|
30
|
-
const exampleData = [
|
|
31
|
-
{
|
|
32
|
-
_links: {
|
|
33
|
-
html: {
|
|
34
|
-
href: 'https://github.com/user/repo/pull/1234',
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
head: {
|
|
38
|
-
ref: 'branch-name',
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
];
|
|
42
|
-
nock('https://api.github.com')
|
|
43
|
-
.get(/repos\/.*\/.*\/pulls/)
|
|
44
|
-
.query(true)
|
|
45
|
-
.reply(HTTP_STATUS.OK, exampleData);
|
|
46
|
-
const gitHubClient = new GitHubClient();
|
|
47
|
-
const result = await gitHubClient.getPullRequestByBranch('user', 'repository', 'branch-name');
|
|
48
|
-
expect(result).toEqual(exampleData[0]);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { expect, describe, test } from 'vitest';
|
|
2
|
-
import { RepositoryService } from './RepositoryService.js';
|
|
3
|
-
describe('RepositoryService', () => {
|
|
4
|
-
const repositoryService = new RepositoryService();
|
|
5
|
-
describe('getFullUrl', () => {
|
|
6
|
-
const normalizedUrl = 'https://github.com/ffflorian/gh-open';
|
|
7
|
-
const testRegex = (str) => {
|
|
8
|
-
const match = repositoryService['parser'].fullUrl.exec(str);
|
|
9
|
-
expect(match[0]).toEqual(expect.any(String));
|
|
10
|
-
const replaced = str.replace(repositoryService['parser'].fullUrl, 'https://$1/$2');
|
|
11
|
-
expect(replaced).toBe(normalizedUrl);
|
|
12
|
-
};
|
|
13
|
-
test('converts complete git URLs', () => {
|
|
14
|
-
const gitUrl = 'git@github.com:ffflorian/gh-open.git';
|
|
15
|
-
testRegex(gitUrl);
|
|
16
|
-
});
|
|
17
|
-
test('converts git URLs without a suffix', () => {
|
|
18
|
-
const gitUrl = 'git@github.com:ffflorian/gh-open';
|
|
19
|
-
testRegex(gitUrl);
|
|
20
|
-
});
|
|
21
|
-
test('converts git URLs without a user', () => {
|
|
22
|
-
const gitUrl = 'github.com:ffflorian/gh-open.git';
|
|
23
|
-
testRegex(gitUrl);
|
|
24
|
-
});
|
|
25
|
-
test('converts complete https URLs', () => {
|
|
26
|
-
const gitUrl = 'https://github.com/ffflorian/gh-open.git';
|
|
27
|
-
testRegex(gitUrl);
|
|
28
|
-
});
|
|
29
|
-
test('converts https URLs without suffix', () => {
|
|
30
|
-
const gitUrl = 'https://github.com/ffflorian/gh-open';
|
|
31
|
-
testRegex(gitUrl);
|
|
32
|
-
});
|
|
33
|
-
test('converts https URLs with a username', () => {
|
|
34
|
-
const gitUrl = 'https://git@github.com/ffflorian/gh-open.git';
|
|
35
|
-
testRegex(gitUrl);
|
|
36
|
-
});
|
|
37
|
-
test('converts https URLs with a username and password', () => {
|
|
38
|
-
const gitUrl = 'https://git:password@github.com/ffflorian/gh-open.git';
|
|
39
|
-
testRegex(gitUrl);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
describe('parseGitConfig', () => {
|
|
43
|
-
const rawUrl = 'git@github.com:ffflorian/gh-open.git';
|
|
44
|
-
const testRegex = (str) => {
|
|
45
|
-
const match = repositoryService['parser'].rawUrl.exec(str);
|
|
46
|
-
expect(match.groups.rawUrl).toBe(rawUrl);
|
|
47
|
-
};
|
|
48
|
-
test('converts a normal git config', () => {
|
|
49
|
-
const gitConfig = `[remote "origin"]
|
|
50
|
-
url = git@github.com:ffflorian/gh-open.git
|
|
51
|
-
fetch = +refs/heads/*:refs/remotes/origin/*
|
|
52
|
-
[branch "main"]
|
|
53
|
-
remote = origin
|
|
54
|
-
merge = refs/heads/main`;
|
|
55
|
-
testRegex(gitConfig);
|
|
56
|
-
});
|
|
57
|
-
describe('parseGitBranch', () => {
|
|
58
|
-
const testRegex = (str, result) => {
|
|
59
|
-
const match = repositoryService['parser'].gitBranch.exec(str);
|
|
60
|
-
expect(match.groups.branch).toBe(result);
|
|
61
|
-
};
|
|
62
|
-
test('detects the main branch', () => {
|
|
63
|
-
const rawBranch = 'main';
|
|
64
|
-
const gitHead = 'ref: refs/heads/main\n';
|
|
65
|
-
testRegex(gitHead, rawBranch);
|
|
66
|
-
});
|
|
67
|
-
test('detects a branch with a slash', () => {
|
|
68
|
-
const rawBranch = 'fix/regex';
|
|
69
|
-
const gitHead = 'ref: refs/heads/fix/regex\n';
|
|
70
|
-
testRegex(gitHead, rawBranch);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
});
|