Skip to content

Commit 3051a86

Browse files
authored
Pull requests created from a fork on a topic branch aren't discovered. (#3512)
Fixes #3511
1 parent c1a3920 commit 3051a86

File tree

6 files changed

+129
-118
lines changed

6 files changed

+129
-118
lines changed

src/github/folderRepositoryManager.ts

+12-19
Original file line numberDiff line numberDiff line change
@@ -1800,25 +1800,18 @@ export class FolderRepositoryManager implements vscode.Disposable {
18001800
return null;
18011801
}
18021802

1803-
const headGitHubRepo = this.gitHubRepositories.find(
1804-
repo => repo.remote.remoteName === this.repository.state.HEAD?.upstream?.remote,
1805-
);
1806-
1807-
// Find the github repo that matches the upstream
1803+
// Search through each github repo to see if it has a PR with this head branch.
18081804
for (const repo of this.gitHubRepositories) {
1809-
if (repo.remote.remoteName === this.repository.state.HEAD.upstream.remote) {
1810-
const matchingPullRequest = await repo.getPullRequestForBranch(
1811-
`${headGitHubRepo?.remote.owner}:${this.repository.state.HEAD.upstream.name}`,
1812-
);
1813-
if (matchingPullRequest && matchingPullRequest.length > 0) {
1814-
return {
1815-
owner: repo.remote.owner,
1816-
repositoryName: repo.remote.repositoryName,
1817-
prNumber: matchingPullRequest[0].number,
1818-
model: matchingPullRequest[0],
1819-
};
1820-
}
1821-
break;
1805+
const matchingPullRequest = await repo.getPullRequestForBranch(
1806+
this.repository.state.HEAD.upstream.name,
1807+
);
1808+
if (matchingPullRequest) {
1809+
return {
1810+
owner: repo.remote.owner,
1811+
repositoryName: repo.remote.repositoryName,
1812+
prNumber: matchingPullRequest.number,
1813+
model: matchingPullRequest,
1814+
};
18221815
}
18231816
}
18241817
return null;
@@ -1905,7 +1898,7 @@ export class FolderRepositoryManager implements vscode.Disposable {
19051898
}
19061899

19071900
createGitHubRepositoryFromOwnerName(owner: string, repositoryName: string): GitHubRepository {
1908-
const existing = this.findExistingGitHubRepository({owner, repositoryName});
1901+
const existing = this.findExistingGitHubRepository({ owner, repositoryName });
19091902
if (existing) {
19101903
return existing;
19111904
}

src/github/githubRepository.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
MentionableUsersResponse,
2626
MilestoneIssuesResponse,
2727
PullRequestResponse,
28+
PullRequestsResponse,
2829
ViewerPermissionResponse,
2930
} from './graphql';
3031
import { IAccount, IMilestone, Issue, PullRequest, RepoAccessAndMergeMethods } from './interface';
@@ -364,26 +365,23 @@ export class GitHubRepository implements vscode.Disposable {
364365
return undefined;
365366
}
366367

367-
async getPullRequestForBranch(remoteAndBranch: string): Promise<PullRequestModel[] | undefined> {
368+
async getPullRequestForBranch(branch: string): Promise<PullRequestModel | undefined> {
368369
try {
369370
Logger.debug(`Fetch pull requests for branch - enter`, GitHubRepository.ID);
370-
const { octokit, remote } = await this.ensure();
371-
const result = await octokit.pulls.list({
372-
owner: remote.owner,
373-
repo: remote.repositoryName,
374-
head: remoteAndBranch
371+
const { query, remote, schema } = await this.ensure();
372+
const { data } = await query<PullRequestsResponse>({
373+
query: schema.PullRequestForHead,
374+
variables: {
375+
owner: remote.owner,
376+
name: remote.repositoryName,
377+
headRefName: branch,
378+
},
375379
});
376-
377-
const pullRequests = result.data
378-
.map(pullRequest => {
379-
return this.createOrUpdatePullRequestModel(
380-
convertRESTPullRequestToRawPullRequest(pullRequest, this),
381-
);
382-
})
383-
.filter(item => item !== null) as PullRequestModel[];
384-
385380
Logger.debug(`Fetch pull requests for branch - done`, GitHubRepository.ID);
386-
return pullRequests;
381+
382+
if (data.repository.pullRequests.nodes.length > 0) {
383+
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data.repository.pullRequests.nodes[0], this));
384+
}
387385
} catch (e) {
388386
Logger.appendLine(`Fetching pull requests for branch failed: ${e}`, GitHubRepository.ID);
389387
if (e.code === 404) {
@@ -702,7 +700,7 @@ export class GitHubRepository implements vscode.Disposable {
702700
},
703701
});
704702
Logger.debug(`Fetch pull request ${id} - done`, GitHubRepository.ID);
705-
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data, this));
703+
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data.repository.pullRequest, this));
706704
} catch (e) {
707705
Logger.appendLine(`GithubRepository> Unable to fetch PR: ${e}`);
708706
return;

src/github/graphql.ts

+8
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,14 @@ export interface IssuesResponse {
504504
};
505505
}
506506

