Skip to content

Commit c6654ad

Browse files
feat(bitbucket): update authentication method, use fetch
1 parent 31946de commit c6654ad

8 files changed

Lines changed: 242 additions & 372 deletions

File tree

eslint.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export default [
3737
"newlines-between": "always",
3838
},
3939
],
40-
"jsdoc/no-undefined-types": [1, { definedTypes: ["NodeJS"] }],
40+
"jsdoc/no-undefined-types": [
41+
1,
42+
{ definedTypes: ["NodeJS", "RequestInit"] },
43+
],
4144
"jsdoc/require-hyphen-before-param-description": "warn",
4245
"unicorn/comment-content": "off",
4346
"unicorn/consistent-optional-chaining": "off",
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { MockAgent } from "undici";
2+
3+
/**
4+
* @param {string} message - Error message
5+
* @returns {object} Bitbucket-shaped error response body
6+
*/
7+
function errorBody(message) {
8+
return { type: "error", error: { message } };
9+
}
10+
11+
/**
12+
* @returns {import("undici").MockAgent} Undici MockAgent
13+
* @see {@link https://undici.nodejs.org/#/docs/api/MockAgent}
14+
*/
15+
export function mockClient() {
16+
const agent = new MockAgent();
17+
agent.disableNetConnect();
18+
19+
const origin = "https://api.bitbucket.org";
20+
const sourcePath = "/2.0/repositories/username/repo/src";
21+
22+
// File exists (foo.txt)
23+
agent
24+
.get(origin)
25+
.intercept({
26+
path: `${sourcePath}/main/foo.txt`,
27+
method: "GET",
28+
query: { format: "meta" },
29+
})
30+
.reply(200, { path: "foo.txt", type: "commit_file" })
31+
.persist();
32+
33+
// File doesn’t exist (404.txt, new.txt, 401.txt)
34+
agent
35+
.get(origin)
36+
.intercept({
37+
path: /\/main\/(404|401|new)\.txt\?format=meta$/,
38+
method: "GET",
39+
})
40+
.reply(404, errorBody("Not found"))
41+
.persist();
42+
43+
// Read file (foo.txt)
44+
agent
45+
.get(origin)
46+
.intercept({ path: `${sourcePath}/main/foo.txt`, method: "GET" })
47+
.reply(200, "foo")
48+
.persist();
49+
50+
// Read file (Not found)
51+
agent
52+
.get(origin)
53+
.intercept({ path: `${sourcePath}/main/404.txt`, method: "GET" })
54+
.reply(404, errorBody("Not found"))
55+
.persist();
56+
57+
/**
58+
* Bitbucket’s `src` endpoint is a single fixed URL for every create,
59+
* update and delete — the file path only appears as a field name inside
60+
* the multipart request body, which Undici’s `MockAgent` can’t inspect
61+
* (it sees a `FormData` body as the literal string `[object FormData]`).
62+
* So, rather than matching per file, these are registered as a queue:
63+
* each `it()` block below consumes the next one in order.
64+
*/
65+
66+
// 1. Creates file (new.txt)
67+
agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(201, {
68+
type: "commit",
69+
});
70+
71+
// 2. Throws error creating file (401.txt)
72+
agent
73+
.get(origin)
74+
.intercept({ path: sourcePath, method: "POST" })
75+
.reply(401, errorBody("Unauthorized"));
76+
77+
// 3. Updates file (foo.txt)
78+
agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, {
79+
type: "commit",
80+
});
81+
82+
// 4. Updates and renames file (bar.txt) …
83+
agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, {
84+
type: "commit",
85+
});
86+
87+
// …5. … then deletes the old path (foo.txt)
88+
agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, {
89+
type: "commit",
90+
});
91+
92+
// 6. Throws error updating file (401.txt)
93+
agent
94+
.get(origin)
95+
.intercept({ path: sourcePath, method: "POST" })
96+
.reply(401, errorBody("Unauthorized"));
97+
98+
// 7. Deletes a file (foo.txt)
99+
agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, {
100+
type: "commit",
101+
});
102+
103+
// 8. Throws error deleting a file (401.txt)
104+
agent
105+
.get(origin)
106+
.intercept({ path: sourcePath, method: "POST" })
107+
.reply(401, errorBody("Unauthorized"));
108+
109+
return agent;
110+
}

package-lock.json

Lines changed: 1 addition & 162 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
"mock-req-res": "^1.2.1",
9898
"mock-session": "^0.0.5",
9999
"mongodb-memory-server": "^11.2.0",
100-
"nock": "^14.0.16",
101100
"prettier": "^3.9.4",
102101
"stylelint": "^17.14.0",
103102
"stylelint-config-recommended": "^18.0.0",

0 commit comments

Comments
 (0)