-
-
Notifications
You must be signed in to change notification settings - Fork 511
/
github.js
130 lines (123 loc) Β· 3.48 KB
/
github.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import nock from 'nock';
const interceptAuthentication = ({ api = 'https://api.github.com', username = 'john' } = {}) => {
nock(api).get('/user').reply(200, {
login: username
});
};
const interceptCollaborator = ({
api = 'https://api.github.com',
owner = 'user',
project = 'repo',
username = 'john'
} = {}) => {
nock(api).get(`/repos/${owner}/${project}/collaborators/${username}`).reply(204);
};
const interceptListReleases = ({
host = 'github.com',
api = 'https://api.github.com',
owner = 'user',
project = 'repo',
tag_name
} = {}) => {
nock(api)
.get(`/repos/${owner}/${project}/releases?per_page=1&page=1`)
.reply(200, [
{
id: 1,
upload_url: `https://uploads.${host}/repos/${owner}/${project}/releases/1/assets{?name,label}`,
html_url: `https://${host}/${owner}/${project}/releases/tag/${tag_name}`,
tag_name,
target_commitish: 'main',
name: `Release ${tag_name}`,
body: 'Description of the release',
draft: false,
prerelease: false
}
]);
};
const interceptCreate = ({
api = 'https://api.github.com',
host = 'github.com',
owner = 'user',
project = 'repo',
body: { tag_name, name = '', generate_release_notes = false, body = null, prerelease = false, draft = false }
} = {}) => {
nock(api)
.post(`/repos/${owner}/${project}/releases`, {
tag_name,
name,
body,
prerelease,
draft,
generate_release_notes
})
.reply(() => {
const id = 1;
const responseBody = {
id,
tag_name,
name,
body,
prerelease,
draft,
generate_release_notes,
upload_url: `https://uploads.${host}/repos/${owner}/${project}/releases/${id}/assets{?name,label}`,
html_url: `https://${host}/${owner}/${project}/releases/tag/${tag_name}`
};
return [200, responseBody, { location: `${api}/repos/${owner}/${project}/releases/${id}` }];
});
};
const interceptUpdate = ({
host = 'github.com',
api = 'https://api.github.com',
owner = 'user',
project = 'repo',
body: { tag_name, name = '', body = null, prerelease = false, draft = false, generate_release_notes = false }
} = {}) => {
nock(api)
.patch(`/repos/${owner}/${project}/releases/1`, { tag_name, name, body, draft, prerelease, generate_release_notes })
.reply(200, {
id: 1,
tag_name,
name,
body,
prerelease,
draft,
generate_release_notes,
upload_url: `https://uploads.${host}/repos/${owner}/${project}/releases/1/assets{?name,label}`,
html_url: `https://${host}/${owner}/${project}/releases/tag/${tag_name}`
});
};
const interceptAsset = ({
api = 'https://api.github.com',
host = 'github.com',
owner = 'user',
project = 'repo',
tagName,
body = {}
} = {}) => {
nock(`https://uploads.${host}`)
.post(`/repos/${owner}/${project}/releases/1/assets`, body)
.query(true)
.reply(200, function () {
const id = 1;
const [, name] = this.req.path.match(/\?name=([^&]+)/);
return {
id,
url: `${api}/repos/${owner}/${project}/releases/assets/${id}`,
name,
label: '',
state: 'uploaded',
size: this.req.headers['content-length'],
browser_download_url: `https://${host}/${owner}/${project}/releases/download/${tagName}/${name}`
};
});
};
export {
interceptAuthentication,
interceptCollaborator,
interceptListReleases,
interceptCreate,
interceptUpdate,
interceptAsset
};