Skip to content

Commit

Permalink
Shorten and fix separate-draft-pr-button (#3922)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jan 31, 2021
1 parent 233c8db commit b4294b8
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions source/features/separate-draft-pr-button.tsx
Expand Up @@ -5,26 +5,16 @@ import * as pageDetect from 'github-url-detection';
import features from '.';

function init(): void | false {
const previewForm = select('.new-pr-form');

// PRs can't be created from some comparison pages:
// Either base is a tag, not a branch; or there already exists a PR.
if (!previewForm) {
return false;
}

const createPrButtonGroup = select([
'.hx_create-pr-button',
'.timeline-comment > :last-child > .BtnGroup' // GHE
]);
if (!createPrButtonGroup) {
// Free accounts can't open Draft PRs in private repos, so this element is missing
const draftPROption = select('.new-pr-form [name="draft"]');
if (!draftPROption) {
// 1. Free accounts can't open Draft PRs in private repos, so this element is missing
// 2. PRs can't be created from some comparison pages: Either base is a tag, not a branch; or there already exists a PR.
return false;
}

const createPrDropdownItems = select.all('.select-menu-item', previewForm);
const initialGroupedButtons = draftPROption.closest('.BtnGroup')!;

for (const dropdownItem of createPrDropdownItems) {
for (const dropdownItem of select.all('.select-menu-item', initialGroupedButtons)) {
let title = select('.select-menu-item-heading', dropdownItem)!.textContent!.trim();
const description = select('.description', dropdownItem)!.textContent!.trim();
const radioButton = select('input[type=radio]', dropdownItem)!;
Expand All @@ -36,7 +26,7 @@ function init(): void | false {
classList.push('btn-primary');
}

createPrButtonGroup.after(
initialGroupedButtons.after(
<button
className={classList.join(' ')}
aria-label={description}
Expand All @@ -49,8 +39,7 @@ function init(): void | false {
);
}

select('details', createPrButtonGroup.parentElement!)!.remove();
createPrButtonGroup.remove();
initialGroupedButtons.remove();
}

void features.add(__filebasename, {
Expand Down

0 comments on commit b4294b8

Please sign in to comment.