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: use private org for location when set #181

Merged
merged 3 commits into from
Jun 18, 2024
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
24 changes: 18 additions & 6 deletions src/app/[organizationId]/forks/[forkId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ const Fork = () => {
const start = pageIndex * pageSize
const end = start + pageSize

const {
data: getConfigData,
isLoading: configLoading,
error: configError,
} = trpc.getConfig.useQuery({
orgId: organizationId as string,
})

const orgLogin = getConfigData?.privateOrg ?? orgData?.login

const {
data: createMirrorData,
isLoading: createMirrorLoading,
Expand Down Expand Up @@ -267,7 +277,7 @@ const Fork = () => {
await deleteMirror({
mirrorName,
orgId: String(orgData?.id),
orgName: orgData?.name ?? '',
orgName: orgLogin ?? '',
}).then((res) => {
if (!res.success) {
openDeleteErrorFlash()
Expand All @@ -287,7 +297,7 @@ const Fork = () => {
)

// show loading table
if (!mirrors || mirrorsLoading) {
if (!mirrors || mirrorsLoading || configLoading) {
return (
<Box>
<ForkHeader forkData={forkData} />
Expand Down Expand Up @@ -361,6 +371,9 @@ const Fork = () => {
<Box sx={{ marginBottom: '10px' }}>
{deleteMirrorLoading && <Loading message="Deleting mirror..." />}
</Box>
<Box sx={{ marginBottom: '10px' }}>
{configError && <ErrorFlash message={configError.message} />}
</Box>
<Box sx={{ marginBottom: '10px' }}>
{listMirrorsError && <ErrorFlash message={listMirrorsError.message} />}
</Box>
Expand Down Expand Up @@ -549,15 +562,15 @@ const Fork = () => {
</Table.Container>
)}
<CreateMirrorDialog
orgLogin={orgData?.login as string}
orgLogin={orgLogin ?? ''}
forkParentName={forkData?.parent?.name as string}
forkParentOwnerLogin={forkData?.parent?.owner.login as string}
closeDialog={closeCreateDialog}
isOpen={isCreateDialogOpen}
createMirror={handleOnCreateMirror}
/>
<EditMirrorDialog
orgLogin={orgData?.login as string}
orgLogin={orgLogin ?? ''}
forkParentName={forkData?.parent?.name as string}
forkParentOwnerLogin={forkData?.parent?.owner.login as string}
orgId={organizationId as string}
Expand All @@ -567,9 +580,8 @@ const Fork = () => {
editMirror={handleOnEditMirror}
/>
<DeleteMirrorDialog
orgLogin={orgData?.login as string}
orgLogin={orgLogin ?? ''}
orgId={organizationId as string}
orgName={orgData?.name as string}
mirrorName={deleteMirrorName as string}
closeDialog={closeDeleteDialog}
isOpen={Boolean(deleteMirrorName)}
Expand Down
2 changes: 2 additions & 0 deletions src/app/api/trpc/trpc-router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import configRouter from 'server/config/router'
import gitRouter from '../../../server/git/router'
import octokitRouter from '../../../server/octokit/router'
import reposRouter from '../../../server/repos/router'
Expand All @@ -13,6 +14,7 @@ export const appRouter = t.mergeRouters(
reposRouter,
octokitRouter,
gitRouter,
configRouter,
healthCheckerRouter,
)

Expand Down
6 changes: 2 additions & 4 deletions src/app/components/dialog/DeleteMirrorDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import { Dialog } from '@primer/react/lib-esm/drafts'
interface DeleteMirrorDialogProps {
orgLogin: string
orgId: string
orgName: string
mirrorName: string
isOpen: boolean
closeDialog: () => void
deleteMirror: (data: {
orgId: string
orgName: string
orgLogin: string
mirrorName: string
}) => void
}

export const DeleteMirrorDialog = ({
orgLogin,
orgId,
orgName,
mirrorName,
isOpen,
closeDialog,
Expand All @@ -36,7 +34,7 @@ export const DeleteMirrorDialog = ({
{
content: 'Delete',
variant: 'danger',
onClick: () => deleteMirror({ orgId, orgName, mirrorName }),
onClick: () => deleteMirror({ orgId, orgLogin, mirrorName }),
},
]}
onClose={closeDialog}
Expand Down
30 changes: 30 additions & 0 deletions src/server/config/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { TRPCError } from '@trpc/server'
import { getConfig } from '../../bot/config'
import { logger } from '../../utils/logger'
import { GetConfigSchema } from './schema'

const configApiLogger = logger.getSubLogger({ name: 'org-api' })

// Get the config values for the given org
export const getConfigHandler = async ({
input,
}: {
input: GetConfigSchema
}) => {
try {
configApiLogger.info('Fetching config', { ...input })

const config = await getConfig(input.orgId)

configApiLogger.debug('Fetched config', config)

return config
} catch (error) {
configApiLogger.error('Error fetching config', { error })

throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: (error as Error).message,
})
Comment on lines +14 to +28
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we add a test for this?

}
}
11 changes: 11 additions & 0 deletions src/server/config/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { procedure, router } from '../../utils/trpc-server'
import { getConfigHandler } from './controller'
import { GetConfigSchema } from './schema'

const configRouter = router({
getConfig: procedure
.input(GetConfigSchema)
.query(({ input }) => getConfigHandler({ input })),
})

export default configRouter
7 changes: 7 additions & 0 deletions src/server/config/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from 'zod'

export const GetConfigSchema = z.object({
orgId: z.string(),
})

export type GetConfigSchema = z.TypeOf<typeof GetConfigSchema>
37 changes: 37 additions & 0 deletions test/server/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as config from '../../src/bot/config'
import * as auth from '../../src/utils/auth'
import configRouter from '../../src/server/config/router'
import { Octomock } from '../octomock'
import { createTestContext } from '../utils/auth'

const om = new Octomock()

jest.mock('../../src/bot/config')

jest.spyOn(auth, 'checkGitHubAuth').mockResolvedValue()

describe('Config router', () => {
beforeEach(() => {
om.resetMocks()
jest.resetAllMocks()
})

it('should fetch the values from the config', async () => {
const caller = configRouter.createCaller(createTestContext())

const configSpy = jest.spyOn(config, 'getConfig').mockResolvedValue({
publicOrg: 'github',
privateOrg: 'github-test',
})

const res = await caller.getConfig({
orgId: 'github',
})

expect(res).toEqual({
publicOrg: 'github',
privateOrg: 'github-test',
})
expect(configSpy).toHaveBeenCalledTimes(1)
})
})
Loading