Skip to content
Navigation Menu
{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
update-pr-from-base-branch - Replace native button
#9366
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9aa632f
`update-pr-from-base-branch` - Replace native button
SunsetTechuila 6622b3f
very minor refactor
SunsetTechuila 8c055f6
update snapshot
SunsetTechuila 9a07edc
move css import
SunsetTechuila a6805e5
prune suppressions
SunsetTechuila 0e28da7
fix state update handling
SunsetTechuila f61f75e
minor tweaks
SunsetTechuila cbab189
review
SunsetTechuila c3c1b1c
1 more comment
SunsetTechuila 0d46b2c
comment
SunsetTechuila b5d694b
remove `mergeabilityRow` var
SunsetTechuila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,8 @@ | ||
| section[aria-label='Conflicts'] | ||
| div[class^='MergeBoxSectionHeader-module__contentLayout']:has( | ||
| > .ButtonGroup | ||
| ) { | ||
| > div[class^='prc-ButtonGroup'] { | ||
| display: none; | ||
| } | ||
| } |
This file contains hidden or 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,42 +1,27 @@ | ||
| import './update-pr-from-base-branch.css'; | ||
|
|
||
| import delegate, {type DelegateEvent} from 'delegate-it'; | ||
| import React from 'dom-chef'; | ||
| import * as pageDetect from 'github-url-detection'; | ||
| import { | ||
| $, $closest, $optional, elementExists, | ||
| $, | ||
| $$, | ||
| $closest, | ||
| $optional, | ||
| elementExists, | ||
| } from 'select-dom'; | ||
| import {CachedFunction} from 'webext-storage-cache'; | ||
|
|
||
| import features from '../feature-manager.js'; | ||
| import updatePullRequestBranch from './update-pr-from-base-branch.gql'; | ||
| import api from '../github-helpers/api.js'; | ||
| import getPrInfo from '../github-helpers/get-pr-info.js'; | ||
| import {expectToken} from '../github-helpers/github-token.js'; | ||
| import {getRepo} from '../github-helpers/index.js'; | ||
| import {getBranches} from '../github-helpers/pr-branches.js'; | ||
| import {deletedHeadRepository, prMergeabilityBoxHeader} from '../github-helpers/selectors.js'; | ||
| import {deletedHeadRepository} from '../github-helpers/selectors.js'; | ||
| import {isArchivedRepoAsync} from '../github-helpers/index.js'; | ||
| import showToast from '../github-helpers/toast.js'; | ||
| import {getIdentifiers} from '../helpers/feature-helpers.js'; | ||
| import observe from '../helpers/selector-observer.js'; | ||
| import updatePullRequestBranch from './update-pr-from-base-branch.gql'; | ||
|
|
||
| // TODO: Use CachedMap after https://github.com/fregante/webext-storage-cache/issues/51 | ||
| const nativeRepos = new CachedFunction('native-update-button', { | ||
| maxAge: { | ||
| days: 10, | ||
| }, | ||
| staleWhileRevalidate: { | ||
| days: 1, | ||
| }, | ||
| async updater(_nameWithOwner: string): Promise<boolean> { | ||
| throw new TypeError('bad usage'); | ||
| }, | ||
| }); | ||
|
|
||
| async function disableFeatureOnRepo(): Promise<void> { | ||
| const repo = getRepo()!.nameWithOwner; | ||
| console.trace('Refined GitHub: Disabling `update-pr-from-base-branch` on', repo); | ||
| features.unload(import.meta.url); | ||
| await nativeRepos.applyOverride([repo], true); | ||
| } | ||
| import features from '../feature-manager.js'; | ||
|
|
||
| const updateMethods = { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention -- Uppercase to match GraphQL enum values | ||
|
|
@@ -100,7 +85,7 @@ async function handler({delegateTarget: button}: DelegateEvent<MouseEvent, HTMLB | |
|
|
||
| const feature = getIdentifiers(import.meta.url); | ||
|
|
||
| function createButton(): JSX.Element { | ||
| function createButtonGroup(): JSX.Element { | ||
| return ( | ||
| <div className="ButtonGroup"> | ||
| {Object.entries(updateMethods).map(([method, label]) => { | ||
|
|
@@ -140,15 +125,13 @@ function createButton(): JSX.Element { | |
| ); | ||
| } | ||
|
|
||
| const nativeUpdateButtonSelector | ||
| = '[aria-label="Conflicts"] [class^="MergeBoxSectionHeader-module__wrapper"] [data-component="buttonContent"]'; | ||
|
|
||
| function canNativelyUpdate(): boolean { | ||
| const nativeButton = $optional(nativeUpdateButtonSelector); | ||
| return nativeButton?.textContent === 'Update branch'; | ||
| function setButtonsDisabledState(base: Element, disabled: boolean): void { | ||
| for (const button of $$('button', base)) { | ||
| button.disabled = disabled; | ||
| } | ||
| } | ||
|
|
||
| async function shouldShowButton(): Promise<boolean> { | ||
| async function isBranchUpdatable(): Promise<boolean> { | ||
| const {base} = getBranches(); | ||
| const prInfo = await getPrInfo(base.relative); | ||
|
|
||
|
|
@@ -158,39 +141,65 @@ async function shouldShowButton(): Promise<boolean> { | |
| return prInfo.needsUpdate && canUpdateBranch && prInfo.mergeable !== 'CONFLICTING'; | ||
| } | ||
|
|
||
| async function addButton(): Promise<void> { | ||
| if (canNativelyUpdate()) { | ||
| // Ideally the "canNativelyUpdate" observer is fired first and this listener isn't reached, but that is not guaranteed. | ||
| await disableFeatureOnRepo(); | ||
| async function manageButtonGroup(stateIcon: Element): Promise<void> { | ||
| const existingButtonGroup = $optional(`.ButtonGroup:has(.${feature.class})`); | ||
|
|
||
| if (elementExists('.octicon-check', stateIcon)) { | ||
| if (!await isBranchUpdatable()) { | ||
| return; | ||
| } | ||
|
|
||
| if (existingButtonGroup) { | ||
| setButtonsDisabledState(existingButtonGroup, false); | ||
| return; | ||
| } | ||
|
|
||
| // The same container as the native button uses | ||
| $('section[aria-label="Conflicts"] div[class^="MergeBoxSectionHeader-module__contentLayout"]') | ||
| .append(createButtonGroup()); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!await shouldShowButton()) { | ||
| // Loading icon, GitHub is determining the mergeability status | ||
| if (stateIcon.className.includes('Spinner')) { | ||
| if (existingButtonGroup) { | ||
| // Disable buttons until the status is determined | ||
| setButtonsDisabledState(existingButtonGroup, true); | ||
|
SunsetTechuila marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (elementExists('.octicon-alert-fill', stateIcon)) { | ||
| // Button group won't exist if it wasn't previously added | ||
|
Member
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. 🙏 |
||
| // For example, if a PR already had conflicts when its page was opened | ||
| existingButtonGroup?.remove(); | ||
| return; | ||
| } | ||
|
|
||
| const mergeabilityRow = $('[aria-label="Conflicts"] [class^="MergeBoxSectionHeader-module__contentLayout"]'); | ||
| mergeabilityRow.append(createButton()); | ||
| throw new TypeError('Unexpected state icon', {cause: stateIcon}); | ||
| } | ||
|
|
||
| async function init(signal: AbortSignal): Promise<false | void> { | ||
| await expectToken(); | ||
| if (await nativeRepos.getCached(getRepo()!.nameWithOwner)) { | ||
| return false; | ||
| } | ||
|
|
||
| delegate(feature.selector, 'click', handler, {signal}); | ||
| observe(prMergeabilityBoxHeader, addButton, {signal}); | ||
| observe(nativeUpdateButtonSelector, disableFeatureOnRepo, {signal}); | ||
| observe( | ||
| 'section[aria-label="Conflicts"] .flex-shrink-0 > :first-child', | ||
| manageButtonGroup, | ||
| {signal}, | ||
| ); | ||
| } | ||
|
|
||
| void features.add(import.meta.url, { | ||
| include: [ | ||
| pageDetect.isPRConversation, | ||
| ], | ||
| exclude: [ | ||
| pageDetect.isClosedConversation, | ||
|
Contributor
Author
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. Closed PR can be reopened |
||
| pageDetect.isMergedPR, | ||
| () => elementExists(deletedHeadRepository), | ||
| isArchivedRepoAsync, | ||
| ], | ||
| awaitDomReady: true, // DOM-based exclusions | ||
| init, | ||
|
|
||
This file contains hidden or 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.
You can’t perform that action at this time.

Uh oh!
There was an error while loading. Please reload this page.