Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meta: Use new APIs in update-pr-from-base-branch #6103

Merged
merged 20 commits into from
Nov 1, 2022
12 changes: 3 additions & 9 deletions source/features/restore-file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,15 @@ import features from '../feature-manager';
import * as api from '../github-helpers/api';
import fetchDom from '../helpers/fetch-dom';
import showToast from '../github-helpers/toast';
import {getConversationNumber} from '../github-helpers';
import {getPrInfo} from '../github-helpers/get-pr-info';

/**
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> => {
yakov116 marked this conversation as resolved.
Show resolved Hide resolved
const {repository} = await api.v4(`
repository() {
pullRequest(number: ${getConversationNumber()!}) {
baseRefOid
}
}
`);
return repository.pullRequest.baseRefOid;
const {baseRefOid} = await getPrInfo();
return baseRefOid;
});

async function getFile(filePath: string): Promise<{isTruncated: boolean; text: string} | undefined> {
Expand Down
14 changes: 7 additions & 7 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';
import {getPrInfo, getPrBranchAheadStatus} from '../github-helpers/get-pr-info';

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

Expand Down Expand Up @@ -50,13 +50,13 @@ 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`),
// TODO: Drop v3 when GHE supports this via v4
pageDetect.isEnterprise()
? api.v3(`compare/${base}...${head}?page=10000`) // `page=10000` avoids fetching any commit information, which is heavy
: getPrBranchAheadStatus(base),
]);
yakov116 marked this conversation as resolved.
Show resolved Hide resolved

if (comparison.status === 'diverged' && pr.viewerCanEditFiles && pr.mergeable !== 'CONFLICTING') {
if (comparison.status.toLowerCase() === 'diverged' && pr.viewerCanEditFiles && pr.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
24 changes: 23 additions & 1 deletion source/github-helpers/get-pr-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ type PullRequestInfo = {
viewerCanEditFiles: boolean;
};

export default async function getPrInfo(number = getConversationNumber()!): Promise<PullRequestInfo> {
type PullRequestAheadStatus = {
status: 'BEHIND' | 'DIVERGED' | 'AHEAD' | 'IDENTICAL';
};

export async function getPrInfo(number = getConversationNumber()!): Promise<PullRequestInfo> {
const {repository} = await api.v4(`
repository() {
pullRequest(number: ${number}) {
Expand All @@ -22,3 +26,21 @@ export default async function getPrInfo(number = getConversationNumber()!): Prom
`);
return repository.pullRequest;
}

// TODO: Merge the 2 functions after it's supported by GHE
export async function getPrBranchAheadStatus(base: string, number = getConversationNumber()!): Promise<PullRequestAheadStatus> {
const {repository} = await api.v4(`
repository() {
pullRequest(number: ${number}) {
headRef {
compare(headRef: "${base}") {
status
behindBy
aheadBy
yakov116 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
`);
return repository.pullRequest.headRef.compare;
}