-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add show-open-prs-of-forks feature
#2880
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
13 commits
Select commit
Hold shift + click to select a range
6a45212
Add `show-open-prs-of-forks` feature
loilo 2d52712
Address review suggestions
loilo 9b795fe
Use origin-agnostic PRs URL
loilo 47c8f9c
Try unescaped smart apostrophe
fregante 2095322
Use more compact cache key for open PR data
loilo 738a420
Move environment checks out of the `getOpenPullRequestsData` function
loilo ee4133e
Minimize cache footprint
loilo 699c012
Fix link to user's pull requests
loilo 306fbb8
Reorganize
fregante 5897e7b
Merge branch 'master' into open-pr-on-fork
fregante 275f43f
Use `pluralize` to make the link more readable
fregante 102e014
Merge branch 'master' into open-pr-on-fork
fregante 9c0386e
Update readme.md
fregante 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
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,108 @@ | ||
| import React from 'dom-chef'; | ||
| import cache from 'webext-storage-cache'; | ||
| import select from 'select-dom'; | ||
| import elementReady from 'element-ready'; | ||
| import * as api from '../libs/api'; | ||
| import features from '../libs/features'; | ||
| import {isForkedRepo, isRepoWithAccess} from '../libs/page-detect'; | ||
| import {getForkedRepo, getUsername, pluralize} from '../libs/utils'; | ||
|
|
||
| function getLinkCopy(count: number): string { | ||
| return pluralize(count, 'one open pull request', '$$ open pull requests'); | ||
| } | ||
|
|
||
| const countPRs = cache.function(async (forkedRepo: string): Promise<[number, number?]> => { | ||
| // Grab the PR count and the first PR's URL | ||
| // This allows to link to the PR directly if only one is found | ||
| const {search} = await api.v4(` | ||
| search( | ||
| first: 1, | ||
| type: ISSUE, | ||
| query: "repo:${forkedRepo} is:pr is:open author:${getUsername()}" | ||
| ) { | ||
| issueCount | ||
| nodes { | ||
| ... on PullRequest { | ||
| number | ||
| } | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| if (search.issueCount === 1) { | ||
| return [1, search.nodes[0].number]; | ||
| } | ||
|
|
||
| return [search.issueCount]; | ||
| }, { | ||
| maxAge: 1 / 2, // Stale after 12 hours | ||
| staleWhileRevalidate: 2, | ||
| cacheKey: ([forkedRepo]): string => 'prs-on-forked-repo:' + forkedRepo | ||
| }); | ||
|
|
||
| async function getPRs(): Promise<[number, string] | []> { | ||
| await elementReady('.repohead + *'); // Wait for the tab bar to be loaded | ||
| if (!isRepoWithAccess()) { | ||
| return []; | ||
| } | ||
|
|
||
| const forkedRepo = getForkedRepo()!; | ||
| const [count, firstPr] = await countPRs(forkedRepo); | ||
| if (count === 1) { | ||
| return [count, `/${forkedRepo}/pull/${firstPr!}`]; | ||
| } | ||
|
|
||
| return [count, `/${forkedRepo}/pulls?q=is%3Apr+is%3Aopen+sort%3Aupdated-desc+author%3A${getUsername()}`]; | ||
| } | ||
|
|
||
| async function initHeadHint(): Promise<void | false> { | ||
| const [count, url] = await getPRs(); | ||
| if (!count) { | ||
| return false; | ||
| } | ||
|
|
||
| select('.fork-flag .text')!.append( | ||
| <> with <a href={url}>{getLinkCopy(count)}</a></> | ||
| ); | ||
| } | ||
|
|
||
| async function initDeleteHint(): Promise<void | false> { | ||
| const [count, url] = await getPRs(); | ||
| if (!count) { | ||
| return false; | ||
| } | ||
|
|
||
| select('details-dialog[aria-label*="Delete"] .Box-body p:first-child')!.after( | ||
| <p className="flash flash-warn"> | ||
| It will also abandon <a href={url}>your {getLinkCopy(count)}</a> in <strong>{getForkedRepo()!}</strong> and you’ll no longer be able to edit {count === 1 ? 'it' : 'them'}. | ||
| </p> | ||
| ); | ||
| } | ||
|
|
||
| features.add({ | ||
| id: __featureName__, | ||
| description: 'In your forked repos, shows number of your open PRs to the original repo.', | ||
| screenshot: 'https://user-images.githubusercontent.com/1922624/76398271-e0648500-637c-11ea-8210-53dda1be9d51.png', | ||
| include: [ | ||
| features.isRepo | ||
| ], | ||
| exclude: [ | ||
| () => !isForkedRepo() | ||
| ], | ||
| load: features.nowAndOnAjaxedPages, | ||
| init: initHeadHint | ||
| }); | ||
|
|
||
| features.add({ | ||
| id: __featureName__, | ||
| description: '', | ||
| screenshot: '', | ||
| include: [ | ||
| features.isRepoSettings | ||
| ], | ||
| exclude: [ | ||
| () => !isForkedRepo() | ||
| ], | ||
| load: features.nowAndOnAjaxedPages, | ||
| init: initDeleteHint | ||
| }); |
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -88,6 +88,10 @@ export const getOwnerAndRepo = (): { | |||||
| return {ownerName, repoName}; | ||||||
| }; | ||||||
|
|
||||||
| export function getForkedRepo(): string | undefined { | ||||||
| return select<HTMLAnchorElement>('.fork-flag a')?.pathname.slice(1); | ||||||
|
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. Also you could use the same meta tags I used in the
Suggested change
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. Sure. 👍 |
||||||
| } | ||||||
|
|
||||||
| export const getReference = (): string | undefined => { | ||||||
| const pathnameParts = location.pathname.split('/'); | ||||||
| if (['commits', 'blob', 'tree', 'blame'].includes(pathnameParts[3])) { | ||||||
|
|
||||||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This is ambiguous. Is it the repo that was forked or the fork itself?
The correct term would be "source repo". Perhaps
getForkSourceRepowould be the correct term.Can you change this name everywhere? Including
const forkedRepo = ...(but notisForkedRepo, that's more clear, I think)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.
I thought so as well, but I trusted the existing naming (the function existed before, I just moved it to utils).
Yes, I'll take care of it when I'm going to handle #2934.
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.
Hm, there's already a function with that name in the
forked-tofeature.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.
How about
getParentRepo: only works if there’s a parent)getSourceRepo: always gets the source repo, whether it’s the parent or the currentThere 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.
Sound's good to me. I'll do some refactoring of these, but I think I'll open a separate PR for that.