Skip to content

Commit

Permalink
feat: Adjust repo upload token settings with a new note (#3372)
Browse files Browse the repository at this point in the history
  • Loading branch information
RulaKhaled authored Oct 11, 2024
1 parent 3250f25 commit c22dc32
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import PropTypes from 'prop-types'
import { useState } from 'react'
import { useParams } from 'react-router'

import { useRegenerateRepoUploadToken } from 'services/repoUploadToken'
import { useUploadTokenRequired } from 'services/uploadTokenRequired'
import A from 'ui/A'
import Button from 'ui/Button'
import TokenWrapper from 'ui/TokenWrapper'
Expand All @@ -19,46 +21,61 @@ function useRegenerateToken() {
}

function RepoUploadToken({ uploadToken }) {
const { provider, owner } = useParams()
const [showModal, setShowModal] = useState(false)
const { mutate, isLoading } = useRegenerateToken()
const { data } = useUploadTokenRequired({
provider,
owner,
enabled: !!uploadToken,
})

if (!uploadToken) {
return null
}

return (
<div className="flex flex-col gap-2 border-2 border-ds-gray-primary p-4 sm:flex-row">
<div className="flex flex-1 flex-col gap-3">
<div className="flex flex-col">
<h3 className="font-semibold">Repository upload token</h3>
<span>
Used for uploading coverage reports{' '}
<A to={{ pageName: 'docs' }} isExternal>
learn more
</A>
</span>
</div>
<p>Add this token to your codecov.yml</p>
<TokenWrapper token={TokenFormatEnum.FIRST_FORMAT + uploadToken} />
<span className="font-semibold ">OR</span>
<p>
If you’d like to add the token directly to your CI/CD Environment:
<div className="flex flex-col gap-2">
{!data?.uploadTokenRequired ? (
<p className="mb-2">
Uploading with token is now not required. You can upload without a
token. Contact your admins to manage the global upload token settings.
</p>
<TokenWrapper token={TokenFormatEnum.SECOND_FORMAT + uploadToken} />
</div>
<div>
<Button
hook="show-modal"
onClick={() => setShowModal(true)}
disabled={isLoading}
>
Regenerate
</Button>
<RegenerateTokenModal
showModal={showModal}
closeModal={() => setShowModal(false)}
regenerateToken={mutate}
isLoading={isLoading}
/>
) : null}
<div className="flex flex-col gap-2 border-2 border-ds-gray-primary p-4 sm:flex-row">
<div className="flex flex-1 flex-col gap-3">
<div className="flex flex-col">
<h3 className="font-semibold">Repository upload token</h3>
<span>
Used for uploading coverage reports{' '}
<A to={{ pageName: 'docs' }} isExternal>
learn more
</A>
</span>
</div>
<p>Add this token to your codecov.yml</p>
<TokenWrapper token={TokenFormatEnum.FIRST_FORMAT + uploadToken} />
<span className="font-semibold ">OR</span>
<p>
If you’d like to add the token directly to your CI/CD Environment:
</p>
<TokenWrapper token={TokenFormatEnum.SECOND_FORMAT + uploadToken} />
</div>
<div>
<Button
hook="show-modal"
onClick={() => setShowModal(true)}
disabled={isLoading}
>
Regenerate
</Button>
<RegenerateTokenModal
showModal={showModal}
closeModal={() => setShowModal(false)}
regenerateToken={mutate}
isLoading={isLoading}
/>
</div>
</div>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ afterAll(() => {

describe('RepoUploadToken', () => {
function setup(
{ uploadToken = undefined, triggerError = false } = {
{
uploadToken = undefined,
triggerError = false,
uploadTokenRequired = false,
} = {
uploadToken: undefined,
triggerError: false,
uploadTokenRequired: false,
}
) {
const user = userEvent.setup()
Expand All @@ -62,6 +67,17 @@ describe('RepoUploadToken', () => {
mocks.useAddNotification.mockReturnValue(addNotification)

server.use(
graphql.query('GetUploadTokenRequired', () => {
return HttpResponse.json({
data: {
owner: {
orgUploadToken: 'test-mock-org-upload-token',
isAdmin: true,
uploadTokenRequired,
},
},
})
}),
graphql.mutation('RegenerateRepositoryUploadToken', (info) => {
mutate(info.request.variables)
if (triggerError) {
Expand Down Expand Up @@ -127,6 +143,28 @@ describe('RepoUploadToken', () => {
const regenerate = screen.getByRole('button', { name: 'Regenerate' })
expect(regenerate).toBeInTheDocument()
})

it('renders upload token required message when uploadTokenRequired is false', async () => {
setup({ uploadTokenRequired: false, uploadToken: 'some-random-token' })
render(<RepoUploadToken uploadToken="old token" />, { wrapper })

const message = await screen.findByText(
'Uploading with token is now not required. You can upload without a token. Contact your admins to manage the global upload token settings.'
)
expect(message).toBeInTheDocument()
})

it('does not render upload token required message when uploadTokenRequired is true', async () => {
setup({ uploadTokenRequired: true, uploadToken: 'some-random-token' })
render(<RepoUploadToken uploadToken="old token" />, { wrapper })

await waitFor(() => expect(queryClient.isFetching()).toBe(0))

const message = screen.queryByText(
'Uploading with token is now not required. You can upload without a token. Contact your admins to manage the global upload token settings.'
)
expect(message).not.toBeInTheDocument()
})
})

describe('when the user clicks on regenerate button', () => {
Expand Down

0 comments on commit c22dc32

Please sign in to comment.