Skip to content

Commit d5df61e

Browse files
authored
GE Creating comment failed when comment on PR (#4801)
Fixes #4791
1 parent bb288c8 commit d5df61e

5 files changed

Lines changed: 115 additions & 13 deletions

File tree

src/github/githubRepository.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { ApolloQueryResult, FetchResult, MutationOptions, NetworkStatus, QueryOptions } from 'apollo-boost';
6+
import { ApolloQueryResult, DocumentNode, FetchResult, MutationOptions, NetworkStatus, QueryOptions } from 'apollo-boost';
77
import * as vscode from 'vscode';
88
import { AuthenticationError, AuthProvider, GitHubServerType, isSamlError } from '../common/authentication';
99
import Logger from '../common/logger';
@@ -197,7 +197,7 @@ export class GitHubRepository implements vscode.Disposable {
197197
}
198198
}
199199

200-
query = async <T>(query: QueryOptions, ignoreSamlErrors: boolean = false): Promise<ApolloQueryResult<T>> => {
200+
query = async <T>(query: QueryOptions, ignoreSamlErrors: boolean = false, legacyFallback?: { query: DocumentNode }): Promise<ApolloQueryResult<T>> => {
201201
const gql = this.authMatchesServer && this.hub && this.hub.graphql;
202202
if (!gql) {
203203
Logger.debug(`Not available for query: ${query}`, GRAPHQL_COMPONENT_ID);
@@ -214,6 +214,11 @@ export class GitHubRepository implements vscode.Disposable {
214214
try {
215215
rsp = await gql.query<T>(query);
216216
} catch (e) {
217+
if (legacyFallback) {
218+
query.query = legacyFallback.query;
219+
return this.query(query, ignoreSamlErrors);
220+
}
221+
217222
// Some queries just result in SAML errors, and some queries we may not want to retry because it will be too disruptive.
218223
if (!ignoreSamlErrors && e.message?.startsWith('GraphQL error: Resource protected by organization SAML enforcement.')) {
219224
await this._credentialStore.recreate();
@@ -226,7 +231,7 @@ export class GitHubRepository implements vscode.Disposable {
226231
return rsp;
227232
};
228233

229-
mutate = async <T>(mutation: MutationOptions<T>): Promise<FetchResult<T>> => {
234+
mutate = async <T>(mutation: MutationOptions<T>, legacyFallback?: { mutation: DocumentNode, deleteProps: string[] }): Promise<FetchResult<T>> => {
230235
const gql = this.authMatchesServer && this.hub && this.hub.graphql;
231236
if (!gql) {
232237
Logger.debug(`Not available for query: ${mutation}`, GRAPHQL_COMPONENT_ID);
@@ -239,7 +244,21 @@ export class GitHubRepository implements vscode.Disposable {
239244
}
240245

241246
Logger.trace(`Request: ${JSON.stringify(mutation, null, 2)}`, GRAPHQL_COMPONENT_ID);
242-
const rsp = await gql.mutate<T>(mutation);
247+
let rsp;
248+
try {
249+
rsp = await gql.mutate<T>(mutation);
250+
} catch {
251+
if (legacyFallback) {
252+
mutation.mutation = legacyFallback.mutation;
253+
if (mutation.variables?.input) {
254+
for (const prop of legacyFallback.deleteProps) {
255+
delete mutation.variables.input[prop];
256+
}
257+
}
258+
}
259+
260+
return this.mutate(mutation);
261+
}
243262
Logger.trace(`Response: ${JSON.stringify(rsp, null, 2)}`, GRAPHQL_COMPONENT_ID);
244263
return rsp;
245264
};

src/github/graphql.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export interface ReviewThread {
194194
originalStartLine: number | null;
195195
originalLine: number;
196196
isOutdated: boolean;
197-
subjectType: SubjectType;
197+
subjectType?: SubjectType;
198198
comments: {
199199
nodes: ReviewComment[];
200200
edges: [{

src/github/pullRequestModel.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,9 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
581581
line: (endLine === undefined) ? 0 : endLine,
582582
side,
583583
subjectType: (startLine === undefined || endLine === undefined) ? SubjectType.FILE : SubjectType.LINE
584-
},
585-
},
586-
});
584+
}
585+
}
586+
}, { mutation: schema.LegacyAddReviewThread, deleteProps: ['subjectType'] });
587587

588588
if (!data) {
589589
throw new Error('Creating review thread failed.');
@@ -892,7 +892,7 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
892892
name: remote.repositoryName,
893893
number: this.number,
894894
},
895-
});
895+
}, false, { query: schema.LegacyPullRequestComments });
896896

897897
const reviewThreads = data.repository.pullRequest.reviewThreads.nodes.map(node => {
898898
return parseGraphQLReviewThread(node, this.githubRepository);
@@ -921,7 +921,7 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
921921
name: remote.repositoryName,
922922
number: this.number,
923923
},
924-
});
924+
}, false, { query: schema.LegacyPullRequestComments });
925925

