Skip to content

Commit

Permalink
fix: Pull Request labels (harness#928)
Browse files Browse the repository at this point in the history
* fix: fix PR labels
  • Loading branch information
3em authored Feb 4, 2025
1 parent 29173a6 commit 2cef2fe
Show file tree
Hide file tree
Showing 33 changed files with 872 additions and 446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
CommitSuggestion,
EnumCheckStatus,
EnumPullReqReviewDecision,
ILabelType,
LabelAssignmentType,
PullReqReviewDecision,
PullRequestCommentBox,
PullRequestFilters,
Expand All @@ -22,7 +24,6 @@ import {

import {
changesInfoData,
Label,
mockActivities,
mockLabelList,
mockPrLabels,
Expand Down Expand Up @@ -85,11 +86,12 @@ const PullRequestConversation: FC<PullRequestConversationProps> = ({ state }) =>

const searchReviewers = ''

const labelsList: Label[] = state === 'complex-1' ? mockLabelList : []
const PRLabels = state === 'complex-1' ? mockPrLabels : { label_data: [] as Label[] }
const labelsList: ILabelType[] = state === 'complex-1' ? mockLabelList : []
const PRLabels = state === 'complex-1' ? mockPrLabels : { label_data: [] as LabelAssignmentType[] }
const searchLabel = ''
const pullReqMetadata = { source_sha: '' }
const reviewers = state === 'complex-1' ? mockReviewers : undefined

return (
<>
<CommitSuggestionsDialog
Expand Down Expand Up @@ -236,26 +238,12 @@ const PullRequestConversation: FC<PullRequestConversationProps> = ({ state }) =>
reviewers={reviewers}
searchQuery={searchReviewers}
setSearchQuery={noop}
labelsList={labelsList?.map(label => {
return {
id: label.id,
key: label.key,
color: label.color
}
})}
PRLabels={PRLabels?.label_data?.map(label => {
return {
id: label.id,
key: label.key,
color: label.color
}
})}
labelsList={labelsList}
PRLabels={PRLabels?.label_data}
searchLabelQuery={searchLabel}
setSearchLabelQuery={noop}
addLabel={noop}
removeLabel={noop}
addLabelError={undefined}
removeLabelError={undefined}
useTranslationStore={useTranslationsStore}
/>
</SandboxLayout.Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { noop } from '@utils/viewUtils'

import {
ColorsEnum,
EnumCheckPayloadKind,
EnumCheckStatus,
EnumPrincipalType,
EnumPullReqActivityKind,
EnumPullReqActivityType,
EnumPullReqReviewDecision,
ILabelType,
LabelAssignmentType,
LabelType,
TypesPullReqActivity
} from '@harnessio/ui/views'

Expand Down Expand Up @@ -874,15 +878,15 @@ export const mockReviewers = [
}
]

export const mockLabelList = [
export const mockLabelList: ILabelType[] = [
{
id: 1,
space_id: 3,
scope: 1,
key: 'P0',
description: '',
type: 'static',
color: 'red',
type: LabelType.STATIC,
color: ColorsEnum.BLUE,
value_count: 0,
created: 1723740581843,
updated: 1723740581843,
Expand All @@ -895,8 +899,8 @@ export const mockLabelList = [
scope: 0,
key: 's',
description: '',
type: 'static',
color: 'blue',
type: LabelType.DYNAMIC,
color: ColorsEnum.CYAN,
value_count: 0,
created: 1736929623159,
updated: 1736929623159,
Expand All @@ -919,7 +923,7 @@ export interface PRLabelsData {
updated: number
}
}>
label_data: Label[]
label_data: LabelAssignmentType[]
}

export const mockPrLabels: PRLabelsData = {
Expand All @@ -941,10 +945,15 @@ export const mockPrLabels: PRLabelsData = {
label_data: [
{
scope: 1,
id: 1,
type: 'static',
key: 'P0',
color: 'red'
id: 9,
type: LabelType.DYNAMIC,
key: '111',
color: ColorsEnum.RED,
assigned_value: {
id: 10,
value: 'newwwww',
color: ColorsEnum.RED
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
import { noop } from '@utils/viewUtils'

export const pullRequestListStore = {
import { ColorsEnum, PullRequestListStore } from '@harnessio/ui/views'

export const pullRequestListStore: PullRequestListStore = {
pullRequests: [
{
author: 'Administrator',
name: 'feat: create skeleton',
reviewRequired: true,
is_draft: false,
merged: null,
name: 'feat: create skeleton',
number: 34,
is_draft: false,
timestamp: 'Nov 14, 2024',
updated: 1731605604491,
sha: '1231',
author: 'Administrator',
reviewRequired: true,
sourceBranch: 'changes',
targetBranch: 'main',
state: 'open'
timestamp: 'Nov 14, 2024',
state: 'open',
updated: 1731605604491,
labels: [
{
color: ColorsEnum.CYAN,
key: 'label-1',
value: 'value-1'
},
{
color: ColorsEnum.RED,
key: 'label-2',
value: 'value-2'
}
]
}
],
totalPages: 1,
page: 1,
openPullReqs: 1,
closedPullReqs: 0,
setPage: noop,
setSearchQuery: null
setPage: noop
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { pullRequestListStore } from './pull-request-list-store'
const PullRequestListWrapper: FC<Partial<PullRequestPageProps>> = props => {
const usePullRequestListStore = useCallback(
() => ({
...pullRequestListStore,
setPage: noop
...pullRequestListStore
}),
[]
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { noop } from '@utils/viewUtils'

import { BranchSelectorTab, IBranchSelectorStore } from '@harnessio/ui/views'
import { BranchSelectorTab, ColorsEnum, IBranchSelectorStore } from '@harnessio/ui/views'

export const repoBranchesStore: IBranchSelectorStore = {
setBranchList: noop,
Expand Down Expand Up @@ -109,7 +109,8 @@ export const repoBranchesStore: IBranchSelectorStore = {
is_draft: true,
merged: null,
timestamp: 'Jan 13, 2025',
reviewRequired: false
reviewRequired: false,
labels: []
}
]
},
Expand Down Expand Up @@ -173,7 +174,19 @@ export const repoBranchesStore: IBranchSelectorStore = {
timestamp: 'Jan 13, 2025',
reviewRequired: false,
state: 'open',
is_draft: false
is_draft: false,
labels: [
{
color: ColorsEnum.CYAN,
key: 'label-1',
value: 'value-1'
},
{
color: ColorsEnum.RED,
key: 'label-2',
value: 'value-2'
}
]
}
]
},
Expand Down Expand Up @@ -210,7 +223,19 @@ export const repoBranchesStore: IBranchSelectorStore = {
timestamp: 'Jan 13, 2025',
reviewRequired: false,
state: 'open',
is_draft: false
is_draft: false,
labels: [
{
color: ColorsEnum.CYAN,
key: 'label-1',
value: 'value-1'
},
{
color: ColorsEnum.RED,
key: 'label-2',
value: 'value-2'
}
]
}
]
},
Expand Down Expand Up @@ -274,7 +299,19 @@ export const repoBranchesStore: IBranchSelectorStore = {
reviewRequired: false,
updated: 1737045230646,
state: 'open',
is_draft: false
is_draft: false,
labels: [
{
color: ColorsEnum.CYAN,
key: 'label-1',
value: 'value-1'
},
{
color: ColorsEnum.RED,
key: 'label-2',
value: 'value-2'
}
]
}
]
},
Expand Down Expand Up @@ -311,7 +348,19 @@ export const repoBranchesStore: IBranchSelectorStore = {
timestamp: 'Jan 13, 2025',
reviewRequired: false,
state: 'open',
is_draft: true
is_draft: true,
labels: [
{
color: ColorsEnum.CYAN,
key: 'label-1',
value: 'value-1'
},
{
color: ColorsEnum.RED,
key: 'label-2',
value: 'value-2'
}
]
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { useCallback, useMemo, useState } from 'react'

import { useAssignLabelMutation, useListLabelsQuery, useUnassignLabelMutation } from '@harnessio/code-service-client'
import { HandleAddLabelType, LabelAssignmentType } from '@harnessio/ui/views'

import { useGetRepoLabelAndValuesData } from '../../repo/labels/hooks/use-get-repo-label-and-values-data'

interface UsePrConversationLabelsProps {
repoRef: string
prId: number
refetchData: () => void
}

/**
* Hook that encapsulates all label-related operations
*/
export const usePrConversationLabels = ({ repoRef, prId, refetchData }: UsePrConversationLabelsProps) => {
const [searchLabel, setSearchLabel] = useState('')

const changeSearchLabel = useCallback((data: string) => {
setSearchLabel(data)
}, [])

const { labels, values: labelsValues } = useGetRepoLabelAndValuesData({
query: searchLabel,
inherited: true,
limit: 100
})

const { data: { body: prLabels } = {}, refetch: refetchPRLabels } = useListLabelsQuery({
repo_ref: repoRef,
pullreq_number: prId,
queryParams: {}
})

const handleOnSuccess = () => {
refetchPRLabels()
refetchData()
}

const { mutate: addLabel } = useAssignLabelMutation(
{
repo_ref: repoRef,
pullreq_number: prId
},
{
onSuccess: handleOnSuccess
}
)

const { mutate: removeLabel } = useUnassignLabelMutation(
{
repo_ref: repoRef,
pullreq_number: prId
},
{
onSuccess: handleOnSuccess
}
)

const handleAddLabel = useCallback((body: HandleAddLabelType) => addLabel({ body }), [addLabel])

const handleRemoveLabel = useCallback((label_id: number) => removeLabel({ label_id }), [removeLabel])

const appliedLabels = useMemo(() => {
return (prLabels?.label_data || []) as LabelAssignmentType[]
}, [prLabels])

return {
searchLabel,
changeSearchLabel,
labels,
labelsValues,
handleAddLabel,
handleRemoveLabel,
appliedLabels
}
}
Loading

0 comments on commit 2cef2fe

Please sign in to comment.