Skip to content

Commit

Permalink
Pull requests created from a fork on a topic branch aren't discovered. (
Browse files Browse the repository at this point in the history
#3512)

Fixes #3511
  • Loading branch information
alexr00 committed May 5, 2022
1 parent c1a3920 commit 3051a86
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 118 deletions.
31 changes: 12 additions & 19 deletions src/github/folderRepositoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1800,25 +1800,18 @@ export class FolderRepositoryManager implements vscode.Disposable {
return null;
}

const headGitHubRepo = this.gitHubRepositories.find(
repo => repo.remote.remoteName === this.repository.state.HEAD?.upstream?.remote,
);

// Find the github repo that matches the upstream
// Search through each github repo to see if it has a PR with this head branch.
for (const repo of this.gitHubRepositories) {
if (repo.remote.remoteName === this.repository.state.HEAD.upstream.remote) {
const matchingPullRequest = await repo.getPullRequestForBranch(
`${headGitHubRepo?.remote.owner}:${this.repository.state.HEAD.upstream.name}`,
);
if (matchingPullRequest && matchingPullRequest.length > 0) {
return {
owner: repo.remote.owner,
repositoryName: repo.remote.repositoryName,
prNumber: matchingPullRequest[0].number,
model: matchingPullRequest[0],
};
}
break;
const matchingPullRequest = await repo.getPullRequestForBranch(
this.repository.state.HEAD.upstream.name,
);
if (matchingPullRequest) {
return {
owner: repo.remote.owner,
repositoryName: repo.remote.repositoryName,
prNumber: matchingPullRequest.number,
model: matchingPullRequest,
};
}
}
return null;
Expand Down Expand Up @@ -1905,7 +1898,7 @@ export class FolderRepositoryManager implements vscode.Disposable {
}

createGitHubRepositoryFromOwnerName(owner: string, repositoryName: string): GitHubRepository {
const existing = this.findExistingGitHubRepository({owner, repositoryName});
const existing = this.findExistingGitHubRepository({ owner, repositoryName });
if (existing) {
return existing;
}
Expand Down
32 changes: 15 additions & 17 deletions src/github/githubRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
MentionableUsersResponse,
MilestoneIssuesResponse,
PullRequestResponse,
PullRequestsResponse,
ViewerPermissionResponse,
} from './graphql';
import { IAccount, IMilestone, Issue, PullRequest, RepoAccessAndMergeMethods } from './interface';
Expand Down Expand Up @@ -364,26 +365,23 @@ export class GitHubRepository implements vscode.Disposable {
return undefined;
}

async getPullRequestForBranch(remoteAndBranch: string): Promise<PullRequestModel[] | undefined> {
async getPullRequestForBranch(branch: string): Promise<PullRequestModel | undefined> {
try {
Logger.debug(`Fetch pull requests for branch - enter`, GitHubRepository.ID);
const { octokit, remote } = await this.ensure();
const result = await octokit.pulls.list({
owner: remote.owner,
repo: remote.repositoryName,
head: remoteAndBranch
const { query, remote, schema } = await this.ensure();
const { data } = await query<PullRequestsResponse>({
query: schema.PullRequestForHead,
variables: {
owner: remote.owner,
name: remote.repositoryName,
headRefName: branch,
},
});

const pullRequests = result.data
.map(pullRequest => {
return this.createOrUpdatePullRequestModel(
convertRESTPullRequestToRawPullRequest(pullRequest, this),
);
})
.filter(item => item !== null) as PullRequestModel[];

Logger.debug(`Fetch pull requests for branch - done`, GitHubRepository.ID);
return pullRequests;

if (data.repository.pullRequests.nodes.length > 0) {
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data.repository.pullRequests.nodes[0], this));
}
} catch (e) {
Logger.appendLine(`Fetching pull requests for branch failed: ${e}`, GitHubRepository.ID);
if (e.code === 404) {
Expand Down Expand Up @@ -702,7 +700,7 @@ export class GitHubRepository implements vscode.Disposable {
},
});
Logger.debug(`Fetch pull request ${id} - done`, GitHubRepository.ID);
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data, this));
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data.repository.pullRequest, this));
} catch (e) {
Logger.appendLine(`GithubRepository> Unable to fetch PR: ${e}`);
return;
Expand Down
8 changes: 8 additions & 0 deletions src/github/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ export interface IssuesResponse {
};
}

export interface PullRequestsResponse {
repository: {
pullRequests: {
nodes: PullRequest[]
}
}
}

