Skip to content

Commit 8fd65b9

Browse files
committed
feat: log & confirm plugins
1 parent 2c3e847 commit 8fd65b9

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Confirm from "prompt-confirm";
2+
3+
export function requestConfirm(octokit, { octoherd }) {
4+
octokit.hook.wrap("request", async (request, options) => {
5+
const requestOptions = octokit.request.endpoint.parse(options);
6+
const isMutatingRequest = ["POST", "PUT", "PATCH", "DELETE"].includes(
7+
options.method
8+
);
9+
const route = `${requestOptions.method} ${requestOptions.url}`;
10+
11+
if (!octoherd.bypassConfirms && isMutatingRequest) {
12+
const prompt = new Confirm(`Do you want to send "${route}"`);
13+
const yes = await prompt.run();
14+
15+
if (!yes) {
16+
const error = new Error(`"${route}" canceled`);
17+
error.cancel = true;
18+
throw error;
19+
}
20+
}
21+
22+
return request(options);
23+
});
24+
}

lib/octokit-plugin-request-log.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export function requestLog(octokit) {
2+
octokit.hook.wrap("request", async (request, options) => {
3+
const start = Date.now();
4+
const requestOptions = octokit.request.endpoint.parse(options);
5+
const path = requestOptions.url.replace(options.baseUrl, "");
6+
7+
return request(options)
8+
.then((response) => {
9+
const time = Date.now() - start;
10+
octokit.log.debug(
11+
{ request: { ...options, time } },
12+
`${requestOptions.method} ${path} - ${response.status} in ${time}ms`
13+
);
14+
return response;
15+
})
16+
17+
.catch((error) => {
18+
const time = Date.now() - start;
19+
20+
octokit.log.debug(
21+
{ request: { ...options, time } },
22+
`${requestOptions.method} ${path} - ${error.status} in ${time}ms`
23+
);
24+
throw error;
25+
});
26+
});
27+
}

0 commit comments

Comments
 (0)