Skip to content

Fix console errors in PR merging box features - #5549

Merged
cheap-glitch merged 2 commits into
mainfrom
fix-error-pr-merge-box-features
Apr 1, 2022
Merged

cheap-glitch merged 2 commits into
mainfrom
fix-error-pr-merge-box-features

Conversation

@cheap-glitch

Copy link
Copy Markdown
Contributor

Fixes #5547

Test URLs

  1. Click on the merge button at the bottom of the page
  2. Click on "Cancel" and reload the page
  3. No console errors should be produced

Screenshots

See the review comments below.

@cheap-glitch cheap-glitch added bug meta Related to Refined GitHub itself labels Mar 28, 2022

@cheap-glitch cheap-glitch left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR drops the session:resume listener used in onPrMergePanelOpen because of its unreliability (see linked issue for details), and adds two extra listeners to allow for more granularity in each feature:

  • onPrMergePanelOpen now reacts only to the opening of the merge panel (i.e. when clicking on one of the green Merge buttons).
  • onPrMergePanelLoad reacts to the initial loading of the merge panel, but in a more reliable way. This is a feature of GitHub that directly show the merge field on page load iff any of their content have been modified from the defaults.
  • onPrCommitMessageRestore is built on the previous listener and is triggered when GitHub restores the modified contents of the merge fields.

// Triggering input event for these fields immediately validates the form
onPrMergePanelOpen,
// For PR merges, GitHub restores any modified commit messages on page load
onPrCommitMessageRestore,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest-commit-title-limit.mp4

Comment on lines -74 to -75
// Only if the user hasn't already interacted with it in this session
if (field && event.type !== 'session:resume') {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The listener explicitly excluded the case of the panel content being restored, so we can just use the more restrictive onPrMergePanelToggle instead.

listeners.push(
...onPrMergePanelOpen(updateCommitTitle),
onPrCommitMessageRestore(updateUI),
onPrMergePanelOpen(updateCommitTitle),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though we still want to show the UI if the commit message has been modified:

sync-pr-commit-title.mp4

}

function watchForNewCommits(): MutationObserver {
let commitObserver: undefined | MutationObserver;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a global listener because it's the simplest way to ensure it's not created twice, since the callback could be called twice in a session: the first time when the merge panel is loaded, and the second if it's closed (with "Cancel") and then reopened (which would trigger the toggle listener).

Comment on lines +155 to +157
async function init(signal: AbortSignal): Promise<Deinit[]> {
// Warn user if it's not yet submitted
window.addEventListener('beforeunload', onBeforeunload);
window.addEventListener('beforeunload', onBeforeunload, {signal});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This anticipates #5523 a bit to make things easier.

Comment on lines +160 to +162
onPrMergePanelLoad(onPrMergePanelHandler),

onPrMergePanelOpen(() => {
const {signal} = deinitController;
if (signal.aborted) {
return;
}

showCheckboxIfNecessary();
const observer = watchForNewCommits();
signal.addEventListener('abort', observer.disconnect, {once: true});
}),
onPrMergePanelOpen(onPrMergePanelHandler),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait-for-checks.mp4

Video taken with the checkbox always enabled for obvious demonstration purposes.

Comment on lines +7 to +14
const getDeduplicatedHandler = mem((callback: EventListener): EventListener => () => {
// Wait for GitHub to update the contents of the merge commit summary field
select('input#merge_title_field')!.addEventListener('change', callback, {once: true});
});

export default function onPrCommitMessageRestore(callback: EventListener): delegate.Subscription {
return onPrMergePanelLoad(getDeduplicatedHandler(callback));
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The contents of the PR merge fields are not immediately restored upon load, so we wait for the panel to load and then attach a listener to the <input> element that will be triggered by GitHub's code.

@cheap-glitch

Copy link
Copy Markdown
Contributor Author

Two related comments that can't be reviews because the files haven't been touched 👇


In clear-pr-merge-commit-message, we only need to react to the user opening the panel: when the PR conversation page is first opened, the panel will always be closed, so the user has to open it at least once before merging.

Then on subsequent reloads, any changes the feature made to the commit description will automatically be restored by GitHub.


The session:resume event is also used in prevent-pr-merge-panel-opening. Since this feature doesn't produce any errors because of it, and because it can be triggered several times with little cost, I haven't touched upon it in this PR.

But it's somewhat unreliable, so it will have to be revised too.

@cheap-glitch
cheap-glitch marked this pull request as ready for review March 28, 2022 15:16
@fregante

Copy link
Copy Markdown
Member

The demo videos look good. It would be good to outline the specific situations that our listeners cover, including past fixes we applied, so that we can use it as a test checklist. For example:

  • Clean PR load, open merge box
  • Clean PR load, open merge box, edit commit title
  • Clean PR load, open merge box, edit commit title, reload
  • Clean PR load, open merge box, new commit comes in
  • ...etc

Or simply list all the "events" that affect the box, like:

  • opening and closing it
  • loading the page after editing
  • etc

@fregante

Copy link
Copy Markdown
Member

This PR can be merged after 22.4.1 as is, though

@cheap-glitch

Copy link
Copy Markdown
Contributor Author

The demo videos look good. It would be good to outline the specific situations that our listeners cover, including past fixes we applied, so that we can use it as a test checklist.

+1

Actually, I've been thinking that we could really use a permanent list of testing URLs for often-modified features or features that have a lot of edge cases (e.g. clean-repo-sidebebar, clean-conversation-headers, etc.), so we don't have to go fishing through old PRs for them.

Just a Markdown file somewhere in the repo would be good enough.

@fregante

Copy link
Copy Markdown
Member

We can include these details in the feature itself. Adding large comments isn't an issue, we can append them to the file, so they follow the init

@cheap-glitch cheap-glitch changed the title Fix console errors in features affecting the PR merge panel Fix console errors in PR merging box features Apr 1, 2022
@cheap-glitch
cheap-glitch merged commit e0cf100 into main Apr 1, 2022
@cheap-glitch
cheap-glitch deleted the fix-error-pr-merge-box-features branch April 1, 2022 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug meta Related to Refined GitHub itself

Development

Successfully merging this pull request may close these issues.

Console errors in PR-related features

2 participants