507+
export interface PullRequestsResponse {
508+
repository: {
509+
pullRequests: {
510+
nodes: PullRequest[]
511+
}
512+
}
513+
}
514+
507515
export interface MaxIssueResponse {
508516
repository: {
509517
issues: {

src/github/queries.gql

+91-77
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,86 @@ fragment ReviewThread on PullRequestReviewThread {
158158
}
159159
}
160160

161+
fragment PullRequestFragment on PullRequest {
162+
number
163+
url
164+
state
165+
body
166+
bodyHTML
167+
title
168+
author {
169+
login
170+
url
171+
avatarUrl
172+
... on User {
173+
email
174+
}
175+
... on Organization {
176+
email
177+
}
178+
}
179+
createdAt
180+
updatedAt
181+
headRef {
182+
...Ref
183+
}
184+
headRefName
185+
headRefOid
186+
headRepository {
187+
owner {
188+
login
189+
}
190+
url
191+
}
192+
baseRef {
193+
...Ref
194+
}
195+
baseRefName
196+
baseRefOid
197+
baseRepository {
198+
owner {
199+
login
200+
}
201+
url
202+
}
203+
labels(first: 50) {
204+
nodes {
205+
name
206+
}
207+
}
208+
merged
209+
mergeable
210+
mergeStateStatus
211+
id
212+
databaseId
213+
isDraft
214+
milestone {
215+
title
216+
dueOn
217+
createdAt
218+
id
219+
}
220+
assignees(first: 10) {
221+
nodes {
222+
login
223+
name
224+
avatarUrl
225+
url
226+
email
227+
}
228+
}
229+
suggestedReviewers {
230+
isAuthor
231+
isCommenter
232+
reviewer {
233+
login
234+
avatarUrl
235+
name
236+
url
237+
}
238+
}
239+
}
240+
161241
query TimelineEvents($owner: String!, $name: String!, $number: Int!, $last: Int = 150) {
162242
repository(owner: $owner, name: $name) {
163243
pullRequest(number: $number) {
@@ -290,83 +370,7 @@ query PullRequestComments($owner: String!, $name: String!, $number: Int!, $first
290370
query PullRequest($owner: String!, $name: String!, $number: Int!) {
291371
repository(owner: $owner, name: $name) {
292372
pullRequest(number: $number) {
293-
number
294-
url
295-
state
296-
body
297-
bodyHTML
298-
title
299-
author {
300-
login
301-
url
302-
avatarUrl
303-
... on User {
304-
email
305-
}
306-
... on Organization {
307-
email
308-
}
309-
}
310-
createdAt
311-
updatedAt
312-
headRef {
313-
...Ref
314-
}
315-
headRefName
316-
headRefOid
317-
headRepository {
318-
owner {
319-
login
320-
}
321-
url
322-
}
323-
baseRef {
324-
...Ref
325-
}
326-
baseRefName
327-
baseRefOid
328-
baseRepository {
329-
owner {
330-
login
331-
}
332-
url
333-
}
334-
labels(first: 50) {
335-
nodes {
336-
name
337-
}
338-
}
339-
merged
340-
mergeable
341-
mergeStateStatus
342-
id
343-
databaseId
344-
isDraft
345-
milestone {
346-
title
347-
dueOn
348-
createdAt
349-
id
350-
}
351-
assignees(first: 10) {
352-
nodes {
353-
login
354-
name
355-
avatarUrl
356-
url
357-
email
358-
}
359-
}
360-
suggestedReviewers {
361-
isAuthor
362-
isCommenter
363-
reviewer {
364-
login
365-
avatarUrl
366-
name
367-
url
368-
}
369-
}
373+
...PullRequestFragment
370374
}
371375
}
372376
rateLimit {
@@ -528,6 +532,16 @@ query PullRequestState($owner: String!, $name: String!, $number: Int!) {
528532
}
529533
}
530534

535+
query PullRequestForHead($owner: String!, $name: String!, $headRefName: String!) {
536+
repository(owner: $owner, name: $name) {
537+
pullRequests(first: 1, headRefName: $headRefName) {
538+
nodes {
539+
...PullRequestFragment
540+
}
541+
}
542+
}
543+
}
544+
531545
mutation AddComment($input: AddPullRequestReviewCommentInput!) {
532546
addPullRequestReviewComment(input: $input) {
533547
comment {

src/github/utils.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,9 @@ export function parseMergeability(mergeability: 'UNKNOWN' | 'MERGEABLE' | 'CONFL
530530
}
531531

532532
export function parseGraphQLPullRequest(
533-
pullRequest: GraphQL.PullRequestResponse,
533+
graphQLPullRequest: GraphQL.PullRequest,
534534
githubRepository: GitHubRepository,
535535
): PullRequest {
536-
const graphQLPullRequest = pullRequest.repository.pullRequest;
537-
538536
return {
539537
id: graphQLPullRequest.databaseId,
540538
graphNodeId: graphQLPullRequest.id,

src/test/view/prsTree.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ describe('GitHub Pull Requests view', function () {
151151
);
152152
});
153153
}).pullRequest;
154-
const prItem0 = parseGraphQLPullRequest(pr0, gitHubRepository);
154+
const prItem0 = parseGraphQLPullRequest(pr0.repository.pullRequest, gitHubRepository);
155155
const pullRequest0 = new PullRequestModel(telemetry, gitHubRepository, remote, prItem0);
156156

157157
const pr1 = gitHubRepository.addGraphQLPullRequest(builder => {
@@ -167,7 +167,7 @@ describe('GitHub Pull Requests view', function () {
167167
);
168168
});
169169
}).pullRequest;
170-
const prItem1 = parseGraphQLPullRequest(pr1, gitHubRepository);
170+
const prItem1 = parseGraphQLPullRequest(pr1.repository.pullRequest, gitHubRepository);
171171
const pullRequest1 = new PullRequestModel(telemetry, gitHubRepository, remote, prItem1);
172172

173173
const repository = new MockRepository();

0 commit comments

Comments
 (0)