-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); | ||
} |