-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathgithub.test.ts
251 lines (217 loc) Β· 7.36 KB
/
github.test.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { describe, it, expect, vi } from "vitest";
const mocks = vi.hoisted(() => {
return {
mockGithubFetch: vi.fn(),
};
});
describe("github", () => {
describe("listGithubReleases", () => {
it("should call githubFetch with the correct parameters", async () => {
vi.mock("ofetch", () => ({
$fetch: mocks.mockGithubFetch,
}));
const config = {
repo: { repo: "owner/repo", domain: "domain" },
tokens: { github: "test-token" },
};
const { listGithubReleases } = await import("../src/github");
await listGithubReleases(config as any);
expect(mocks.mockGithubFetch).toHaveBeenCalledWith(
"/repos/owner/repo/releases",
{
baseURL: "https://domain/api/v3",
headers: {
authorization: "Bearer test-token",
"x-github-api-version": "2022-11-28",
},
query: {
per_page: 100,
},
}
);
});
});
describe("getGithubReleaseByTag", () => {
it("should call githubFetch with the correct parameters", async () => {
vi.mock("ofetch", () => ({
$fetch: mocks.mockGithubFetch,
}));
const config = {
repo: { repo: "owner/repo", domain: "domain" },
tokens: { github: "test-token" },
};
const { getGithubReleaseByTag } = await import("../src/github");
await getGithubReleaseByTag(config as any, "v1.0.0");
expect(mocks.mockGithubFetch).toHaveBeenCalledWith(
"/repos/owner/repo/releases/tags/v1.0.0",
{
baseURL: "https://domain/api/v3",
headers: {
authorization: "Bearer test-token",
"x-github-api-version": "2022-11-28",
},
}
);
});
});
describe("getGithubChangelog", () => {
it("should call githubFetch with the correct parameters", async () => {
vi.mock("ofetch", () => ({
$fetch: mocks.mockGithubFetch,
}));
const config = {
repo: { repo: "owner/repo", domain: "domain" },
tokens: { github: "test-token" },
};
const { getGithubChangelog } = await import("../src/github");
await getGithubChangelog(config as any);
expect(mocks.mockGithubFetch).toHaveBeenCalledWith(
"https://raw.githubusercontent.com/owner/repo/main/CHANGELOG.md",
{
baseURL: "https://domain/api/v3",
headers: {
authorization: "Bearer test-token",
"x-github-api-version": "2022-11-28",
},
}
);
});
});
describe("createGithubRelease", () => {
it("should call githubFetch with the correct parameters", async () => {
vi.mock("ofetch", () => ({
$fetch: mocks.mockGithubFetch,
}));
const config = {
repo: { repo: "owner/repo", domain: "domain" },
tokens: { github: "test-token" },
};
const { createGithubRelease } = await import("../src/github");
await createGithubRelease(config as any, { tag_name: "v1.0.0" });
expect(mocks.mockGithubFetch).toHaveBeenCalledWith(
"/repos/owner/repo/releases",
{
baseURL: "https://domain/api/v3",
body: {
tag_name: "v1.0.0",
},
headers: {
authorization: "Bearer test-token",
"x-github-api-version": "2022-11-28",
},
method: "POST",
}
);
});
});
describe("updateGithubRelease", () => {
it("should call githubFetch with the correct parameters", async () => {
vi.mock("ofetch", () => ({
$fetch: mocks.mockGithubFetch,
}));
const config = {
repo: { repo: "owner/repo", domain: "domain" },
tokens: { github: "test-token" },
};
const { updateGithubRelease } = await import("../src/github");
await updateGithubRelease(config as any, "id", { tag_name: "v1.0.0" });
expect(mocks.mockGithubFetch).toHaveBeenCalledWith(
"/repos/owner/repo/releases/id",
{
baseURL: "https://domain/api/v3",
body: {
tag_name: "v1.0.0",
},
headers: {
authorization: "Bearer test-token",
"x-github-api-version": "2022-11-28",
},
method: "PATCH",
}
);
});
});
describe("githubNewReleaseURL", () => {
it("should return expected value", async () => {
const config = {
repo: { repo: "owner/repo", domain: "domain" },
tokens: { github: "test-token" },
};
const release = { version: "1.0.0", body: "Release notes" };
const { githubNewReleaseURL } = await import("../src/github");
const actual = githubNewReleaseURL(config as any, release);
expect(actual).toBe(
"https://domain/owner/repo/releases/new?tag=v1.0.0&title=v1.0.0&body=Release%20notes"
);
});
});
describe("resolveGithubToken", () => {
it("should return the token if it exists CHANGELOGEN_TOKENS_GITHUB", async () => {
process.env.CHANGELOGEN_TOKENS_GITHUB = "CHANGELOGEN_TOKENS_GITHUB";
const config = {
tokens: { github: "token" },
};
const { resolveGithubToken } = await import("../src/github");
const actual = await resolveGithubToken(config as any);
expect(actual).toBe("CHANGELOGEN_TOKENS_GITHUB");
delete process.env.CHANGELOGEN_TOKENS_GITHUB;
});
it("should return the token if it exists GITHUB_TOKEN", async () => {
process.env.GITHUB_TOKEN = "GITHUB_TOKEN";
const config = {
tokens: { github: "token" },
};
const { resolveGithubToken } = await import("../src/github");
const actual = await resolveGithubToken(config as any);
expect(actual).toBe("GITHUB_TOKEN");
delete process.env.GITHUB_TOKEN;
});
it("should return the token if it exists GH_TOKEN", async () => {
process.env.GH_TOKEN = "GH_TOKEN";
const config = {
tokens: { github: "token" },
};
const { resolveGithubToken } = await import("../src/github");
const actual = await resolveGithubToken(config as any);
expect(actual).toBe("GH_TOKEN");
delete process.env.GH_TOKEN;
});
});
describe("syncGithubRelease", () => {
it("if tokens undefined, should return status is manual", async () => {
vi.mock("ofetch", () => ({
$fetch: mocks.mockGithubFetch,
}));
const config = {
repo: { repo: "owner/repo", domain: "domain" },
tokens: { github: undefined },
};
const release = {
version: "1.0.0",
body: "Release notes",
};
const { syncGithubRelease } = await import("../src/github");
const actual = await syncGithubRelease(config as any, release);
expect(actual.status).toBe("manual");
});
it("if currentGhRelease exists, should return status is updated", async () => {
vi.mock("ofetch", () => ({
$fetch: mocks.mockGithubFetch.mockResolvedValue({
id: "id",
tag_name: "v1.0.0",
}),
}));
const config = {
repo: { repo: "owner/repo", domain: "domain" },
tokens: { github: "test-token" },
};
const release = {
version: "1.0.0",
body: "Release notes",
};
const { syncGithubRelease } = await import("../src/github");
const actual = await syncGithubRelease(config as any, release);
expect(actual.status).toBe("updated");
});
});
});