Browse Source

Sorts GitHub PRs ourselves

GitHub associatedPullRequests GraphQL api doesn't seem to honor the sort, so do it ourselves
main
Eric Amodio 4 years ago
parent
commit
19715cdf86
3 changed files with 31 additions and 17 deletions
  1. +0
    -1
      src/git/remotes/github.ts
  2. +0
    -2
      src/git/remotes/provider.ts
  3. +31
    -14
      src/github/github.ts

+ 0
- 1
src/git/remotes/github.ts View File

@ -200,7 +200,6 @@ export class GitHubRemote extends RemoteProviderWithApi {
options?: {
avatarSize?: number;
include?: PullRequestState[];
limit?: number;
},
): Promise<PullRequest | undefined> {
const [owner, repo] = this.splitPath();

+ 0
- 2
src/git/remotes/provider.ts View File

@ -438,7 +438,6 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
options?: {
avatarSize?: number;
include?: PullRequestState[];
limit?: number;
},
): Promise<PullRequest | undefined> {
const cc = Logger.getCorrelationContext();
@ -465,7 +464,6 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
options?: {
avatarSize?: number;
include?: PullRequestState[];
limit?: number;
},
): Promise<PullRequest | undefined>;

+ 31
- 14
src/github/github.ts View File

@ -237,7 +237,6 @@ export class GitHubApi {
baseUrl?: string;
avatarSize?: number;
include?: GitHubPullRequestState[];
limit?: number;
},
): Promise<PullRequest | undefined> {
const cc = Logger.getCorrelationContext();
@ -273,8 +272,6 @@ export class GitHubApi {
}
}`;
options = { limit: 1, ...options };
const rsp = await graphql<{
repository:
| {
@ -292,16 +289,27 @@ export class GitHubApi {
owner: owner,
repo: repo,
branch: branch,
// Since GitHub sort doesn't seem to really work, look for a max of 10 PRs and then sort them ourselves
limit: 10,
headers: { authorization: `Bearer ${token}` },
...options,
});
const pr = rsp?.repository?.refs.nodes[0]?.associatedPullRequests?.nodes?.[0];
if (pr == null) return undefined;
// GitHub seems to sometimes return PRs for forks
if (pr.repository.owner.login !== owner) return undefined;
// Filter to ensure we only get the owner we expect, as GitHub seems to sometimes return PRs for forks
const prs = rsp?.repository?.refs.nodes[0]?.associatedPullRequests?.nodes?.filter(
pr => pr.repository.owner.login === owner,
);
if (prs == null || prs.length === 0) return undefined;
if (prs.length > 1) {
prs.sort(
(a, b) =>
(a.state === 'OPEN' ? -1 : 1) - (b.state === 'OPEN' ? -1 : 1) ||
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(),
);
}
return GitHubPullRequest.from(pr, provider);
return GitHubPullRequest.from(prs[0], provider);
} catch (ex) {
Logger.error(ex, cc);
@ -354,7 +362,7 @@ export class GitHubApi {
repository(name: $repo, owner: $owner) {
object(oid: $ref) {
... on Commit {
associatedPullRequests(first: 1, orderBy: {field: UPDATED_AT, direction: DESC}) {
associatedPullRequests(first: 2, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes {
author {
login
@ -399,12 +407,21 @@ export class GitHubApi {
...options,
});
const pr = rsp?.repository?.object?.associatedPullRequests?.nodes?.[0];
if (pr == null) return undefined;
// GitHub seems to sometimes return PRs for forks
if (pr.repository.owner.login !== owner) return undefined;
// Filter to ensure we only get the owner we expect, as GitHub seems to sometimes return PRs for forks
const prs = rsp?.repository?.object?.associatedPullRequests?.nodes?.filter(
pr => pr.repository.owner.login === owner,
);
if (prs == null || prs.length === 0) return undefined;
if (prs.length > 1) {
prs.sort(
(a, b) =>
(a.state === 'OPEN' ? -1 : 1) - (b.state === 'OPEN' ? -1 : 1) ||
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(),
);
}
return GitHubPullRequest.from(pr, provider);
return GitHubPullRequest.from(prs[0], provider);
} catch (ex) {
Logger.error(ex, cc);

Loading…
Cancel
Save