@boxxy/review-noslop 0.2.5 → 0.2.6

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.
@@ -6,7 +6,6 @@ export interface GateReadingInput {
6
6
  readonly head: CommitSha;
7
7
  readonly review: ReviewResult;
8
8
  readonly list: FindingList | null;
9
- readonly listExitCode: number | null;
10
9
  readonly outcome: PullRequestCheck;
11
10
  }
12
11
  export declare function buildGateReading(input: GateReadingInput): GateReading;
@@ -0,0 +1 @@
1
+ export declare function findForgeRefusal(error: unknown): string | null;
@@ -5,7 +5,6 @@ export type ParsedReviewBody = {
5
5
  readonly head: CommitSha;
6
6
  readonly review: ReviewResult;
7
7
  readonly list: FindingList | null;
8
- readonly listExitCode: number | null;
9
8
  } | {
10
9
  readonly kind: 'not-covered';
11
10
  readonly head: CommitSha;
@@ -0,0 +1 @@
1
+ export declare function buildMockRequestError(status: number, message: string): Error;
@@ -5,6 +5,10 @@ interface FakeNoslopClientInput {
5
5
  readonly head: string;
6
6
  readonly outcome: PullRequestCheck;
7
7
  readonly down?: boolean;
8
+ readonly refuses?: {
9
+ readonly status: number;
10
+ readonly on: 'post' | 'check';
11
+ };
8
12
  }
9
13
  export interface FakeNoslopClient extends NoslopClient {
10
14
  readonly posted: ReviewResult[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boxxy/review-noslop",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "boxxy's review gate on noslop: the review command, and the check and post of its result",
5
5
  "homepage": "https://github.com/zgeoff/boxxy/tree/main/adapters/review-noslop#readme",
6
6
  "bugs": "https://github.com/zgeoff/boxxy/issues",
@@ -30,7 +30,7 @@
30
30
  "typecheck": "tsrs --noEmit"
31
31
  },
32
32
  "dependencies": {
33
- "@boxxy/model": "0.2.5",
33
+ "@boxxy/model": "0.2.6",
34
34
  "@noslop/cli": "0.4.29",
35
35
  "ts-pattern": "5.9.2",
36
36
  "zod": "4.6.7"
@@ -10,9 +10,9 @@ export interface GateReadingInput {
10
10
  readonly head: CommitSha;
11
11
  readonly review: ReviewResult;
12
12
 
13
- // the `noslop findings` output from before the post, and its exit code
13
+ // the `noslop findings` output from before the post; null when the session could not read it,
14
+ // as on a repo whose base branch config names no `github.app`
14
15
  readonly list: FindingList | null;
15
- readonly listExitCode: number | null;
16
16
 
17
17
  // `check` after the post: the findings still open at the head
18
18
  readonly outcome: PullRequestCheck;
@@ -26,7 +26,8 @@ interface KnownFinding {
26
26
  const closedWithoutFixStates = new Set(['not-a-defect', 'deferred', 'accepted-risk']);
27
27
 
28
28
  // The noslop check run is never read: it passes at a head that no review read, and after a failed
29
- // call. Only `check`'s own answer, after the post, counts.
29
+ // call. Only `check`'s own answer, made with the App's keys after the post, decides the status;
30
+ // the session's list only adds severities, paths and the findings closed without a fix.
30
31
  export function buildGateReading(input: GateReadingInput): GateReading {
31
32
  const listed = input.list?.findings ?? [];
32
33
 
@@ -57,10 +58,6 @@ export function buildGateReading(input: GateReadingInput): GateReading {
57
58
  answerHint: `Run \`noslop findings --pr ${input.round.pullRequest.number} --repo ${input.round.pullRequest.repo} --json\`, and reply to each open finding with a disposition: fixed, not-a-defect, deferred or accepted-risk.`,
58
59
  };
59
60
 
60
- if (input.listExitCode !== 0 || input.list === null) {
61
- return { status: 'blocked', why: 'unreadable', ...run };
62
- }
63
-
64
61
  if (input.review.failedCalls.length > 0) {
65
62
  return { status: 'blocked', why: 'failed-call', ...run };
66
63
  }
@@ -4,6 +4,7 @@ import { sendResult } from '@noslop/cli/post';
4
4
  import { buildGateCost } from './build-gate-cost.ts';
5
5
  import { buildGateReading } from './build-gate-reading.ts';
6
6
  import { buildReviewCommand } from './build-review-command.ts';
7
+ import { findForgeRefusal } from './find-forge-refusal.ts';
7
8
  import { parseReviewBody } from './parse-review-body.ts';
8
9
  import type { NoslopReviewGateOptions } from './types.ts';
9
10
 
@@ -21,36 +22,49 @@ export function createNoslopReviewGate(options: NoslopReviewGateOptions): Review
21
22
  return { status: 'not-covered', head: parsed.head, cost: parsed.cost };
22
23
  }
23
24
 
25
+ const cost = buildGateCost(parsed.review);
26
+
24
27
  const posted = await client
25
28
  .sendResult(parsed.review, { env: options.appEnv, verify: true })
26
- .then(() => 'posted' as const)
29
+ .then(() => ({ kind: 'posted' as const }))
27
30
  .catch((error: unknown) => {
28
31
  if (isRefusal(error)) {
29
- return 'refused' as const;
32
+ return { kind: 'refused' as const, reason: null };
30
33
  }
31
34
 
32
- throw error;
35
+ return { kind: 'refused' as const, reason: requireForgeRefusal(error) };
33
36
  });
34
37
 
35
- if (posted === 'refused') {
36
- return { status: 'not-covered', head: parsed.head, cost: buildGateCost(parsed.review) };
38
+ if (posted.kind === 'refused') {
39
+ return posted.reason === null
40
+ ? { status: 'not-covered', head: parsed.head, cost }
41
+ : { status: 'not-covered', head: parsed.head, cost, reason: posted.reason };
37
42
  }
38
43
 
39
44
  const [owner = '', repo = ''] = round.pullRequest.repo.split('/');
40
45
 
41
- const outcome = await client.check(
42
- { owner, repo, number: round.pullRequest.number },
43
- { env: options.appEnv, readOnly: true },
44
- );
46
+ const outcome = await client
47
+ .check(
48
+ { owner, repo, number: round.pullRequest.number },
49
+ { env: options.appEnv, readOnly: true },
50
+ )
51
+ .then((answer) => ({ kind: 'read' as const, answer }))
52
+ .catch((error: unknown) => ({
53
+ kind: 'refused' as const,
54
+ reason: requireForgeRefusal(error),
55
+ }));
56
+
57
+ if (outcome.kind === 'refused') {
58
+ return { status: 'not-covered', head: parsed.head, cost, reason: outcome.reason };
59
+ }
45
60
 
46
- return buildGateReading({ round, ...parsed, outcome });
61
+ return buildGateReading({ round, ...parsed, outcome: outcome.answer });
47
62
  },
48
63
  };
49
64
  }
50
65
 
51
66
  // noslop wraps every failure of its check in one error, a network failure included. Only a cause
52
- // that says the result does not verify, such as an old head, is a refusal; any other failure
53
- // throws, so the session sends again.
67
+ // that says the result does not verify, such as an old head, is a refusal of the result itself.
54
68
  function isRefusal(error: unknown): boolean {
55
69
  return (
56
70
  error instanceof Error &&
@@ -58,3 +72,16 @@ function isRefusal(error: unknown): boolean {
58
72
  error.cause.message.startsWith('the result does not verify')
59
73
  );
60
74
  }
75
+
76
+ // A forge that refused the App, such as a 404 for a repo the App is not installed on, gives the
77
+ // same answer to every send, so the gate records it. Any other failure throws, so the session
78
+ // sends again.
79
+ function requireForgeRefusal(error: unknown): string {
80
+ const refusal = findForgeRefusal(error);
81
+
82
+ if (refusal === null) {
83
+ throw error;
84
+ }
85
+
86
+ return refusal;
87
+ }
@@ -0,0 +1,56 @@
1
+ // Octokit's request error holds GitHub's HTTP status in `status`, and noslop keeps it in the cause
2
+ // of the error it throws; a network failure has status 500. Only a 4xx refusal that a second send
3
+ // gets again finds a reason: a rate limit, as noslop's own retry reads one, finds none.
4
+ export function findForgeRefusal(error: unknown): string | null {
5
+ const answer = findRequestError(error, new Set());
6
+
7
+ if (answer === null || answer.status < 400 || answer.status > 499 || isRateLimit(answer)) {
8
+ return null;
9
+ }
10
+
11
+ const message = error instanceof Error ? error.message : String(error);
12
+
13
+ if (/rate limit/iu.test(message)) {
14
+ return null;
15
+ }
16
+
17
+ return `GitHub answered ${answer.status} to the noslop App: ${message}`;
18
+ }
19
+
20
+ interface RequestError {
21
+ readonly status: number;
22
+ readonly headers: Readonly<Record<string, unknown>>;
23
+ }
24
+
25
+ function findRequestError(error: unknown, seen: Set<unknown>): RequestError | null {
26
+ if (!(error instanceof Error) || seen.has(error)) {
27
+ return null;
28
+ }
29
+
30
+ seen.add(error);
31
+
32
+ if (!('status' in error) || typeof error.status !== 'number') {
33
+ return findRequestError(error.cause, seen);
34
+ }
35
+
36
+ const response = 'response' in error ? error.response : null;
37
+
38
+ const headers =
39
+ typeof response === 'object' && response !== null && 'headers' in response
40
+ ? response.headers
41
+ : null;
42
+
43
+ return {
44
+ status: error.status,
45
+ headers: typeof headers === 'object' && headers !== null ? { ...headers } : {},
46
+ };
47
+ }
48
+
49
+ // GitHub marks a rate limit with 429, a `retry-after` header, or no requests left in the window
50
+ function isRateLimit(answer: RequestError): boolean {
51
+ return (
52
+ answer.status === 429 ||
53
+ answer.headers['retry-after'] !== undefined ||
54
+ answer.headers['x-ratelimit-remaining'] === '0'
55
+ );
56
+ }
@@ -18,8 +18,9 @@ export type ParsedReviewBody =
18
18
  readonly kind: 'postable';
19
19
  readonly head: CommitSha;
20
20
  readonly review: ReviewResult;
21
+
22
+ // null when `noslop findings` failed or printed no list
21
23
  readonly list: FindingList | null;
22
- readonly listExitCode: number | null;
23
24
  }
24
25
  | { readonly kind: 'not-covered'; readonly head: CommitSha; readonly cost: GateCost };
25
26
 
@@ -63,8 +64,7 @@ export function parseReviewBody(round: GateRound, body: JsonValue): ParsedReview
63
64
  kind: 'postable',
64
65
  head: head.data,
65
66
  review: review.data,
66
- list: list.success ? list.data : null,
67
- listExitCode: envelope.data.findings.exitCode,
67
+ list: list.success && envelope.data.findings.exitCode === 0 ? list.data : null,
68
68
  };
69
69
  }
70
70
 
@@ -0,0 +1,5 @@
1
+ // The shape of Octokit's request error, which noslop throws for an HTTP answer it does not expect:
2
+ // an `HttpError` with GitHub's status in `status`.
3
+ export function buildMockRequestError(status: number, message: string): Error {
4
+ return Object.assign(new Error(message), { name: 'HttpError', status });
5
+ }
@@ -2,6 +2,7 @@ import type { ReviewResult } from '@noslop/cli';
2
2
  import { reviewResultSchema } from '@noslop/cli';
3
3
  import type { PullRequestCheck } from '@noslop/cli/check';
4
4
  import type { NoslopClient } from '../types.ts';
5
+ import { buildMockRequestError } from './build-mock-request-error.ts';
5
6
 
6
7
  interface FakeNoslopClientInput {
7
8
  // the pull request's head on the forge
@@ -12,6 +13,10 @@ interface FakeNoslopClientInput {
12
13
 
13
14
  // true: GitHub gives no answer, so every call fails as a network failure does
14
15
  readonly down?: boolean;
16
+
17
+ // GitHub answers this HTTP status to the App's reads, as it answers 404 for a repo the App is
18
+ // not installed on; `on` names the call that gets it
19
+ readonly refuses?: { readonly status: number; readonly on: 'post' | 'check' };
15
20
  }
16
21
 
17
22
  export interface FakeNoslopClient extends NoslopClient {
@@ -41,6 +46,19 @@ export function createFakeNoslopClient(input: FakeNoslopClientInput): FakeNoslop
41
46
  );
42
47
  }
43
48
 
49
+ if (input.refuses?.on === 'post') {
50
+ const answer = buildRequestError(input.refuses.status);
51
+
52
+ return Promise.reject(
53
+ new Error(
54
+ `noslop posts only a verified result, so it refuses this one: ${answer.message}`,
55
+ {
56
+ cause: answer,
57
+ },
58
+ ),
59
+ );
60
+ }
61
+
44
62
  if (review.head !== input.head) {
45
63
  const reason = `the result does not verify: it reviews ${review.head.slice(0, 12)}, but the head of the pull request is ${input.head.slice(0, 12)}`;
46
64
 
@@ -60,9 +78,23 @@ export function createFakeNoslopClient(input: FakeNoslopClientInput): FakeNoslop
60
78
  return Promise.reject(new Error('fetch failed'));
61
79
  }
62
80
 
81
+ if (input.refuses?.on === 'check') {
82
+ return Promise.reject(buildRequestError(input.refuses.status));
83
+ }
84
+
63
85
  checked += 1;
64
86
 
65
87
  return Promise.resolve(input.outcome);
66
88
  },
67
89
  };
68
90
  }
91
+
92
+ // The shape of Octokit's request error, which noslop throws for an HTTP answer it does not expect
93
+ function buildRequestError(status: number): Error {
94
+ const text = status === 404 ? 'Not Found' : 'Refused';
95
+
96
+ return buildMockRequestError(
97
+ status,
98
+ `${text} - https://docs.github.com/rest/pulls/pulls#get-a-pull-request`,
99
+ );
100
+ }