659 lines
28 KiB
Vue
659 lines
28 KiB
Vue
<script setup lang="ts">
|
|
import { useLocalization } from '@/composables/useLocalization';
|
|
import {
|
|
Card,
|
|
CardContent,
|
|
CardDescription,
|
|
CardHeader,
|
|
CardTitle,
|
|
} from '@/components/ui/card';
|
|
|
|
import { Link, useForm, usePage } from '@inertiajs/vue3';
|
|
import { CheckCircle2, GitBranchPlus, XCircle } from 'lucide-vue-next';
|
|
import { computed, ref, watch } from 'vue';
|
|
import ProjectTicketBranchController from '@/actions/App/Http/Controllers/ProjectTicketBranchController';
|
|
import FormFeedback from '@/components/FormFeedback.vue';
|
|
import FeedbackState from '@/components/FeedbackState.vue';
|
|
import InputError from '@/components/InputError.vue';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import { Button } from '@/components/ui/button';
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from '@/components/ui/dialog';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Label } from '@/components/ui/label';
|
|
import { useFormProtection } from '@/composables/useFormProtection';
|
|
import { edit as editGitConnections } from '@/routes/git-connections';
|
|
import type { BranchNameValidationModel } from '@/types/models/branch-name-validation-model';
|
|
import type { RepositoryConnectionModel } from '@/types/models/repository-connection-model';
|
|
import type {
|
|
TicketDetailGitProps,
|
|
TicketBranchFormData,
|
|
} from '@/types/ticket-detail';
|
|
const props = defineProps<TicketDetailGitProps>();
|
|
const { t } = useLocalization();
|
|
const appName = String(usePage().props.name);
|
|
const draftKey = `projects:${props.project.id}:tickets:${props.ticket.id}:edit`;
|
|
const visibleRepositoryBranches = computed(() =>
|
|
props.repositoryBranches.filter(
|
|
(repositoryBranch) =>
|
|
repositoryBranch.branches.length > 0 ||
|
|
repositoryBranch.error !== null ||
|
|
!repositoryBranch.repository_connection.has_personal_access,
|
|
),
|
|
);
|
|
|
|
const visibleRepositoryPullRequests = computed(() =>
|
|
props.repositoryPullRequests.filter(
|
|
(repositoryPullRequest) =>
|
|
repositoryPullRequest.pull_requests.length > 0 ||
|
|
repositoryPullRequest.error !== null ||
|
|
!repositoryPullRequest.repository_connection.has_personal_access,
|
|
),
|
|
);
|
|
|
|
const linkedRepositoryConnections = computed<RepositoryConnectionModel[]>(() =>
|
|
props.repositoryBranches.map(
|
|
(repositoryBranch) => repositoryBranch.repository_connection,
|
|
),
|
|
);
|
|
|
|
const canCreateBranches = computed(
|
|
() => props.canEditTicket && linkedRepositoryConnections.value.length > 0,
|
|
);
|
|
const defaultBranchRepositoryConnectionId = computed(() =>
|
|
String(
|
|
linkedRepositoryConnections.value.find(
|
|
(repositoryConnection) => repositoryConnection.has_personal_access,
|
|
)?.id ??
|
|
linkedRepositoryConnections.value[0]?.id ??
|
|
'',
|
|
),
|
|
);
|
|
|
|
const isCreateBranchDialogOpen = ref(false);
|
|
const isCheckingBranchName = ref(false);
|
|
const branchNameValidation = ref<BranchNameValidationModel | null>(null);
|
|
|
|
const branchCreationForm = useForm<TicketBranchFormData>(`${draftKey}:branch`, {
|
|
branch_name: props.suggestedBranchName,
|
|
repository_connection_id: defaultBranchRepositoryConnectionId.value,
|
|
});
|
|
const branchProtection = useFormProtection(branchCreationForm, {
|
|
isDirty: () => isCreateBranchDialogOpen.value && branchCreationForm.isDirty,
|
|
});
|
|
|
|
const updateBranchDialogOpen = (open: boolean): void => {
|
|
if (!open && !branchProtection.confirmLeave()) {
|
|
return;
|
|
}
|
|
isCreateBranchDialogOpen.value = open;
|
|
};
|
|
|
|
const selectedBranchRepositoryConnection = computed(
|
|
(): RepositoryConnectionModel | null =>
|
|
linkedRepositoryConnections.value.find(
|
|
(repositoryConnection) =>
|
|
String(repositoryConnection.id) ===
|
|
branchCreationForm.repository_connection_id,
|
|
) ?? null,
|
|
);
|
|
|
|
const selectedBranchRepositoryHasPersonalAccess = computed(
|
|
() =>
|
|
selectedBranchRepositoryConnection.value?.has_personal_access ?? false,
|
|
);
|
|
|
|
const initializeBranchCreationForm = (): void => {
|
|
if (branchCreationForm.isDirty) {
|
|
return;
|
|
}
|
|
branchCreationForm.clearErrors();
|
|
branchCreationForm.branch_name = props.suggestedBranchName;
|
|
branchCreationForm.repository_connection_id =
|
|
defaultBranchRepositoryConnectionId.value;
|
|
branchNameValidation.value = null;
|
|
isCheckingBranchName.value = false;
|
|
};
|
|
|
|
watch(
|
|
() => isCreateBranchDialogOpen.value,
|
|
(isOpen) => {
|
|
if (!isOpen) {
|
|
branchCreationForm.clearErrors();
|
|
branchNameValidation.value = null;
|
|
isCheckingBranchName.value = false;
|
|
|
|
return;
|
|
}
|
|
|
|
initializeBranchCreationForm();
|
|
},
|
|
);
|
|
|
|
watch(
|
|
[
|
|
() => isCreateBranchDialogOpen.value,
|
|
() => branchCreationForm.repository_connection_id,
|
|
() => branchCreationForm.branch_name,
|
|
],
|
|
(
|
|
[isOpen, repositoryConnectionId, branchName],
|
|
_previousValues,
|
|
onCleanup,
|
|
) => {
|
|
if (!isOpen) {
|
|
return;
|
|
}
|
|
|
|
const repositoryConnection = selectedBranchRepositoryConnection.value;
|
|
|
|
if (repositoryConnection === null || repositoryConnectionId === '') {
|
|
branchNameValidation.value = null;
|
|
isCheckingBranchName.value = false;
|
|
|
|
return;
|
|
}
|
|
|
|
if (!repositoryConnection.has_personal_access) {
|
|
branchNameValidation.value = {
|
|
is_valid: false,
|
|
message: t('ticketDetail.connectGitFirst'),
|
|
};
|
|
isCheckingBranchName.value = false;
|
|
|
|
return;
|
|
}
|
|
|
|
if (branchName.trim() === '') {
|
|
branchNameValidation.value = {
|
|
is_valid: false,
|
|
message: t('ticketDetail.branchRequired'),
|
|
};
|
|
isCheckingBranchName.value = false;
|
|
|
|
return;
|
|
}
|
|
|
|
const abortController = new AbortController();
|
|
const timeoutId = window.setTimeout(async () => {
|
|
isCheckingBranchName.value = true;
|
|
|
|
try {
|
|
const response = await fetch(
|
|
ProjectTicketBranchController.validate.url(
|
|
{
|
|
project: props.project.id,
|
|
ticket: props.ticket.id,
|
|
},
|
|
{
|
|
query: {
|
|
branch_name: branchName,
|
|
repository_connection_id:
|
|
repositoryConnectionId,
|
|
},
|
|
},
|
|
),
|
|
{
|
|
credentials: 'same-origin',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
},
|
|
method: 'GET',
|
|
signal: abortController.signal,
|
|
},
|
|
);
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Branch validation failed.');
|
|
}
|
|
|
|
branchNameValidation.value =
|
|
(await response.json()) as BranchNameValidationModel;
|
|
} catch (error) {
|
|
if (
|
|
error instanceof DOMException &&
|
|
error.name === 'AbortError'
|
|
) {
|
|
return;
|
|
}
|
|
|
|
branchNameValidation.value = {
|
|
is_valid: false,
|
|
message: t('ticketDetail.branchValidationFailed', {
|
|
app: appName,
|
|
}),
|
|
};
|
|
} finally {
|
|
if (!abortController.signal.aborted) {
|
|
isCheckingBranchName.value = false;
|
|
}
|
|
}
|
|
}, 300);
|
|
|
|
onCleanup(() => {
|
|
abortController.abort();
|
|
window.clearTimeout(timeoutId);
|
|
isCheckingBranchName.value = false;
|
|
});
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
|
|
const submitBranchCreation = (): void => {
|
|
if (
|
|
!canCreateBranches.value ||
|
|
!selectedBranchRepositoryHasPersonalAccess.value ||
|
|
branchNameValidation.value?.is_valid !== true
|
|
) {
|
|
return;
|
|
}
|
|
|
|
branchProtection.submit(
|
|
ProjectTicketBranchController.store({
|
|
project: props.project.id,
|
|
ticket: props.ticket.id,
|
|
}),
|
|
{
|
|
onSuccess: () => {
|
|
isCreateBranchDialogOpen.value = false;
|
|
},
|
|
preserveScroll: true,
|
|
},
|
|
);
|
|
};
|
|
</script>
|
|
|
|
<template>
|
|
<section
|
|
id="ticket-git"
|
|
aria-labelledby="ticket-git-title"
|
|
class="wrap-anywhere flex min-w-0 scroll-mt-6 flex-col gap-4"
|
|
>
|
|
<h2 id="ticket-git-title" class="text-lg font-semibold">
|
|
{{ t('ticketDetail.git') }}
|
|
</h2>
|
|
<div class="grid min-w-0 gap-6">
|
|
<Card class="h-fit">
|
|
<CardHeader
|
|
class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between"
|
|
>
|
|
<div class="space-y-1.5">
|
|
<CardTitle>{{ t('ticketDetail.branches') }}</CardTitle>
|
|
<CardDescription>{{
|
|
t('ticketDetail.branchesDescription')
|
|
}}</CardDescription>
|
|
</div>
|
|
|
|
<Dialog
|
|
:open="isCreateBranchDialogOpen"
|
|
@update:open="updateBranchDialogOpen"
|
|
>
|
|
<Button
|
|
v-if="canCreateBranches"
|
|
type="button"
|
|
variant="outline"
|
|
class="gap-2"
|
|
@click="isCreateBranchDialogOpen = true"
|
|
>
|
|
<GitBranchPlus class="size-4" />{{
|
|
t('ticketDetail.createBranch')
|
|
}}</Button
|
|
>
|
|
|
|
<DialogContent class="sm:max-w-lg">
|
|
<DialogHeader class="space-y-3">
|
|
<DialogTitle>{{
|
|
t('ticketDetail.createBranchTitle')
|
|
}}</DialogTitle>
|
|
<DialogDescription>{{
|
|
t('ticketDetail.createBranchDescription')
|
|
}}</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<form
|
|
class="grid gap-4"
|
|
@submit.prevent="submitBranchCreation"
|
|
>
|
|
<FormFeedback
|
|
:state="branchProtection.state.value"
|
|
@retry="submitBranchCreation"
|
|
/>
|
|
<div class="grid min-w-0 gap-2">
|
|
<Label for="repository_connection_id">{{
|
|
t('ticketDetail.repository')
|
|
}}</Label>
|
|
<select
|
|
id="repository_connection_id"
|
|
v-model="
|
|
branchCreationForm.repository_connection_id
|
|
"
|
|
:disabled="
|
|
branchCreationForm.processing
|
|
"
|
|
class="min-h-control border-input bg-background shadow-xs focus-visible:border-ring focus-visible:ring-ring/50 w-full min-w-0 rounded-md border px-3 py-2 text-sm outline-none transition-[color,box-shadow] focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-60"
|
|
>
|
|
<option
|
|
v-for="repositoryConnection in linkedRepositoryConnections"
|
|
:key="repositoryConnection.id"
|
|
:value="
|
|
String(repositoryConnection.id)
|
|
"
|
|
>
|
|
{{
|
|
`${repositoryConnection.repository_full_name}${repositoryConnection.has_personal_access ? '' : ' (connect personal Git access)'}`
|
|
}}
|
|
</option>
|
|
</select>
|
|
<InputError
|
|
:message="
|
|
branchCreationForm.errors
|
|
.repository_connection_id
|
|
"
|
|
/>
|
|
</div>
|
|
|
|
<div class="grid min-w-0 gap-2">
|
|
<Label for="branch_name">{{
|
|
t('ticketDetail.branchName')
|
|
}}</Label>
|
|
<Input
|
|
id="branch_name"
|
|
v-model="branchCreationForm.branch_name"
|
|
:disabled="
|
|
branchCreationForm.processing ||
|
|
!selectedBranchRepositoryHasPersonalAccess
|
|
"
|
|
/>
|
|
<InputError
|
|
:message="
|
|
branchCreationForm.errors
|
|
.branch_name
|
|
"
|
|
/>
|
|
</div>
|
|
|
|
<div
|
|
class="rounded-xl border px-4 py-3"
|
|
:class="
|
|
branchNameValidation?.is_valid
|
|
? 'border-emerald-200 bg-emerald-50 text-emerald-900 dark:border-emerald-900/70 dark:bg-emerald-950/40 dark:text-emerald-100'
|
|
: 'border-amber-200 bg-amber-50 text-amber-900 dark:border-amber-900/70 dark:bg-amber-950/40 dark:text-amber-100'
|
|
"
|
|
>
|
|
<div class="flex items-start gap-3">
|
|
<CheckCircle2
|
|
v-if="
|
|
branchNameValidation?.is_valid
|
|
"
|
|
class="mt-0.5 size-4 shrink-0"
|
|
/>
|
|
<XCircle
|
|
v-else-if="
|
|
branchNameValidation !== null
|
|
"
|
|
class="mt-0.5 size-4 shrink-0"
|
|
/>
|
|
<p class="text-sm">
|
|
{{
|
|
isCheckingBranchName
|
|
? t(
|
|
'ticketDetail.checkingBranch',
|
|
)
|
|
: (branchNameValidation?.message ??
|
|
t(
|
|
'ticketDetail.selectRepository',
|
|
))
|
|
}}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div
|
|
v-if="
|
|
!selectedBranchRepositoryHasPersonalAccess &&
|
|
selectedBranchRepositoryConnection !==
|
|
null
|
|
"
|
|
class="border-border rounded-xl border border-dashed px-4 py-4"
|
|
>
|
|
<p class="text-muted-foreground text-sm">
|
|
{{ t('ticketDetail.branchAccess') }}
|
|
</p>
|
|
|
|
<Button
|
|
as-child
|
|
variant="outline"
|
|
size="sm"
|
|
class="mt-3"
|
|
>
|
|
<Link :href="editGitConnections()">{{
|
|
t('ticketDetail.openGitConnections')
|
|
}}</Link>
|
|
</Button>
|
|
</div>
|
|
|
|
<DialogFooter class="gap-2">
|
|
<Button
|
|
type="button"
|
|
variant="secondary"
|
|
@click="updateBranchDialogOpen(false)"
|
|
>{{ t('common.cancel') }}</Button
|
|
>
|
|
|
|
<Button
|
|
type="submit"
|
|
:disabled="
|
|
branchCreationForm.processing ||
|
|
isCheckingBranchName ||
|
|
branchNameValidation?.is_valid !==
|
|
true
|
|
"
|
|
>{{
|
|
t('ticketDetail.createNow')
|
|
}}</Button
|
|
>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</CardHeader>
|
|
|
|
<CardContent class="space-y-4">
|
|
<FeedbackState
|
|
v-if="visibleRepositoryBranches.length === 0"
|
|
state="empty"
|
|
:title="t('tickets.noBranches')"
|
|
:description="t('tickets.noBranchesDescription')"
|
|
/>
|
|
|
|
<div v-else class="space-y-3">
|
|
<div
|
|
v-for="repositoryBranch in visibleRepositoryBranches"
|
|
:key="repositoryBranch.repository_connection.id"
|
|
class="border-border rounded-xl border px-4 py-4"
|
|
>
|
|
<div class="flex flex-wrap items-center gap-2">
|
|
<Badge variant="secondary">
|
|
{{
|
|
repositoryBranch.repository_connection
|
|
.provider_label
|
|
}}
|
|
</Badge>
|
|
<span class="font-medium">
|
|
{{
|
|
repositoryBranch.repository_connection
|
|
.repository_full_name
|
|
}}
|
|
</span>
|
|
</div>
|
|
|
|
<p class="text-muted-foreground mt-2 text-sm">
|
|
{{
|
|
repositoryBranch.repository_connection
|
|
.base_url
|
|
}}
|
|
</p>
|
|
|
|
<div
|
|
v-if="
|
|
!repositoryBranch.repository_connection
|
|
.has_personal_access
|
|
"
|
|
class="mt-3 space-y-3"
|
|
>
|
|
<p class="text-muted-foreground text-sm">
|
|
{{ t('ticketDetail.inspectBranches') }}
|
|
</p>
|
|
|
|
<Button as-child variant="outline" size="sm">
|
|
<Link :href="editGitConnections()">{{
|
|
t('ticketDetail.openGitConnections')
|
|
}}</Link>
|
|
</Button>
|
|
</div>
|
|
|
|
<div
|
|
v-else-if="repositoryBranch.error"
|
|
class="mt-3 rounded-xl border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-900 dark:border-amber-900/70 dark:bg-amber-950/40 dark:text-amber-100"
|
|
>
|
|
{{ repositoryBranch.error }}
|
|
</div>
|
|
|
|
<div v-else class="mt-3 flex flex-wrap gap-2">
|
|
<Badge
|
|
v-for="branch in repositoryBranch.branches"
|
|
:key="branch.name"
|
|
variant="outline"
|
|
class="px-3 py-1"
|
|
>
|
|
{{ branch.name }}
|
|
</Badge>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card class="h-fit">
|
|
<CardHeader>
|
|
<CardTitle>{{ t('ticketDetail.pullRequests') }}</CardTitle>
|
|
<CardDescription>{{
|
|
t('ticketDetail.pullRequestsDescription')
|
|
}}</CardDescription>
|
|
</CardHeader>
|
|
|
|
<CardContent class="space-y-4">
|
|
<FeedbackState
|
|
v-if="visibleRepositoryPullRequests.length === 0"
|
|
state="empty"
|
|
:title="t('tickets.noPullRequests')"
|
|
:description="t('tickets.noPullRequestsDescription')"
|
|
/>
|
|
|
|
<div v-else class="space-y-3">
|
|
<div
|
|
v-for="repositoryPullRequest in visibleRepositoryPullRequests"
|
|
:key="
|
|
repositoryPullRequest.repository_connection.id
|
|
"
|
|
class="border-border rounded-xl border px-4 py-4"
|
|
>
|
|
<div class="flex flex-wrap items-center gap-2">
|
|
<Badge variant="secondary">
|
|
{{
|
|
repositoryPullRequest
|
|
.repository_connection
|
|
.provider_label
|
|
}}
|
|
</Badge>
|
|
<span class="font-medium">
|
|
{{
|
|
repositoryPullRequest
|
|
.repository_connection
|
|
.repository_full_name
|
|
}}
|
|
</span>
|
|
</div>
|
|
|
|
<p class="text-muted-foreground mt-2 text-sm">
|
|
{{
|
|
repositoryPullRequest.repository_connection
|
|
.base_url
|
|
}}
|
|
</p>
|
|
|
|
<div
|
|
v-if="
|
|
!repositoryPullRequest.repository_connection
|
|
.has_personal_access
|
|
"
|
|
class="mt-3 space-y-3"
|
|
>
|
|
<p class="text-muted-foreground text-sm">
|
|
{{ t('ticketDetail.inspectPullRequests') }}
|
|
</p>
|
|
|
|
<Button as-child variant="outline" size="sm">
|
|
<Link :href="editGitConnections()">{{
|
|
t('ticketDetail.openGitConnections')
|
|
}}</Link>
|
|
</Button>
|
|
</div>
|
|
|
|
<div
|
|
v-else-if="repositoryPullRequest.error"
|
|
class="mt-3 rounded-xl border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-900 dark:border-amber-900/70 dark:bg-amber-950/40 dark:text-amber-100"
|
|
>
|
|
{{ repositoryPullRequest.error }}
|
|
</div>
|
|
|
|
<div v-else class="mt-3 space-y-3">
|
|
<div
|
|
v-for="pullRequest in repositoryPullRequest.pull_requests"
|
|
:key="pullRequest.html_url"
|
|
class="border-border/70 rounded-xl border px-4 py-3"
|
|
>
|
|
<div
|
|
class="flex flex-wrap items-center gap-2"
|
|
>
|
|
<Badge variant="outline">
|
|
#{{ pullRequest.number }}
|
|
</Badge>
|
|
<Badge variant="secondary">
|
|
{{ pullRequest.status }}
|
|
</Badge>
|
|
<a
|
|
:href="pullRequest.html_url"
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
class="text-foreground hover:text-primary font-medium transition"
|
|
>
|
|
{{ pullRequest.title }}
|
|
</a>
|
|
</div>
|
|
<p
|
|
class="text-muted-foreground mt-2 text-sm"
|
|
>
|
|
{{
|
|
t('ticketDetail.sourceBranch', {
|
|
name: pullRequest.source_branch,
|
|
})
|
|
}}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
</section>
|
|
</template>
|