diff --git a/modules/gitea/client.ts b/modules/gitea/client.ts index 76e6a285..9aedbbad 100644 --- a/modules/gitea/client.ts +++ b/modules/gitea/client.ts @@ -244,13 +244,23 @@ export class GiteaClient { return all; } + /** A repository, created under the tools' own user, or with `owner` in that organisation (novox/hq issue 1124). + * The forge's refusal to create in an organisation (403 not allowed, 404 no such organisation, 409 exists) is + * thrown with its status and its reason, naming the organisation. */ async createRepo(data: { name: string; description?: string; private?: boolean; auto_init?: boolean; - }): Promise { - return GiteaClient.mapRepo(await this.request("/user/repos", { method: "POST", body: JSON.stringify(data) })); + }, owner?: string): Promise { + const options = { method: "POST", body: JSON.stringify(data) }; + if (owner === undefined) return GiteaClient.mapRepo(await this.request("/user/repos", options)); + const path = `/orgs/${pathSegment("organisation", owner)}/repos`; + try { + return GiteaClient.mapRepo(await this.request(path, options)); + } catch (e) { + throw new Error(`the forge did not create ${owner}/${data.name} in the organisation ${owner}: ${(e as Error).message}`); + } } async deleteRepo(owner: string, repo: string): Promise { diff --git a/modules/gitea/tools/index.ts b/modules/gitea/tools/index.ts index 37fd2a6f..772a124e 100644 --- a/modules/gitea/tools/index.ts +++ b/modules/gitea/tools/index.ts @@ -82,8 +82,10 @@ function allGiteaTools(gitea: GiteaClient, admin?: () => GiteaAdmin): ToolDefini }, { name: "gitea_create_repo", - description: "Create a repository owned by the authenticated user.", + description: "Create a repository: with owner, in that organisation (the forge's refusal said with its status " + + "and reason); without owner, under the tools' own user.", input: { + owner: { type: "string", description: "an organisation to create the repository in (default: the tools' own user)" }, name: { type: "string", description: "the repository name" }, description: { type: "string", description: "an optional description" }, private: { type: "boolean", description: "private repo (default true)" }, @@ -95,7 +97,7 @@ function allGiteaTools(gitea: GiteaClient, admin?: () => GiteaAdmin): ToolDefini description: args.description ? String(args.description) : undefined, private: args.private === undefined ? true : Boolean(args.private), auto_init: args.auto_init === undefined ? true : Boolean(args.auto_init), - }); + }, args.owner === undefined ? undefined : String(args.owner)); return { repo }; }, },