export interface MaxIssueResponse {
repository: {
issues: {
Expand Down
168 changes: 91 additions & 77 deletions src/github/queries.gql
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,86 @@ fragment ReviewThread on PullRequestReviewThread {
}
}

fragment PullRequestFragment on PullRequest {
number
url
state
body
bodyHTML
title
author {
login
url
avatarUrl
... on User {
email
}
... on Organization {
email
}
}
createdAt
updatedAt
headRef {
...Ref
}
headRefName
headRefOid
headRepository {
owner {
login
}
url
}
baseRef {
...Ref
}
baseRefName
baseRefOid
baseRepository {
owner {
login
}
url
}
labels(first: 50) {
nodes {
name
}
}
merged
mergeable
mergeStateStatus
id
databaseId
isDraft
milestone {
title
dueOn
createdAt
id
}
assignees(first: 10) {
nodes {
login
name
avatarUrl
url
email
}
}
suggestedReviewers {
isAuthor
isCommenter
reviewer {
login
avatarUrl
name
url
}
}
}

query TimelineEvents($owner: String!, $name: String!, $number: Int!, $last: Int = 150) {
repository(owner: $owner, name: $name) {
pullRequest(number: $number) {
Expand Down Expand Up @@ -290,83 +370,7 @@ query PullRequestComments($owner: String!, $name: String!, $number: Int!, $first
query PullRequest($owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
pullRequest(number: $number) {
number
url
state
body
bodyHTML
title
author {
login
url
avatarUrl
... on User {
email
}
... on Organization {
email
}
}
createdAt
updatedAt
headRef {
...Ref
}
headRefName
headRefOid
headRepository {
owner {
login
}
url
}
baseRef {
...Ref
}
baseRefName
baseRefOid
baseRepository {
owner {
login
}
url
}
labels(first: 50) {
nodes {
name
}
}
merged
mergeable
mergeStateStatus
id
databaseId
isDraft
milestone {
title
dueOn
createdAt
id
}
assignees(first: 10) {
nodes {
login
name
avatarUrl
url
email
}
}
suggestedReviewers {
isAuthor
isCommenter
reviewer {
login
avatarUrl
name
url
}
}
...PullRequestFragment
}
}
rateLimit {
Expand Down Expand Up @@ -528,6 +532,16 @@ query PullRequestState($owner: String!, $name: String!, $number: Int!) {
}
}

query PullRequestForHead($owner: String!, $name: String!, $headRefName: String!) {
repository(owner: $owner, name: $name) {
pullRequests(first: 1, headRefName: $headRefName) {
nodes {
...PullRequestFragment
}
}
}
}

mutation AddComment($input: AddPullRequestReviewCommentInput!) {
addPullRequestReviewComment(input: $input) {
comment {
Expand Down
4 changes: 1 addition & 3 deletions src/github/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,9 @@ export function parseMergeability(mergeability: 'UNKNOWN' | 'MERGEABLE' | 'CONFL
}

export function parseGraphQLPullRequest(
pullRequest: GraphQL.PullRequestResponse,
graphQLPullRequest: GraphQL.PullRequest,
githubRepository: GitHubRepository,
): PullRequest {
const graphQLPullRequest = pullRequest.repository.pullRequest;

return {
id: graphQLPullRequest.databaseId,
graphNodeId: graphQLPullRequest.id,
Expand Down
4 changes: 2 additions & 2 deletions src/test/view/prsTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('GitHub Pull Requests view', function () {
);
});
}).pullRequest;
const prItem0 = parseGraphQLPullRequest(pr0, gitHubRepository);
const prItem0 = parseGraphQLPullRequest(pr0.repository.pullRequest, gitHubRepository);
const pullRequest0 = new PullRequestModel(telemetry, gitHubRepository, remote, prItem0);

const pr1 = gitHubRepository.addGraphQLPullRequest(builder => {
Expand All @@ -167,7 +167,7 @@ describe('GitHub Pull Requests view', function () {
);
});
}).pullRequest;
const prItem1 = parseGraphQLPullRequest(pr1, gitHubRepository);
const prItem1 = parseGraphQLPullRequest(pr1.repository.pullRequest, gitHubRepository);
const pullRequest1 = new PullRequestModel(telemetry, gitHubRepository, remote, prItem1);

const repository = new MockRepository();
Expand Down

0 comments on commit 3051a86

Please sign in to comment.