Skip to content
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

Link to specific open pull request instead of search page of open pull requests #678

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 18 additions & 9 deletions packages/keystatic/src/app/dashboard/BranchSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,24 @@ export function BranchSection(props: { config: Config }) {
)}
</DialogTrigger>

{!isDefaultBranch && (
<ActionButton
href={`${repoURL}/pull/new/${branchInfo.currentBranch}`}
target="_blank"
>
<Icon src={gitPullRequestIcon} />
<Text>{localizedString.format('createPullRequest')}</Text>
</ActionButton>
)}
{!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
Loading