@pagopa/dx-cli 0.22.4 → 0.23.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/README.md +18 -0
- package/dist/adapters/commander/commands/add.d.ts +2 -6
- package/dist/adapters/commander/commands/add.js +4 -8
- package/dist/adapters/commander/commands/init.d.ts +2 -6
- package/dist/adapters/commander/commands/init.js +6 -5
- package/dist/adapters/commander/commands/savemoney.d.ts +11 -0
- package/dist/adapters/commander/commands/savemoney.js +30 -5
- package/dist/adapters/commander/commands/spec.d.ts +10 -0
- package/dist/adapters/commander/commands/spec.js +13 -0
- package/dist/adapters/commander/index.js +6 -2
- package/dist/adapters/commander/spec.d.ts +16 -0
- package/dist/adapters/commander/spec.js +54 -0
- package/dist/adapters/plop/generators/environment/actions.js +3 -3
- package/dist/adapters/plop/index.js +3 -5
- package/dist/adapters/plop/templates-path.d.ts +5 -0
- package/dist/adapters/plop/templates-path.js +6 -0
- package/dist/domain/dependencies.d.ts +15 -2
- package/dist/domain/spec.d.ts +50 -0
- package/dist/domain/spec.js +8 -0
- package/dist/index.js +15 -12
- package/package.json +5 -5
- package/templates/monorepo/nx.json +0 -1
- package/dist/adapters/azure/__tests__/cloud-account-repository.test.d.ts +0 -1
- package/dist/adapters/azure/__tests__/cloud-account-repository.test.js +0 -95
- package/dist/adapters/azure/__tests__/cloud-account-service.test.d.ts +0 -1
- package/dist/adapters/azure/__tests__/cloud-account-service.test.js +0 -378
- package/dist/adapters/codemods/__tests__/registry.test.d.ts +0 -1
- package/dist/adapters/codemods/__tests__/registry.test.js +0 -56
- package/dist/adapters/codemods/__tests__/use-azure-appsvc.test.d.ts +0 -1
- package/dist/adapters/codemods/__tests__/use-azure-appsvc.test.js +0 -77
- package/dist/adapters/codemods/__tests__/use-pnpm.test.d.ts +0 -1
- package/dist/adapters/codemods/__tests__/use-pnpm.test.js +0 -148
- package/dist/adapters/commander/__tests__/env.test.d.ts +0 -1
- package/dist/adapters/commander/__tests__/env.test.js +0 -45
- package/dist/adapters/commander/__tests__/error-reporting.test.d.ts +0 -1
- package/dist/adapters/commander/__tests__/error-reporting.test.js +0 -63
- package/dist/adapters/commander/__tests__/exit-with-error.test.d.ts +0 -1
- package/dist/adapters/commander/__tests__/exit-with-error.test.js +0 -92
- package/dist/adapters/commander/__tests__/global-options.test.d.ts +0 -1
- package/dist/adapters/commander/__tests__/global-options.test.js +0 -33
- package/dist/adapters/commander/commands/__tests__/add.test.d.ts +0 -4
- package/dist/adapters/commander/commands/__tests__/add.test.js +0 -167
- package/dist/adapters/commander/commands/__tests__/init.test.d.ts +0 -4
- package/dist/adapters/commander/commands/__tests__/init.test.js +0 -48
- package/dist/adapters/commander/commands/__tests__/preconditions.test.d.ts +0 -1
- package/dist/adapters/commander/commands/__tests__/preconditions.test.js +0 -32
- package/dist/adapters/commander/presenters/__tests__/index.test.d.ts +0 -1
- package/dist/adapters/commander/presenters/__tests__/index.test.js +0 -23
- package/dist/adapters/commander/presenters/__tests__/json.test.d.ts +0 -1
- package/dist/adapters/commander/presenters/__tests__/json.test.js +0 -108
- package/dist/adapters/commander/presenters/__tests__/text.test.d.ts +0 -1
- package/dist/adapters/commander/presenters/__tests__/text.test.js +0 -60
- package/dist/adapters/github/__tests__/github-repo.spec.d.ts +0 -1
- package/dist/adapters/github/__tests__/github-repo.spec.js +0 -67
- package/dist/adapters/node/__tests__/data.d.ts +0 -18
- package/dist/adapters/node/__tests__/data.js +0 -22
- package/dist/adapters/node/__tests__/package-json.test.d.ts +0 -1
- package/dist/adapters/node/__tests__/package-json.test.js +0 -86
- package/dist/adapters/node/__tests__/repository.test.d.ts +0 -1
- package/dist/adapters/node/__tests__/repository.test.js +0 -77
- package/dist/adapters/node/fs/__tests__/file-reader.test.d.ts +0 -1
- package/dist/adapters/node/fs/__tests__/file-reader.test.js +0 -80
- package/dist/adapters/node/json/__tests__/index.test.d.ts +0 -1
- package/dist/adapters/node/json/__tests__/index.test.js +0 -14
- package/dist/adapters/octokit/__tests__/index.test.d.ts +0 -1
- package/dist/adapters/octokit/__tests__/index.test.js +0 -414
- package/dist/adapters/pagopa-technology/__tests__/authorization.test.d.ts +0 -4
- package/dist/adapters/pagopa-technology/__tests__/authorization.test.js +0 -548
- package/dist/adapters/plop/__tests__/run-actions.test.d.ts +0 -1
- package/dist/adapters/plop/__tests__/run-actions.test.js +0 -68
- package/dist/adapters/plop/actions/__tests__/init-cloud-accounts.test.d.ts +0 -1
- package/dist/adapters/plop/actions/__tests__/init-cloud-accounts.test.js +0 -171
- package/dist/adapters/plop/actions/__tests__/provision-terraform-backend.test.d.ts +0 -1
- package/dist/adapters/plop/actions/__tests__/provision-terraform-backend.test.js +0 -134
- package/dist/adapters/plop/generators/environment/__tests__/actions.test.d.ts +0 -2
- package/dist/adapters/plop/generators/environment/__tests__/actions.test.js +0 -92
- package/dist/adapters/plop/generators/environment/__tests__/prompts.test.d.ts +0 -1
- package/dist/adapters/plop/generators/environment/__tests__/prompts.test.js +0 -182
- package/dist/adapters/plop/helpers/__tests__/resource-prefix.test.d.ts +0 -1
- package/dist/adapters/plop/helpers/__tests__/resource-prefix.test.js +0 -113
- package/dist/adapters/plop/helpers/__tests__/terraform-state-key.test.d.ts +0 -1
- package/dist/adapters/plop/helpers/__tests__/terraform-state-key.test.js +0 -35
- package/dist/adapters/plop/helpers/__tests__/validate-prompt.test.d.ts +0 -1
- package/dist/adapters/plop/helpers/__tests__/validate-prompt.test.js +0 -60
- package/dist/adapters/yaml/__tests__/index.test.d.ts +0 -1
- package/dist/adapters/yaml/__tests__/index.test.js +0 -53
- package/dist/domain/__tests__/data.d.ts +0 -19
- package/dist/domain/__tests__/data.js +0 -28
- package/dist/domain/__tests__/environment.test.d.ts +0 -1
- package/dist/domain/__tests__/environment.test.js +0 -332
- package/dist/domain/__tests__/info.test.d.ts +0 -1
- package/dist/domain/__tests__/info.test.js +0 -77
- package/dist/domain/__tests__/package-json.test.d.ts +0 -1
- package/dist/domain/__tests__/package-json.test.js +0 -39
- package/dist/domain/__tests__/repository.test.d.ts +0 -1
- package/dist/domain/__tests__/repository.test.js +0 -111
- package/dist/domain/__tests__/workspace.test.d.ts +0 -1
- package/dist/domain/__tests__/workspace.test.js +0 -57
- package/dist/use-cases/__tests__/apply-codemod.test.d.ts +0 -1
- package/dist/use-cases/__tests__/apply-codemod.test.js +0 -71
- package/dist/use-cases/__tests__/list-codemods.test.d.ts +0 -1
- package/dist/use-cases/__tests__/list-codemods.test.js +0 -37
- package/dist/use-cases/__tests__/request-authorization.test.d.ts +0 -4
- package/dist/use-cases/__tests__/request-authorization.test.js +0 -43
|
@@ -1,414 +0,0 @@
|
|
|
1
|
-
import { err, ok } from "neverthrow";
|
|
2
|
-
import { RequestError } from "octokit";
|
|
3
|
-
import { SemVer } from "semver";
|
|
4
|
-
import { beforeEach, describe, expect, it } from "vitest";
|
|
5
|
-
import { mockDeep, mockReset } from "vitest-mock-extended";
|
|
6
|
-
import { FileNotFoundError, PullRequest, Repository, RepositoryNotFoundError, } from "../../../domain/github.js";
|
|
7
|
-
import { fetchLatestRelease, fetchLatestTag } from "../index.js";
|
|
8
|
-
import { OctokitGitHubService } from "../index.js";
|
|
9
|
-
const makeEnv = () => {
|
|
10
|
-
const mockOctokit = mockDeep();
|
|
11
|
-
const githubService = new OctokitGitHubService(mockOctokit);
|
|
12
|
-
return {
|
|
13
|
-
githubService,
|
|
14
|
-
mockOctokit,
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
// eslint-disable-next-line max-lines-per-function
|
|
18
|
-
describe("OctokitGitHubService", () => {
|
|
19
|
-
describe("getRepository", () => {
|
|
20
|
-
it("should return a Repository when the repository exists", async () => {
|
|
21
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
22
|
-
const owner = "pagopa";
|
|
23
|
-
const name = "dx";
|
|
24
|
-
const mockResponse = {
|
|
25
|
-
data: {
|
|
26
|
-
full_name: "pagopa/dx",
|
|
27
|
-
name: "dx",
|
|
28
|
-
owner: {
|
|
29
|
-
login: "pagopa",
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
mockOctokit.rest.repos.get.mockResolvedValue(mockResponse);
|
|
34
|
-
const result = await githubService.getRepository(owner, name);
|
|
35
|
-
expect(result).toBeInstanceOf(Repository);
|
|
36
|
-
expect(result.name).toBe("dx");
|
|
37
|
-
expect(result.owner).toBe("pagopa");
|
|
38
|
-
expect(result.fullName).toBe("pagopa/dx");
|
|
39
|
-
expect(result.url).toBe("https://github.com/pagopa/dx");
|
|
40
|
-
expect(result.ssh).toBe("git@github.com:pagopa/dx.git");
|
|
41
|
-
expect(mockOctokit.rest.repos.get).toHaveBeenCalledWith({
|
|
42
|
-
owner,
|
|
43
|
-
repo: name,
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
it("should throw an error when the repository does not exist (404)", async () => {
|
|
47
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
48
|
-
const owner = "pagopa";
|
|
49
|
-
const name = "non-existent";
|
|
50
|
-
const error = new RequestError("Not Found", 404, {
|
|
51
|
-
request: {
|
|
52
|
-
headers: {},
|
|
53
|
-
method: "GET",
|
|
54
|
-
url: "https://api.github.com/repos/pagopa/non-existent",
|
|
55
|
-
},
|
|
56
|
-
response: {
|
|
57
|
-
data: { message: "Not Found" },
|
|
58
|
-
headers: {},
|
|
59
|
-
status: 404,
|
|
60
|
-
url: "https://api.github.com/repos/pagopa/non-existent",
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
mockOctokit.rest.repos.get.mockRejectedValue(error);
|
|
64
|
-
await expect(githubService.getRepository(owner, name)).rejects.toThrow(RepositoryNotFoundError);
|
|
65
|
-
await expect(githubService.getRepository(owner, name)).rejects.toThrowError("Repository pagopa/non-existent not found");
|
|
66
|
-
});
|
|
67
|
-
it("should throw an error when the API call fails", async () => {
|
|
68
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
69
|
-
const owner = "pagopa";
|
|
70
|
-
const name = "dx";
|
|
71
|
-
const error = new RequestError("API Error", 500, {
|
|
72
|
-
request: {
|
|
73
|
-
headers: {},
|
|
74
|
-
method: "GET",
|
|
75
|
-
url: "https://api.github.com/repos/pagopa/dx",
|
|
76
|
-
},
|
|
77
|
-
response: {
|
|
78
|
-
data: { message: "API Error" },
|
|
79
|
-
headers: {},
|
|
80
|
-
status: 500,
|
|
81
|
-
url: "https://api.github.com/repos/pagopa/dx",
|
|
82
|
-
},
|
|
83
|
-
});
|
|
84
|
-
mockOctokit.rest.repos.get.mockRejectedValue(error);
|
|
85
|
-
await expect(githubService.getRepository(owner, name)).rejects.toThrowError("Failed to fetch repository pagopa/dx");
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
describe("createPullRequest", () => {
|
|
89
|
-
it("should return a PullRequest when creation succeeds", async () => {
|
|
90
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
91
|
-
const params = {
|
|
92
|
-
base: "main",
|
|
93
|
-
body: "This is a test PR",
|
|
94
|
-
head: "feature-branch",
|
|
95
|
-
owner: "pagopa",
|
|
96
|
-
repo: "dx",
|
|
97
|
-
title: "Test Pull Request",
|
|
98
|
-
};
|
|
99
|
-
const mockResponse = {
|
|
100
|
-
data: {
|
|
101
|
-
html_url: "https://github.com/pagopa/dx/pull/123",
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
mockOctokit.rest.pulls.create.mockResolvedValue(mockResponse);
|
|
105
|
-
const result = await githubService.createPullRequest(params);
|
|
106
|
-
expect(result).toBeInstanceOf(PullRequest);
|
|
107
|
-
expect(result.url).toBe("https://github.com/pagopa/dx/pull/123");
|
|
108
|
-
expect(mockOctokit.rest.pulls.create).toHaveBeenCalledWith({
|
|
109
|
-
base: params.base,
|
|
110
|
-
body: params.body,
|
|
111
|
-
head: params.head,
|
|
112
|
-
owner: params.owner,
|
|
113
|
-
repo: params.repo,
|
|
114
|
-
title: params.title,
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
it("should throw an error when PR creation fails", async () => {
|
|
118
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
119
|
-
const params = {
|
|
120
|
-
base: "main",
|
|
121
|
-
body: "This is a test PR",
|
|
122
|
-
head: "feature-branch",
|
|
123
|
-
owner: "pagopa",
|
|
124
|
-
repo: "dx",
|
|
125
|
-
title: "Test Pull Request",
|
|
126
|
-
};
|
|
127
|
-
const error = new RequestError("Validation Failed", 422, {
|
|
128
|
-
request: {
|
|
129
|
-
headers: {},
|
|
130
|
-
method: "POST",
|
|
131
|
-
url: "https://api.github.com/repos/pagopa/dx/pulls",
|
|
132
|
-
},
|
|
133
|
-
response: {
|
|
134
|
-
data: { message: "Validation Failed" },
|
|
135
|
-
headers: {},
|
|
136
|
-
status: 422,
|
|
137
|
-
url: "https://api.github.com/repos/pagopa/dx/pulls",
|
|
138
|
-
},
|
|
139
|
-
});
|
|
140
|
-
mockOctokit.rest.pulls.create.mockRejectedValue(error);
|
|
141
|
-
await expect(githubService.createPullRequest(params)).rejects.toThrowError("Failed to create pull request in pagopa/dx");
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
describe("getFileContent", () => {
|
|
145
|
-
it("should return file content and sha when file exists", async () => {
|
|
146
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
147
|
-
const params = {
|
|
148
|
-
owner: "pagopa",
|
|
149
|
-
path: "src/test.tf",
|
|
150
|
-
ref: "main",
|
|
151
|
-
repo: "test-repo",
|
|
152
|
-
};
|
|
153
|
-
const fileContent = "test content";
|
|
154
|
-
const mockResponse = {
|
|
155
|
-
data: {
|
|
156
|
-
content: Buffer.from(fileContent).toString("base64"),
|
|
157
|
-
sha: "abc123sha",
|
|
158
|
-
type: "file",
|
|
159
|
-
},
|
|
160
|
-
};
|
|
161
|
-
mockOctokit.rest.repos.getContent.mockResolvedValue(mockResponse);
|
|
162
|
-
const result = await githubService.getFileContent(params);
|
|
163
|
-
expect(result.content).toBe(fileContent);
|
|
164
|
-
expect(result.sha).toBe("abc123sha");
|
|
165
|
-
expect(mockOctokit.rest.repos.getContent).toHaveBeenCalledWith({
|
|
166
|
-
owner: params.owner,
|
|
167
|
-
path: params.path,
|
|
168
|
-
ref: params.ref,
|
|
169
|
-
repo: params.repo,
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
it("should throw FileNotFoundError when file does not exist (404)", async () => {
|
|
173
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
174
|
-
const params = {
|
|
175
|
-
owner: "pagopa",
|
|
176
|
-
path: "non-existent.tf",
|
|
177
|
-
repo: "test-repo",
|
|
178
|
-
};
|
|
179
|
-
const error = new RequestError("Not Found", 404, {
|
|
180
|
-
request: {
|
|
181
|
-
headers: {},
|
|
182
|
-
method: "GET",
|
|
183
|
-
url: "https://api.github.com/repos/pagopa/test-repo/contents/non-existent.tf",
|
|
184
|
-
},
|
|
185
|
-
response: {
|
|
186
|
-
data: { message: "Not Found" },
|
|
187
|
-
headers: {},
|
|
188
|
-
status: 404,
|
|
189
|
-
url: "https://api.github.com/repos/pagopa/test-repo/contents/non-existent.tf",
|
|
190
|
-
},
|
|
191
|
-
});
|
|
192
|
-
mockOctokit.rest.repos.getContent.mockRejectedValue(error);
|
|
193
|
-
await expect(githubService.getFileContent(params)).rejects.toThrow(FileNotFoundError);
|
|
194
|
-
await expect(githubService.getFileContent(params)).rejects.toThrowError("File not found: non-existent.tf");
|
|
195
|
-
});
|
|
196
|
-
it("should throw an error when API call fails", async () => {
|
|
197
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
198
|
-
const params = {
|
|
199
|
-
owner: "pagopa",
|
|
200
|
-
path: "src/test.tf",
|
|
201
|
-
repo: "test-repo",
|
|
202
|
-
};
|
|
203
|
-
const error = new RequestError("Server Error", 500, {
|
|
204
|
-
request: {
|
|
205
|
-
headers: {},
|
|
206
|
-
method: "GET",
|
|
207
|
-
url: "https://api.github.com/repos/pagopa/test-repo/contents/src/test.tf",
|
|
208
|
-
},
|
|
209
|
-
response: {
|
|
210
|
-
data: { message: "Server Error" },
|
|
211
|
-
headers: {},
|
|
212
|
-
status: 500,
|
|
213
|
-
url: "https://api.github.com/repos/pagopa/test-repo/contents/src/test.tf",
|
|
214
|
-
},
|
|
215
|
-
});
|
|
216
|
-
mockOctokit.rest.repos.getContent.mockRejectedValue(error);
|
|
217
|
-
await expect(githubService.getFileContent(params)).rejects.toThrowError("Failed to get file content: src/test.tf");
|
|
218
|
-
});
|
|
219
|
-
});
|
|
220
|
-
describe("createBranch", () => {
|
|
221
|
-
it("should create a new branch from an existing ref", async () => {
|
|
222
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
223
|
-
const params = {
|
|
224
|
-
branchName: "feats/new-feature",
|
|
225
|
-
fromRef: "main",
|
|
226
|
-
owner: "pagopa",
|
|
227
|
-
repo: "test-repo",
|
|
228
|
-
};
|
|
229
|
-
const refResponse = {
|
|
230
|
-
data: {
|
|
231
|
-
object: {
|
|
232
|
-
sha: "mainbranchsha123",
|
|
233
|
-
},
|
|
234
|
-
},
|
|
235
|
-
};
|
|
236
|
-
mockOctokit.rest.git.getRef.mockResolvedValue(refResponse);
|
|
237
|
-
mockOctokit.rest.git.createRef.mockResolvedValue({});
|
|
238
|
-
await githubService.createBranch(params);
|
|
239
|
-
expect(mockOctokit.rest.git.getRef).toHaveBeenCalledWith({
|
|
240
|
-
owner: params.owner,
|
|
241
|
-
ref: "heads/main",
|
|
242
|
-
repo: params.repo,
|
|
243
|
-
});
|
|
244
|
-
expect(mockOctokit.rest.git.createRef).toHaveBeenCalledWith({
|
|
245
|
-
owner: params.owner,
|
|
246
|
-
ref: "refs/heads/feats/new-feature",
|
|
247
|
-
repo: params.repo,
|
|
248
|
-
sha: "mainbranchsha123",
|
|
249
|
-
});
|
|
250
|
-
});
|
|
251
|
-
it("should throw an error when branch creation fails", async () => {
|
|
252
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
253
|
-
const params = {
|
|
254
|
-
branchName: "feats/new-feature",
|
|
255
|
-
fromRef: "main",
|
|
256
|
-
owner: "pagopa",
|
|
257
|
-
repo: "test-repo",
|
|
258
|
-
};
|
|
259
|
-
const refResponse = {
|
|
260
|
-
data: {
|
|
261
|
-
object: {
|
|
262
|
-
sha: "mainbranchsha123",
|
|
263
|
-
},
|
|
264
|
-
},
|
|
265
|
-
};
|
|
266
|
-
const error = new RequestError("Reference already exists", 422, {
|
|
267
|
-
request: {
|
|
268
|
-
headers: {},
|
|
269
|
-
method: "POST",
|
|
270
|
-
url: "https://api.github.com/repos/pagopa/test-repo/git/refs",
|
|
271
|
-
},
|
|
272
|
-
response: {
|
|
273
|
-
data: { message: "Reference already exists" },
|
|
274
|
-
headers: {},
|
|
275
|
-
status: 422,
|
|
276
|
-
url: "https://api.github.com/repos/pagopa/test-repo/git/refs",
|
|
277
|
-
},
|
|
278
|
-
});
|
|
279
|
-
mockOctokit.rest.git.getRef.mockResolvedValue(refResponse);
|
|
280
|
-
mockOctokit.rest.git.createRef.mockRejectedValue(error);
|
|
281
|
-
await expect(githubService.createBranch(params)).rejects.toThrowError("Failed to create branch: feats/new-feature");
|
|
282
|
-
});
|
|
283
|
-
it("should throw an error when source ref does not exist", async () => {
|
|
284
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
285
|
-
const params = {
|
|
286
|
-
branchName: "feats/new-feature",
|
|
287
|
-
fromRef: "non-existent",
|
|
288
|
-
owner: "pagopa",
|
|
289
|
-
repo: "test-repo",
|
|
290
|
-
};
|
|
291
|
-
const error = new RequestError("Not Found", 404, {
|
|
292
|
-
request: {
|
|
293
|
-
headers: {},
|
|
294
|
-
method: "GET",
|
|
295
|
-
url: "https://api.github.com/repos/pagopa/test-repo/git/ref/heads/non-existent",
|
|
296
|
-
},
|
|
297
|
-
response: {
|
|
298
|
-
data: { message: "Not Found" },
|
|
299
|
-
headers: {},
|
|
300
|
-
status: 404,
|
|
301
|
-
url: "https://api.github.com/repos/pagopa/test-repo/git/ref/heads/non-existent",
|
|
302
|
-
},
|
|
303
|
-
});
|
|
304
|
-
mockOctokit.rest.git.getRef.mockRejectedValue(error);
|
|
305
|
-
await expect(githubService.createBranch(params)).rejects.toThrowError("Failed to create branch: feats/new-feature");
|
|
306
|
-
});
|
|
307
|
-
});
|
|
308
|
-
describe("updateFile", () => {
|
|
309
|
-
it("should update a file successfully", async () => {
|
|
310
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
311
|
-
const params = {
|
|
312
|
-
branch: "feats/new-feature",
|
|
313
|
-
content: "updated content",
|
|
314
|
-
message: "Update file",
|
|
315
|
-
owner: "pagopa",
|
|
316
|
-
path: "src/test.tf",
|
|
317
|
-
repo: "test-repo",
|
|
318
|
-
sha: "existingfilesha123",
|
|
319
|
-
};
|
|
320
|
-
mockOctokit.rest.repos.createOrUpdateFileContents.mockResolvedValue({});
|
|
321
|
-
await githubService.updateFile(params);
|
|
322
|
-
expect(mockOctokit.rest.repos.createOrUpdateFileContents).toHaveBeenCalledWith({
|
|
323
|
-
branch: params.branch,
|
|
324
|
-
content: Buffer.from(params.content).toString("base64"),
|
|
325
|
-
message: params.message,
|
|
326
|
-
owner: params.owner,
|
|
327
|
-
path: params.path,
|
|
328
|
-
repo: params.repo,
|
|
329
|
-
sha: params.sha,
|
|
330
|
-
});
|
|
331
|
-
});
|
|
332
|
-
it("should throw an error when file update fails", async () => {
|
|
333
|
-
const { githubService, mockOctokit } = makeEnv();
|
|
334
|
-
const params = {
|
|
335
|
-
branch: "feats/new-feature",
|
|
336
|
-
content: "updated content",
|
|
337
|
-
message: "Update file",
|
|
338
|
-
owner: "pagopa",
|
|
339
|
-
path: "src/test.tf",
|
|
340
|
-
repo: "test-repo",
|
|
341
|
-
sha: "wrongsha",
|
|
342
|
-
};
|
|
343
|
-
const error = new RequestError("Conflict", 409, {
|
|
344
|
-
request: {
|
|
345
|
-
headers: {},
|
|
346
|
-
method: "PUT",
|
|
347
|
-
url: "https://api.github.com/repos/pagopa/test-repo/contents/src/test.tf",
|
|
348
|
-
},
|
|
349
|
-
response: {
|
|
350
|
-
data: { message: "Conflict" },
|
|
351
|
-
headers: {},
|
|
352
|
-
status: 409,
|
|
353
|
-
url: "https://api.github.com/repos/pagopa/test-repo/contents/src/test.tf",
|
|
354
|
-
},
|
|
355
|
-
});
|
|
356
|
-
mockOctokit.rest.repos.createOrUpdateFileContents.mockRejectedValue(error);
|
|
357
|
-
await expect(githubService.updateFile(params)).rejects.toThrowError("Failed to update file: src/test.tf");
|
|
358
|
-
});
|
|
359
|
-
});
|
|
360
|
-
});
|
|
361
|
-
describe("octokit adapter", () => {
|
|
362
|
-
const owner = "test-owner";
|
|
363
|
-
const repo = "test-repo";
|
|
364
|
-
const client = mockDeep();
|
|
365
|
-
beforeEach(() => {
|
|
366
|
-
mockReset(client);
|
|
367
|
-
});
|
|
368
|
-
describe("fetchLatestTag", () => {
|
|
369
|
-
it("should return the highest valid semver tag", async () => {
|
|
370
|
-
client.request.mockResolvedValueOnce({
|
|
371
|
-
data: [
|
|
372
|
-
{ name: "v1.0.0" },
|
|
373
|
-
{ name: "2.0.0-beta.1" },
|
|
374
|
-
{ name: "2.0.0" },
|
|
375
|
-
{ name: "not-a-version" },
|
|
376
|
-
],
|
|
377
|
-
headers: {},
|
|
378
|
-
status: 200,
|
|
379
|
-
url: "",
|
|
380
|
-
});
|
|
381
|
-
const result = await fetchLatestTag({ client, owner, repo });
|
|
382
|
-
expect(result).toStrictEqual(ok(new SemVer("2.0.0")));
|
|
383
|
-
expect(client.request).toHaveBeenCalledWith("GET /repos/{owner}/{repo}/tags", { owner, repo });
|
|
384
|
-
});
|
|
385
|
-
it("returns null if there are no valid semver tags", async () => {
|
|
386
|
-
client.request.mockResolvedValueOnce({
|
|
387
|
-
data: [{ name: "foo" }, { name: "bar" }],
|
|
388
|
-
headers: {},
|
|
389
|
-
status: 200,
|
|
390
|
-
url: "",
|
|
391
|
-
});
|
|
392
|
-
const result = await fetchLatestTag({ client, owner, repo });
|
|
393
|
-
expect(result).toStrictEqual(ok(null));
|
|
394
|
-
});
|
|
395
|
-
it("should return an error when client promise fails", async () => {
|
|
396
|
-
client.request.mockRejectedValueOnce(new Error("an error"));
|
|
397
|
-
const result = await fetchLatestTag({ client, owner, repo });
|
|
398
|
-
expect(result).toStrictEqual(err(new Error("Failed to fetch tags for test-owner/test-repo")));
|
|
399
|
-
});
|
|
400
|
-
});
|
|
401
|
-
describe("fetchLatestRelease", () => {
|
|
402
|
-
it("should return parsed semver for the latest release tag", async () => {
|
|
403
|
-
client.request.mockResolvedValueOnce({
|
|
404
|
-
data: { tag_name: "v1.2.3" },
|
|
405
|
-
headers: {},
|
|
406
|
-
status: 200,
|
|
407
|
-
url: "",
|
|
408
|
-
});
|
|
409
|
-
const result = await fetchLatestRelease({ client, owner, repo });
|
|
410
|
-
expect(result).toStrictEqual(ok(new SemVer("v1.2.3")));
|
|
411
|
-
expect(client.request).toHaveBeenCalledWith("GET /repos/{owner}/{repo}/releases/latest", { owner, repo });
|
|
412
|
-
});
|
|
413
|
-
});
|
|
414
|
-
});
|