@popcorn245/cornfield 0.41.0 → 0.42.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.
@@ -18,10 +18,16 @@
18
18
  * remembered 15 minutes ago; a different person, or one who is no longer a maintainer, is 403
19
19
  * 6. the PR is open and holds the proposal (404)
20
20
  * 7. the proposal file's SHA-256 at the PR's head: when the caller names the version it read (`sha256`; the API
21
- * requires it) and the head holds another, 409, so a review counts only for the version that was read. An
22
- * approval records the head's hash (`proposal`) and its mockups' (`mockups`), as the server read them, never the caller's word
21
+ * requires it, the button sends the one it was shown) and the head holds another, 409, so a review counts only for the
22
+ * version that was read. The button also names the PR's head commit it was shown (`headSha`): another head now is 409 too,
23
+ * so an approval never covers (or lands) a push the maintainer did not see. An approval records the head's hash
24
+ * (`proposal`) and its mockups' (`mockups`), as the server read them, never the caller's word
23
25
  * 8. with the API, the token once more (`stillAllowed`): one revoked or expired while the forge was being asked is 403
24
- * 9. the comment is posted as that person (`postReview`)
26
+ * 9. a repeat (spec 0007: a second tab, a retried call): the same approval, by the same person, of the same version, that
27
+ * already stands as the latest review, and would start nothing new (no landing on approval, or one already waiting at this
28
+ * head) is answered 200 with the review that stands and `"posted": false`, and nothing is posted or fired. Anything else
29
+ * (another maintainer, another note or edits, a landing that was cancelled or left open) is posted as before
30
+ * 10. the comment is posted as that person (`postReview`)
25
31
  *
26
32
  * Every attempt that gets past step 1 is a row in `actions` (`record`), whatever the answer: a posted review and a forge
27
33
  * that refused it are written by `postReview`, every refusal before that here. The audit never holds a token's secret,
@@ -38,7 +44,8 @@ import { ForgeError, isMaintainer } from './forge.js';
38
44
  import { PROPOSAL_FILE } from './files.js';
39
45
  import { findPullProposal, forgeWords, forgetReviewState, readPullProposal } from './proposal-pulls.js';
40
46
  import { hashPullMockups } from './mockups.js';
41
- import { postReview, proposalHash, validateReview } from './reviews.js';
47
+ import { latestReview, postReview, proposalHash, validateReview } from './reviews.js';
48
+ import { forgetDecision } from './decision.js';
42
49
  import { Refused, countAttempt, throttle } from './throttle.js';
43
50
  import { proposalPayload } from './hooks.js';
44
51
 
@@ -71,7 +78,7 @@ const MAX_PULL = 999_999_999;
71
78
  * JSON, it was too large): it is refused here, after the flood count and with a row, like every other refusal
72
79
  * @returns {Promise<import('./reviews.js').FoundReview>}
73
80
  */
