@jsenv/github-check-run
Advanced tools
Comparing version 0.0.13 to 0.1.0
{ | ||
"name": "@jsenv/github-check-run", | ||
"version": "0.0.13", | ||
"version": "0.1.0", | ||
"description": "Interact with GitHub check rest API", | ||
@@ -37,5 +37,5 @@ "license": "MIT", | ||
"dependencies": { | ||
"@jsenv/fetch": "1.1.40", | ||
"@jsenv/fetch": "1.1.41", | ||
"@jsenv/log": "3.4.1" | ||
} | ||
} |
@@ -17,6 +17,6 @@ /** | ||
commitSha, | ||
checkStatus = "in_progress", | ||
checkName, | ||
checkTitle, | ||
checkSummary, | ||
checkStatus = "in_progress", | ||
}) => { | ||
@@ -78,38 +78,26 @@ if (typeof githubToken !== "string") { | ||
let checkConclusion; | ||
const update = async ({ | ||
const updateState = async ({ | ||
status, | ||
conclusion, | ||
title, | ||
summary, | ||
title = check.title, | ||
summary = check.summary, | ||
annotations = [], | ||
}) => { | ||
if (typeof title !== "string") { | ||
throw new TypeError(`title must be a string, got ${title}`); | ||
} | ||
if (typeof summary !== "string") { | ||
throw new TypeError(`summary must be a string, got ${summary}`); | ||
} | ||
if (conclusion) { | ||
checkConclusion = conclusion; | ||
} | ||
let annotationsSent = 0; | ||
const annotationsBatch = annotations.slice(annotationsSent, 50); | ||
const body = { | ||
...(status ? { status } : {}), | ||
...(conclusion ? { conclusion } : {}), | ||
output: { | ||
title: title === undefined ? check.title : title, | ||
summary: summary === undefined ? check.summary : summary, | ||
...(annotationsBatch.length ? { annotations: annotationsBatch } : {}), | ||
}, | ||
}; | ||
try { | ||
const body = { | ||
...(status ? { status } : {}), | ||
...(conclusion ? { conclusion } : {}), | ||
output: { | ||
title, | ||
summary, | ||
...(annotationsBatch.length ? { annotations: annotationsBatch } : {}), | ||
}, | ||
}; | ||
logger.debug(`PATCH check ${check.html_url} | ||
--- body --- | ||
${JSON.stringify(body, null, " ")}`); | ||
await PATCH({ | ||
check = await PATCH({ | ||
url: check.url, | ||
@@ -128,11 +116,2 @@ githubToken, | ||
} | ||
if (status) { | ||
checkStatus = status; | ||
} | ||
if (title) { | ||
checkTitle = title; | ||
} | ||
if (summary) { | ||
checkSummary = summary; | ||
} | ||
@@ -142,3 +121,3 @@ annotationsSent += annotationsBatch.length; | ||
const annotationsBatch = annotations.slice(annotationsSent, 50); | ||
await PATCH({ | ||
check = await PATCH({ | ||
url: check.url, | ||
@@ -160,2 +139,3 @@ githubToken, | ||
let pendingAbortController; | ||
let pendingProgressPromise = Promise.resolve(); | ||
let msBetweenProgressCalls = 500; | ||
@@ -165,13 +145,47 @@ | ||
progress: async ({ title, summary, annotations = [] }) => { | ||
if (checkConclusion === "failure") { | ||
if (check.conclusion === "failure") { | ||
throw new Error(`cannot progress() after fail()`); | ||
} | ||
if (checkConclusion === "success") { | ||
if (check.conclusion === "success") { | ||
throw new Error(`cannot progress() after pass()`); | ||
} | ||
const nowMs = Date.now(); | ||
const isFirstCall = !lastProgressCall; | ||
lastProgressCall = nowMs; | ||
if (isFirstCall) { | ||
await update({ | ||
pendingProgressPromise = (async () => { | ||
const nowMs = Date.now(); | ||
const isFirstCall = !lastProgressCall; | ||
lastProgressCall = nowMs; | ||
if (isFirstCall) { | ||
await updateState({ | ||
title, | ||
summary, | ||
annotations, | ||
}); | ||
return; | ||
} | ||
if (pendingAbortController) { | ||
pendingAbortController.abort(); | ||
} | ||
const msEllapsedSinceLastProgressCall = nowMs - lastProgressCall; | ||
const msEllapsedIsBigEnough = | ||
msEllapsedSinceLastProgressCall > msBetweenProgressCalls; | ||
if (msEllapsedIsBigEnough) { | ||
annotations = [...pendingAnnotations, ...annotations]; | ||
pendingAnnotations.length = 0; | ||
await updateState({ | ||
title, | ||
summary, | ||
annotations, | ||
}); | ||
return; | ||
} | ||
pendingAnnotations.push(...annotations); | ||
pendingAbortController = new AbortController(); | ||
await new Promise((resolve) => { | ||
pendingAbortController.signal.onabort = resolve; | ||
setTimeout(resolve, msBetweenProgressCalls); | ||
}); | ||
if (pendingAbortController && pendingAbortController.signal.aborted) { | ||
return; | ||
} | ||
pendingAbortController = null; | ||
await updateState({ | ||
title, | ||
@@ -181,49 +195,18 @@ summary, | ||
}); | ||
return; | ||
} | ||
})(); | ||
await pendingProgressPromise; | ||
pendingProgressPromise = Promise.resolve(); | ||
}, | ||
fail: async ({ title, summary, annotations } = {}) => { | ||
await pendingProgressPromise; | ||
if (pendingAbortController) { | ||
pendingAbortController.abort(); | ||
} | ||
const msEllapsedSinceLastProgressCall = nowMs - lastProgressCall; | ||
const msEllapsedIsBigEnough = | ||
msEllapsedSinceLastProgressCall > msBetweenProgressCalls; | ||
if (msEllapsedIsBigEnough) { | ||
annotations = [...pendingAnnotations, ...annotations]; | ||
pendingAnnotations.length = 0; | ||
await update({ | ||
title, | ||
summary, | ||
annotations, | ||
}); | ||
return; | ||
} | ||
pendingAnnotations.push(...annotations); | ||
pendingAbortController = new AbortController(); | ||
await new Promise((resolve) => { | ||
pendingAbortController.signal.onabort = resolve; | ||
setTimeout(resolve, msBetweenProgressCalls); | ||
}); | ||
if (pendingAbortController && pendingAbortController.signal.aborted) { | ||
return; | ||
} | ||
pendingAbortController = null; | ||
await update({ | ||
title, | ||
summary, | ||
annotations, | ||
}); | ||
}, | ||
fail: ({ title, summary, annotations } = {}) => { | ||
if (checkConclusion === "failure") { | ||
if (check.conclusion === "failure") { | ||
throw new Error(`already failed`); | ||
} | ||
if (checkConclusion === "success") { | ||
if (check.conclusion === "success") { | ||
throw new Error(`cannot fail() after pass()`); | ||
} | ||
if (pendingAbortController) { | ||
pendingAbortController.abort(); | ||
} | ||
// TODO: wait to any update before PATCH | ||
return update({ | ||
return updateState({ | ||
status: "completed", | ||
@@ -236,15 +219,14 @@ conclusion: "failure", | ||
}, | ||
pass: ({ title, summary, annotations } = {}) => { | ||
if (checkConclusion === "success") { | ||
throw new Error(`already passed`); | ||
pass: async ({ title, summary, annotations } = {}) => { | ||
await pendingProgressPromise; | ||
if (pendingAbortController) { | ||
pendingAbortController.abort(); | ||
} | ||
if (checkConclusion === "failure") { | ||
if (check.conclusion === "failure") { | ||
throw new Error(`cannot pass() after fail()`); | ||
} | ||
if (pendingAbortController) { | ||
pendingAbortController.abort(); | ||
if (check.conclusion === "success") { | ||
throw new Error(`already passed`); | ||
} | ||
// TODO: wait to any pending update before PATCH | ||
return update({ | ||
return updateState({ | ||
status: "completed", | ||
@@ -251,0 +233,0 @@ conclusion: "success", |
12778
368
+ Added@jsenv/fetch@1.1.41(transitive)
+ Added@jsenv/server@15.1.3(transitive)
- Removed@jsenv/fetch@1.1.40(transitive)
- Removed@jsenv/log@3.4.0(transitive)
- Removed@jsenv/server@15.1.2(transitive)
- Removedsupports-color@9.3.1(transitive)
Updated@jsenv/fetch@1.1.41