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

fix: Fix link to custom gh app for self-hosted #3492

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import mapKeys from 'lodash/mapKeys'

export const DEFAULT_GH_APP = 'codecov'

const defaultConfig = {
API_URL: '',
STRIPE_KEY: '',
Expand All @@ -8,7 +10,7 @@ const defaultConfig = {
SENTRY_PROFILING_SAMPLE_RATE: 0.1,
SENTRY_SESSION_SAMPLE_RATE: 0.1,
SENTRY_ERROR_SAMPLE_RATE: 1.0,
GH_APP: 'codecov',
GH_APP: DEFAULT_GH_APP,
GH_APP_AI: 'codecov-ai',
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('useStaticNavLinks', () => {
${links.uploaderCLI} | ${'https://docs.codecov.com/docs/codecov-uploader#using-the-cli-to-upload-reports-with-codecovio-cloud'}
${links.integrityCheck} | ${'https://docs.codecov.com/docs/codecov-uploader#integrity-checking-the-uploader'}
${links.codecovGithubApp} | ${'https://github.com/apps/codecov'}
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe replace this one too? Not sure where used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah good catch. I updated to pull from the var (which is set to codecov by default). It's used in some banners it looks like - I won't hide in those cases since the close-enough link is probably better than no link

${links.codecovGithubAppSelectTarget} | ${'https://github.com/apps/codecov/installations/select_target'}
${links.codecovGithubAppSelectTarget} | ${`https://github.com/apps/${config.GH_APP}/installations/select_target`}
${links.teamBot} | ${'https://docs.codecov.com/docs/team-bot'}
${links.runtimeInsights} | ${'https://docs.codecov.com/docs/runtime-insights'}
${links.graphAuthorization} | ${'https://docs.codecov.com/reference/authorization#about-graphs'}
Expand Down
3 changes: 2 additions & 1 deletion src/services/navigation/useNavLinks/useStaticNavLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ export function useStaticNavLinks() {
openNewTab: true,
},
codecovGithubAppSelectTarget: {
path: () => 'https://github.com/apps/codecov/installations/select_target',
path: () =>
`https://github.com/apps/${config.GH_APP}/installations/select_target`,
isExternalLink: true,
text: 'Codecov Github App',
openNewTab: true,
Expand Down
10 changes: 9 additions & 1 deletion src/ui/ContextSwitcher/ContextSwitcher.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { useParams } from 'react-router-dom'
import { useIntersection } from 'react-use'
import useClickAway from 'react-use/lib/useClickAway'

import config, { DEFAULT_GH_APP } from 'config'

import { useUpdateDefaultOrganization } from 'services/defaultOrganization'
import { providerToName } from 'shared/utils/provider'
import A from 'ui/A'
Expand Down Expand Up @@ -129,6 +131,12 @@ function ContextSwitcher({
const defaultOrgUsername = currentUser?.defaultOrgUsername

const isGh = providerToName(provider) === 'Github'
const isSelfHosted = config.IS_SELF_HOSTED
const isCustomGitHubApp = config.GH_APP !== DEFAULT_GH_APP

// self-hosted cannot use default "codecov" app (must set up custom one)
const shouldShowGitHubInstallLink =
isGh && (isSelfHosted ? isCustomGitHubApp : true)

return (
<div id="context-switcher" className="relative text-sm" ref={wrapperRef}>
Expand Down Expand Up @@ -167,7 +175,7 @@ function ContextSwitcher({
role="listbox"
aria-labelledby="listbox-label"
>
{isGh ? (
{shouldShowGitHubInstallLink ? (
<li className="flex justify-between border-b border-ds-border-line px-4 py-3">
<A to={{ pageName: 'codecovAppInstallation' }}>
<Icon name="plus-circle" />
Expand Down
105 changes: 105 additions & 0 deletions src/ui/ContextSwitcher/ContextSwitcher.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { graphql, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { MemoryRouter, Route, Switch } from 'react-router-dom'

import config, { DEFAULT_GH_APP } from 'config'

import { useImage } from 'services/image'

import ContextSwitcher from './ContextSwitcher'
Expand Down Expand Up @@ -690,4 +692,107 @@ describe('ContextSwitcher', () => {
})
})
})

describe('when on self-hosted', () => {
beforeEach(() => {
config.IS_SELF_HOSTED = true
setup()
})

afterEach(() => {
config.GH_APP = DEFAULT_GH_APP
vi.clearAllMocks()
})

it('renders the custom app link if set', async () => {
config.GH_APP = 'custom-app'
render(
<ContextSwitcher
activeContext={{
username: 'laudna',
avatarUrl: 'http://127.0.0.1/avatar-url',
}}
contexts={[
{
owner: {
username: 'laudna',
avatarUrl: 'https://github.com/laudna.png?size=40',
},
pageName: 'owner',
},
{
owner: {
username: 'spotify',
avatarUrl: 'https://github.com/spotify.png?size=40',
},
pageName: 'owner',
},
]}
currentUser={{
defaultOrgUsername: 'spotify',
}}
src="imageUrl"
isLoading={false}
error={null}
/>,
{
wrapper: wrapper(),
}
)

const installCopy = await screen.findByText(/Install Codecov GitHub app/)
expect(installCopy).toBeInTheDocument()
expect(installCopy).toHaveAttribute(
'href',
'https://github.com/apps/custom-app/installations/new'
)
expect(installCopy).not.toHaveAttribute(
'href',
'https://github.com/apps/codecov/installations/new'
)
})

it('renders no link if custom app env var is not set', async () => {
config.IS_SELF_HOSTED = true
render(
<ContextSwitcher
activeContext={{
username: 'laudna',
avatarUrl: 'http://127.0.0.1/avatar-url',
}}
contexts={[
{
owner: {
username: 'laudna',
avatarUrl: 'https://github.com/laudna.png?size=40',
},
pageName: 'owner',
},
{
owner: {
username: 'spotify',
avatarUrl: 'https://github.com/spotify.png?size=40',
},
pageName: 'owner',
},
]}
currentUser={{
defaultOrgUsername: 'spotify',
}}
src="imageUrl"
isLoading={false}
error={null}
/>,
{
wrapper: wrapper(),
}
)

const installCopy = screen.queryByText(/Install Codecov GitHub app/)
expect(installCopy).not.toBeInTheDocument()

const switchCopy = await screen.findByText(/Switch context/)
expect(switchCopy).toBeInTheDocument()
})
})
})
Loading