Skip to content

Commit

Permalink
fix: fix GitHub installation URL (#1358)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Aug 26, 2024
1 parent 1ca84d2 commit 7962532
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
32 changes: 25 additions & 7 deletions apps/frontend/src/containers/GitHub.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,37 @@ function useLoginUrl(redirect: string | null | undefined) {
)}`;
}

export function getInstallationUrl(app: "main" | "light") {
const baseURL = () => {
/**
* Get the URL to install the GitHub app.
*/
export function getGitHubAppManageURL(app: "main" | "light") {
const baseURL = (() => {
switch (app) {
case "main":
return config.get("github.appUrl");
return new URL(config.get("github.appUrl"));
case "light":
return config.get("githubLight.appUrl");
return new URL(config.get("githubLight.appUrl"));
default:
assertNever(app);
}
};
const url = new URL("/installations/new", baseURL());
url.searchParams.set("state", window.location.pathname);
})();
// /installations/new let you manage the installed app
// that's why we use it here
return new URL(
`${baseURL.pathname}/installations/new`,
baseURL.origin,
).toString();
}

export function getGitHubMainAppInstallUrl(input: { pathname: string }) {
const url = new URL(getGitHubAppManageURL("main"));
url.searchParams.set("state", input.pathname);
return url.toString();
}

export function getGitHubLightAppInstallUrl(input: { accountId: string }) {
const url = new URL(getGitHubAppManageURL("light"));
url.searchParams.set("state", JSON.stringify({ accountId: input.accountId }));
return url.toString();
}

Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/src/containers/GithubInstallationsSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { Popover } from "@/ui/Popover";
import { Select, SelectButton } from "@/ui/Select";

import { getInstallationUrl } from "./GitHub";
import { getGitHubAppManageURL } from "./GitHub";

const InstallationFragment = graphql(`
fragment GithubInstallationsSelect_GhApiInstallation on GhApiInstallation {
Expand Down Expand Up @@ -95,7 +95,7 @@ export const GithubInstallationsSelect = React.forwardRef<
})}
<ListBoxSeparator />
<ListBoxItem
href={getInstallationUrl(props.app)}
href={getGitHubAppManageURL(props.app)}
target="_blank"
textValue="Add GitHub Account"
>
Expand Down
7 changes: 5 additions & 2 deletions apps/frontend/src/containers/GithubRepositoryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { Time } from "@/ui/Time";

import { Query } from "./Apollo";
import { getInstallationUrl } from "./GitHub";
import { getGitHubAppManageURL } from "./GitHub";

const InstallationQuery = graphql(`
query GithubRepositoryList_ghApiInstallationRepositories(
Expand Down Expand Up @@ -186,7 +186,10 @@ export function GithubRepositoryList(props: {
<ListRow className="p-4 text-sm">
<div>
Repository not in the list?{" "}
<Link href={getInstallationUrl(props.app)} target="_blank">
<Link
href={getGitHubAppManageURL(props.app)}
target="_blank"
>
Manage repositories
</Link>
</div>
Expand Down
5 changes: 2 additions & 3 deletions apps/frontend/src/containers/Project/ConnectRepository.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { PageLoader } from "@/ui/PageLoader";
import { TextInput } from "@/ui/TextInput";
import { getItem, removeItem, setItem } from "@/util/storage";

import { getGitHubMainAppInstallUrl } from "../GitHub";
import { GitLabLogo } from "../GitLab";
import {
GitlabProjectList,
Expand Down Expand Up @@ -207,9 +208,7 @@ function GitHubButton(props: {
<LinkButton
variant="github"
size={props.size}
href={`${config.get(
"github.appUrl",
)}/installations/new?state=${encodeURIComponent(pathname)}`}
href={getGitHubMainAppInstallUrl({ pathname })}
>
<ButtonIcon>
<MarkGithubIcon />
Expand Down
11 changes: 3 additions & 8 deletions apps/frontend/src/containers/Team/GitHubLight.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from "react";
import { MarkGithubIcon } from "@primer/octicons-react";

import config from "@/config";
import { FragmentType, graphql, useFragment } from "@/gql";
import { LinkButton } from "@/ui/Button";
import {
Expand All @@ -13,6 +12,7 @@ import {
} from "@/ui/Card";
import { Link } from "@/ui/Link";

import { getGitHubAppManageURL, getGitHubLightAppInstallUrl } from "../GitHub";
import { AccountLink } from "../GithubAccountLink";

const TeamFragment = graphql(`
Expand Down Expand Up @@ -76,17 +76,12 @@ export function TeamGitHubLight(props: {
.
</p>
{team.githubLightInstallation ? (
<LinkButton
variant="secondary"
href={`${config.get("githubLight.appUrl")}/installations/new`}
>
<LinkButton variant="secondary" href={getGitHubAppManageURL("light")}>
Uninstall
</LinkButton>
) : (
<LinkButton
href={`${config.get(
"githubLight.appUrl",
)}/installations/new?state=${encodeURIComponent(JSON.stringify({ accountId: team.id }))}`}
href={getGitHubLightAppInstallUrl({ accountId: team.id })}
>
Install GitHub App
</LinkButton>
Expand Down

0 comments on commit 7962532

Please sign in to comment.