Skip to content

Commit

Permalink
feat(github): Fetch PRs using cacheable REST (#14907)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Apr 15, 2022
1 parent c2adeff commit d663859
Show file tree
Hide file tree
Showing 9 changed files with 619 additions and 395 deletions.
41 changes: 17 additions & 24 deletions lib/modules/platform/github/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`modules/platform/github/index getBranchPr(branchName) should reopen an autoclosed PR 1`] = `
Object {
"displayNumber": "Pull Request #91",
"number": 91,
"sourceBranch": "somebranch",
"sourceRepo": "some/repo",
"state": "open",
"title": "Some title",
}
`;

exports[`modules/platform/github/index getBranchPr(branchName) should return the PR object 1`] = `
exports[`modules/platform/github/index getBranchPr(branchName) should cache and return the PR object 1`] = `
Object {
"body": "dummy body",
"displayNumber": "Pull Request #91",
"number": 91,
"sourceBranch": "somebranch",
Expand All @@ -22,8 +12,9 @@ Object {
}
`;

exports[`modules/platform/github/index getBranchPr(branchName) should return the PR object in fork mode 1`] = `
exports[`modules/platform/github/index getBranchPr(branchName) should cache and return the PR object in fork mode 1`] = `
Object {
"body": "dummy body",
"displayNumber": "Pull Request #90",
"number": 90,
"sourceBranch": "somebranch",
Expand All @@ -33,33 +24,33 @@ Object {
}
`;

exports[`modules/platform/github/index getPr(prNo) should return PR from closed graphql result 1`] = `
exports[`modules/platform/github/index getBranchPr(branchName) should reopen and cache autoclosed PR 1`] = `
Object {
"body": "dummy body",
"displayNumber": "Pull Request #2499",
"number": 2499,
"sourceBranch": "renovate/delay-4.x",
"state": "merged",
"title": "build(deps): update dependency delay to v4.0.1",
"displayNumber": "Pull Request #91",
"number": 91,
"sourceBranch": "somebranch",
"sourceRepo": "some/repo",
"state": "open",
"title": "old title",
}
`;

exports[`modules/platform/github/index getPr(prNo) should return PR from graphql result 1`] = `
exports[`modules/platform/github/index getPr(prNo) should return PR 1`] = `
Object {
"body": "Some body",
"body": "dummy body",
"displayNumber": "Pull Request #2500",
"hasAssignees": true,
"hasReviewers": true,
"number": 2500,
"sourceBranch": "renovate/jest-monorepo",
"sourceRepo": "some/repo",
"state": "open",
"targetBranch": "master",
"title": "chore(deps): update dependency jest to v23.6.0",
}
`;

exports[`modules/platform/github/index getPr(prNo) should return a PR object - 0 1`] = `
Object {
"body": "dummy body",
"createdAt": "01-01-2022",
"displayNumber": "Pull Request #1234",
"hasAssignees": true,
Expand All @@ -77,6 +68,7 @@ Object {

exports[`modules/platform/github/index getPr(prNo) should return a PR object - 1 1`] = `
Object {
"body": "dummy body",
"displayNumber": "Pull Request #1234",
"hasAssignees": true,
"hasReviewers": true,
Expand All @@ -89,6 +81,7 @@ Object {

exports[`modules/platform/github/index getPr(prNo) should return a PR object - 2 1`] = `
Object {
"body": "dummy body",
"displayNumber": "Pull Request #1234",
"number": 1234,
"sourceBranch": "some/branch",
Expand Down
39 changes: 6 additions & 33 deletions lib/modules/platform/github/common.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,16 @@
import is from '@sindresorhus/is';
import { PrState } from '../../../types';
import type { Pr } from '../types';
import type { GhGraphQlPr, GhRestPr } from './types';
import type { GhRestPr } from './types';

/**
* @see https://developer.github.com/v4/object/pullrequest/
* @see https://docs.github.com/en/rest/reference/pulls#list-pull-requests
*/
export function coerceGraphqlPr(pr: GhGraphQlPr): Pr {
const result: Pr = {
number: pr.number,
displayNumber: `Pull Request #${pr.number}`,
title: pr.title,
state: pr.state ? pr.state.toLowerCase() : PrState.Open,
sourceBranch: pr.headRefName,
body: pr.body ? pr.body : 'dummy body',
};

if (pr.baseRefName) {
result.targetBranch = pr.baseRefName;
export function coerceRestPr(pr: GhRestPr | null | undefined): Pr | null {
if (!pr) {
return null;
}

if (pr.assignees) {
result.hasAssignees = !!(pr.assignees.totalCount > 0);
}

if (pr.reviewRequests) {
result.hasReviewers = !!(pr.reviewRequests.totalCount > 0);
}

if (pr.labels?.nodes) {
result.labels = pr.labels.nodes.map((label) => label.name);
}

return result;
}

/**
* @see https://docs.github.com/en/rest/reference/pulls#list-pull-requests
*/
export function coerceRestPr(pr: GhRestPr): Pr {
const result: Pr = {
displayNumber: `Pull Request #${pr.number}`,
number: pr.number,
Expand All @@ -48,6 +20,7 @@ export function coerceRestPr(pr: GhRestPr): Pr {
pr.state === PrState.Closed && is.string(pr.merged_at)
? PrState.Merged
: pr.state,
body: pr.body ?? 'dummy body',
};

if (pr.head?.sha) {
Expand Down

0 comments on commit d663859

Please sign in to comment.