-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add pr-base-commit
feature
#6538
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
68abffa
Lint
yakov116 ac82965
Add behind count to update branch
yakov116 f1fe231
Lint
yakov116 3c3b1a3
Enable for enterprise
yakov116 cc751e9
Lint
yakov116 cc732b7
Move behind by up
yakov116 a12d36f
Merge commit 'cc732b79272f571f9f2a15eb77cdd41e84f1b097'
fregante daf892b
Move "Update" button to mergeability box
fregante e70c3fb
Activate button
fregante 8a88724
Don't show when native exists
fregante 4fa0bb3
Integrate in native "can be merged" row
fregante b87c979
Add one more test URL
fregante 1dd76ae
Add draft PR URL
fregante ba54d80
Early return
fregante 7b5cf03
Drop duplicate check
fregante c67860c
Add meta
fregante dfe482d
Merge remote-tracking branch 'origin/update-pr' into base-commit
fregante 298265e
Yakov style; extracted
fregante b86f46f
Style optimization 1/?
fregante 9a3f49d
Fix URL, it must compare the base commit to the latest base branch co…
fregante 8269685
Lint
fregante 61dd9c5
Extract into its own feature
fregante d08e8d9
/2
fregante 6af48fb
Lint
fregante 359a2cc
Merge remote-tracking branch 'origin/main' into base-commit
fregante 364507e
Flip ahead/behind; drop GHE support
fregante 04541e6
Add to readme; finalize copy
fregante b4fe378
Fix `linkifyCommit`
fregante d5ab97c
Lint
fregante File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React from 'dom-chef'; | ||
import select from 'select-dom'; | ||
|
||
import * as pageDetect from 'github-url-detection'; | ||
|
||
import features from '../feature-manager'; | ||
import observe from '../helpers/selector-observer'; | ||
import * as api from '../github-helpers/api'; | ||
import {getBranches} from '../github-helpers/pr-branches'; | ||
import getPrInfo, {PullRequestInfo} from '../github-helpers/get-pr-info'; | ||
import pluralize from '../helpers/pluralize'; | ||
import {buildRepoURL} from '../github-helpers'; | ||
import {linkifyCommit} from '../github-helpers/dom-formatters'; | ||
import {removeTextNodeContaining} from '../helpers/dom-utils'; | ||
|
||
function getBaseCommitNotice(prInfo: PullRequestInfo): JSX.Element { | ||
const {base} = getBranches(); | ||
const commit = linkifyCommit(prInfo.baseRefOid); | ||
const count = pluralize(prInfo.behindBy, '$$ commit'); | ||
const countLink = ( | ||
<a href={buildRepoURL('compare', `${prInfo.baseRefOid.slice(0, 8)}...${base.branch}`)}> | ||
{count} | ||
</a> | ||
); | ||
return ( | ||
<>It’s {countLink} behind (base commit: {commit})</> | ||
); | ||
} | ||
|
||
async function addInfo(statusMeta: Element): Promise<void> { | ||
// Selector copied from GitHub. Don't @ me | ||
// This excludes hidden ".status-meta" items without adding this longass selector to the observer | ||
// Added: .rgh-update-pr-from-base-branch-row | ||
if (!statusMeta.closest('.merge-pr.is-merging .merging-body, .merge-pr.is-merging .merge-commit-author-email-info, .merge-pr.is-merging-solo .merging-body, .merge-pr.is-merging-jump .merging-body, .merge-pr.is-merging-group .merging-body, .merge-pr.is-rebasing .rebasing-body, .merge-pr.is-squashing .squashing-body, .merge-pr.is-squashing .squash-commit-author-email-info, .merge-pr.is-merging .branch-action-state-error-if-merging .merging-body-merge-warning, .rgh-update-pr-from-base-branch-row')) { | ||
return; | ||
} | ||
|
||
const {base} = getBranches(); | ||
const prInfo = await getPrInfo(base.relative); | ||
if (!prInfo.needsUpdate) { | ||
return; | ||
} | ||
|
||
const previousMessage = statusMeta.firstChild!; // Extract now because it won't be the first child anymore | ||
statusMeta.prepend(getBaseCommitNotice(prInfo)); | ||
removeTextNodeContaining(previousMessage, 'Merging can be performed automatically.'); | ||
} | ||
|
||
async function init(signal: AbortSignal): Promise<false | void> { | ||
await api.expectToken(); | ||
|
||
observe('.branch-action-item .status-meta', addInfo, {signal}); | ||
} | ||
|
||
void features.add(import.meta.url, { | ||
include: [ | ||
pageDetect.isPRConversation, | ||
], | ||
exclude: [ | ||
pageDetect.isClosedPR, | ||
() => select('.head-ref')!.title === 'This repository has been deleted', | ||
], | ||
awaitDomReady: true, // DOM-based exclusions | ||
init, | ||
}); | ||
|
||
/* | ||
Test URLs | ||
|
||
PR without conflicts | ||
https://github.com/refined-github/sandbox/pull/60 | ||
|
||
Draft PR without conflicts | ||
https://github.com/refined-github/sandbox/pull/61 | ||
|
||
Native "Update branch" button | ||
(pick a conflict-free PR from https://github.com/refined-github/refined-github/pulls?q=is%3Apr+is%3Aopen+sort%3Acreated-asc) | ||
|
||
Native "Resolve conflicts" button | ||
https://github.com/refined-github/sandbox/pull/9 | ||
|
||
Cross-repo PR with long branch names | ||
https://github.com/refined-github/sandbox/pull/13 | ||
|
||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,17 @@ | ||
import * as pageDetect from 'github-url-detection'; | ||
|
||
import * as api from './api'; | ||
import {getConversationNumber} from '.'; | ||
|
||
type PullRequestInfo = { | ||
// TODO: Use this for `restore-file` when GHE supports `compare` | ||
export type PullRequestInfo = { | ||
// TODO: Use this for `restore-file` | ||
baseRefOid: string; | ||
// https://docs.github.com/en/graphql/reference/enums#mergeablestate | ||
mergeable: 'CONFLICTING' | 'MERGEABLE' | 'UNKNOWN'; | ||
viewerCanEditFiles: boolean; | ||
needsUpdate: boolean; | ||
behindBy: number; | ||
}; | ||
|
||
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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's been 6 months. Dropping |
||
return; | ||
} | ||
|
||
return repository.pullRequest; | ||
} | ||
|
||
export default async function getPrInfo(base: string, number = getConversationNumber()!): Promise<PullRequestInfo> { | ||
const {repository} = await api.v4(` | ||
repository() { | ||
pullRequest(number: ${number}) { | ||
|
@@ -39,17 +21,25 @@ export default async function getPrInfo(base: string, head: string, number = get | |
headRef { | ||
compare(headRef: "${base}") { | ||
status | ||
behindBy | ||
aheadBy | ||
} | ||
} | ||
} | ||
} | ||
`); | ||
|
||
if (repository.pullRequest.headRef.compare.status !== 'DIVERGED') { | ||
return; | ||
} | ||
|
||
return repository.pullRequest; | ||
const { | ||
baseRefOid, | ||
mergeable, | ||
viewerCanEditFiles, | ||
headRef, | ||
} = repository.pullRequest; | ||
return { | ||
baseRefOid, | ||
mergeable, | ||
viewerCanEditFiles, | ||
// The comparison in the API is base -> head, so it must be flipped | ||
behindBy: headRef.compare.aheadBy, | ||
needsUpdate: headRef.compare.status === 'DIVERGED', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Final look:
reaction-avatars
downgraded