@in-the-loop-labs/pair-review 5.3.7 → 5.4.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/README.md +8 -0
- package/package.json +3 -3
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin-code-critic/.claude-plugin/plugin.json +1 -1
- package/plugin-pair-loop/.claude-plugin/plugin.json +1 -1
- package/public/css/pr.css +389 -13
- package/public/js/components/AIPanel.js +317 -99
- package/public/js/components/ReviewHistoryModal.js +294 -0
- package/public/js/components/ReviewModal.js +96 -7
- package/public/js/components/SplitButton.js +14 -0
- package/public/js/local.js +4 -8
- package/public/js/modules/comment-manager.js +2 -1
- package/public/js/modules/file-comment-manager.js +6 -2
- package/public/js/modules/pierre-bridge.js +35 -1
- package/public/js/modules/suggestion-manager.js +4 -1
- package/public/js/pr.js +174 -66
- package/public/js/utils/posted-comment.js +259 -14
- package/public/js/vendor/pierre-diffs-worker.js +41 -13485
- package/public/js/vendor/pierre-diffs.js +131 -1364
- package/public/pr.html +1 -0
- package/src/chat/api-reference.js +22 -2
- package/src/config.js +28 -4
- package/src/database.js +177 -56
- package/src/github/client.js +70 -15
- package/src/github/impl/graphql/pending-review-comments.js +50 -11
- package/src/github/impl/graphql/pending-review.js +8 -2
- package/src/github/impl/graphql/review-comment-edit.js +89 -0
- package/src/github/impl/host/pending-review-comments.js +41 -10
- package/src/github/impl/host/review-comment-edit.js +36 -0
- package/src/github/impl/rest/pending-review.js +6 -2
- package/src/github/impl/rest/review-comment-edit.js +44 -0
- package/src/github/impl/review-comment-edit-utils.js +45 -0
- package/src/github/operations/pending-review.js +4 -3
- package/src/github/operations/review-comment-edit.js +35 -0
- package/src/github/record-submission.js +78 -0
- package/src/github/update-posted-comment.js +115 -0
- package/src/main.js +24 -44
- package/src/routes/mcp.js +1 -0
- package/src/routes/pr.js +147 -106
package/src/github/client.js
CHANGED
|
@@ -6,6 +6,7 @@ const { GitHubApiError, isComplexityError } = require('./errors');
|
|
|
6
6
|
const pendingReviewOps = require('./operations/pending-review');
|
|
7
7
|
const reviewLifecycleOps = require('./operations/review-lifecycle');
|
|
8
8
|
const pendingReviewCommentsOps = require('./operations/pending-review-comments');
|
|
9
|
+
const reviewCommentEditOps = require('./operations/review-comment-edit');
|
|
9
10
|
|
|
10
11
|
// Defaults used when `GitHubClient` is constructed from a bare token
|
|
11
12
|
// string (i.e. without a resolved binding). These mirror the
|
|
@@ -17,7 +18,8 @@ const DEFAULT_FEATURES = Object.freeze({
|
|
|
17
18
|
pending_review_check: 'graphql',
|
|
18
19
|
stack_walker: 'graphql',
|
|
19
20
|
review_lifecycle: 'graphql',
|
|
20
|
-
pending_review_comments: 'graphql'
|
|
21
|
+
pending_review_comments: 'graphql',
|
|
22
|
+
review_comment_edit: 'graphql'
|
|
21
23
|
});
|
|
22
24
|
|
|
23
25
|
/**
|
|
@@ -390,6 +392,37 @@ class GitHubClient {
|
|
|
390
392
|
}
|
|
391
393
|
}
|
|
392
394
|
|
|
395
|
+
/**
|
|
396
|
+
* Update an existing review comment in place, including a pending draft.
|
|
397
|
+
* Never creates a comment or retries an update as a create operation.
|
|
398
|
+
* @param {Object} ref - {owner, repo, pull_number, github_comment_id,
|
|
399
|
+
* github_comment_node_id, github_review_id, github_node_id}. The last
|
|
400
|
+
* node id identifies the review, not the comment. Only needed fields
|
|
401
|
+
* for the chosen transport must be supplied; missing IDs use safe reads.
|
|
402
|
+
* @param {string} body - Already-formatted wire body; sent unchanged.
|
|
403
|
+
* @returns {Promise<{github_comment_id: string|null,
|
|
404
|
+
* github_comment_node_id: string|null, github_comment_url: string|null, body: string}>}
|
|
405
|
+
* @throws {Error} code: not_found, forbidden, invalid_ref; other errors
|
|
406
|
+
* retain their cause and include operation/transport context.
|
|
407
|
+
*/
|
|
408
|
+
async updateReviewComment(ref, body) {
|
|
409
|
+
return reviewCommentEditOps.updateReviewComment(this.octokit, this.features, ref, body);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Paginate one review's comments, including the author's pending draft.
|
|
414
|
+
* @param {Object} ref - Same shape as updateReviewComment; provide a
|
|
415
|
+
* github_review_id and/or github_node_id (review node ID).
|
|
416
|
+
* @returns {Promise<Array<Object>>} REST-shaped candidates (string id,
|
|
417
|
+
* node_id, html_url, user.login, in_reply_to_id, path, line, start_line,
|
|
418
|
+
* side, start_side, original_line, original_start_line, subject_type,
|
|
419
|
+
* body, created_at), plus github_comment_id/github_comment_node_id/
|
|
420
|
+
* github_comment_url/author_login aliases. Bodies are never normalized.
|
|
421
|
+
*/
|
|
422
|
+
async listCommentsForReview(ref) {
|
|
423
|
+
return reviewCommentEditOps.listCommentsForReview(this.octokit, this.features, ref);
|
|
424
|
+
}
|
|
425
|
+
|
|
393
426
|
/**
|
|
394
427
|
* Validate GitHub token by making a test API call
|
|
395
428
|
* @returns {Promise<boolean>} Whether the token is valid
|
|
@@ -686,9 +719,11 @@ class GitHubClient {
|
|
|
686
719
|
*
|
|
687
720
|
* @param {string} nodeId - GraphQL node id
|
|
688
721
|
* @param {Object} [prContext] - `{ owner, repo, prNumber, reviewId? }`
|
|
722
|
+
* @param {Object} [options]
|
|
723
|
+
* @param {boolean} [options.strict=false] - Throw lookup failures other than not found
|
|
689
724
|
*/
|
|
690
|
-
async getReviewById(nodeId, prContext) {
|
|
691
|
-
return pendingReviewOps.getReviewById(this.octokit, this.features, nodeId, prContext);
|
|
725
|
+
async getReviewById(nodeId, prContext, options) {
|
|
726
|
+
return pendingReviewOps.getReviewById(this.octokit, this.features, nodeId, prContext, options);
|
|
692
727
|
}
|
|
693
728
|
|
|
694
729
|
/**
|
|
@@ -734,6 +769,7 @@ class GitHubClient {
|
|
|
734
769
|
// the alt-host REST/extension path rather than GraphQL. Keep the
|
|
735
770
|
// messages accurate to what really ran.
|
|
736
771
|
const transport = this.apiHost ? 'alt-host' : 'GraphQL';
|
|
772
|
+
const submission = { postedComments: [], reviewRetained: false, pendingReview: null };
|
|
737
773
|
try {
|
|
738
774
|
console.log(`Creating review (${transport}) for PR ${prNodeId} with ${comments.length} comments`);
|
|
739
775
|
|
|
@@ -783,10 +819,13 @@ class GitHubClient {
|
|
|
783
819
|
? { ...prContext, reviewId: reviewDatabaseId !== null ? reviewDatabaseId : prContext.reviewId }
|
|
784
820
|
: (reviewDatabaseId !== null ? { reviewId: reviewDatabaseId } : null);
|
|
785
821
|
|
|
822
|
+
submission.pendingReview = { id: reviewId, databaseId: reviewDatabaseId };
|
|
823
|
+
submission.reviewRetained = true;
|
|
786
824
|
let successfulComments = 0;
|
|
787
825
|
if (comments.length > 0) {
|
|
788
826
|
console.log(`Step 2: Adding ${comments.length} comments in batches...`);
|
|
789
827
|
let batchResult;
|
|
828
|
+
submission.postedComments = null; // Unknown until the transport reports a subset.
|
|
790
829
|
try {
|
|
791
830
|
batchResult = await this.addCommentsInBatches(prNodeId, reviewId, comments, 10, downstreamPrContext);
|
|
792
831
|
} catch (commentError) {
|
|
@@ -797,8 +836,10 @@ class GitHubClient {
|
|
|
797
836
|
// `batchResult.failed` was returned, leaving a pending review
|
|
798
837
|
// behind on these throws when we created it ourselves.
|
|
799
838
|
console.error(`CRITICAL: comment batch threw before completion: ${commentError.message}`);
|
|
839
|
+
submission.postedComments = commentError.postedComments ?? null;
|
|
800
840
|
if (!usedExistingReview) {
|
|
801
841
|
const cleaned = await this.deletePendingReview(reviewId, downstreamPrContext);
|
|
842
|
+
submission.reviewRetained = !cleaned;
|
|
802
843
|
if (!cleaned) {
|
|
803
844
|
console.warn('Warning: Failed to clean up pending review - manual cleanup may be required');
|
|
804
845
|
}
|
|
@@ -813,6 +854,7 @@ class GitHubClient {
|
|
|
813
854
|
throw wrapped;
|
|
814
855
|
}
|
|
815
856
|
successfulComments = batchResult.successCount;
|
|
857
|
+
submission.postedComments = batchResult.posted ?? null;
|
|
816
858
|
|
|
817
859
|
if (batchResult.failed) {
|
|
818
860
|
const failedCount = comments.length - successfulComments;
|
|
@@ -820,6 +862,7 @@ class GitHubClient {
|
|
|
820
862
|
console.error(`CRITICAL: ${failedCount} of ${comments.length} comments failed to add to GitHub`);
|
|
821
863
|
if (!usedExistingReview) {
|
|
822
864
|
const cleaned = await this.deletePendingReview(reviewId, downstreamPrContext);
|
|
865
|
+
submission.reviewRetained = !cleaned;
|
|
823
866
|
if (!cleaned) {
|
|
824
867
|
console.warn('Warning: Failed to clean up pending review - manual cleanup may be required');
|
|
825
868
|
}
|
|
@@ -848,7 +891,8 @@ class GitHubClient {
|
|
|
848
891
|
databaseId: result.databaseId,
|
|
849
892
|
html_url: result.url,
|
|
850
893
|
state: result.state,
|
|
851
|
-
comments_count: successfulComments
|
|
894
|
+
comments_count: successfulComments,
|
|
895
|
+
postedComments: submission.postedComments
|
|
852
896
|
};
|
|
853
897
|
|
|
854
898
|
} catch (error) {
|
|
@@ -859,10 +903,10 @@ class GitHubClient {
|
|
|
859
903
|
// label the message with the actual transport for accuracy.
|
|
860
904
|
if (error.errors) {
|
|
861
905
|
const messages = error.errors.map(e => e.message).join(', ');
|
|
862
|
-
throw new Error(`GitHub ${transport} error: ${messages}`);
|
|
906
|
+
throw Object.assign(new Error(`GitHub ${transport} error: ${messages}`), submission);
|
|
863
907
|
}
|
|
864
908
|
|
|
865
|
-
throw new Error(`Failed to submit review (${transport}): ${error.message}`);
|
|
909
|
+
throw Object.assign(new Error(`Failed to submit review (${transport}): ${error.message}`), submission);
|
|
866
910
|
}
|
|
867
911
|
}
|
|
868
912
|
|
|
@@ -884,6 +928,7 @@ class GitHubClient {
|
|
|
884
928
|
// historical; the real transport may be the alt-host REST/extension
|
|
885
929
|
// path rather than GraphQL. See createReviewGraphQL for the rationale.
|
|
886
930
|
const transport = this.apiHost ? 'alt-host' : 'GraphQL';
|
|
931
|
+
const submission = { postedComments: [], reviewRetained: false, pendingReview: null };
|
|
887
932
|
try {
|
|
888
933
|
console.log(`Creating draft review (${transport}) for PR ${prNodeId} with ${comments.length} comments`);
|
|
889
934
|
|
|
@@ -933,10 +978,13 @@ class GitHubClient {
|
|
|
933
978
|
? { ...prContext, reviewId: reviewDatabaseId !== null ? reviewDatabaseId : prContext.reviewId }
|
|
934
979
|
: (reviewDatabaseId !== null ? { reviewId: reviewDatabaseId } : null);
|
|
935
980
|
|
|
981
|
+
submission.pendingReview = { id: reviewId, databaseId: reviewDatabaseId };
|
|
982
|
+
submission.reviewRetained = true;
|
|
936
983
|
let successfulComments = 0;
|
|
937
984
|
if (comments.length > 0) {
|
|
938
985
|
console.log(`Step 2: Adding ${comments.length} comments in batches...`);
|
|
939
986
|
let batchResult;
|
|
987
|
+
submission.postedComments = null; // Unknown until the transport reports a subset.
|
|
940
988
|
try {
|
|
941
989
|
batchResult = await this.addCommentsInBatches(prNodeId, reviewId, comments, 10, downstreamPrContext);
|
|
942
990
|
} catch (commentError) {
|
|
@@ -947,8 +995,10 @@ class GitHubClient {
|
|
|
947
995
|
// `batchResult.failed` was returned, leaving a pending review
|
|
948
996
|
// behind on these throws when we created it ourselves.
|
|
949
997
|
console.error(`CRITICAL: comment batch threw before completion: ${commentError.message}`);
|
|
998
|
+
submission.postedComments = commentError.postedComments ?? null;
|
|
950
999
|
if (!usedExistingReview) {
|
|
951
1000
|
const cleaned = await this.deletePendingReview(reviewId, downstreamPrContext);
|
|
1001
|
+
submission.reviewRetained = !cleaned;
|
|
952
1002
|
if (!cleaned) {
|
|
953
1003
|
console.warn('Warning: Failed to clean up pending review - manual cleanup may be required');
|
|
954
1004
|
}
|
|
@@ -963,6 +1013,7 @@ class GitHubClient {
|
|
|
963
1013
|
throw wrapped;
|
|
964
1014
|
}
|
|
965
1015
|
successfulComments = batchResult.successCount;
|
|
1016
|
+
submission.postedComments = batchResult.posted ?? null;
|
|
966
1017
|
|
|
967
1018
|
if (batchResult.failed) {
|
|
968
1019
|
const failedCount = comments.length - successfulComments;
|
|
@@ -971,6 +1022,7 @@ class GitHubClient {
|
|
|
971
1022
|
console.error(`CRITICAL: ${failedCount} of ${comments.length} comments failed to add to draft review`);
|
|
972
1023
|
if (!usedExistingReview) {
|
|
973
1024
|
const cleaned = await this.deletePendingReview(reviewId, downstreamPrContext);
|
|
1025
|
+
submission.reviewRetained = !cleaned;
|
|
974
1026
|
if (!cleaned) {
|
|
975
1027
|
console.warn('Warning: Failed to clean up pending review - manual cleanup may be required');
|
|
976
1028
|
}
|
|
@@ -993,7 +1045,8 @@ class GitHubClient {
|
|
|
993
1045
|
databaseId: reviewDatabaseId,
|
|
994
1046
|
html_url: reviewUrl,
|
|
995
1047
|
state: 'PENDING',
|
|
996
|
-
comments_count: successfulComments
|
|
1048
|
+
comments_count: successfulComments,
|
|
1049
|
+
postedComments: submission.postedComments
|
|
997
1050
|
};
|
|
998
1051
|
|
|
999
1052
|
} catch (error) {
|
|
@@ -1003,10 +1056,10 @@ class GitHubClient {
|
|
|
1003
1056
|
// harmless on the REST/host path. Label with the actual transport.
|
|
1004
1057
|
if (error.errors) {
|
|
1005
1058
|
const messages = error.errors.map(e => e.message).join(', ');
|
|
1006
|
-
throw new Error(`GitHub ${transport} error: ${messages}`);
|
|
1059
|
+
throw Object.assign(new Error(`GitHub ${transport} error: ${messages}`), submission);
|
|
1007
1060
|
}
|
|
1008
1061
|
|
|
1009
|
-
throw new Error(`Failed to create draft review (${transport}): ${error.message}`);
|
|
1062
|
+
throw Object.assign(new Error(`Failed to create draft review (${transport}): ${error.message}`), submission);
|
|
1010
1063
|
}
|
|
1011
1064
|
}
|
|
1012
1065
|
|
|
@@ -1135,12 +1188,14 @@ class GitHubClient {
|
|
|
1135
1188
|
* @returns {Promise<{login: string, name: string, avatar_url: string}>}
|
|
1136
1189
|
*/
|
|
1137
1190
|
async getAuthenticatedUser() {
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1191
|
+
// Share in-flight requests as well as successful reads. A failed lookup
|
|
1192
|
+
// must not poison the cache for a later recovery attempt.
|
|
1193
|
+
if (!this._authenticatedUser) {
|
|
1194
|
+
this._authenticatedUser = this.octokit.rest.users.getAuthenticated()
|
|
1195
|
+
.then(({ data }) => ({ login: data.login, name: data.name, avatar_url: data.avatar_url }))
|
|
1196
|
+
.catch(error => { this._authenticatedUser = null; throw error; });
|
|
1197
|
+
}
|
|
1198
|
+
return this._authenticatedUser;
|
|
1144
1199
|
}
|
|
1145
1200
|
|
|
1146
1201
|
/**
|
|
@@ -12,7 +12,9 @@ const { isComplexityError } = require('../../errors');
|
|
|
12
12
|
*
|
|
13
13
|
* The return shape (success count, failed flag, failedDetails) is the
|
|
14
14
|
* same shape `GitHubClient.addCommentsInBatches` historically returned;
|
|
15
|
-
* callers depend on
|
|
15
|
+
* callers depend on those fields for cleanup and error reporting. The
|
|
16
|
+
* additive `posted` array maps successful aliases back to input indexes;
|
|
17
|
+
* null means a transport failure left the subset unknown.
|
|
16
18
|
*/
|
|
17
19
|
|
|
18
20
|
const MIN_BATCH_SIZE = 1;
|
|
@@ -78,7 +80,10 @@ function isSafeToRetry(error) {
|
|
|
78
80
|
* @param {Array} batch - Slice of comments to include in this mutation
|
|
79
81
|
* @returns {string} The full mutation text
|
|
80
82
|
*/
|
|
81
|
-
function buildBatchMutation(batch) {
|
|
83
|
+
function buildBatchMutation(batch, { includeCommentMetadata = true } = {}) {
|
|
84
|
+
const threadSelection = includeCommentMetadata
|
|
85
|
+
? 'thread { id comments(first: 1) { nodes { id fullDatabaseId url } } }'
|
|
86
|
+
: 'thread { id }';
|
|
82
87
|
const commentMutations = batch.map((comment, index) => {
|
|
83
88
|
const isFileLevel = comment.isFileLevel || !comment.line;
|
|
84
89
|
|
|
@@ -91,7 +96,7 @@ function buildBatchMutation(batch) {
|
|
|
91
96
|
subjectType: FILE
|
|
92
97
|
body: ${JSON.stringify(comment.body)}
|
|
93
98
|
}) {
|
|
94
|
-
|
|
99
|
+
${threadSelection}
|
|
95
100
|
}
|
|
96
101
|
`;
|
|
97
102
|
}
|
|
@@ -111,7 +116,7 @@ function buildBatchMutation(batch) {
|
|
|
111
116
|
side: ${side}
|
|
112
117
|
body: ${JSON.stringify(comment.body)}
|
|
113
118
|
}) {
|
|
114
|
-
|
|
119
|
+
${threadSelection}
|
|
115
120
|
}
|
|
116
121
|
`;
|
|
117
122
|
}).join('\n');
|
|
@@ -134,18 +139,29 @@ function buildBatchMutation(batch) {
|
|
|
134
139
|
* @param {string} reviewId - GraphQL node ID for the pending review
|
|
135
140
|
* @param {Array} comments - Array of comments with path, line (optional), side, body, isFileLevel
|
|
136
141
|
* @param {number} [batchSize=10] - Initial batch size
|
|
137
|
-
* @returns {Promise<{successCount: number, failed: boolean, failedDetails: string[]}>}
|
|
142
|
+
* @returns {Promise<{successCount: number, failed: boolean, failedDetails: string[], posted: Array|null}>}
|
|
138
143
|
*/
|
|
139
144
|
async function addCommentsInBatches(octokit, prNodeId, reviewId, comments, batchSize = DEFAULT_BATCH_SIZE) {
|
|
140
145
|
if (comments.length === 0) {
|
|
141
|
-
return { successCount: 0, failed: false, failedDetails: [] };
|
|
146
|
+
return { successCount: 0, failed: false, failedDetails: [], posted: [] };
|
|
142
147
|
}
|
|
143
148
|
|
|
144
149
|
let currentBatchSize = batchSize;
|
|
145
150
|
let remaining = comments.slice();
|
|
146
151
|
let totalSuccessful = 0;
|
|
147
152
|
const failedDetails = [];
|
|
153
|
+
const posted = [];
|
|
154
|
+
const recordPosted = (result, index) => {
|
|
155
|
+
const comment = result.thread.comments?.nodes?.[0];
|
|
156
|
+
posted.push({
|
|
157
|
+
index,
|
|
158
|
+
github_comment_id: comment?.fullDatabaseId != null ? String(comment.fullDatabaseId) : null,
|
|
159
|
+
github_comment_node_id: comment?.id ?? null,
|
|
160
|
+
github_comment_url: comment?.url ?? null
|
|
161
|
+
});
|
|
162
|
+
};
|
|
148
163
|
let batchNumber = 0;
|
|
164
|
+
let includeCommentMetadata = true;
|
|
149
165
|
|
|
150
166
|
logger.info(`Adding ${comments.length} comments in batches of up to ${currentBatchSize}`);
|
|
151
167
|
|
|
@@ -154,7 +170,7 @@ async function addCommentsInBatches(octokit, prNodeId, reviewId, comments, batch
|
|
|
154
170
|
const batch = remaining.slice(0, currentBatchSize);
|
|
155
171
|
logger.info(`Adding comments batch ${batchNumber} (${batch.length} comments, ${remaining.length} remaining)...`);
|
|
156
172
|
|
|
157
|
-
|
|
173
|
+
let batchMutation = buildBatchMutation(batch, { includeCommentMetadata });
|
|
158
174
|
|
|
159
175
|
// Try the batch, with one retry on failure
|
|
160
176
|
let batchResult = null;
|
|
@@ -174,6 +190,24 @@ async function addCommentsInBatches(octokit, prNodeId, reviewId, comments, batch
|
|
|
174
190
|
} catch (error) {
|
|
175
191
|
batchError = error;
|
|
176
192
|
|
|
193
|
+
// Inspect partial results before any retry, including complexity
|
|
194
|
+
// retries. Replaying this mutation would duplicate successful aliases.
|
|
195
|
+
// Stop this batch and report its exact subset to the orchestrator.
|
|
196
|
+
if (error.data) break;
|
|
197
|
+
|
|
198
|
+
// Older GraphQL hosts may not expose fullDatabaseId. Only a pure
|
|
199
|
+
// field-validation rejection is safe to retry: validation precedes
|
|
200
|
+
// all mutation execution. Never replay partial or transport failures.
|
|
201
|
+
const metadataValidationError = error.errors?.length && error.errors.every(e =>
|
|
202
|
+
(e.extensions?.code === 'undefinedField' && e.extensions?.fieldName === 'fullDatabaseId') ||
|
|
203
|
+
/(?:Cannot query field ["']fullDatabaseId["']|Field ["']fullDatabaseId["'] doesn.t exist)/i.test(e.message || ''));
|
|
204
|
+
if (includeCommentMetadata && metadataValidationError && isSafeToRetry(error)) {
|
|
205
|
+
includeCommentMetadata = false;
|
|
206
|
+
batchMutation = buildBatchMutation(batch, { includeCommentMetadata });
|
|
207
|
+
logger.warn('GraphQL host lacks fullDatabaseId; posting without optional comment metadata');
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
|
|
177
211
|
// Complexity/cost limit: halve batch size and re-attempt. Only when
|
|
178
212
|
// re-sending is safe: never after data (some comments may have
|
|
179
213
|
// landed) or a 5xx response (the batch may have run).
|
|
@@ -238,6 +272,7 @@ async function addCommentsInBatches(octokit, prNodeId, reviewId, comments, batch
|
|
|
238
272
|
const commentResult = batchError.data[`comment${i}`];
|
|
239
273
|
if (commentResult && commentResult.thread && commentResult.thread.id) {
|
|
240
274
|
batchSuccessful++;
|
|
275
|
+
recordPosted(commentResult, comments.length - remaining.length + i);
|
|
241
276
|
} else {
|
|
242
277
|
const ghError = perCommentErrors[`comment${i}`] || 'No error details available';
|
|
243
278
|
const location = `${batch[i].path}:${batch[i].line || 'file-level'}`;
|
|
@@ -247,7 +282,7 @@ async function addCommentsInBatches(octokit, prNodeId, reviewId, comments, batch
|
|
|
247
282
|
}
|
|
248
283
|
if (batchSuccessful < batch.length) {
|
|
249
284
|
logger.error(`CRITICAL: Batch ${batchNumber} had ${batch.length - batchSuccessful} failures`);
|
|
250
|
-
return { successCount: totalSuccessful + batchSuccessful, failed: true, failedDetails };
|
|
285
|
+
return { successCount: totalSuccessful + batchSuccessful, failed: true, failedDetails, posted };
|
|
251
286
|
}
|
|
252
287
|
logger.info(`Batch ${batchNumber} complete (recovered from partial error): ${batchSuccessful} comments added`);
|
|
253
288
|
totalSuccessful += batchSuccessful;
|
|
@@ -259,7 +294,10 @@ async function addCommentsInBatches(octokit, prNodeId, reviewId, comments, batch
|
|
|
259
294
|
const location = `${batch[i].path}:${batch[i].line || 'file-level'}`;
|
|
260
295
|
failedDetails.push(`${location} - ${ghError}`);
|
|
261
296
|
}
|
|
262
|
-
|
|
297
|
+
// The earlier batches are known only if GitHub did not run this one;
|
|
298
|
+
// a failure it may have applied leaves the subset unknown.
|
|
299
|
+
return { successCount: totalSuccessful, failed: true, failedDetails,
|
|
300
|
+
posted: isSafeToRetry(batchError) ? posted : null };
|
|
263
301
|
}
|
|
264
302
|
} else if (batchResult) {
|
|
265
303
|
let batchSuccessful = 0;
|
|
@@ -267,6 +305,7 @@ async function addCommentsInBatches(octokit, prNodeId, reviewId, comments, batch
|
|
|
267
305
|
const commentResult = batchResult[`comment${i}`];
|
|
268
306
|
if (commentResult && commentResult.thread && commentResult.thread.id) {
|
|
269
307
|
batchSuccessful++;
|
|
308
|
+
recordPosted(commentResult, comments.length - remaining.length + i);
|
|
270
309
|
} else {
|
|
271
310
|
const location = `${batch[i].path}:${batch[i].line || 'file-level'}`;
|
|
272
311
|
logger.warn(`Comment ${i} in batch ${batchNumber} failed to add: ${location} - No error details available`);
|
|
@@ -276,7 +315,7 @@ async function addCommentsInBatches(octokit, prNodeId, reviewId, comments, batch
|
|
|
276
315
|
|
|
277
316
|
if (batchSuccessful < batch.length) {
|
|
278
317
|
logger.error(`CRITICAL: Batch ${batchNumber} had ${batch.length - batchSuccessful} failures`);
|
|
279
|
-
return { successCount: totalSuccessful + batchSuccessful, failed: true, failedDetails };
|
|
318
|
+
return { successCount: totalSuccessful + batchSuccessful, failed: true, failedDetails, posted };
|
|
280
319
|
}
|
|
281
320
|
|
|
282
321
|
totalSuccessful += batchSuccessful;
|
|
@@ -287,7 +326,7 @@ async function addCommentsInBatches(octokit, prNodeId, reviewId, comments, batch
|
|
|
287
326
|
}
|
|
288
327
|
|
|
289
328
|
logger.info(`All batches complete: ${totalSuccessful} total comments added`);
|
|
290
|
-
return { successCount: totalSuccessful, failed: false, failedDetails };
|
|
329
|
+
return { successCount: totalSuccessful, failed: false, failedDetails, posted };
|
|
291
330
|
}
|
|
292
331
|
|
|
293
332
|
module.exports = {
|
|
@@ -114,9 +114,11 @@ async function getPendingReviewForUser(octokit, owner, repo, prNumber) {
|
|
|
114
114
|
*
|
|
115
115
|
* @param {Object} octokit - Octokit instance (must expose .graphql)
|
|
116
116
|
* @param {string} nodeId - GraphQL node ID for the review
|
|
117
|
+
* @param {Object} [options]
|
|
118
|
+
* @param {boolean} [options.strict=false] - Throw errors other than confirmed absence
|
|
117
119
|
* @returns {Promise<Object|null>} Review data or null if not found
|
|
118
120
|
*/
|
|
119
|
-
async function getReviewById(octokit, nodeId) {
|
|
121
|
+
async function getReviewById(octokit, nodeId, { strict = false } = {}) {
|
|
120
122
|
try {
|
|
121
123
|
logger.debug(`Fetching review by node ID: ${nodeId}`);
|
|
122
124
|
|
|
@@ -137,12 +139,16 @@ async function getReviewById(octokit, nodeId) {
|
|
|
137
139
|
url: review.url
|
|
138
140
|
};
|
|
139
141
|
} catch (error) {
|
|
140
|
-
|
|
142
|
+
const isNotFound = e => e.type === 'NOT_FOUND' || e.message?.includes('not found');
|
|
143
|
+
// Mixed GraphQL errors are not evidence that the review was deleted.
|
|
144
|
+
if (error.status === 404 || (error.errors?.length &&
|
|
145
|
+
(strict ? error.errors.every(e => e.type === 'NOT_FOUND') : error.errors.some(isNotFound)))) {
|
|
141
146
|
logger.debug(`Review not found for node ID: ${nodeId}`);
|
|
142
147
|
return null;
|
|
143
148
|
}
|
|
144
149
|
|
|
145
150
|
logger.warn(`Error fetching review by node ID ${nodeId}: ${error.message}`);
|
|
151
|
+
if (strict) throw error;
|
|
146
152
|
return null;
|
|
147
153
|
}
|
|
148
154
|
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Copyright 2026 Tim Perkins (tjwp) | SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
const { failure, requireFields, normalizeComment, updateResult } = require('../review-comment-edit-utils');
|
|
3
|
+
const restImpl = require('../rest/review-comment-edit');
|
|
4
|
+
|
|
5
|
+
async function commentNodeId(octokit, ref) {
|
|
6
|
+
if (ref?.github_comment_node_id) return ref.github_comment_node_id;
|
|
7
|
+
requireFields(ref, ['owner', 'repo', 'github_comment_id']);
|
|
8
|
+
const { data } = await octokit.rest.pulls.getReviewComment({
|
|
9
|
+
owner: ref.owner, repo: ref.repo, comment_id: ref.github_comment_id
|
|
10
|
+
});
|
|
11
|
+
if (!data.node_id) throw failure('invalid_ref', 'Comment read did not return a node id');
|
|
12
|
+
return data.node_id;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function reviewNodeId(octokit, ref) {
|
|
16
|
+
if (ref?.github_node_id) return ref.github_node_id;
|
|
17
|
+
requireFields(ref, ['owner', 'repo', 'pull_number', 'github_review_id']);
|
|
18
|
+
const { data } = await octokit.rest.pulls.getReview({
|
|
19
|
+
owner: ref.owner, repo: ref.repo, pull_number: ref.pull_number,
|
|
20
|
+
review_id: ref.github_review_id
|
|
21
|
+
});
|
|
22
|
+
if (!data.node_id) throw failure('invalid_ref', 'Review read did not return a node id');
|
|
23
|
+
return data.node_id;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function updateReviewComment(octokit, ref, body) {
|
|
27
|
+
const id = await commentNodeId(octokit, ref);
|
|
28
|
+
const result = await octokit.graphql(`mutation($id: ID!, $body: String!) {
|
|
29
|
+
updatePullRequestReviewComment(input: {pullRequestReviewCommentId: $id, body: $body}) {
|
|
30
|
+
pullRequestReviewComment { id fullDatabaseId url body }
|
|
31
|
+
}
|
|
32
|
+
}`, { id, body });
|
|
33
|
+
const row = result.updatePullRequestReviewComment?.pullRequestReviewComment;
|
|
34
|
+
if (!row) throw failure('not_found', 'Updated review comment was not returned');
|
|
35
|
+
return updateResult({ id: row.fullDatabaseId, node_id: row.id, html_url: row.url, body: row.body });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function listCommentsForReview(octokit, ref) {
|
|
39
|
+
const id = await reviewNodeId(octokit, ref);
|
|
40
|
+
const rows = [];
|
|
41
|
+
let cursor = null;
|
|
42
|
+
let reviewId = ref.github_review_id;
|
|
43
|
+
do {
|
|
44
|
+
const result = await octokit.graphql(`query($id: ID!, $cursor: String) {
|
|
45
|
+
node(id: $id) { ... on PullRequestReview {
|
|
46
|
+
fullDatabaseId
|
|
47
|
+
comments(first: 100, after: $cursor) {
|
|
48
|
+
nodes {
|
|
49
|
+
id fullDatabaseId url body path line startLine originalLine originalStartLine
|
|
50
|
+
subjectType createdAt author { login } replyTo { id fullDatabaseId }
|
|
51
|
+
}
|
|
52
|
+
pageInfo { hasNextPage endCursor }
|
|
53
|
+
}
|
|
54
|
+
} }
|
|
55
|
+
}`, { id, cursor });
|
|
56
|
+
if (!result.node?.comments) throw failure('not_found', 'Review not found');
|
|
57
|
+
reviewId = reviewId ?? result.node.fullDatabaseId;
|
|
58
|
+
for (const row of result.node.comments.nodes) {
|
|
59
|
+
rows.push(normalizeComment({
|
|
60
|
+
id: row.fullDatabaseId, node_id: row.id, html_url: row.url,
|
|
61
|
+
body: row.body, path: row.path, line: row.line, start_line: row.startLine,
|
|
62
|
+
original_line: row.originalLine, original_start_line: row.originalStartLine,
|
|
63
|
+
subject_type: row.subjectType?.toLowerCase(), created_at: row.createdAt,
|
|
64
|
+
user: row.author, in_reply_to_id: row.replyTo ? (row.replyTo.fullDatabaseId ?? row.replyTo.id ?? 'unknown-reply') : null
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
const info = result.node.comments.pageInfo;
|
|
68
|
+
if (!info.hasNextPage) break;
|
|
69
|
+
if (!info.endCursor || info.endCursor === cursor) throw new Error('Review comment pagination did not advance');
|
|
70
|
+
cursor = info.endCursor;
|
|
71
|
+
} while (true);
|
|
72
|
+
|
|
73
|
+
// GitHub's GraphQL comment type exposes no diff side fields. Enrich from
|
|
74
|
+
// the same review's REST list (also includes the author's pending comments),
|
|
75
|
+
// never infer RIGHT or mistake an outdated comment for a file comment.
|
|
76
|
+
if (rows.some(row => row.subject_type !== 'file')) {
|
|
77
|
+
requireFields(ref, ['owner', 'repo', 'pull_number']);
|
|
78
|
+
if (!reviewId) throw failure('invalid_ref', 'Review has no REST id for diff-side lookup');
|
|
79
|
+
const geometry = await restImpl.listCommentsForReview(octokit, { ...ref, github_review_id: reviewId });
|
|
80
|
+
const byId = new Map(geometry.map(row => [row.id, row]));
|
|
81
|
+
for (const row of rows) {
|
|
82
|
+
const match = row.id == null ? null : byId.get(row.id);
|
|
83
|
+
if (match) { row.side = match.side; row.start_side = match.start_side; }
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return rows;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
module.exports = { updateReviewComment, listCommentsForReview };
|
|
@@ -55,7 +55,8 @@ const logger = require('../../../utils/logger');
|
|
|
55
55
|
* When set to `false`, each wire comment is sent as the complete request body
|
|
56
56
|
* in its own POST. This supports hosts whose review-comments extension is
|
|
57
57
|
* singular while preserving the generic batch contract by default. Any 2xx
|
|
58
|
-
* counts as success
|
|
58
|
+
* counts as success; optional response ids/URL are captured in `posted`.
|
|
59
|
+
* A 400/422 fails only
|
|
59
60
|
* that comment; any other failure stops the loop and reports the remaining
|
|
60
61
|
* comments as skipped.
|
|
61
62
|
*
|
|
@@ -182,11 +183,11 @@ function toWireComment(comment, commitId) {
|
|
|
182
183
|
* @param {Array} comments - Comments with `{ path, line?, start_line?,
|
|
183
184
|
* side?, body, isFileLevel? }`. Same shape the GraphQL impl accepts.
|
|
184
185
|
* @param {number} [_batchSize] - Ignored; kept for signature parity.
|
|
185
|
-
* @returns {Promise<{successCount: number, failed: boolean, failedDetails: string[]}>}
|
|
186
|
+
* @returns {Promise<{successCount: number, failed: boolean, failedDetails: string[], posted: Array|null}>}
|
|
186
187
|
*/
|
|
187
188
|
async function addCommentsInBatches(octokit, features, prContext, reviewId, comments, _batchSize) {
|
|
188
189
|
if (!comments || comments.length === 0) {
|
|
189
|
-
return { successCount: 0, failed: false, failedDetails: [] };
|
|
190
|
+
return { successCount: 0, failed: false, failedDetails: [], posted: [] };
|
|
190
191
|
}
|
|
191
192
|
|
|
192
193
|
if (!prContext || typeof prContext !== 'object') {
|
|
@@ -261,15 +262,23 @@ async function addCommentsInBatches(octokit, features, prContext, reviewId, comm
|
|
|
261
262
|
if (!useBatchRequest) {
|
|
262
263
|
let successCount = 0;
|
|
263
264
|
const failedDetails = [];
|
|
265
|
+
let posted = [];
|
|
264
266
|
const locationOf = (c) => (c.line ? `${c.path}:${c.line}` : `${c.path}:file-level`);
|
|
265
267
|
|
|
266
268
|
for (let index = 0; index < wireComments.length; index++) {
|
|
267
269
|
try {
|
|
268
|
-
await octokit.request(`POST ${endpoint}`, {
|
|
270
|
+
const response = await octokit.request(`POST ${endpoint}`, {
|
|
269
271
|
headers,
|
|
270
272
|
data: wireComments[index]
|
|
271
273
|
});
|
|
272
274
|
successCount++;
|
|
275
|
+
const data = response?.data;
|
|
276
|
+
posted.push({
|
|
277
|
+
index,
|
|
278
|
+
github_comment_id: data?.id != null ? String(data.id) : null,
|
|
279
|
+
github_comment_node_id: data?.node_id ?? null,
|
|
280
|
+
github_comment_url: data?.html_url ?? null
|
|
281
|
+
});
|
|
273
282
|
} catch (error) {
|
|
274
283
|
const status = error && (error.status || error.statusCode);
|
|
275
284
|
const message = (error && error.message) || 'Unknown error';
|
|
@@ -282,6 +291,9 @@ async function addCommentsInBatches(octokit, features, prContext, reviewId, comm
|
|
|
282
291
|
// shares the endpoint, review and credentials with the remaining
|
|
283
292
|
// comments and would fail them all, so stop here.
|
|
284
293
|
if (status !== 400 && status !== 422) {
|
|
294
|
+
// A lost response/server error may have attached this comment.
|
|
295
|
+
// The subset is unknown, unlike an explicit validation rejection.
|
|
296
|
+
posted = null;
|
|
285
297
|
for (const skipped of comments.slice(index + 1)) {
|
|
286
298
|
failedDetails.push(
|
|
287
299
|
`${locationOf(skipped)} - skipped after ${status || 'network error'}`
|
|
@@ -304,7 +316,8 @@ async function addCommentsInBatches(octokit, features, prContext, reviewId, comm
|
|
|
304
316
|
return {
|
|
305
317
|
successCount,
|
|
306
318
|
failed: failedDetails.length > 0,
|
|
307
|
-
failedDetails
|
|
319
|
+
failedDetails,
|
|
320
|
+
posted
|
|
308
321
|
};
|
|
309
322
|
}
|
|
310
323
|
|
|
@@ -333,7 +346,8 @@ async function addCommentsInBatches(octokit, features, prContext, reviewId, comm
|
|
|
333
346
|
return {
|
|
334
347
|
successCount: 0,
|
|
335
348
|
failed: true,
|
|
336
|
-
failedDetails
|
|
349
|
+
failedDetails,
|
|
350
|
+
posted: null
|
|
337
351
|
};
|
|
338
352
|
}
|
|
339
353
|
|
|
@@ -348,7 +362,7 @@ async function addCommentsInBatches(octokit, features, prContext, reviewId, comm
|
|
|
348
362
|
|
|
349
363
|
const failedDetails = [];
|
|
350
364
|
for (const entry of failedList) {
|
|
351
|
-
const idx = typeof entry
|
|
365
|
+
const idx = typeof entry?.index === 'number' ? entry.index : null;
|
|
352
366
|
const errMsg = (entry && (entry.error_message || entry.message)) || 'Unknown error';
|
|
353
367
|
const source = idx !== null && idx >= 0 && idx < comments.length ? comments[idx] : null;
|
|
354
368
|
const location = source
|
|
@@ -358,10 +372,25 @@ async function addCommentsInBatches(octokit, features, prContext, reviewId, comm
|
|
|
358
372
|
logger.warn(`Host comment failed: ${location} - ${errMsg}`);
|
|
359
373
|
}
|
|
360
374
|
|
|
375
|
+
// Only infer the complement when every failure has a unique valid index
|
|
376
|
+
// and the host explicitly accounts for every submitted comment. No ids
|
|
377
|
+
// are inferred from an undocumented batch response shape.
|
|
378
|
+
const failedIndexes = new Set(failedList.map(entry => entry?.index));
|
|
379
|
+
const knownSubset = hasExplicitAdded && Number.isInteger(added) && added >= 0 &&
|
|
380
|
+
added + failedList.length === comments.length &&
|
|
381
|
+
failedIndexes.size === failedList.length &&
|
|
382
|
+
[...failedIndexes].every(index => Number.isInteger(index) && index >= 0 && index < comments.length);
|
|
383
|
+
const posted = knownSubset
|
|
384
|
+
? comments.flatMap((_, index) => failedIndexes.has(index) ? [] : [{
|
|
385
|
+
index, github_comment_id: null, github_comment_node_id: null, github_comment_url: null
|
|
386
|
+
}])
|
|
387
|
+
: null;
|
|
388
|
+
|
|
361
389
|
// Sanity check: if the host reported an explicit count, the accounted-
|
|
362
390
|
// for items (added + failed) should equal the total submitted. If they
|
|
363
391
|
// don't, the host response is internally inconsistent — log a warning
|
|
364
|
-
// but
|
|
392
|
+
// but preserve the legacy explicit counts. `posted` remains null: callers
|
|
393
|
+
// must not use these counts to guess which comments were attached.
|
|
365
394
|
if (hasExplicitAdded && added + failedList.length !== comments.length) {
|
|
366
395
|
logger.warn(
|
|
367
396
|
`Host pending_review_comments inconsistent counts: added=${added}, ` +
|
|
@@ -376,7 +405,8 @@ async function addCommentsInBatches(octokit, features, prContext, reviewId, comm
|
|
|
376
405
|
return {
|
|
377
406
|
successCount: added,
|
|
378
407
|
failed: true,
|
|
379
|
-
failedDetails
|
|
408
|
+
failedDetails,
|
|
409
|
+
posted
|
|
380
410
|
};
|
|
381
411
|
}
|
|
382
412
|
|
|
@@ -388,7 +418,8 @@ async function addCommentsInBatches(octokit, features, prContext, reviewId, comm
|
|
|
388
418
|
return {
|
|
389
419
|
successCount,
|
|
390
420
|
failed: false,
|
|
391
|
-
failedDetails: []
|
|
421
|
+
failedDetails: [],
|
|
422
|
+
posted
|
|
392
423
|
};
|
|
393
424
|
}
|
|
394
425
|
|