@open-cr-agent/vcs-github 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/LICENSE +202 -0
- package/README.md +3 -0
- package/dist/adapter.d.ts +61 -0
- package/dist/adapter.js +410 -0
- package/dist/client.d.ts +101 -0
- package/dist/client.js +283 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/plugin.d.ts +3 -0
- package/dist/plugin.js +47 -0
- package/dist/render.d.ts +15 -0
- package/dist/render.js +182 -0
- package/dist/retry.d.ts +11 -0
- package/dist/retry.js +38 -0
- package/dist/state.d.ts +14 -0
- package/dist/state.js +110 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
# @open-cr-agent/vcs-github
|
|
2
|
+
|
|
3
|
+
The adapter for GitHub pull requests of [Open-CR-Agent](https://github.com/jma49/Open-CR-Agent), the multi-agent code reviewer. Most users want [`@open-cr-agent/cli`](https://www.npmjs.com/package/@open-cr-agent/cli), which provides the `ocra` command and installs this package; see the [manual](https://ocra.majincheng.com). All `@open-cr-agent` packages are released together at one version. Apache-2.0.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type ChangeRequest, type CodeMatch, type FileDiff, type PriorReview, type ReviewReport, type VcsAdapter } from "@open-cr-agent/core";
|
|
2
|
+
import { type GitHubApi, type PullRequest } from "./client.js";
|
|
3
|
+
export interface GitHubPullRequest {
|
|
4
|
+
owner: string;
|
|
5
|
+
repo: string;
|
|
6
|
+
number: number;
|
|
7
|
+
}
|
|
8
|
+
export type CodeSource = Pick<VcsAdapter, "getDiff" | "readFile" | "searchCode">;
|
|
9
|
+
export interface History {
|
|
10
|
+
filesChangedSince(from: string, to: string): Promise<{
|
|
11
|
+
files: string[];
|
|
12
|
+
} | {
|
|
13
|
+
reason: string;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
export interface GitHubAdapterOptions {
|
|
17
|
+
pullRequest: GitHubPullRequest;
|
|
18
|
+
api: GitHubApi;
|
|
19
|
+
code: CodeSource;
|
|
20
|
+
botLogin: string;
|
|
21
|
+
requestChanges?: boolean;
|
|
22
|
+
history?: History;
|
|
23
|
+
snapshot?: PullRequest;
|
|
24
|
+
}
|
|
25
|
+
export declare const DEFAULT_BOT_LOGIN = "github-actions[bot]";
|
|
26
|
+
export declare function declinesFinding(reply: string): boolean;
|
|
27
|
+
export declare class GitHubAdapter implements VcsAdapter {
|
|
28
|
+
private readonly options;
|
|
29
|
+
readonly name = "github";
|
|
30
|
+
private pullRequest;
|
|
31
|
+
private previous;
|
|
32
|
+
private reviewThreads;
|
|
33
|
+
private issueComments;
|
|
34
|
+
private readonly writers;
|
|
35
|
+
private prior;
|
|
36
|
+
constructor(options: GitHubAdapterOptions);
|
|
37
|
+
getChangeRequest(): Promise<ChangeRequest>;
|
|
38
|
+
private override;
|
|
39
|
+
private unedited;
|
|
40
|
+
private canWrite;
|
|
41
|
+
getDiff(): Promise<FileDiff[]>;
|
|
42
|
+
readFile(path: string): Promise<string | undefined>;
|
|
43
|
+
searchCode(literal: string): Promise<CodeMatch[]>;
|
|
44
|
+
getPriorReview(): Promise<PriorReview | undefined>;
|
|
45
|
+
private priorState;
|
|
46
|
+
private changesSince;
|
|
47
|
+
private repliesByPeople;
|
|
48
|
+
publish(report: ReviewReport): Promise<{
|
|
49
|
+
warnings: string[];
|
|
50
|
+
}>;
|
|
51
|
+
private syncChangeRequest;
|
|
52
|
+
private resolveFixedThreads;
|
|
53
|
+
private lastEditor;
|
|
54
|
+
private isBot;
|
|
55
|
+
private postReview;
|
|
56
|
+
private threads;
|
|
57
|
+
private pr;
|
|
58
|
+
private comments;
|
|
59
|
+
private summaryComment;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=adapter.d.ts.map
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import { errorMessage, } from "@open-cr-agent/core";
|
|
2
|
+
import { GitHubApiError, } from "./client.js";
|
|
3
|
+
import { FINDING_MARKER, inlineComment, renderSummary } from "./render.js";
|
|
4
|
+
import { readState, SUMMARY_MARKER } from "./state.js";
|
|
5
|
+
export const DEFAULT_BOT_LOGIN = "github-actions[bot]";
|
|
6
|
+
const OVERRIDE = /^\/ocra override ([0-9a-fA-F]{40}(?:[0-9a-fA-F]{24})?)\s+(\S.*)$/m;
|
|
7
|
+
const MAX_OVERRIDE_REASON = 300;
|
|
8
|
+
const MAX_REPLIES = 3;
|
|
9
|
+
const MAX_REPLY_CHARS = 1_000;
|
|
10
|
+
const DECLINE = /^(won['’]?t fix|wontfix|will not fix|by design|false positive|not a bug|working as intended|intended behaviou?r)(?=$|[\s.,;:!—–-])/i;
|
|
11
|
+
const COMMAND = /^\/ocra dismiss\b/im;
|
|
12
|
+
// A dismissal takes a clear decline: an explicit `/ocra dismiss`, or a reply
|
|
13
|
+
// that opens with a decline and does not ask. Words anywhere else in a reply
|
|
14
|
+
// ("this is not intended", "is this by design?", "acknowledged, will fix")
|
|
15
|
+
// do not dismiss, since a dismissed finding leaves the verdict. Disagreement
|
|
16
|
+
// ("I disagree") keeps the finding reported.
|
|
17
|
+
export function declinesFinding(reply) {
|
|
18
|
+
if (COMMAND.test(reply))
|
|
19
|
+
return true;
|
|
20
|
+
const opening = reply
|
|
21
|
+
.trim()
|
|
22
|
+
.split(/(?<=[.!?])\s|\n/)[0]
|
|
23
|
+
?.trim() ?? "";
|
|
24
|
+
return DECLINE.test(opening) && !opening.endsWith("?");
|
|
25
|
+
}
|
|
26
|
+
export class GitHubAdapter {
|
|
27
|
+
options;
|
|
28
|
+
name = "github";
|
|
29
|
+
pullRequest;
|
|
30
|
+
previous;
|
|
31
|
+
reviewThreads;
|
|
32
|
+
issueComments;
|
|
33
|
+
writers = new Map();
|
|
34
|
+
prior;
|
|
35
|
+
constructor(options) {
|
|
36
|
+
this.options = options;
|
|
37
|
+
}
|
|
38
|
+
async getChangeRequest() {
|
|
39
|
+
const pr = await this.pr();
|
|
40
|
+
const { owner, repo } = this.options.pullRequest;
|
|
41
|
+
const override = await this.override(pr);
|
|
42
|
+
return {
|
|
43
|
+
id: `${owner}/${repo}#${pr.number}`,
|
|
44
|
+
title: pr.title,
|
|
45
|
+
description: pr.body ?? "",
|
|
46
|
+
baseSha: pr.base.sha,
|
|
47
|
+
headSha: pr.head.sha,
|
|
48
|
+
...(override ? { override } : {}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
// "Break glass": `/ocra override <commit> <reason>` from someone with
|
|
52
|
+
// write access other than the author lets a blocking verdict pass for that
|
|
53
|
+
// commit only, so a later push with new problems needs a new decision. The
|
|
54
|
+
// author can never overrule the review of their own change. Comments are
|
|
55
|
+
// best effort: without them there is simply no override.
|
|
56
|
+
async override(pr) {
|
|
57
|
+
const comments = await this.comments().catch(() => []);
|
|
58
|
+
const author = pr.user?.login;
|
|
59
|
+
let found;
|
|
60
|
+
for (const c of comments) {
|
|
61
|
+
const login = c.user?.login;
|
|
62
|
+
const match = OVERRIDE.exec(c.body);
|
|
63
|
+
if (!match || !login || login === author || this.isBot(login))
|
|
64
|
+
continue;
|
|
65
|
+
const [, commit = "", reason = ""] = match;
|
|
66
|
+
// The full commit id: a short prefix can be matched by a new commit.
|
|
67
|
+
if (commit.toLowerCase() !== pr.head.sha)
|
|
68
|
+
continue;
|
|
69
|
+
if (!(await this.unedited(c.node_id, login)) || !(await this.canWrite(login)))
|
|
70
|
+
continue;
|
|
71
|
+
found = { by: login, reason: reason.trim().slice(0, MAX_OVERRIDE_REASON) };
|
|
72
|
+
}
|
|
73
|
+
return found;
|
|
74
|
+
}
|
|
75
|
+
// Anyone with write access can edit anyone's comment, and GitHub keeps
|
|
76
|
+
// showing the original author: a command counts only when nobody else
|
|
77
|
+
// edited it. Unknown means no.
|
|
78
|
+
async unedited(nodeId, login) {
|
|
79
|
+
if (!nodeId)
|
|
80
|
+
return false;
|
|
81
|
+
try {
|
|
82
|
+
const editor = await this.options.api.commentEditor(nodeId);
|
|
83
|
+
return editor === undefined || editor === login;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// Write access from the repository's permissions, memoized per login;
|
|
90
|
+
// a failed lookup means no.
|
|
91
|
+
canWrite(login) {
|
|
92
|
+
let known = this.writers.get(login);
|
|
93
|
+
if (!known) {
|
|
94
|
+
known = this.options.api.canWrite(login).catch(() => false);
|
|
95
|
+
this.writers.set(login, known);
|
|
96
|
+
}
|
|
97
|
+
return known;
|
|
98
|
+
}
|
|
99
|
+
getDiff() {
|
|
100
|
+
return this.options.code.getDiff();
|
|
101
|
+
}
|
|
102
|
+
readFile(path) {
|
|
103
|
+
return this.options.code.readFile(path);
|
|
104
|
+
}
|
|
105
|
+
searchCode(literal) {
|
|
106
|
+
return this.options.code.searchCode(literal);
|
|
107
|
+
}
|
|
108
|
+
async getPriorReview() {
|
|
109
|
+
const prior = await this.priorState();
|
|
110
|
+
if ("untrusted" in prior)
|
|
111
|
+
return { findings: [], fullReviewReason: prior.untrusted };
|
|
112
|
+
const { state } = prior;
|
|
113
|
+
if (!state)
|
|
114
|
+
return undefined;
|
|
115
|
+
const { dismissed, replies } = await this.repliesByPeople(state.findings);
|
|
116
|
+
return {
|
|
117
|
+
findings: state.findings.map((f) => dismissed.has(f.fingerprint) ? { ...f, dismissed: true } : f),
|
|
118
|
+
...(await this.changesSince(state)),
|
|
119
|
+
...(state.tier ? { tier: state.tier } : {}),
|
|
120
|
+
...(Object.keys(replies).length > 0 ? { replies } : {}),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
// Anyone with write access can edit the summary comment, including a pull
|
|
124
|
+
// request's author. Every part of the state is then suspect: a head skips
|
|
125
|
+
// their next changes, a `commented` flag hides a finding from both the
|
|
126
|
+
// inline comments and the summary. So the state counts only when ocra was
|
|
127
|
+
// the last to edit the comment; otherwise the run starts over.
|
|
128
|
+
priorState() {
|
|
129
|
+
this.prior ??= (async () => {
|
|
130
|
+
const comment = await this.summaryComment();
|
|
131
|
+
const state = comment ? readState(comment.body) : undefined;
|
|
132
|
+
if (!comment || !state)
|
|
133
|
+
return {};
|
|
134
|
+
const editor = await this.lastEditor(comment);
|
|
135
|
+
if (editor === undefined) {
|
|
136
|
+
return { untrusted: "could not check who last edited the previous review's summary" };
|
|
137
|
+
}
|
|
138
|
+
if (!this.isBot(editor)) {
|
|
139
|
+
return { untrusted: "the previous review's summary was edited by someone else" };
|
|
140
|
+
}
|
|
141
|
+
return { state };
|
|
142
|
+
})();
|
|
143
|
+
return this.prior;
|
|
144
|
+
}
|
|
145
|
+
async changesSince(state) {
|
|
146
|
+
const { history } = this.options;
|
|
147
|
+
if (!state.head)
|
|
148
|
+
return { fullReviewReason: "the previous review did not record its commit" };
|
|
149
|
+
if (!history)
|
|
150
|
+
return { fullReviewReason: "no repository history to compare with" };
|
|
151
|
+
const head = (await this.pr()).head.sha;
|
|
152
|
+
const changed = await history.filesChangedSince(state.head, head);
|
|
153
|
+
if ("reason" in changed)
|
|
154
|
+
return { fullReviewReason: changed.reason };
|
|
155
|
+
return {
|
|
156
|
+
changedSince: {
|
|
157
|
+
head: state.head,
|
|
158
|
+
files: [...new Set([...changed.files, ...(state.pending ?? [])])],
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
// A thread of ocra's that a reviewer resolved, or answered with a clear
|
|
163
|
+
// "won't fix", dismisses its finding. A reviewer is someone with write
|
|
164
|
+
// access other than the pull request's author, in a comment nobody else
|
|
165
|
+
// edited: otherwise the author could resolve or argue a finding away. Any
|
|
166
|
+
// other reply from a reviewer dismisses nothing, but the judge weighs it
|
|
167
|
+
// when the finding comes back (and cannot drop a confirmed critical for
|
|
168
|
+
// it). Replies are collected for every thread of ocra's, not only tracked
|
|
169
|
+
// findings, so a finding a reply argued away does not return without it.
|
|
170
|
+
// Thread data is best effort: without it nothing is dismissed or replied.
|
|
171
|
+
async repliesByPeople(findings) {
|
|
172
|
+
const commented = new Set(findings.filter((f) => f.commented).map((f) => f.fingerprint));
|
|
173
|
+
const author = (await this.pr()).user?.login;
|
|
174
|
+
const reviewer = async (login) => login !== undefined && login !== author && !this.isBot(login) && (await this.canWrite(login));
|
|
175
|
+
const threads = await this.threads().catch(() => []);
|
|
176
|
+
const dismissed = new Set();
|
|
177
|
+
const replies = {};
|
|
178
|
+
for (const thread of threads) {
|
|
179
|
+
const [first, ...rest] = thread.comments;
|
|
180
|
+
const fingerprint = first && FINDING_MARKER.exec(first.body)?.[1];
|
|
181
|
+
if (!first || !fingerprint || !this.isBot(first.author))
|
|
182
|
+
continue;
|
|
183
|
+
const said = [];
|
|
184
|
+
let declined = false;
|
|
185
|
+
for (const r of rest) {
|
|
186
|
+
const own = r.editor === undefined || r.editor === r.author;
|
|
187
|
+
if (!own || !(await reviewer(r.author)))
|
|
188
|
+
continue;
|
|
189
|
+
if (declinesFinding(r.body))
|
|
190
|
+
declined = true;
|
|
191
|
+
else if (r.body.trim() !== "")
|
|
192
|
+
said.push(r.body.trim().slice(0, MAX_REPLY_CHARS));
|
|
193
|
+
}
|
|
194
|
+
if (commented.has(fingerprint)) {
|
|
195
|
+
const resolved = thread.isResolved && (await reviewer(thread.resolvedBy));
|
|
196
|
+
if (resolved || declined) {
|
|
197
|
+
dismissed.add(fingerprint);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (said.length > 0)
|
|
202
|
+
replies[fingerprint] = said.slice(-MAX_REPLIES);
|
|
203
|
+
}
|
|
204
|
+
return { dismissed, replies };
|
|
205
|
+
}
|
|
206
|
+
async publish(report) {
|
|
207
|
+
const { number } = this.options.pullRequest;
|
|
208
|
+
const pr = await this.pr();
|
|
209
|
+
const previous = await this.summaryComment();
|
|
210
|
+
const prior = await this.priorState();
|
|
211
|
+
const trusted = "state" in prior ? prior.state : undefined;
|
|
212
|
+
const before = trusted?.findings ?? [];
|
|
213
|
+
const alreadyCommented = new Set([
|
|
214
|
+
...before.filter((f) => f.commented).map((f) => f.fingerprint),
|
|
215
|
+
...(trusted?.posted ?? []),
|
|
216
|
+
]);
|
|
217
|
+
const fresh = report.findings.flatMap((f) => {
|
|
218
|
+
if (alreadyCommented.has(f.fingerprint))
|
|
219
|
+
return [];
|
|
220
|
+
const comment = inlineComment(f);
|
|
221
|
+
return comment ? [{ fingerprint: f.fingerprint, comment }] : [];
|
|
222
|
+
});
|
|
223
|
+
const changeRequests = await this.syncChangeRequest(number, report);
|
|
224
|
+
const posted = await this.postReview(number, pr.head.sha, report, fresh, changeRequests.request);
|
|
225
|
+
const commented = new Set([...alreadyCommented, ...posted]);
|
|
226
|
+
const current = new Set(report.findings.map((f) => f.fingerprint));
|
|
227
|
+
const rereview = report.rereview;
|
|
228
|
+
const quiet = [
|
|
229
|
+
...(rereview?.notReproduced ?? []),
|
|
230
|
+
...(rereview?.notRechecked ?? []),
|
|
231
|
+
...(rereview?.unchanged ?? []),
|
|
232
|
+
...(rereview?.dismissed ?? []),
|
|
233
|
+
];
|
|
234
|
+
// Low-confidence findings (--ultra) are shown, not counted; stored as
|
|
235
|
+
// ordinary findings they would count on the next push.
|
|
236
|
+
const state = [
|
|
237
|
+
...report.findings
|
|
238
|
+
.filter((f) => !f.lowConfidence)
|
|
239
|
+
.map((f) => ({
|
|
240
|
+
fingerprint: f.fingerprint,
|
|
241
|
+
title: f.title,
|
|
242
|
+
file: f.file,
|
|
243
|
+
severity: f.severity,
|
|
244
|
+
commented: commented.has(f.fingerprint),
|
|
245
|
+
...(f.quote ? { quote: f.quote } : {}),
|
|
246
|
+
...(f.verification ? { verification: f.verification } : {}),
|
|
247
|
+
})),
|
|
248
|
+
...quiet.filter((f) => !current.has(f.fingerprint)),
|
|
249
|
+
];
|
|
250
|
+
const pending = report.coverage
|
|
251
|
+
.filter((c) => c.status === "failed" || c.status === "unreviewed")
|
|
252
|
+
.map((c) => c.path);
|
|
253
|
+
const tracked = new Set(state.map((f) => f.fingerprint));
|
|
254
|
+
const untracked = [...commented].filter((fp) => !tracked.has(fp));
|
|
255
|
+
const body = renderSummary({
|
|
256
|
+
report,
|
|
257
|
+
commented,
|
|
258
|
+
state: {
|
|
259
|
+
findings: state,
|
|
260
|
+
head: report.changeRequest.headSha,
|
|
261
|
+
pending,
|
|
262
|
+
tier: report.tier,
|
|
263
|
+
...(untracked.length > 0 ? { posted: untracked } : {}),
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
if (previous)
|
|
267
|
+
await this.options.api.updateIssueComment(previous.id, body);
|
|
268
|
+
else
|
|
269
|
+
await this.options.api.createIssueComment(number, body);
|
|
270
|
+
return {
|
|
271
|
+
warnings: [
|
|
272
|
+
...changeRequests.warnings,
|
|
273
|
+
...(await changeRequests.withdraw()),
|
|
274
|
+
...(await this.resolveFixedThreads(number, report)),
|
|
275
|
+
],
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
// With requestChanges on, ocra's "changes requested" follows the verdict:
|
|
279
|
+
// requested once while it blocks, withdrawn (dismissed) once it no longer
|
|
280
|
+
// does. GitHub keeps a reviewer's last blocking review until it is
|
|
281
|
+
// dismissed, so otherwise a fixed pull request stayed blocked.
|
|
282
|
+
async syncChangeRequest(number, report) {
|
|
283
|
+
const none = { request: false, warnings: [], withdraw: async () => [] };
|
|
284
|
+
if (this.options.requestChanges !== true)
|
|
285
|
+
return none;
|
|
286
|
+
// An override lets the commit pass, so it lifts the request too.
|
|
287
|
+
const blocking = report.verdict === "significant_concerns" && report.changeRequest.override === undefined;
|
|
288
|
+
let active;
|
|
289
|
+
try {
|
|
290
|
+
active = (await this.options.api.listReviews(number))
|
|
291
|
+
.filter((r) => r.state === "CHANGES_REQUESTED" && r.user && this.isBot(r.user.login))
|
|
292
|
+
.map((r) => r.id);
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
return {
|
|
296
|
+
request: blocking,
|
|
297
|
+
warnings: [`could not list earlier reviews: ${errorMessage(error)}`],
|
|
298
|
+
withdraw: async () => [],
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
if (blocking)
|
|
302
|
+
return { ...none, request: active.length === 0 };
|
|
303
|
+
return {
|
|
304
|
+
request: false,
|
|
305
|
+
warnings: [],
|
|
306
|
+
withdraw: async () => {
|
|
307
|
+
const warnings = [];
|
|
308
|
+
for (const id of active) {
|
|
309
|
+
await this.options.api
|
|
310
|
+
.dismissReview(number, id, `ocra: no blocking findings remain (verdict: ${report.verdict.replaceAll("_", " ")}).`)
|
|
311
|
+
.catch((error) => warnings.push(`could not withdraw ocra's request for changes: ${errorMessage(error)}`));
|
|
312
|
+
}
|
|
313
|
+
return warnings;
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
// Resolving threads is a courtesy: the summary already lists what was
|
|
318
|
+
// fixed, so a failure here is a warning, not a failed publish.
|
|
319
|
+
async resolveFixedThreads(number, report) {
|
|
320
|
+
const fixed = new Set((report.rereview?.fixed ?? []).filter((f) => f.commented).map((f) => f.fingerprint));
|
|
321
|
+
if (fixed.size === 0)
|
|
322
|
+
return [];
|
|
323
|
+
try {
|
|
324
|
+
for (const thread of await this.threads()) {
|
|
325
|
+
const first = thread.comments[0];
|
|
326
|
+
const fingerprint = first && FINDING_MARKER.exec(first.body)?.[1];
|
|
327
|
+
if (thread.isResolved || !fingerprint || !fixed.has(fingerprint))
|
|
328
|
+
continue;
|
|
329
|
+
if (!this.isBot(first.author))
|
|
330
|
+
continue;
|
|
331
|
+
await this.options.api.resolveReviewThread(thread.id);
|
|
332
|
+
}
|
|
333
|
+
return [];
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
return [`could not resolve the threads of fixed findings: ${errorMessage(error)}`];
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
// The account that last edited the comment (ocra itself when nobody did),
|
|
340
|
+
// or undefined when that cannot be told.
|
|
341
|
+
async lastEditor(comment) {
|
|
342
|
+
if (!comment.node_id)
|
|
343
|
+
return undefined;
|
|
344
|
+
try {
|
|
345
|
+
return (await this.options.api.commentEditor(comment.node_id)) ?? this.options.botLogin;
|
|
346
|
+
}
|
|
347
|
+
catch {
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
// REST names the Actions bot "github-actions[bot]", GraphQL "github-actions".
|
|
352
|
+
isBot(login) {
|
|
353
|
+
return login === this.options.botLogin || `${login}[bot]` === this.options.botLogin;
|
|
354
|
+
}
|
|
355
|
+
// Returns the fingerprints that now have inline comments. GitHub rejects
|
|
356
|
+
// positions outside its own diff with 422; those findings then stay in the
|
|
357
|
+
// summary instead of failing the publish.
|
|
358
|
+
async postReview(number, commitId, report, fresh, requestChanges) {
|
|
359
|
+
// GitHub rejects the whole review when one comment sits on a file it
|
|
360
|
+
// shows no diff for (too large, too many files); those go to the summary.
|
|
361
|
+
if (fresh.length > 0) {
|
|
362
|
+
const shown = await this.options.api.filesWithDiff(number).catch(() => undefined);
|
|
363
|
+
if (shown)
|
|
364
|
+
fresh = fresh.filter((f) => shown.has(f.comment.path));
|
|
365
|
+
}
|
|
366
|
+
if (fresh.length === 0 && !requestChanges)
|
|
367
|
+
return [];
|
|
368
|
+
const review = {
|
|
369
|
+
commit_id: commitId,
|
|
370
|
+
event: requestChanges ? "REQUEST_CHANGES" : "COMMENT",
|
|
371
|
+
body: `ocra: ${report.findings.length} finding(s); details in the summary comment.`,
|
|
372
|
+
};
|
|
373
|
+
try {
|
|
374
|
+
await this.options.api.createReview(number, {
|
|
375
|
+
...review,
|
|
376
|
+
comments: fresh.map((f) => f.comment),
|
|
377
|
+
});
|
|
378
|
+
return fresh.map((f) => f.fingerprint);
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
if (!(error instanceof GitHubApiError) || error.status !== 422)
|
|
382
|
+
throw error;
|
|
383
|
+
// The request for changes still matters; if GitHub refuses it too (for
|
|
384
|
+
// example on the token owner's own pull request), the summary remains.
|
|
385
|
+
if (requestChanges) {
|
|
386
|
+
await this.options.api.createReview(number, { ...review, comments: [] }).catch(() => { });
|
|
387
|
+
}
|
|
388
|
+
return [];
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
threads() {
|
|
392
|
+
this.reviewThreads ??= this.options.api.listReviewThreads(this.options.pullRequest.number);
|
|
393
|
+
return this.reviewThreads;
|
|
394
|
+
}
|
|
395
|
+
pr() {
|
|
396
|
+
this.pullRequest ??= this.options.snapshot
|
|
397
|
+
? Promise.resolve(this.options.snapshot)
|
|
398
|
+
: this.options.api.getPullRequest(this.options.pullRequest.number);
|
|
399
|
+
return this.pullRequest;
|
|
400
|
+
}
|
|
401
|
+
comments() {
|
|
402
|
+
this.issueComments ??= this.options.api.listIssueComments(this.options.pullRequest.number);
|
|
403
|
+
return this.issueComments;
|
|
404
|
+
}
|
|
405
|
+
summaryComment() {
|
|
406
|
+
this.previous ??= this.comments().then((comments) => comments.findLast((c) => c.user?.login === this.options.botLogin && c.body.includes(SUMMARY_MARKER)));
|
|
407
|
+
return this.previous;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
//# sourceMappingURL=adapter.js.map
|