926926
const comments = data.repository.pullRequest.reviewThreads.nodes
927927
.map(node => node.comments.nodes.map(comment => parseGraphQLComment(comment, node.isResolved, this.githubRepository), remote))
@@ -1465,7 +1465,7 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
14651465
threadId,
14661466
},
14671467
},
1468-
});
1468+
}, { mutation: schema.LegacyResolveReviewThread, deleteProps: [] });
14691469

14701470
if (!data) {
14711471
// Undo optimistic update
@@ -1505,7 +1505,7 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
15051505
threadId,
15061506
},
15071507
},
1508-
});
1508+
}, { mutation: schema.LegacyUnresolveReviewThread, deleteProps: [] });
15091509

15101510
if (!data) {
15111511
// Undo optimistic update

src/github/queries.gql

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,25 @@ fragment ReviewThread on PullRequestReviewThread {
160160
}
161161
}
162162

163+
fragment LegacyReviewThread on PullRequestReviewThread {
164+
id
165+
isResolved
166+
viewerCanResolve
167+
viewerCanUnresolve
168+
path
169+
diffSide
170+
line
171+
startLine
172+
originalStartLine
173+
originalLine
174+
isOutdated
175+
comments(first: 100) {
176+
nodes {
177+
...ReviewComment
178+
}
179+
}
180+
}
181+
163182
fragment PullRequestFragment on PullRequest {
164183
number
165184
url
@@ -541,6 +560,46 @@ query PullRequestComments($owner: String!, $name: String!, $number: Int!, $first
541560
}
542561
}
543562

563+
query LegacyPullRequestComments($owner: String!, $name: String!, $number: Int!, $first: Int = 100) {
564+
repository(owner: $owner, name: $name) {
565+
pullRequest(number: $number) {
566+
reviewThreads(first: $first) {
567+
nodes {
568+
id
569+
isResolved
570+
viewerCanResolve
571+
viewerCanUnresolve
572+
path
573+
diffSide
574+
startLine
575+
line
576+
originalStartLine
577+
originalLine
578+
isOutdated
579+
comments(first: 100) {
580+
edges {
581+
node {
582+
pullRequestReview {
583+
databaseId
584+
}
585+
}
586+
}
587+
nodes {
588+
...ReviewComment
589+
}
590+
}
591+
}
592+
}
593+
}
594+
}
595+
rateLimit {
596+
limit
597+
cost
598+
remaining
599+
resetAt
600+
}
601+
}
602+
544603
query Viewer {
545604
viewer {
546605
login
@@ -792,6 +851,14 @@ mutation AddReviewThread($input: AddPullRequestReviewThreadInput!) {
792851
}
793852
}
794853

854+
mutation LegacyAddReviewThread($input: AddPullRequestReviewThreadInput!) {
855+
addPullRequestReviewThread(input: $input) {
856+
thread {
857+
...LegacyReviewThread
858+
}
859+
}
860+
}
861+
795862
mutation AddReviewers($input: RequestReviewsInput!) {
796863
requestReviews(input: $input) {
797864
pullRequest {
@@ -1427,6 +1494,14 @@ mutation ResolveReviewThread($input: ResolveReviewThreadInput!) {
14271494
}
14281495
}
14291496

1497+
mutation LegacyResolveReviewThread($input: ResolveReviewThreadInput!) {
1498+
resolveReviewThread(input: $input) {
1499+
thread {
1500+
...LegacyReviewThread
1501+
}
1502+
}
1503+
}
1504+
14301505
mutation UnresolveReviewThread($input: UnresolveReviewThreadInput!) {
14311506
unresolveReviewThread(input: $input) {
14321507
thread {
@@ -1435,6 +1510,14 @@ mutation UnresolveReviewThread($input: UnresolveReviewThreadInput!) {
14351510
}
14361511
}
14371512

1513+
mutation LegacyUnresolveReviewThread($input: UnresolveReviewThreadInput!) {
1514+
unresolveReviewThread(input: $input) {
1515+
thread {
1516+
...LegacyReviewThread
1517+
}
1518+
}
1519+
}
1520+
14381521
mutation EnablePullRequestAutoMerge($input: EnablePullRequestAutoMergeInput!) {
14391522
enablePullRequestAutoMerge(input: $input) {
14401523
pullRequest {

src/github/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ export function parseGraphQLReviewThread(thread: GraphQL.ReviewThread, githubRep
434434
diffSide: thread.diffSide,
435435
isOutdated: thread.isOutdated,
436436
comments: thread.comments.nodes.map(comment => parseGraphQLComment(comment, thread.isResolved, githubRepository)),
437-
subjectType: thread.subjectType
437+
subjectType: thread.subjectType ?? SubjectType.LINE
438438
};
439439
}
440440

0 commit comments

Comments
 (0)