@jsenv/github-check-run 0.0.13 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/github-check-run",
3
- "version": "0.0.13",
3
+ "version": "0.1.0",
4
4
  "description": "Interact with GitHub check rest API",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -34,7 +34,7 @@
34
34
  "test": "node ./scripts/test.mjs"
35
35
  },
36
36
  "dependencies": {
37
- "@jsenv/fetch": "1.1.40",
37
+ "@jsenv/fetch": "1.1.41",
38
38
  "@jsenv/log": "3.4.1"
39
39
  }
40
40
  }
@@ -14,10 +14,10 @@ export const startGithubCheckRun = async ({
14
14
  repositoryOwner,
15
15
  repositoryName,
16
16
  commitSha,
17
- checkStatus = "in_progress",
18
17
  checkName,
19
18
  checkTitle,
20
19
  checkSummary,
20
+ checkStatus = "in_progress",
21
21
  }) => {
22
22
  if (typeof githubToken !== "string") {
23
23
  throw new TypeError(
@@ -74,42 +74,30 @@ export const startGithubCheckRun = async ({
74
74
  return { progress: () => {}, fail: () => {}, pass: () => {} };
75
75
  }
76
76
 
77
- let checkConclusion;
78
-
79
- const update = async ({
77
+ const updateState = async ({
80
78
  status,
81
79
  conclusion,
82
- title,
83
- summary,
80
+ title = check.title,
81
+ summary = check.summary,
84
82
  annotations = [],
85
83
  }) => {
86
- if (typeof title !== "string") {
87
- throw new TypeError(`title must be a string, got ${title}`);
88
- }
89
- if (typeof summary !== "string") {
90
- throw new TypeError(`summary must be a string, got ${summary}`);
91
- }
92
- if (conclusion) {
93
- checkConclusion = conclusion;
94
- }
95
-
96
84
  let annotationsSent = 0;
97
85
  const annotationsBatch = annotations.slice(annotationsSent, 50);
86
+ const body = {
87
+ ...(status ? { status } : {}),
88
+ ...(conclusion ? { conclusion } : {}),
89
+ output: {
90
+ title: title === undefined ? check.title : title,
91
+ summary: summary === undefined ? check.summary : summary,
92
+ ...(annotationsBatch.length ? { annotations: annotationsBatch } : {}),
93
+ },
94
+ };
98
95
 
99
96
  try {
100
- const body = {
101
- ...(status ? { status } : {}),
102
- ...(conclusion ? { conclusion } : {}),
103
- output: {
104
- title,
105
- summary,
106
- ...(annotationsBatch.length ? { annotations: annotationsBatch } : {}),
107
- },
108
- };
109
97
  logger.debug(`PATCH check ${check.html_url}
110
98
  --- body ---
111
99
  ${JSON.stringify(body, null, " ")}`);
112
- await PATCH({
100
+ check = await PATCH({
113
101
  url: check.url,
114
102
  githubToken,
115
103
  body,
@@ -123,20 +111,11 @@ ${JSON.stringify(body, null, " ")}`);
123
111
  );
124
112
  return;
125
113
  }
126
- if (status) {
127
- checkStatus = status;
128
- }
129
- if (title) {
130
- checkTitle = title;
131
- }
132
- if (summary) {
133
- checkSummary = summary;
134
- }
135
114
 
136
115
  annotationsSent += annotationsBatch.length;
137
116
  while (annotationsSent < annotations.length) {
138
117
  const annotationsBatch = annotations.slice(annotationsSent, 50);
139
- await PATCH({
118
+ check = await PATCH({
140
119
  url: check.url,
141
120
  githubToken,
142
121
  body: {
@@ -153,72 +132,76 @@ ${JSON.stringify(body, null, " ")}`);
153
132
  let lastProgressCall;
154
133
  let pendingAnnotations = [];
155
134
  let pendingAbortController;
135
+ let pendingProgressPromise = Promise.resolve();
156
136
  let msBetweenProgressCalls = 500;
157
137
 
158
138
  return {
159
139
  progress: async ({ title, summary, annotations = [] }) => {
160
- if (checkConclusion === "failure") {
140
+ if (check.conclusion === "failure") {
161
141
  throw new Error(`cannot progress() after fail()`);
162
142
  }
163
- if (checkConclusion === "success") {
143
+ if (check.conclusion === "success") {
164
144
  throw new Error(`cannot progress() after pass()`);
165
145
  }
166
- const nowMs = Date.now();
167
- const isFirstCall = !lastProgressCall;
168
- lastProgressCall = nowMs;
169
- if (isFirstCall) {
170
- await update({
146
+ pendingProgressPromise = (async () => {
147
+ const nowMs = Date.now();
148
+ const isFirstCall = !lastProgressCall;
149
+ lastProgressCall = nowMs;
150
+ if (isFirstCall) {
151
+ await updateState({
152
+ title,
153
+ summary,
154
+ annotations,
155
+ });
156
+ return;
157
+ }
158
+ if (pendingAbortController) {
159
+ pendingAbortController.abort();
160
+ }
161
+ const msEllapsedSinceLastProgressCall = nowMs - lastProgressCall;
162
+ const msEllapsedIsBigEnough =
163
+ msEllapsedSinceLastProgressCall > msBetweenProgressCalls;
164
+ if (msEllapsedIsBigEnough) {
165
+ annotations = [...pendingAnnotations, ...annotations];
166
+ pendingAnnotations.length = 0;
167
+ await updateState({
168
+ title,
169
+ summary,
170
+ annotations,
171
+ });
172
+ return;
173
+ }
174
+ pendingAnnotations.push(...annotations);
175
+ pendingAbortController = new AbortController();
176
+ await new Promise((resolve) => {
177
+ pendingAbortController.signal.onabort = resolve;
178
+ setTimeout(resolve, msBetweenProgressCalls);
179
+ });
180
+ if (pendingAbortController && pendingAbortController.signal.aborted) {
181
+ return;
182
+ }
183
+ pendingAbortController = null;
184
+ await updateState({
171
185
  title,
172
186
  summary,
173
187
  annotations,
174
188
  });
175
- return;
176
- }
189
+ })();
190
+ await pendingProgressPromise;
191
+ pendingProgressPromise = Promise.resolve();
192
+ },
193
+ fail: async ({ title, summary, annotations } = {}) => {
194
+ await pendingProgressPromise;
177
195
  if (pendingAbortController) {
178
196
  pendingAbortController.abort();
179
197
  }
180
- const msEllapsedSinceLastProgressCall = nowMs - lastProgressCall;
181
- const msEllapsedIsBigEnough =
182
- msEllapsedSinceLastProgressCall > msBetweenProgressCalls;
183
- if (msEllapsedIsBigEnough) {
184
- annotations = [...pendingAnnotations, ...annotations];
185
- pendingAnnotations.length = 0;
186
- await update({
187
- title,
188
- summary,
189
- annotations,
190
- });
191
- return;
192
- }
193
- pendingAnnotations.push(...annotations);
194
- pendingAbortController = new AbortController();
195
- await new Promise((resolve) => {
196
- pendingAbortController.signal.onabort = resolve;
197
- setTimeout(resolve, msBetweenProgressCalls);
198
- });
199
- if (pendingAbortController && pendingAbortController.signal.aborted) {
200
- return;
201
- }
202
- pendingAbortController = null;
203
- await update({
204
- title,
205
- summary,
206
- annotations,
207
- });
208
- },
209
- fail: ({ title, summary, annotations } = {}) => {
210
- if (checkConclusion === "failure") {
198
+ if (check.conclusion === "failure") {
211
199
  throw new Error(`already failed`);
212
200
  }
213
- if (checkConclusion === "success") {
201
+ if (check.conclusion === "success") {
214
202
  throw new Error(`cannot fail() after pass()`);
215
203
  }
216
-
217
- if (pendingAbortController) {
218
- pendingAbortController.abort();
219
- }
220
- // TODO: wait to any update before PATCH
221
- return update({
204
+ return updateState({
222
205
  status: "completed",
223
206
  conclusion: "failure",
224
207
  title,
@@ -226,19 +209,18 @@ ${JSON.stringify(body, null, " ")}`);
226
209
  annotations,
227
210
  });
228
211
  },
229
- pass: ({ title, summary, annotations } = {}) => {
230
- if (checkConclusion === "success") {
231
- throw new Error(`already passed`);
212
+ pass: async ({ title, summary, annotations } = {}) => {
213
+ await pendingProgressPromise;
214
+ if (pendingAbortController) {
215
+ pendingAbortController.abort();
232
216
  }
233
- if (checkConclusion === "failure") {
217
+ if (check.conclusion === "failure") {
234
218
  throw new Error(`cannot pass() after fail()`);
235
219
  }
236
-
237
- if (pendingAbortController) {
238
- pendingAbortController.abort();
220
+ if (check.conclusion === "success") {
221
+ throw new Error(`already passed`);
239
222
  }
240
- // TODO: wait to any pending update before PATCH
241
- return update({
223
+ return updateState({
242
224
  status: "completed",
243
225
  conclusion: "success",
244
226
  title,