131 lines
3.5 KiB
Markdown
131 lines
3.5 KiB
Markdown
---
|
|
description: Create a new Git repository on Gitea
|
|
agent: build
|
|
---
|
|
|
|
# create-gitea-repo
|
|
|
|
Create a new Git repository on Gitea.
|
|
|
|
## Features
|
|
|
|
- Create new repository under specified organization or user via Gitea API
|
|
- Support creating private or public repositories
|
|
- Automatically add remote repository address after successful creation
|
|
|
|
## User Input Format
|
|
|
|
User can specify parameters in the following format:
|
|
|
|
```
|
|
<owner>/<repo> [private|public]
|
|
```
|
|
|
|
- `owner`: Organization name or username (required)
|
|
- `repo`: Repository name (required)
|
|
- `private|public`: Visibility (optional, default `private`)
|
|
|
|
**Examples**:
|
|
- `ai/my-project` - Create private repository my-project under ai organization
|
|
- `ai/my-project public` - Create public repository my-project under ai organization
|
|
- `voson/test private` - Create private repository test under voson user
|
|
|
|
## Configuration
|
|
|
|
Use the following configuration when executing command:
|
|
|
|
| Config Item | Value |
|
|
| --- | --- |
|
|
| Gitea Server URL | `https://git.digitevents.com/` |
|
|
| API Token | `{env:GITEA_API_TOKEN}` |
|
|
|
|
## Steps
|
|
|
|
### 1. Parse User Input
|
|
|
|
Parse from user input:
|
|
- `owner`: Organization name or username
|
|
- `repo`: Repository name
|
|
- `visibility`: `private` (default) or `public`
|
|
|
|
**Input validation**:
|
|
- If user didn't provide `owner/repo` format input, prompt user for correct format and terminate execution
|
|
- Repository name can only contain letters, numbers, underscores, hyphens and dots
|
|
|
|
### 2. Call Gitea API to Create Repository
|
|
|
|
Use curl to call Gitea API:
|
|
|
|
```bash
|
|
curl -s -X POST "https://git.digitevents.com/api/v1/orgs/<owner>/repos" \
|
|
-H "Authorization: token $GITEA_API_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"name": "<repo>",
|
|
"private": <true|false>
|
|
}'
|
|
```
|
|
|
|
> Note: If owner is personal user instead of organization, API path should be `/api/v1/user/repos`, but usually try organization API first.
|
|
|
|
### 3. Handle Response
|
|
|
|
**Success** (HTTP 201):
|
|
- Extract repository info from response:
|
|
- `html_url`: Repository web URL
|
|
- `clone_url`: HTTPS clone URL
|
|
- `ssh_url`: SSH clone URL
|
|
- Output creation success message
|
|
|
|
**Failure**:
|
|
- If 404 error, owner may not exist or no permission
|
|
- If 409 error, repository already exists
|
|
- Output error message and terminate
|
|
|
|
### 4. Ask Whether to Add Remote Repository
|
|
|
|
Ask user whether to add the newly created repository as current project's remote repository.
|
|
|
|
**If user confirms**:
|
|
|
|
1. Check if current directory is a Git repository:
|
|
```bash
|
|
git rev-parse --is-inside-work-tree
|
|
```
|
|
|
|
2. If not a Git repository, ask whether to initialize:
|
|
```bash
|
|
git init
|
|
```
|
|
|
|
3. Check if origin remote already exists:
|
|
```bash
|
|
git remote get-url origin
|
|
```
|
|
|
|
4. Add or update remote repository:
|
|
- If no origin: `git remote add origin <clone_url>`
|
|
- If origin exists: Ask whether to overwrite, after confirmation execute `git remote set-url origin <clone_url>`
|
|
|
|
### 5. Output Result Summary
|
|
|
|
Output creation result summary table:
|
|
|
|
```
|
|
Repository created successfully!
|
|
|
|
| Property | Value |
|
|
|----------|-------|
|
|
| Repository Name | owner/repo |
|
|
| Web URL | https://git.digitevents.com/owner/repo |
|
|
| Clone URL (HTTPS) | https://git.digitevents.com/owner/repo.git |
|
|
| Clone URL (SSH) | git@git.digitevents.com:owner/repo.git |
|
|
| Private | Yes/No |
|
|
```
|
|
|
|
## Notes
|
|
|
|
- **Permission check**: Ensure Token has permission to create repository
|
|
- **Organization vs User**: Creating organization repository and user repository use different API endpoints
|
|
- **Repository naming**: Follow Gitea naming rules, avoid special characters
|