Skip to content

Commit

Permalink
Link to specific open pull request instead of search page of open pul…
Browse files Browse the repository at this point in the history
…l requests
  • Loading branch information
emmatown committed Oct 18, 2023
1 parent 6b02eec commit 2a6202e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-radios-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Link to specific open pull request instead of search page of open pull requests
10 changes: 9 additions & 1 deletion packages/keystatic/src/app/dashboard/BranchSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,22 @@ export function BranchSection(props: { config: Config }) {
)}
</DialogTrigger>

{!isDefaultBranch && (
{!isDefaultBranch && branchInfo.pullRequestNumber === undefined ? (
<ActionButton
href={`${repoURL}/pull/new/${branchInfo.currentBranch}`}
target="_blank"
>
<Icon src={gitPullRequestIcon} />
<Text>{localizedString.format('createPullRequest')}</Text>
</ActionButton>
) : (
<ActionButton
href={`${repoURL}/pull/${branchInfo.pullRequestNumber}`}
target="_blank"
>
<Icon src={gitPullRequestIcon} />
<Text>Pull request #{branchInfo.pullRequestNumber}</Text>
</ActionButton>
)}
</Flex>
</DashboardSection>
Expand Down
17 changes: 11 additions & 6 deletions packages/keystatic/src/app/shell/data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,16 @@ export function GitHubAppShellProvider(props: {
}),
[baseCommit, repo?.id]
);
const pullRequestNumber =
currentBranchRef?.associatedPullRequests.nodes?.[0]?.number;
const branchInfo = useMemo(
() => ({
defaultBranch: repo?.defaultBranchRef?.name ?? '',
currentBranch: props.currentBranch,
baseCommit: baseCommit || '',
repositoryId: repo?.id ?? '',
allBranches: repo?.refs?.nodes?.map(x => x?.name).filter(isDefined) ?? [],
hasPullRequests: !!currentBranchRef?.associatedPullRequests.totalCount,
pullRequestNumber,
branchNameToId: new Map(
repo?.refs?.nodes?.filter(isDefined).map(x => [x.name, x.id])
),
Expand All @@ -348,7 +350,7 @@ export function GitHubAppShellProvider(props: {
repo?.refs?.nodes,
props.currentBranch,
baseCommit,
currentBranchRef?.associatedPullRequests.totalCount,
pullRequestNumber,
data?.repository?.owner.login,
data?.repository?.name,
]
Expand Down Expand Up @@ -462,8 +464,11 @@ export const Ref_base = gql`
}
}
}
associatedPullRequests(states: [OPEN]) {
totalCount
associatedPullRequests(states: [OPEN], first: 1) {
nodes {
id
number
}
}
}
` as import('../../../__generated__/ts-gql/Ref_base').type;
Expand Down Expand Up @@ -605,15 +610,15 @@ export const BranchInfoContext = createContext<{
allBranches: string[];
branchNameToId: Map<string, string>;
defaultBranch: string;
hasPullRequests: boolean;
pullRequestNumber: number | undefined;
branchNameToBaseCommit: Map<string, string>;
mainOwner: string;
mainRepo: string;
}>({
currentBranch: '',
allBranches: [],
defaultBranch: '',
hasPullRequests: false,
pullRequestNumber: undefined,
branchNameToId: new Map(),
branchNameToBaseCommit: new Map(),
mainOwner: '',
Expand Down
45 changes: 19 additions & 26 deletions packages/keystatic/src/app/shell/topbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Avatar } from '@keystar/ui/avatar';
import { ActionButton, Button } from '@keystar/ui/button';
import { AlertDialog, DialogContainer } from '@keystar/ui/dialog';
import { Icon } from '@keystar/ui/icon';
import { listTodoIcon } from '@keystar/ui/icon/icons/listTodoIcon';
import { logOutIcon } from '@keystar/ui/icon/icons/logOutIcon';
import { gitPullRequestIcon } from '@keystar/ui/icon/icons/gitPullRequestIcon';
import { gitBranchPlusIcon } from '@keystar/ui/icon/icons/gitBranchPlusIcon';
Expand Down Expand Up @@ -471,27 +470,27 @@ function GitMenu() {
];

if (!isDefaultBranch) {
prSection.push({
key: 'create-pull-request',
icon: gitPullRequestIcon,
label: stringFormatter.format('createPullRequest'),
});
if (!data.hasPullRequests) {
if (data.pullRequestNumber === undefined) {
prSection.push({
key: 'create-pull-request',
icon: gitPullRequestIcon,
label: stringFormatter.format('createPullRequest'),
});
} else {
prSection.push({
key: 'view-pull-request',
icon: gitPullRequestIcon,
label: `Pull Request #${data.pullRequestNumber}`,
});
}
if (data.pullRequestNumber === undefined) {
branchSection.push({
key: 'delete-branch',
icon: trash2Icon,
label: stringFormatter.format('deleteBranch'),
});
}
}

if (data.hasPullRequests) {
prSection.push({
key: 'related-pull-requests',
icon: listTodoIcon,
label: stringFormatter.format('viewPullRequests'),
});
}
if (fork) {
repoSection.push({
key: 'fork',
Expand Down Expand Up @@ -527,7 +526,7 @@ function GitMenu() {
fork,
data.currentBranch,
data.defaultBranch,
data.hasPullRequests,
data.pullRequestNumber,
stringFormatter,
]);
const router = useRouter();
Expand All @@ -547,16 +546,10 @@ function GitMenu() {
toggleDeleteBranchDialog();
break;
}
case 'related-pull-requests':
let query = [
['is', 'pr'],
['is', 'open'],
['head', data.currentBranch],
]
.map(([key, value]) => encodeURIComponent(`${key}:${value}`))
.join('+');

openBlankTargetSafely(`${repoURL}/pulls?q=${query}`);
case 'view-pull-request':
openBlankTargetSafely(
`${repoURL}/pull/${data.pullRequestNumber}`
);
break;
case 'create-pull-request':
openBlankTargetSafely(
Expand Down

0 comments on commit 2a6202e

Please sign in to comment.