fix: [CODE-1577] include pr author in browser param in PR listing page (#2868)

* fix: [CODE-1577] remove redundant code
* refactor: [code-1577] address comments
* fix: [code-1577] lint
* fix: [CODE-1577] address comments
* fix: [code-1577] lint
* fix: [code-1577] lint
* fix: [code-1577] lint
* fix: [code-1577] lint
* feat: [code-1577] add author in URL
This commit is contained in:
Ritik Kapoor 2024-11-06 10:20:51 +00:00 committed by Harness
parent 1c43fbde25
commit 68db486133
6 changed files with 136 additions and 5 deletions

View File

@ -26,6 +26,10 @@ import (
"github.com/swaggest/openapi-go/openapi3"
)
type principalInfoRequest struct {
ID int64 `path:"id"`
}
var QueryParameterQueryPrincipals = openapi3.ParameterOrRef{
Parameter: &openapi3.Parameter{
Name: request.QueryParamQuery,
@ -74,4 +78,16 @@ func buildPrincipals(reflector *openapi3.Reflector) {
_ = reflector.SetJSONResponse(&opList, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opList, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodGet, "/principals", opList)
getPrincipal := openapi3.Operation{}
getPrincipal.WithTags("principals")
getPrincipal.WithMapOfAnything(map[string]interface{}{"operationId": "getPrincipal"})
_ = reflector.SetRequest(&getPrincipal, new(principalInfoRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&getPrincipal, new(types.PrincipalInfo), http.StatusOK)
_ = reflector.SetJSONResponse(&getPrincipal, new(usererror.Error), http.StatusBadRequest)
_ = reflector.SetJSONResponse(&getPrincipal, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&getPrincipal, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&getPrincipal, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&getPrincipal, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodGet, "/principals/{id}", getPrincipal)
}

View File

@ -52,7 +52,12 @@ import { usePageIndex } from 'hooks/usePageIndex'
import { useGetSpaceParam } from 'hooks/useGetSpaceParam'
import { useUpdateQueryParams } from 'hooks/useUpdateQueryParams'
import { useQueryParams } from 'hooks/useQueryParams'
import type { TypesPullReq, RepoRepositoryOutput, TypesLabelPullReqAssignmentInfo } from 'services/code'
import type {
TypesPullReq,
RepoRepositoryOutput,
TypesLabelPullReqAssignmentInfo,
TypesPrincipalInfo
} from 'services/code'
import { ResourceListingPagination } from 'components/ResourceListingPagination/ResourceListingPagination'
import { NoResultCard } from 'components/NoResultCard/NoResultCard'
import { PipeSeparator } from 'components/PipeSeparator/PipeSeparator'
@ -62,6 +67,7 @@ import { LoadingSpinner } from 'components/LoadingSpinner/LoadingSpinner'
import useSpaceSSE from 'hooks/useSpaceSSE'
import { TimePopoverWithLocal } from 'utils/timePopoverLocal/TimePopoverWithLocal'
import { Label } from 'components/Label/Label'
import { getConfig } from 'services/config'
import { PullRequestsContentHeader } from './PullRequestsContentHeader/PullRequestsContentHeader'
import css from './PullRequests.module.scss'
@ -70,20 +76,32 @@ const SSE_EVENTS = ['pullreq_updated']
export default function PullRequests() {
const { getString } = useStrings()
const history = useHistory()
const { routes, hooks, standalone } = useAppContext()
const { routes, hooks, standalone, routingId } = useAppContext()
const { CODE_PULLREQ_LABELS: isLabelEnabled } = hooks?.useFeatureFlags()
const [searchTerm, setSearchTerm] = useState<string | undefined>()
const browserParams = useQueryParams<PageBrowserProps>()
const [filter, setFilter] = useState(browserParams?.state || (PullRequestFilterOption.OPEN as string))
const [authorFilter, setAuthorFilter] = useState<string>()
const [authorFilter, setAuthorFilter] = useState<string>(browserParams?.author ?? '')
const [labelFilter, setLabelFilter] = useState<LabelFilterObj[]>([])
const space = useGetSpaceParam()
const { updateQueryParams, replaceQueryParams } = useUpdateQueryParams()
const pageInit = browserParams.page ? parseInt(browserParams.page) : 1
const [page, setPage] = usePageIndex(pageInit)
const { data: principal, refetch: refetchPrincipal } = useGet<TypesPrincipalInfo>({
base: getConfig('code/api/v1'),
path: `/principals/${browserParams.author}`,
queryParams: {
accountIdentifier: routingId
},
lazy: true,
debounce: 500
})
useEffect(() => {
const params = {
...browserParams,
...(Boolean(authorFilter) && { author: authorFilter }),
...(page > 1 && { page: page.toString() }),
...(filter && { state: filter })
}
@ -94,7 +112,18 @@ export default function PullRequests() {
delete updateParams.page
replaceQueryParams(updateParams, undefined, true)
}
}, [page, filter]) // eslint-disable-line react-hooks/exhaustive-deps
if (!authorFilter && browserParams.author) {
const paramList = { ...params }
delete paramList.author
replaceQueryParams(paramList, undefined, true)
}
if (browserParams.author) {
refetchPrincipal()
}
}, [page, filter, authorFilter]) // eslint-disable-line react-hooks/exhaustive-deps
const { repoMetadata, error, loading, refetch } = useGetRepositoryMetadata()
const {
data,
@ -385,6 +414,7 @@ export default function PullRequests() {
setSearchTerm(value)
setPage(1)
}}
activePullRequestAuthorObj={principal}
activePullRequestAuthorFilterOption={authorFilter}
activePullRequestLabelFilterOption={labelFilter}
onPullRequestAuthorFilterChanged={_authorFilter => {

View File

@ -45,6 +45,7 @@ interface PullRequestsContentHeaderProps extends Pick<GitInfoProps, 'repoMetadat
loading?: boolean
activePullRequestFilterOption?: string
activePullRequestAuthorFilterOption?: string
activePullRequestAuthorObj?: TypesPrincipalInfo | null
activePullRequestLabelFilterOption?: LabelFilterObj[]
onPullRequestFilterChanged: React.Dispatch<React.SetStateAction<string>>
onPullRequestAuthorFilterChanged: (authorFilter: string) => void
@ -60,6 +61,7 @@ export function PullRequestsContentHeader({
onSearchTermChanged,
activePullRequestFilterOption = PullRequestFilterOption.OPEN,
activePullRequestAuthorFilterOption,
activePullRequestAuthorObj,
activePullRequestLabelFilterOption,
repoMetadata
}: PullRequestsContentHeaderProps) {
@ -158,7 +160,11 @@ export function PullRequestsContentHeader({
}
}
)
const authorsList = await moveCurrentUserToTop(fetchedAuthors, currentUser, query)
const authors = [...fetchedAuthors, ...(activePullRequestAuthorObj ? [activePullRequestAuthorObj] : [])]
const authorsList = await moveCurrentUserToTop(authors, currentUser, query)
const updatedAuthorsList = Array.isArray(authorsList)
? ([
...(authorsList || []).map(item => ({

View File

@ -2580,6 +2580,36 @@ export const useListPrincipals = (props: UseListPrincipalsProps) =>
...props
})
export interface GetPrincipalPathParams {
id: number
}
export type GetPrincipalProps = Omit<
GetProps<TypesPrincipalInfo, UsererrorError, void, GetPrincipalPathParams>,
'path'
> &
GetPrincipalPathParams
export const GetPrincipal = ({ id, ...props }: GetPrincipalProps) => (
<Get<TypesPrincipalInfo, UsererrorError, void, GetPrincipalPathParams>
path={`/principals/${id}`}
base={getConfig('code/api/v1')}
{...props}
/>
)
export type UseGetPrincipalProps = Omit<
UseGetProps<TypesPrincipalInfo, UsererrorError, void, GetPrincipalPathParams>,
'path'
> &
GetPrincipalPathParams
export const useGetPrincipal = ({ id, ...props }: UseGetPrincipalProps) =>
useGet<TypesPrincipalInfo, UsererrorError, void, GetPrincipalPathParams>(
(paramsInPath: GetPrincipalPathParams) => `/principals/${paramsInPath.id}`,
{ base: getConfig('code/api/v1'), pathParams: { id }, ...props }
)
export interface OnRegisterQueryParams {
/**
* If set to true the token is also returned as a cookie.

View File

@ -1042,6 +1042,54 @@ paths:
description: Internal Server Error
tags:
- principals
/principals/{id}:
get:
operationId: getPrincipal
parameters:
- in: path
name: id
required: true
schema:
type: integer
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/TypesPrincipalInfo'
description: OK
'400':
content:
application/json:
schema:
$ref: '#/components/schemas/UsererrorError'
description: Bad Request
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/UsererrorError'
description: Unauthorized
'403':
content:
application/json:
schema:
$ref: '#/components/schemas/UsererrorError'
description: Forbidden
'404':
content:
application/json:
schema:
$ref: '#/components/schemas/UsererrorError'
description: Not Found
'500':
content:
application/json:
schema:
$ref: '#/components/schemas/UsererrorError'
description: Internal Server Error
tags:
- principals
/register:
post:
operationId: onRegister

View File

@ -99,6 +99,7 @@ export const getErrorMessage = (error: Unknown): string | undefined =>
error ? get(error, 'data.error', get(error, 'data.message', get(error, 'message', error))) : undefined
export interface PageBrowserProps {
author?: string
page?: string
state?: string
tab?: string