diff --git a/src/core/api/endpoints.ts b/src/core/api/endpoints.ts index fcb7afe..e4278e3 100644 --- a/src/core/api/endpoints.ts +++ b/src/core/api/endpoints.ts @@ -1686,6 +1686,47 @@ export function listIssueAttachments( ); } +// --- issue subscriptions --------------------------------------------------- + +export function checkIssueSubscription( + client: GiteaClient, + owner: string, + repo: string, + index: number, + options?: RequestOptions, +): Promise<{ subscribed?: boolean }> { + return client.get(`${repoPath(owner, repo)}/issues/${index}/subscriptions/check`, options); +} + +export function subscribeIssue( + client: GiteaClient, + owner: string, + repo: string, + index: number, + user: string, + options?: RequestOptions, +): Promise { + return client.put( + `${repoPath(owner, repo)}/issues/${index}/subscriptions/${enc(user)}`, + undefined, + options, + ); +} + +export function unsubscribeIssue( + client: GiteaClient, + owner: string, + repo: string, + index: number, + user: string, + options?: RequestOptions, +): Promise { + return client.delete( + `${repoPath(owner, repo)}/issues/${index}/subscriptions/${enc(user)}`, + options, + ); +} + // --------------------------------------------------------------------------- // Admin (read-only) // --------------------------------------------------------------------------- diff --git a/src/core/api/query-keys.ts b/src/core/api/query-keys.ts index b1fae65..6595538 100644 --- a/src/core/api/query-keys.ts +++ b/src/core/api/query-keys.ts @@ -87,6 +87,9 @@ export const queryKeys = { issueAttachments: (accountId: string, owner: string, name: string, index: number) => [...queryKeys.issue(accountId, owner, name, index), 'attachments'] as const, + issueSubscription: (accountId: string, owner: string, name: string, index: number, user: string) => + [...queryKeys.issue(accountId, owner, name, index), 'subscription', user] as const, + // --- actions ------------------------------------------------------------- actionsRoot: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'actions'] as const, diff --git a/src/features/actions/queries.ts b/src/features/actions/queries.ts index fe96f02..d1a9dc4 100644 --- a/src/features/actions/queries.ts +++ b/src/features/actions/queries.ts @@ -12,6 +12,7 @@ import { import type { RawResponse } from '@/core/api/client'; import { cancelActionRun, + dispatchWorkflow, fetchActionJobLogs, fetchActionRun, listActionRunJobs, @@ -19,6 +20,7 @@ import { listRunArtifacts, listWorkflows, rerunActionRun, + type DispatchWorkflowInput, type ListActionRunsParams, } from '@/core/api/endpoints'; import { queryKeys } from '@/core/api/query-keys'; @@ -38,6 +40,21 @@ export function useWorkflows(owner: string, name: string) { }); } +/** Dispatch a workflow run (`workflow_dispatch`). */ +export function useDispatchWorkflow(owner: string, name: string, workflowId: string) { + const { client, account } = useSession(); + const queryClient = useQueryClient(); + const accountId = account?.id ?? ''; + + return useMutation({ + mutationFn: (input: DispatchWorkflowInput) => + dispatchWorkflow(client!, owner, name, workflowId, input), + onSuccess: () => { + void queryClient.invalidateQueries({ queryKey: queryKeys.actionsRoot(accountId, owner, name) }); + }, + }); +} + export function useActionRuns( owner: string, name: string, diff --git a/src/features/issues/queries.ts b/src/features/issues/queries.ts index 8bed485..9aa4001 100644 --- a/src/features/issues/queries.ts +++ b/src/features/issues/queries.ts @@ -19,6 +19,7 @@ import { import type { RawResponse } from '@/core/api/client'; import { addIssueReaction, + checkIssueSubscription, createIssue, createIssueComment, createPull, @@ -43,6 +44,8 @@ import { removeIssueReaction, replyToReviewComment, searchIssues, + subscribeIssue, + unsubscribeIssue, updateIssue, updateIssueComment, type CreateIssueInput, @@ -546,3 +549,48 @@ export function useIssueAttachments(owner: string, name: string, index: number) staleTime: 60_000, }); } + +export function useIssueSubscription( + owner: string, + name: string, + index: number, + user: string | undefined, +) { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + + return useQuery<{ subscribed?: boolean }>({ + queryKey: queryKeys.issueSubscription(accountId, owner, name, index, user ?? ''), + enabled: Boolean(client) && Boolean(accountId) && index > 0 && Boolean(user), + queryFn: ({ signal }) => checkIssueSubscription(client!, owner, name, index, { signal }), + staleTime: 60_000, + retry: false, + }); +} + +export function useToggleIssueSubscription( + owner: string, + name: string, + index: number, + user: string | undefined, +) { + const { client, account } = useSession(); + const queryClient = useQueryClient(); + const accountId = account?.id ?? ''; + + return useMutation({ + mutationFn: async (subscribed: boolean): Promise => { + if (!user) return; + if (subscribed) { + await unsubscribeIssue(client!, owner, name, index, user); + } else { + await subscribeIssue(client!, owner, name, index, user); + } + }, + onSuccess: () => { + void queryClient.invalidateQueries({ + queryKey: queryKeys.issueSubscription(accountId, owner, name, index, user ?? ''), + }); + }, + }); +} diff --git a/src/features/repository/screens/actions-settings-screen.tsx b/src/features/repository/screens/actions-settings-screen.tsx index db9a4c3..561d1a9 100644 --- a/src/features/repository/screens/actions-settings-screen.tsx +++ b/src/features/repository/screens/actions-settings-screen.tsx @@ -6,6 +6,7 @@ import { Screen } from '@/components/layout/screen'; import { AppText } from '@/components/ui/app-text'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; +import { ChipSelect } from '@/components/ui/chip-select'; import { Field } from '@/components/ui/field'; import { IconButton } from '@/components/ui/icon-button'; import { ErrorState, LoadingView } from '@/components/ui/state-views'; @@ -18,6 +19,7 @@ import { useRepoSecrets, useRepoVariables, } from '@/features/repositories/settings-queries'; +import { useDispatchWorkflow, useWorkflows } from '@/features/actions/queries'; import { spacing } from '@/theme/tokens'; import type { RepositoryStackParamList } from '@/navigation/types'; import type { ScreenProps } from '@/navigation/typing'; @@ -40,6 +42,10 @@ export function ActionsSettingsScreen({ route }: Props) { const [secretValue, setSecretValue] = useState(''); const [varName, setVarName] = useState(''); const [varValue, setVarValue] = useState(''); + const workflows = useWorkflows(owner, name); + const [selectedWorkflow, setSelectedWorkflow] = useState(null); + const [dispatchRefName, setDispatchRefName] = useState(''); + const dispatch = useDispatchWorkflow(owner, name, selectedWorkflow ?? ''); if (secrets.isPending || variables.isPending) return ; if (secrets.isError || variables.isError) { @@ -118,6 +124,41 @@ export function ActionsSettingsScreen({ route }: Props) { return ( + {t('repoSettings.runWorkflow')} + + ({ + id: String(workflow.id), + label: workflow.name ?? workflow.path ?? '', + }))} + selectedIds={selectedWorkflow ? [selectedWorkflow] : []} + onToggle={(id) => setSelectedWorkflow(id)} + emptyLabel={t('repoSettings.noWorkflows')} + single + /> + +