74
- export async function submitReview(ctx, { pull, slug, review, sha256 = null, problem }) {
81
+ export async function submitReview(ctx, { pull, slug, review, sha256 = null, headSha = null, problem }) {
75
82
  const { who, repo, via } = ctx;
76
83
  const now = ctx.limits.now ?? Date.now;
77
84
  const validPull = typeof pull === 'number' && Number.isInteger(pull) && pull >= 1 && pull <= MAX_PULL;
@@ -79,7 +86,7 @@ export async function submitReview(ctx, { pull, slug, review, sha256 = null, pro
79
86
  // What the audit row says was asked: only what is small, plain and of the right shape, never the whole body
80
87
  // A slug or a hash only when it has the shape of one: anything else the caller sent is not kept (it could be a secret pasted in the wrong field)
81
88
  const slugShown = typeof slug === 'string' && slug.length <= 200 && PROPOSAL_FILE.test(`${slug}.json`) ? slug : undefined;
82
- const asked = /** @type {Record<string, unknown>} */ ({ slug: slugShown, pull: validPull ? pull : undefined, ...(isSha256(sha256) ? { sha256 } : {}), ...(via ? { via: 'api', token: via.token } : {}) });
89
+ const asked = /** @type {Record<string, unknown>} */ ({ slug: slugShown, pull: validPull ? pull : undefined, ...(isSha256(sha256) ? { sha256 } : {}), ...(isCommit(headSha) ? { headSha: headSha.toLowerCase() } : {}), ...(via ? { via: 'api', token: via.token } : {}) });
83
90
 
84
91
  const flood = countAttempt(ctx.limits, who.userId, now());
85
92
  if (flood) throw flood;
@@ -97,6 +104,7 @@ export async function submitReview(ctx, { pull, slug, review, sha256 = null, pro
97
104
  if ((sha256 !== null && sha256 !== undefined) || ctx.requireSha256) {
98
105
  if (!isSha256(sha256)) throw new Refused(400, 'Say which version you read: "sha256" is the proposal file\'s SHA-256, 64 lower-case hex digits.', 'invalid');
99
106
  }
107
+ if (headSha !== null && headSha !== undefined && !isCommit(headSha)) throw new Refused(400, 'Say which head you were shown: "headSha" is a commit\'s SHA, 40 or 64 hex digits.', 'invalid');
100
108
  release = throttle(ctx.limits, who.userId, `review|${pull}|${slug}`, now());
101
109
 
102
110
  if (review !== null && typeof review === 'object') {
@@ -110,13 +118,14 @@ export async function submitReview(ctx, { pull, slug, review, sha256 = null, pro
110
118
  const forge = await ctx.forge();
111
119
  const me = await forge.me();
112
120
  if (String(me.id) !== who.userId) throw new Refused(403, 'The forge says this sign-in is someone else now; sign in again.', 'forbidden');
113
- if (!isMaintainer(await forge.permission(repo, me.login))) throw new Refused(403, `Only the repo's maintainers review proposals; ${me.login} is not one now.`, 'forbidden');
121
+ if (!isMaintainer(await forge.permission(repo, me.login))) throw new Refused(403, `${me.login} no longer maintains this repo, so the forge would not count the review.`, 'forbidden');
114
122
 
115
123
  const entry = await findPullProposal(forge, ctx.where, slug, pull);
116
124
  if (!entry) throw new Refused(404, `Pull request #${pull} is not open or does not hold the proposal ${slug}.`, 'not-found');
117
125
  // The proposal as the head holds it now: a later push that changes it is not this review's
118
126
  const head = proposalHash(await readPullProposal(forge, ctx.where, entry));
119
127
  if (typeof sha256 === 'string' && sha256 !== head) throw new Refused(409, `${slug} has changed since you read it (the head of pull request #${pull} holds ${head.slice(0, 12)}…, not ${sha256.slice(0, 12)}…); read it again and review that version.`, 'conflict');
128
+ if (typeof headSha === 'string' && headSha.toLowerCase() !== String(entry.sha).toLowerCase()) throw new Refused(409, `Pull request #${pull} was pushed to since you looked (its head is ${String(entry.sha).slice(0, 7)} now, not ${headSha.slice(0, 7)}); nothing was posted. Look at it again, then review that.`, 'conflict');
120
129
 
121
130
  let checkedReview = checked.value;
122
131
  if (checkedReview.verdict === 'approved') {
@@ -129,8 +138,18 @@ export async function submitReview(ctx, { pull, slug, review, sha256 = null, pro
129
138
  const why = await ctx.stillAllowed();
130
139
  if (why) throw new Refused(403, why, 'forbidden');
131
140
  }
141
+ // A repeat (step 9): this person's approval of this version stands already, and posting it again would start nothing new. The answer is
142
+ // the review that stands, with nothing posted, no hook fired and no landing begun; it is a row in the audit table like every attempt.
143
+ if (checkedReview.verdict === 'approved') {
144
+ const standing = await latestReview(forge, pull, slug);
145
+ if (isRepeat(standing, checkedReview, me.login) && (!ctx.lander || stillLanding(ctx.lander, pull, entry.sha))) {
146
+ await record({ login: me.login, action: 'review', target, request: asked, status: 200, answer: { posted: false, comment: standing.comment?.id ?? null } });
147
+ return { ...standing, posted: false };
148
+ }
149
+ }
132
150
  const posted = await postReview(forge, pull, slug, checkedReview, { login: me.login, record, via });
133
151
  forgetReviewState(ctx.where, pull); // the listing's review state for this PR is read again (spec 0010)
152
+ forgetDecision(ctx.where, pull); // and the line above Approve: an approval can start a landing
134
153
  // The one place a review is posted, so the button and the API fire the same event; the actor is who the forge says
135
154
  // the poster is (with the API, the token's owner), never a name the caller sent
136
155
  ctx.hooks?.emit(posted.verdict === 'approved' ? 'spec.approved' : 'spec.sentBack', () => proposalPayload(forge, ctx.where, entry, { actor: me.login, note: checkedReview.note, edits: checkedReview.edits }));
@@ -174,5 +193,31 @@ export function refusal(err) {
174
193
  return { status: 500, body: { error: 'The review could not be saved; the log says why.', code: 'unavailable' }, headers: {} };
175
194
  }
176
195
 
196
+ /**
197
+ * Whether `review`, about to be posted by `login`, says nothing that the review standing does not already say: the same person's approval of
198
+ * the same version (the file and its mockups as the head holds them), with the same note and edits.
199
+ * @param {import('./reviews.js').FoundReview | null} standing
200
+ * @param {import('./reviews.js').Review} review the checked approval, with what the server recorded it covers
201
+ * @param {string} login
202
+ */
203
+ export function isRepeat(standing, review, login) {
204
+ if (!standing || standing.by !== login || !standsApproved(standing, review.proposal, review.mockups ?? [])) return false;
205
+ return (standing.note ?? '') === (review.note ?? '') && JSON.stringify(standing.edits ?? []) === JSON.stringify(review.edits ?? []);
206
+ }
207
+
208
+ /** Whether a landing on approval already waits on PR `n` at this head (so an approval again would begin nothing new) */
209
+ export const stillLanding = (lander, n, sha) => String(lander.pending(n)?.sha ?? '').toLowerCase() === String(sha).toLowerCase();
210
+
211
+ /**
212
+ * Whether a review that stands is an approval of exactly this: the proposal file as the head holds it and its mockups (what an approval covers).
213
+ * @param {import('./reviews.js').FoundReview | null} standing
214
+ * @param {string} head the proposal file's SHA-256 at the head
215
+ * @param {{ node: string, path: string, sha256: string }[]} mockups the mockups as the head holds them
216
+ */
217
+ export const standsApproved = (standing, head, mockups) => !!standing && standing.verdict === 'approved' && standing.proposal === head && JSON.stringify(standing.mockups ?? []) === JSON.stringify(mockups);
218
+
219
+ /** A commit's SHA as a caller names it: 40 hex digits (SHA-1) or 64 (SHA-256 repositories) */
220
+ export const isCommit = (v) => typeof v === 'string' && /^(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64})$/.test(v);
221
+
177
222
  /** The checks on a `sha256` the caller names: 64 lower-case hex digits */
178
223
  export const isSha256 = (v) => typeof v === 'string' && SHA256.test(v);