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
11 changes: 2 additions & 9 deletions source/features/update-pr-from-base-branch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,9 @@ 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 {mergeable, viewerCanEditFiles, headRef} = await getPrInfo(getBranches().base);

if (comparison.status === 'diverged' && pr.viewerCanEditFiles && pr.mergeable !== 'CONFLICTING') {
if (headRef.compare.status === 'DIVERGED' && viewerCanEditFiles && 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
14 changes: 13 additions & 1 deletion source/github-helpers/get-pr-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@ type PullRequestInfo = {
// https://docs.github.com/en/graphql/reference/enums#mergeablestate
mergeable: 'CONFLICTING' | 'MERGEABLE' | 'UNKNOWN';
viewerCanEditFiles: boolean;
headRef: {
compare: {
status: 'BEHIND' | 'DIVERGED' | 'AHEAD' | 'IDENTICAL';
};
};
};

export default async function getPrInfo(number = getConversationNumber()!): Promise<PullRequestInfo> {
export default async function getPrInfo(base: string, number = getConversationNumber()!): Promise<PullRequestInfo> {
const {repository} = await api.v4(`
repository() {
pullRequest(number: ${number}) {
baseRefOid
mergeable
viewerCanEditFiles
headRef {
compare(headRef: "${base}") {
status
behindBy
aheadBy
yakov116 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
`);
Expand Down