Skip to content

Commit

Permalink
Meta: Use new APIs in update-pr-from-base-branch (#6103)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakov116 committed Nov 1, 2022
1 parent 6a2d80a commit 987b566
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
6 changes: 3 additions & 3 deletions source/features/restore-file.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'dom-chef';
import select from 'select-dom';
import onetime from 'onetime';
import pushForm from 'push-form';
import * as pageDetect from 'github-url-detection';
import delegate, {DelegateEvent} from 'delegate-it';
Expand All @@ -15,7 +14,8 @@ import {getConversationNumber} from '../github-helpers';
Get the current base commit of this PR. It should change after rebases and merges in this PR.
This value is not consistently available on the page (appears in `/files` but not when only 1 commit is selected)
*/
const getBaseReference = onetime(async (): Promise<string> => {
// TODO: Replace this with `get-pr-info` when GHE supports it
async function getBaseReference(): Promise<string> {
const {repository} = await api.v4(`
repository() {
pullRequest(number: ${getConversationNumber()!}) {
Expand All @@ -24,7 +24,7 @@ const getBaseReference = onetime(async (): Promise<string> => {
}
`);
return repository.pullRequest.baseRefOid;
});
}

async function getFile(filePath: string): Promise<{isTruncated: boolean; text: string} | undefined> {
const {repository} = await api.v4(`
Expand Down
15 changes: 6 additions & 9 deletions source/features/update-pr-from-base-branch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import * as pageDetect from 'github-url-detection';
import delegate, {DelegateEvent} from 'delegate-it';

import features from '../feature-manager';
import observe from '../helpers/selector-observer';
import * as api from '../github-helpers/api';
import getPrInfo from '../github-helpers/get-pr-info';
import {getConversationNumber} from '../github-helpers';
import observe from '../helpers/selector-observer';

const selectorForPushablePRNotice = '.merge-pr > .color-fg-muted:first-child';

Expand Down Expand Up @@ -48,15 +48,12 @@ async function handler({delegateTarget}: DelegateEvent): Promise<void> {

async function addButton(position: Element): Promise<void> {
const {base, head} = getBranches();
const [pr, comparison] = await Promise.all([
getPrInfo(),

// TODO: Find how to determine whether the branch needs to be updated via v4
// `page=10000` avoids fetching any commit information, which is heavy
api.v3(`compare/${base}...${head}?page=10000`),
]);
const prInfo = await getPrInfo(base, head);
if (!prInfo) {
return;
}

if (comparison.status === 'diverged' && pr.viewerCanEditFiles && pr.mergeable !== 'CONFLICTING') {
if (prInfo.viewerCanEditFiles && prInfo.mergeable !== 'CONFLICTING') {
position.append(' ', (
<span className="status-meta d-inline-block rgh-update-pr-from-base-branch">
You can <button type="button" className="btn-link">update the base branch</button>.
Expand Down
37 changes: 34 additions & 3 deletions source/github-helpers/get-pr-info.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
import * as pageDetect from 'github-url-detection';

import * as api from './api';
import {getConversationNumber} from '.';

type PullRequestInfo = {
// TODO: Probably can be used for #3863 and #4679
// TODO: Use this for `restore-file` when GHE supports `compare`
baseRefOid: string;

// https://docs.github.com/en/graphql/reference/enums#mergeablestate
mergeable: 'CONFLICTING' | 'MERGEABLE' | 'UNKNOWN';
viewerCanEditFiles: boolean;
};

export default async function getPrInfo(number = getConversationNumber()!): Promise<PullRequestInfo> {
export default async function getPrInfo(base: string, head: string, number = getConversationNumber()!): Promise<PullRequestInfo | undefined> {
if (pageDetect.isEnterprise()) {
const {repository} = await api.v4(`
repository() {
pullRequest(number: ${number}) {
mergeable
viewerCanEditFiles
}
}
`);

const compare = await api.v3(`compare/${base}...${head}?page=10000`); // `page=10000` avoids fetching any commit information, which is heavy
if (compare.status !== 'diverged') {
return;
}

return repository.pullRequest;
}

const {repository} = await api.v4(`
repository() {
pullRequest(number: ${number}) {
baseRefOid
mergeable
viewerCanEditFiles
headRef {
compare(headRef: "${base}") {
status
behindBy
aheadBy
}
}
}
}
`);

if (repository.pullRequest.headRef.compare.status !== 'DIVERGED') {
return;
}

return repository.pullRequest;
}

0 comments on commit 987b566

Please sign in to comment.