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

remove team button support #53199

Merged
merged 10 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions client/web/src/repo/blob/own/FileOwnershipEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ interface Props {
makeOwnerButton?: React.ReactElement
repoID: string
filePath: string
userID?: string
setRemoveOwnerError: any
refetch: any
}
Expand All @@ -43,7 +42,6 @@ export const FileOwnershipEntry: React.FunctionComponent<Props> = ({
makeOwnerButton,
repoID,
filePath,
userID,
refetch,
setRemoveOwnerError,
}) => {
Expand All @@ -57,6 +55,9 @@ export const FileOwnershipEntry: React.FunctionComponent<Props> = ({
return owner.email
}

const userID = owner.__typename === 'Person' && owner.user?.__typename === 'User' ? owner.user.id : undefined
const teamID = owner.__typename === 'Team' ? owner.id : undefined

const email = findEmail()

const assignedOwnerReasons: AssignedOwnerFields[] = reasons
Expand Down Expand Up @@ -119,7 +120,8 @@ export const FileOwnershipEntry: React.FunctionComponent<Props> = ({
onError={setRemoveOwnerError}
repoId={repoID}
path={filePath}
userId={userID}
userID={userID}
teamID={teamID}
isDirectAssigned={isDirectAssigned}
/>
))}
Expand Down
4 changes: 2 additions & 2 deletions client/web/src/repo/blob/own/OwnerList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export const OwnerList: React.FunctionComponent<OwnerListProps> = ({
)
)
.map((ownership, index) => {
if (ownership.owner.__typename === 'Team') {
}
coury-clark marked this conversation as resolved.
Show resolved Hide resolved
const userId =
ownership.owner.__typename === 'Person' &&
ownership.owner.user?.__typename === 'User'
Expand All @@ -162,7 +164,6 @@ export const OwnerList: React.FunctionComponent<OwnerListProps> = ({
<FileOwnershipEntry
refetch={refetch}
owner={ownership.owner}
userID={userId}
repoID={repoID}
filePath={filePath}
reasons={ownership.reasons}
Expand Down Expand Up @@ -211,7 +212,6 @@ export const OwnerList: React.FunctionComponent<OwnerListProps> = ({
owner={ownership.owner}
reasons={ownership.reasons}
makeOwnerButton={makeOwnerButton?.(userId)}
userID={userId}
repoID={repoID}
filePath={filePath}
refetch={refetch}
Expand Down
69 changes: 44 additions & 25 deletions client/web/src/repo/blob/own/RemoveOwnerButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ import { ErrorLike, asError } from '@sourcegraph/common'
import { useMutation } from '@sourcegraph/http-client'
import { Button, Icon, Tooltip } from '@sourcegraph/wildcard'

import { RemoveAssignedOwnerResult, RemoveAssignedOwnerVariables } from '../../../graphql-operations'
import {
RemoveAssignedOwnerResult,
RemoveAssignedOwnerVariables,
RemoveAssignedTeamResult,
RemoveAssignedTeamVariables,
} from '../../../graphql-operations'

import { REMOVE_ASSIGNED_OWNER } from './grapqlQueries'
import { REMOVE_ASSIGNED_OWNER, REMOVE_ASSIGNED_TEAM } from './grapqlQueries'

export interface RemoveOwnerButtonProps {
onSuccess: () => Promise<any>
onError: (e: Error) => void
repoId: string
path: string
userId?: string
userID?: string
teamID?: string
isDirectAssigned: boolean
}

Expand All @@ -24,35 +30,44 @@ export const RemoveOwnerButton: React.FC<RemoveOwnerButtonProps> = ({
onError,
repoId,
path,
userId,
userID,
teamID,
isDirectAssigned,
}) => {
const tooltipContent = !isDirectAssigned
? 'Ownership can only be modified at the same direct path as it was assigned.'
: 'Remove ownership'

const [removeAssignedOwner, { loading }] = useMutation<RemoveAssignedOwnerResult, RemoveAssignedOwnerVariables>(
REMOVE_ASSIGNED_OWNER,
{}
)
const [removeAssignedOwner, { loading: removeLoading }] = useMutation<
RemoveAssignedOwnerResult,
RemoveAssignedOwnerVariables
>(REMOVE_ASSIGNED_OWNER, {})
const [removeAssignedTeam, { loading: removeTeamLoading }] = useMutation<
RemoveAssignedTeamResult,
RemoveAssignedTeamVariables
>(REMOVE_ASSIGNED_TEAM, {})

const createInputObject = (id: string) => ({
variables: {
input: {
absolutePath: path,
assignedOwnerID: id,
repoID: repoId,
},
},
onCompleted: async () => {
await onSuccess()
},
onError: (errors: ErrorLike) => {
onError(asError(errors))
},
})

const removeOwner: () => Promise<void> = async () => {
if (userId) {
await removeAssignedOwner({
variables: {
input: {
absolutePath: path,
assignedOwnerID: userId,
repoID: repoId,
},
},
onCompleted: async () => {
await onSuccess()
},
onError: (errors: ErrorLike) => {
onError(asError(errors))
},
})
if (userID) {
await removeAssignedOwner(createInputObject(userID))
} else if (teamID) {
await removeAssignedTeam(createInputObject(teamID))
}
}

Expand All @@ -67,7 +82,11 @@ export const RemoveOwnerButton: React.FC<RemoveOwnerButtonProps> = ({
size="sm"
disabled={!isDirectAssigned}
>
<Icon color="white" aria-hidden={true} svgPath={loading ? mdiLoading : mdiDelete} />
<Icon
color="white"
aria-hidden={true}
svgPath={removeLoading || removeTeamLoading ? mdiLoading : mdiDelete}
/>
Remove owner
</Button>
</Tooltip>
Expand Down
9 changes: 9 additions & 0 deletions client/web/src/repo/blob/own/grapqlQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const OWNER_FIELDS = gql`
}
}
... on Team {
id
name
teamDisplayName: displayName
avatarURL
Expand Down Expand Up @@ -199,3 +200,11 @@ export const REMOVE_ASSIGNED_OWNER = gql`
}
}
`

export const REMOVE_ASSIGNED_TEAM = gql`
mutation RemoveAssignedTeam($input: AssignOwnerOrTeamInput!) {
removeAssignedTeam(input: $input) {
alwaysNil
}
}
`
6 changes: 5 additions & 1 deletion enterprise/cmd/frontend/internal/own/resolvers/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ func (r *ownResolver) computeAssignedTeams(ctx context.Context, db edb.Enterpris

fetchedTeams := make(map[int32]*types.Team)

isDirectMatch := false
for _, summary := range assignedTeamSummaries {
var team *types.Team
teamID := summary.OwnerTeamID
Expand All @@ -810,14 +811,17 @@ func (r *ownResolver) computeAssignedTeams(ctx context.Context, db edb.Enterpris
team = teamFromDB
fetchedTeams[teamID] = teamFromDB
}
if blob.Path() == summary.FilePath {
isDirectMatch = true
}
res := ownershipResolver{
db: db,
resolvedOwner: &codeowners.Team{
Team: team,
},
reasons: []*ownershipReasonResolver{
{
&assignedOwner{},
&assignedOwner{directMatch: isDirectMatch},
},
},
}
Expand Down