Skip to content

Commit

Permalink
feat: log & confirm plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Feb 26, 2021
1 parent 2c3e847 commit 8fd65b9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/octokit-plugin-request-confirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Confirm from "prompt-confirm";

export function requestConfirm(octokit, { octoherd }) {
octokit.hook.wrap("request", async (request, options) => {
const requestOptions = octokit.request.endpoint.parse(options);
const isMutatingRequest = ["POST", "PUT", "PATCH", "DELETE"].includes(
options.method
);
const route = `${requestOptions.method} ${requestOptions.url}`;

if (!octoherd.bypassConfirms && isMutatingRequest) {
const prompt = new Confirm(`Do you want to send "${route}"`);
const yes = await prompt.run();

if (!yes) {
const error = new Error(`"${route}" canceled`);
error.cancel = true;
throw error;
}
}

return request(options);
});
}
27 changes: 27 additions & 0 deletions lib/octokit-plugin-request-log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export function requestLog(octokit) {
octokit.hook.wrap("request", async (request, options) => {
const start = Date.now();
const requestOptions = octokit.request.endpoint.parse(options);
const path = requestOptions.url.replace(options.baseUrl, "");

return request(options)
.then((response) => {
const time = Date.now() - start;
octokit.log.debug(
{ request: { ...options, time } },
`${requestOptions.method} ${path} - ${response.status} in ${time}ms`
);
return response;
})

.catch((error) => {
const time = Date.now() - start;

octokit.log.debug(
{ request: { ...options, time } },
`${requestOptions.method} ${path} - ${error.status} in ${time}ms`
);
throw error;
});
});
}

0 comments on commit 8fd65b9

Please sign in to comment.