diff --git a/src/core/api/endpoints.test.ts b/src/core/api/endpoints.test.ts index 2c71162..a74b42d 100644 --- a/src/core/api/endpoints.test.ts +++ b/src/core/api/endpoints.test.ts @@ -6,7 +6,16 @@ */ import { GiteaClient } from './client'; -import { createPullReview, mergePull } from './endpoints'; +import { + addRepoKey, + createOrg, + createRelease, + createPullReview, + listActionRuns, + mergePull, + updateRepo, + updateUserSettings, +} from './endpoints'; function response(body = '{}', status = 200): Response { return { @@ -47,4 +56,53 @@ describe('endpoint request bodies', () => { expect(body.event).toBe('REQUEST_CHANGES'); expect(body.body).toBe('please'); }); + + it('patches repository settings', async () => { + const { fetchImpl, client } = capture(); + await updateRepo(client, 'octo', 'repo', { description: 'D', private: true, has_wiki: false }); + expect(fetchImpl.mock.calls[0][0]).toBe('https://codeberg.org/api/v1/repos/octo/repo'); + expect(postedBody(fetchImpl)).toMatchObject({ + description: 'D', + private: true, + has_wiki: false, + }); + }); + + it('creates a release with a tag', async () => { + const { fetchImpl, client } = capture(); + await createRelease(client, 'octo', 'repo', { tag_name: 'v1.2.0', name: 'R', draft: false }); + expect(fetchImpl.mock.calls[0][0]).toBe('https://codeberg.org/api/v1/repos/octo/repo/releases'); + expect(postedBody(fetchImpl)).toMatchObject({ tag_name: 'v1.2.0', name: 'R' }); + }); + + it('adds a deploy key with read-only flag', async () => { + const { fetchImpl, client } = capture(); + await addRepoKey(client, 'octo', 'repo', { title: 'CI', key: 'ssh-ed25519 AAAA', read_only: true }); + expect(fetchImpl.mock.calls[0][0]).toBe('https://codeberg.org/api/v1/repos/octo/repo/keys'); + expect(postedBody(fetchImpl)).toMatchObject({ title: 'CI', key: 'ssh-ed25519 AAAA', read_only: true }); + }); + + it('updates user profile settings', async () => { + const { fetchImpl, client } = capture(); + await updateUserSettings(client, { full_name: 'Alice', location: 'NL' }); + expect(fetchImpl.mock.calls[0][0]).toBe('https://codeberg.org/api/v1/user/settings'); + expect(fetchImpl.mock.calls[0][1].method).toBe('PATCH'); + expect(postedBody(fetchImpl)).toMatchObject({ full_name: 'Alice', location: 'NL' }); + }); + + it('creates an organization', async () => { + const { fetchImpl, client } = capture(); + await createOrg(client, { username: 'acme', visibility: 'private' }); + expect(fetchImpl.mock.calls[0][0]).toBe('https://codeberg.org/api/v1/orgs'); + expect(postedBody(fetchImpl)).toMatchObject({ username: 'acme', visibility: 'private' }); + }); + + it('builds Action run filter query parameters', async () => { + const { fetchImpl, client } = capture(); + await listActionRuns(client, 'octo', 'repo', { status: 'success', branch: 'main', limit: 10 }); + const url = fetchImpl.mock.calls[0][0] as string; + expect(url).toContain('/repos/octo/repo/actions/runs?'); + expect(url).toContain('status=success'); + expect(url).toContain('branch=main'); + }); }); diff --git a/src/core/api/endpoints.ts b/src/core/api/endpoints.ts index 849a477..8a318d8 100644 --- a/src/core/api/endpoints.ts +++ b/src/core/api/endpoints.ts @@ -24,6 +24,7 @@ import type { Comment, Commit, ContentsEntry, + DeployKey, GpgKey, Issue, Label, @@ -42,6 +43,7 @@ import type { Subscription, Tag, User, + UserSettings, Webhook, } from './types'; @@ -620,6 +622,85 @@ export function listUserTokens( }); } +// --------------------------------------------------------------------------- +// Repository deploy keys +// --------------------------------------------------------------------------- + +export function listRepoKeys( + client: GiteaClient, + owner: string, + repo: string, + params: PageParams = {}, + options?: RequestOptions, +) { + return client.raw(`${repoPath(owner, repo)}/keys`, { + ...options, + query: { page: params.page ?? 1, limit: params.limit ?? 50 }, + }); +} + +export interface AddRepoKeyInput { + title: string; + key: string; + read_only?: boolean; +} + +export function addRepoKey( + client: GiteaClient, + owner: string, + repo: string, + input: AddRepoKeyInput, + options?: RequestOptions, +): Promise { + return client.post(`${repoPath(owner, repo)}/keys`, input, options); +} + +export function deleteRepoKey( + client: GiteaClient, + owner: string, + repo: string, + id: number, + options?: RequestOptions, +): Promise { + return client.delete(`${repoPath(owner, repo)}/keys/${id}`, options); +} + +// --------------------------------------------------------------------------- +// User profile settings & organizations (write) +// --------------------------------------------------------------------------- + +export function fetchUserSettings( + client: GiteaClient, + options?: RequestOptions, +): Promise { + return client.get('/user/settings', options); +} + +export function updateUserSettings( + client: GiteaClient, + input: Partial, + options?: RequestOptions, +): Promise { + return client.patch('/user/settings', input, options); +} + +export interface CreateOrgInput { + username: string; + full_name?: string; + description?: string; + website?: string; + location?: string; + visibility?: 'public' | 'limited' | 'private'; +} + +export function createOrg( + client: GiteaClient, + input: CreateOrgInput, + options?: RequestOptions, +): Promise { + return client.post('/orgs', input, options); +} + // --------------------------------------------------------------------------- // Contents, README, branches, tags // --------------------------------------------------------------------------- diff --git a/src/core/api/query-keys.ts b/src/core/api/query-keys.ts index 671ba52..67fc58a 100644 --- a/src/core/api/query-keys.ts +++ b/src/core/api/query-keys.ts @@ -63,6 +63,9 @@ export const queryKeys = { repoProtections: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'branch-protections'] as const, + repoKeys: (accountId: string, owner: string, name: string) => + [...queryKeys.repo(accountId, owner, name), 'keys'] as const, + // --- actions ------------------------------------------------------------- actionsWorkflows: (accountId: string, owner: string, name: string) => [...queryKeys.repo(accountId, owner, name), 'actions', 'workflows'] as const, diff --git a/src/core/api/types.ts b/src/core/api/types.ts index d410ffa..e7df63f 100644 --- a/src/core/api/types.ts +++ b/src/core/api/types.ts @@ -558,6 +558,16 @@ export interface AccessToken { last_used_at?: string; } +/** Editable user profile fields (GET/PATCH /user/settings). */ +export interface UserSettings { + full_name?: string; + website?: string; + location?: string; + description?: string; + language?: string; + theme?: string; +} + export interface Activity { id?: number; user_id?: number; diff --git a/src/features/profile/queries.ts b/src/features/profile/queries.ts index 563af6f..2a7e399 100644 --- a/src/features/profile/queries.ts +++ b/src/features/profile/queries.ts @@ -1,18 +1,30 @@ /** Account-level data: organizations, packages and credential listings. */ -import { useInfiniteQuery, type InfiniteData } from '@tanstack/react-query'; +import { useInfiniteQuery, useMutation, useQuery, useQueryClient, type InfiniteData } from '@tanstack/react-query'; import type { RawResponse } from '@/core/api/client'; import { + createOrg, + fetchUserSettings, listOrgRepos, listPackages, listUserGpgKeys, listUserKeys, listUserOrgs, listUserTokens, + updateUserSettings, + type CreateOrgInput, } from '@/core/api/endpoints'; import { queryKeys } from '@/core/api/query-keys'; -import type { AccessToken, GpgKey, Organization, Package, PublicKey, Repository } from '@/core/api/types'; +import type { + AccessToken, + GpgKey, + Organization, + Package, + PublicKey, + Repository, + UserSettings, +} from '@/core/api/types'; import { useSession } from '@/features/accounts/use-session'; function useAccountList( @@ -89,3 +101,40 @@ export function useUserTokens(username: string, limit = 50) { (page, signal) => listUserTokens(client!, username, { page, limit }, { signal }), ); } + +/** Editable profile settings for the signed-in user. */ +export function useUserSettings() { + const { client, account } = useSession(); + const accountId = account?.id ?? ''; + return useQuery({ + queryKey: [...queryKeys.account(accountId), 'settings'], + enabled: Boolean(client) && Boolean(accountId), + queryFn: ({ signal }) => fetchUserSettings(client!, { signal }), + staleTime: 5 * 60_000, + }); +} + +export function useUpdateUserSettings() { + const { client, account } = useSession(); + const queryClient = useQueryClient(); + const accountId = account?.id ?? ''; + return useMutation({ + mutationFn: (input: Partial) => updateUserSettings(client!, input), + onSuccess: () => { + void queryClient.invalidateQueries({ queryKey: [...queryKeys.account(accountId), 'settings'] }); + void queryClient.invalidateQueries({ queryKey: queryKeys.me(accountId) }); + }, + }); +} + +export function useCreateOrg() { + const { client, account } = useSession(); + const queryClient = useQueryClient(); + const accountId = account?.id ?? ''; + return useMutation({ + mutationFn: (input: CreateOrgInput) => createOrg(client!, input), + onSuccess: () => { + void queryClient.invalidateQueries({ queryKey: queryKeys.account(accountId) }); + }, + }); +} diff --git a/src/features/profile/screens/create-org-screen.tsx b/src/features/profile/screens/create-org-screen.tsx new file mode 100644 index 0000000..7c5d2e4 --- /dev/null +++ b/src/features/profile/screens/create-org-screen.tsx @@ -0,0 +1,118 @@ +import { useState } from 'react'; +import { Alert, StyleSheet, View } from 'react-native'; +import { useTranslation } from 'react-i18next'; +import { useNavigation } from '@react-navigation/native'; +import type { NativeStackNavigationProp } from '@react-navigation/native-stack'; + +import { Screen } from '@/components/layout/screen'; +import { AppText } from '@/components/ui/app-text'; +import { Button } from '@/components/ui/button'; +import { Field } from '@/components/ui/field'; +import { SegmentedControl } from '@/components/ui/segmented'; +import { apiErrorMessage } from '@/core/api/error-text'; +import { useCreateOrg } from '@/features/profile/queries'; +import { spacing } from '@/theme/tokens'; +import type { SettingsStackParamList } from '@/navigation/types'; + +type Visibility = 'public' | 'limited' | 'private'; + +/** Create an organization. */ +export function CreateOrgScreen() { + const { t } = useTranslation(); + const navigation = useNavigation>(); + const create = useCreateOrg(); + + const [username, setUsername] = useState(''); + const [fullName, setFullName] = useState(''); + const [location, setLocation] = useState(''); + const [visibility, setVisibility] = useState('public'); + const [submitted, setSubmitted] = useState(false); + + const usernameError = + submitted && username.trim().length === 0 ? t('organizations.usernameRequired') : undefined; + + const submit = () => { + setSubmitted(true); + if (username.trim().length === 0) return; + create.mutate( + { + username: username.trim(), + full_name: fullName.trim() || undefined, + location: location.trim() || undefined, + visibility, + }, + { + onSuccess: () => { + Alert.alert(t('organizations.createdTitle'), t('organizations.createdBody')); + navigation.goBack(); + }, + onError: (error) => Alert.alert(t('errors.title'), apiErrorMessage(error, t)), + }, + ); + }; + + return ( + + + + + + + {t('organizations.visibility')} + + + + value={visibility} + onChange={setVisibility} + accessibilityLabel={t('organizations.visibility')} + options={[ + { value: 'public', label: t('organizations.visPublic') }, + { value: 'limited', label: t('organizations.visLimited') }, + { value: 'private', label: t('organizations.visPrivate') }, + ]} + /> + + + + {create.isError ? ( + + {apiErrorMessage(create.error, t)} + + ) : null} + +