release-it-gitea 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +20 -0
- package/README.md +191 -0
- package/lib/global.d.d.ts +86 -0
- package/lib/global.d.js +0 -0
- package/lib/index.d.ts +79 -0
- package/lib/index.js +245 -0
- package/package.json +91 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
'Software'), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# release-it-gitea
|
|
2
|
+
|
|
3
|
+
一个用于 [release-it](https://github.com/release-it/release-it) 的 Gitea 插件,可以在生成版本和 changelog 后自动将它们推送到指定的 Gitea 服务器的仓库发布中。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- ✅ 自动创建 Gitea 发布
|
|
8
|
+
- ✅ 支持自定义发布标题和说明
|
|
9
|
+
- ✅ 支持预发布和草稿发布
|
|
10
|
+
- ✅ 支持模板变量替换
|
|
11
|
+
- ✅ 自动检测并更新已存在的发布
|
|
12
|
+
- ✅ 完整的错误处理和日志记录
|
|
13
|
+
- ✅ TypeScript 支持
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install --save-dev release-it-gitea
|
|
19
|
+
# 或
|
|
20
|
+
pnpm add -D release-it-gitea
|
|
21
|
+
# 或
|
|
22
|
+
yarn add -D release-it-gitea
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 配置
|
|
26
|
+
|
|
27
|
+
### 1. 设置环境变量
|
|
28
|
+
|
|
29
|
+
首先,你需要设置 Gitea API token:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
export GITEA_TOKEN="your-gitea-api-token"
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 2. 配置 release-it
|
|
36
|
+
|
|
37
|
+
在你的 `.release-it.json` 文件中添加插件配置:
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"plugins": {
|
|
42
|
+
"release-it-gitea": {
|
|
43
|
+
"host": "https://gitea.example.com",
|
|
44
|
+
"owner": "your-username",
|
|
45
|
+
"repository": "your-repo",
|
|
46
|
+
"release": true,
|
|
47
|
+
"releaseTitle": "v${version}",
|
|
48
|
+
"releaseNotes": "${changelog}",
|
|
49
|
+
"prerelease": false,
|
|
50
|
+
"draft": false
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
或者在 `package.json` 中:
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{
|
|
60
|
+
"release-it": {
|
|
61
|
+
"plugins": {
|
|
62
|
+
"release-it-gitea": {
|
|
63
|
+
"host": "https://gitea.example.com",
|
|
64
|
+
"owner": "your-username",
|
|
65
|
+
"repository": "your-repo",
|
|
66
|
+
"release": true
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## 配置选项
|
|
74
|
+
|
|
75
|
+
| 选项 | 类型 | 默认值 | 描述 |
|
|
76
|
+
| -------------- | --------- | ---------------------- | -------------------- |
|
|
77
|
+
| `host` | `string` | **必需** | Gitea 服务器 URL |
|
|
78
|
+
| `owner` | `string` | 从 git remote 自动检测 | 仓库所有者 |
|
|
79
|
+
| `repository` | `string` | 从 git remote 自动检测 | 仓库名称 |
|
|
80
|
+
| `release` | `boolean` | `true` | 是否创建发布 |
|
|
81
|
+
| `releaseTitle` | `string` | `"v${version}"` | 发布标题模板 |
|
|
82
|
+
| `releaseNotes` | `string` | `"${changelog}"` | 发布说明模板 |
|
|
83
|
+
| `prerelease` | `boolean` | `false` | 是否为预发布 |
|
|
84
|
+
| `draft` | `boolean` | `false` | 是否为草稿 |
|
|
85
|
+
| `tokenRef` | `string` | `"GITEA_TOKEN"` | API token 环境变量名 |
|
|
86
|
+
| `timeout` | `number` | `30000` | 请求超时时间(毫秒) |
|
|
87
|
+
|
|
88
|
+
## 模板变量
|
|
89
|
+
|
|
90
|
+
在 `releaseTitle` 和 `releaseNotes` 中可以使用以下模板变量:
|
|
91
|
+
|
|
92
|
+
- `${version}` - 当前版本号
|
|
93
|
+
- `${latestVersion}` - 上一个版本号
|
|
94
|
+
- `${changelog}` - 生成的 changelog
|
|
95
|
+
- `${name}` - 项目名称
|
|
96
|
+
- `${repo.owner}` - 仓库所有者
|
|
97
|
+
- `${repo.repository}` - 仓库名称
|
|
98
|
+
- `${branchName}` - 当前分支名
|
|
99
|
+
|
|
100
|
+
## 使用示例
|
|
101
|
+
|
|
102
|
+
### 基础配置
|
|
103
|
+
|
|
104
|
+
```json
|
|
105
|
+
{
|
|
106
|
+
"plugins": {
|
|
107
|
+
"release-it-gitea": {
|
|
108
|
+
"host": "https://gitea.example.com"
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 完整配置
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"plugins": {
|
|
119
|
+
"release-it-gitea": {
|
|
120
|
+
"host": "https://gitea.example.com",
|
|
121
|
+
"owner": "myorg",
|
|
122
|
+
"repository": "myproject",
|
|
123
|
+
"release": true,
|
|
124
|
+
"releaseTitle": "Release ${version}",
|
|
125
|
+
"releaseNotes": "## 更新内容\n\n${changelog}\n\n---\n\n完整更新日志请查看 [CHANGELOG.md](./CHANGELOG.md)",
|
|
126
|
+
"prerelease": false,
|
|
127
|
+
"draft": false,
|
|
128
|
+
"tokenRef": "GITEA_TOKEN",
|
|
129
|
+
"timeout": 30000
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### 预发布配置
|
|
136
|
+
|
|
137
|
+
```json
|
|
138
|
+
{
|
|
139
|
+
"plugins": {
|
|
140
|
+
"release-it-gitea": {
|
|
141
|
+
"host": "https://gitea.example.com",
|
|
142
|
+
"releaseTitle": "v${version} (预发布)",
|
|
143
|
+
"prerelease": true
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## 工作流程
|
|
150
|
+
|
|
151
|
+
1. **初始化阶段** - 验证配置和 API 连接
|
|
152
|
+
2. **发布阶段** - 创建或更新 Gitea 发布
|
|
153
|
+
3. **发布后阶段** - 显示发布链接
|
|
154
|
+
|
|
155
|
+
## 错误处理
|
|
156
|
+
|
|
157
|
+
插件包含完整的错误处理:
|
|
158
|
+
|
|
159
|
+
- 配置验证
|
|
160
|
+
- API token 验证
|
|
161
|
+
- 网络连接检查
|
|
162
|
+
- API 请求错误处理
|
|
163
|
+
- 详细的错误日志
|
|
164
|
+
|
|
165
|
+
## 开发
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
# 克隆仓库
|
|
169
|
+
git clone https://github.com/lib-pack/release-it-gitea.git
|
|
170
|
+
cd release-it-gitea
|
|
171
|
+
|
|
172
|
+
# 安装依赖
|
|
173
|
+
pnpm install
|
|
174
|
+
|
|
175
|
+
# 构建
|
|
176
|
+
pnpm build
|
|
177
|
+
|
|
178
|
+
# 运行测试
|
|
179
|
+
pnpm test
|
|
180
|
+
|
|
181
|
+
# 代码检查
|
|
182
|
+
pnpm lint
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## 许可证
|
|
186
|
+
|
|
187
|
+
MIT
|
|
188
|
+
|
|
189
|
+
## 贡献
|
|
190
|
+
|
|
191
|
+
欢迎提交 Issue 和 Pull Request!
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
declare module "release-it" {
|
|
2
|
+
interface Config {
|
|
3
|
+
gitea?: GiteaConfig;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface Context {
|
|
7
|
+
branchName: string;
|
|
8
|
+
changelog: string;
|
|
9
|
+
latestVersion: string;
|
|
10
|
+
name: string;
|
|
11
|
+
releaseUrl?: string;
|
|
12
|
+
repo: {
|
|
13
|
+
host: string;
|
|
14
|
+
owner: string;
|
|
15
|
+
project: string;
|
|
16
|
+
protocol: string;
|
|
17
|
+
remote: string;
|
|
18
|
+
repository: string;
|
|
19
|
+
};
|
|
20
|
+
version: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class Plugin {
|
|
24
|
+
config: Config;
|
|
25
|
+
context: Context;
|
|
26
|
+
log: {
|
|
27
|
+
error: (message: string) => void;
|
|
28
|
+
exec: (command: string) => void;
|
|
29
|
+
info: (message: string) => void;
|
|
30
|
+
verbose: (message: string) => void;
|
|
31
|
+
warn: (message: string) => void;
|
|
32
|
+
};
|
|
33
|
+
shell: {
|
|
34
|
+
exec: (command: string) => Promise<string>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
constructor(config: Config);
|
|
38
|
+
|
|
39
|
+
static disablePlugin(): void;
|
|
40
|
+
static isEnabled(config?: Config): boolean;
|
|
41
|
+
afterRelease(): Promise<void>;
|
|
42
|
+
beforeBump(): Promise<void>;
|
|
43
|
+
beforeRelease(): Promise<void>;
|
|
44
|
+
bump(): Promise<void>;
|
|
45
|
+
getIncrement(): string;
|
|
46
|
+
getIncrementedVersion(): string;
|
|
47
|
+
getIncrementedVersionCI(): string;
|
|
48
|
+
getInitialOptions(): unknown;
|
|
49
|
+
getLatestVersion(): string;
|
|
50
|
+
getName(): string;
|
|
51
|
+
init(): Promise<void>;
|
|
52
|
+
release(): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface GiteaConfig {
|
|
57
|
+
/** Gitea 服务器的完整 URL 地址 */
|
|
58
|
+
host: string;
|
|
59
|
+
|
|
60
|
+
/** 仓库所有者的用户名或组织名 */
|
|
61
|
+
owner: string;
|
|
62
|
+
|
|
63
|
+
/** 仓库名称 */
|
|
64
|
+
repository: string;
|
|
65
|
+
|
|
66
|
+
/** 是否启用发布创建功能 */
|
|
67
|
+
release?: boolean;
|
|
68
|
+
|
|
69
|
+
/** 发布标题的模板字符串,支持变量替换 */
|
|
70
|
+
releaseTitle?: string;
|
|
71
|
+
|
|
72
|
+
/** 发布说明的模板字符串,支持变量替换 */
|
|
73
|
+
releaseNotes?: string;
|
|
74
|
+
|
|
75
|
+
/** 是否标记为预发布版本 */
|
|
76
|
+
prerelease?: boolean;
|
|
77
|
+
|
|
78
|
+
/** 是否创建为草稿状态 */
|
|
79
|
+
draft?: boolean;
|
|
80
|
+
|
|
81
|
+
/** 存储 API token 的环境变量名称 */
|
|
82
|
+
tokenRef?: string;
|
|
83
|
+
|
|
84
|
+
/** API 请求的超时时间,单位为毫秒 */
|
|
85
|
+
timeout?: number;
|
|
86
|
+
}
|
package/lib/global.d.js
ADDED
|
File without changes
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Plugin, Config } from 'release-it';
|
|
2
|
+
|
|
3
|
+
declare class GiteaPlugin extends Plugin {
|
|
4
|
+
private giteaConfig;
|
|
5
|
+
constructor(config: Config);
|
|
6
|
+
static isEnabled(config?: Config): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* 获取并验证 Gitea 配置信息
|
|
9
|
+
* @returns 验证后的 Gitea 配置对象
|
|
10
|
+
* @throws 当配置缺失或无效时抛出错误
|
|
11
|
+
*/
|
|
12
|
+
private getGiteaConfig;
|
|
13
|
+
/**
|
|
14
|
+
* 从环境变量获取 API token.
|
|
15
|
+
* @returns API token 字符串
|
|
16
|
+
* @throws 当 token 不存在时抛出错误
|
|
17
|
+
*/
|
|
18
|
+
private getToken;
|
|
19
|
+
/**
|
|
20
|
+
* 构建完整的 API 请求 URL.
|
|
21
|
+
* @param endpoint API 端点路径
|
|
22
|
+
* @returns 完整的 API URL
|
|
23
|
+
*/
|
|
24
|
+
private buildApiUrl;
|
|
25
|
+
/**
|
|
26
|
+
* 发送 HTTP 请求到 Gitea API.
|
|
27
|
+
* @param endpoint API 端点路径
|
|
28
|
+
* @param options 请求选项
|
|
29
|
+
* @param options.body 请求体数据
|
|
30
|
+
* @param options.method HTTP 方法
|
|
31
|
+
* @returns API 响应数据
|
|
32
|
+
* @throws 当请求失败时抛出错误
|
|
33
|
+
*/
|
|
34
|
+
private apiRequest;
|
|
35
|
+
/**
|
|
36
|
+
* 检查指定标签的发布是否已存在.
|
|
37
|
+
* @param tagName Git 标签名
|
|
38
|
+
* @returns 发布是否存在
|
|
39
|
+
* @throws 当 API 请求失败时抛出错误
|
|
40
|
+
*/
|
|
41
|
+
private releaseExists;
|
|
42
|
+
/**
|
|
43
|
+
* 创建新的 Gitea 发布.
|
|
44
|
+
* @param releaseData 发布数据
|
|
45
|
+
* @returns 创建的发布信息
|
|
46
|
+
* @throws 当创建失败时抛出错误
|
|
47
|
+
*/
|
|
48
|
+
private createRelease;
|
|
49
|
+
/**
|
|
50
|
+
* 更新已存在的 Gitea 发布.
|
|
51
|
+
* @param tagName Git 标签名
|
|
52
|
+
* @param releaseData 更新的发布数据
|
|
53
|
+
* @returns 更新后的发布信息
|
|
54
|
+
* @throws 当更新失败时抛出错误
|
|
55
|
+
*/
|
|
56
|
+
private updateRelease;
|
|
57
|
+
/**
|
|
58
|
+
* 替换模板字符串中的变量占位符.
|
|
59
|
+
* @param template 包含变量占位符的模板字符串
|
|
60
|
+
* @returns 替换变量后的字符串
|
|
61
|
+
*/
|
|
62
|
+
private interpolate;
|
|
63
|
+
/**
|
|
64
|
+
* 初始化插件,验证配置和连接.
|
|
65
|
+
* @throws 当配置无效或连接失败时抛出错误
|
|
66
|
+
*/
|
|
67
|
+
init(): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* 执行发布操作,创建或更新 Gitea 发布.
|
|
70
|
+
* @throws 当发布创建失败时抛出错误
|
|
71
|
+
*/
|
|
72
|
+
release(): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* 发布完成后的清理和通知操作
|
|
75
|
+
*/
|
|
76
|
+
afterRelease(): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { GiteaPlugin as default };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import fetch from "node-fetch";
|
|
2
|
+
import { Plugin } from "release-it";
|
|
3
|
+
class GiteaPlugin extends Plugin {
|
|
4
|
+
giteaConfig;
|
|
5
|
+
constructor(config) {
|
|
6
|
+
super(config);
|
|
7
|
+
this.giteaConfig = this.getGiteaConfig();
|
|
8
|
+
}
|
|
9
|
+
static isEnabled(config) {
|
|
10
|
+
return Boolean(config?.gitea?.release);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 获取并验证 Gitea 配置信息
|
|
14
|
+
* @returns 验证后的 Gitea 配置对象
|
|
15
|
+
* @throws 当配置缺失或无效时抛出错误
|
|
16
|
+
*/
|
|
17
|
+
getGiteaConfig() {
|
|
18
|
+
const gitea = this.config.gitea;
|
|
19
|
+
if (!gitea) {
|
|
20
|
+
throw new Error("Gitea \u914D\u7F6E\u672A\u627E\u5230");
|
|
21
|
+
}
|
|
22
|
+
const config = {
|
|
23
|
+
draft: gitea.draft ?? false,
|
|
24
|
+
host: gitea.host,
|
|
25
|
+
owner: gitea.owner ?? this.context.repo.owner,
|
|
26
|
+
prerelease: gitea.prerelease ?? false,
|
|
27
|
+
release: gitea.release !== false,
|
|
28
|
+
releaseNotes: gitea.releaseNotes ?? "${changelog}",
|
|
29
|
+
releaseTitle: gitea.releaseTitle ?? "v${version}",
|
|
30
|
+
repository: gitea.repository ?? this.context.repo.repository,
|
|
31
|
+
timeout: gitea.timeout ?? 3e4,
|
|
32
|
+
tokenRef: gitea.tokenRef ?? "GITEA_TOKEN"
|
|
33
|
+
};
|
|
34
|
+
if (!config.host) {
|
|
35
|
+
throw new Error("Gitea host \u914D\u7F6E\u662F\u5FC5\u9700\u7684");
|
|
36
|
+
}
|
|
37
|
+
if (!config.owner) {
|
|
38
|
+
throw new Error("Gitea owner \u914D\u7F6E\u662F\u5FC5\u9700\u7684");
|
|
39
|
+
}
|
|
40
|
+
if (!config.repository) {
|
|
41
|
+
throw new Error("Gitea repository \u914D\u7F6E\u662F\u5FC5\u9700\u7684");
|
|
42
|
+
}
|
|
43
|
+
return config;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 从环境变量获取 API token.
|
|
47
|
+
* @returns API token 字符串
|
|
48
|
+
* @throws 当 token 不存在时抛出错误
|
|
49
|
+
*/
|
|
50
|
+
getToken() {
|
|
51
|
+
const tokenRef = this.giteaConfig.tokenRef;
|
|
52
|
+
if (!tokenRef) {
|
|
53
|
+
throw new Error("Token \u73AF\u5883\u53D8\u91CF\u540D\u672A\u914D\u7F6E");
|
|
54
|
+
}
|
|
55
|
+
const token = process.env[tokenRef];
|
|
56
|
+
if (!token) {
|
|
57
|
+
throw new Error(`Gitea API token \u672A\u627E\u5230\u3002\u8BF7\u8BBE\u7F6E\u73AF\u5883\u53D8\u91CF ${tokenRef}`);
|
|
58
|
+
}
|
|
59
|
+
return token;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 构建完整的 API 请求 URL.
|
|
63
|
+
* @param endpoint API 端点路径
|
|
64
|
+
* @returns 完整的 API URL
|
|
65
|
+
*/
|
|
66
|
+
buildApiUrl(endpoint) {
|
|
67
|
+
const host = this.giteaConfig.host.replace(/\/$/, "");
|
|
68
|
+
return `${host}/api/v1${endpoint}`;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 发送 HTTP 请求到 Gitea API.
|
|
72
|
+
* @param endpoint API 端点路径
|
|
73
|
+
* @param options 请求选项
|
|
74
|
+
* @param options.body 请求体数据
|
|
75
|
+
* @param options.method HTTP 方法
|
|
76
|
+
* @returns API 响应数据
|
|
77
|
+
* @throws 当请求失败时抛出错误
|
|
78
|
+
*/
|
|
79
|
+
async apiRequest(endpoint, options = {}) {
|
|
80
|
+
const url = this.buildApiUrl(endpoint);
|
|
81
|
+
const token = this.getToken();
|
|
82
|
+
const requestOptions = {
|
|
83
|
+
body: options.body ? JSON.stringify(options.body) : void 0,
|
|
84
|
+
headers: {
|
|
85
|
+
Accept: "application/json",
|
|
86
|
+
Authorization: `token ${token}`,
|
|
87
|
+
"Content-Type": "application/json"
|
|
88
|
+
},
|
|
89
|
+
method: options.method ?? "GET",
|
|
90
|
+
timeout: this.giteaConfig.timeout
|
|
91
|
+
};
|
|
92
|
+
this.log.verbose(`\u53D1\u9001 ${requestOptions.method} \u8BF7\u6C42\u5230: ${url}`);
|
|
93
|
+
try {
|
|
94
|
+
const response = await fetch(url, requestOptions);
|
|
95
|
+
if (!response.ok) {
|
|
96
|
+
const errorText = await response.text();
|
|
97
|
+
throw new Error(
|
|
98
|
+
`Gitea API \u8BF7\u6C42\u5931\u8D25 (${response.status.toString()}): ${errorText}`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
const data = await response.json();
|
|
102
|
+
return data;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (error instanceof Error) {
|
|
105
|
+
throw new Error(`Gitea API \u8BF7\u6C42\u5931\u8D25: ${error.message}`);
|
|
106
|
+
}
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 检查指定标签的发布是否已存在.
|
|
112
|
+
* @param tagName Git 标签名
|
|
113
|
+
* @returns 发布是否存在
|
|
114
|
+
* @throws 当 API 请求失败时抛出错误
|
|
115
|
+
*/
|
|
116
|
+
async releaseExists(tagName) {
|
|
117
|
+
try {
|
|
118
|
+
const endpoint = `/repos/${this.giteaConfig.owner}/${this.giteaConfig.repository}/releases/tags/${tagName}`;
|
|
119
|
+
await this.apiRequest(endpoint);
|
|
120
|
+
return true;
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (error instanceof Error && error.message.includes("404")) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 创建新的 Gitea 发布.
|
|
130
|
+
* @param releaseData 发布数据
|
|
131
|
+
* @returns 创建的发布信息
|
|
132
|
+
* @throws 当创建失败时抛出错误
|
|
133
|
+
*/
|
|
134
|
+
async createRelease(releaseData) {
|
|
135
|
+
const endpoint = `/repos/${this.giteaConfig.owner}/${this.giteaConfig.repository}/releases`;
|
|
136
|
+
return await this.apiRequest(endpoint, {
|
|
137
|
+
body: releaseData,
|
|
138
|
+
method: "POST"
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 更新已存在的 Gitea 发布.
|
|
143
|
+
* @param tagName Git 标签名
|
|
144
|
+
* @param releaseData 更新的发布数据
|
|
145
|
+
* @returns 更新后的发布信息
|
|
146
|
+
* @throws 当更新失败时抛出错误
|
|
147
|
+
*/
|
|
148
|
+
async updateRelease(tagName, releaseData) {
|
|
149
|
+
const endpoint = `/repos/${this.giteaConfig.owner}/${this.giteaConfig.repository}/releases/tags/${tagName}`;
|
|
150
|
+
return await this.apiRequest(endpoint, {
|
|
151
|
+
body: releaseData,
|
|
152
|
+
method: "PATCH"
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* 替换模板字符串中的变量占位符.
|
|
157
|
+
* @param template 包含变量占位符的模板字符串
|
|
158
|
+
* @returns 替换变量后的字符串
|
|
159
|
+
*/
|
|
160
|
+
interpolate(template) {
|
|
161
|
+
return template.replace(/\$\{version\}/g, this.context.version).replace(/\$\{latestVersion\}/g, this.context.latestVersion).replace(/\$\{changelog\}/g, this.context.changelog).replace(/\$\{name\}/g, this.context.name).replace(/\$\{repo\.owner\}/g, this.context.repo.owner).replace(/\$\{repo\.repository\}/g, this.context.repo.repository).replace(/\$\{branchName\}/g, this.context.branchName);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* 初始化插件,验证配置和连接.
|
|
165
|
+
* @throws 当配置无效或连接失败时抛出错误
|
|
166
|
+
*/
|
|
167
|
+
async init() {
|
|
168
|
+
this.log.info("\u521D\u59CB\u5316 Gitea \u63D2\u4EF6");
|
|
169
|
+
try {
|
|
170
|
+
this.getToken();
|
|
171
|
+
this.log.verbose("Gitea API token \u9A8C\u8BC1\u6210\u529F");
|
|
172
|
+
} catch (error) {
|
|
173
|
+
if (error instanceof Error) {
|
|
174
|
+
this.log.error(error.message);
|
|
175
|
+
}
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
const endpoint = `/repos/${this.giteaConfig.owner}/${this.giteaConfig.repository}`;
|
|
180
|
+
await this.apiRequest(endpoint);
|
|
181
|
+
this.log.verbose("Gitea API \u8FDE\u63A5\u6D4B\u8BD5\u6210\u529F");
|
|
182
|
+
} catch (error) {
|
|
183
|
+
if (error instanceof Error) {
|
|
184
|
+
this.log.error(`\u65E0\u6CD5\u8FDE\u63A5\u5230 Gitea \u4ED3\u5E93: ${error.message}`);
|
|
185
|
+
}
|
|
186
|
+
throw error;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* 执行发布操作,创建或更新 Gitea 发布.
|
|
191
|
+
* @throws 当发布创建失败时抛出错误
|
|
192
|
+
*/
|
|
193
|
+
async release() {
|
|
194
|
+
if (!this.giteaConfig.release) {
|
|
195
|
+
this.log.info("Gitea \u53D1\u5E03\u529F\u80FD\u5DF2\u7981\u7528");
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const tagName = `v${this.context.version}`;
|
|
199
|
+
const releaseTitle = this.interpolate(
|
|
200
|
+
this.giteaConfig.releaseTitle ?? "v${version}"
|
|
201
|
+
);
|
|
202
|
+
const releaseNotes = this.interpolate(
|
|
203
|
+
this.giteaConfig.releaseNotes ?? "${changelog}"
|
|
204
|
+
);
|
|
205
|
+
this.log.info(`\u51C6\u5907\u521B\u5EFA Gitea \u53D1\u5E03: ${releaseTitle}`);
|
|
206
|
+
const releaseData = {
|
|
207
|
+
body: releaseNotes,
|
|
208
|
+
draft: this.giteaConfig.draft ?? false,
|
|
209
|
+
name: releaseTitle,
|
|
210
|
+
prerelease: this.giteaConfig.prerelease ?? false,
|
|
211
|
+
tag_name: tagName
|
|
212
|
+
};
|
|
213
|
+
try {
|
|
214
|
+
const exists = await this.releaseExists(tagName);
|
|
215
|
+
let release;
|
|
216
|
+
if (exists) {
|
|
217
|
+
this.log.info(`\u53D1\u5E03 ${tagName} \u5DF2\u5B58\u5728\uFF0C\u6B63\u5728\u66F4\u65B0...`);
|
|
218
|
+
release = await this.updateRelease(tagName, releaseData);
|
|
219
|
+
} else {
|
|
220
|
+
this.log.info(`\u6B63\u5728\u521B\u5EFA\u65B0\u53D1\u5E03 ${tagName}...`);
|
|
221
|
+
release = await this.createRelease(releaseData);
|
|
222
|
+
}
|
|
223
|
+
this.log.info(`\u2705 Gitea \u53D1\u5E03\u521B\u5EFA\u6210\u529F: ${release.html_url}`);
|
|
224
|
+
this.context.releaseUrl = release.html_url;
|
|
225
|
+
} catch (error) {
|
|
226
|
+
if (error instanceof Error) {
|
|
227
|
+
this.log.error(`\u274C \u521B\u5EFA Gitea \u53D1\u5E03\u5931\u8D25: ${error.message}`);
|
|
228
|
+
}
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 发布完成后的清理和通知操作
|
|
234
|
+
*/
|
|
235
|
+
async afterRelease() {
|
|
236
|
+
if (this.context.releaseUrl) {
|
|
237
|
+
this.log.info(`\u{1F389} \u53D1\u5E03\u5B8C\u6210! \u67E5\u770B\u53D1\u5E03: ${this.context.releaseUrl}`);
|
|
238
|
+
}
|
|
239
|
+
return Promise.resolve();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
var index_default = GiteaPlugin;
|
|
243
|
+
export {
|
|
244
|
+
index_default as default
|
|
245
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "release-it-gitea",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "release-it gitea plugin",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"gitea",
|
|
7
|
+
"release-it",
|
|
8
|
+
"release-it-plugin"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/lib-pack/release-it-gitea.git"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "lyda",
|
|
17
|
+
"email": "1829913225@qq.com"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "lib/index.js",
|
|
21
|
+
"files": [
|
|
22
|
+
"LICENSE.md",
|
|
23
|
+
"README.md",
|
|
24
|
+
"lib/",
|
|
25
|
+
"package.json"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup",
|
|
29
|
+
"format": "prettier .",
|
|
30
|
+
"lint": "eslint . --max-warnings 0",
|
|
31
|
+
"lint:knip": "knip",
|
|
32
|
+
"lint:md": "markdownlint \"**/*.md\" \".github/**/*.md\" --rules sentences-per-line",
|
|
33
|
+
"lint:packages": "pnpm dedupe --check",
|
|
34
|
+
"lint:spelling": "cspell \"**\" \".github/**/*\"",
|
|
35
|
+
"prepare": "husky",
|
|
36
|
+
"test": "vitest",
|
|
37
|
+
"tsc": "tsc"
|
|
38
|
+
},
|
|
39
|
+
"lint-staged": {
|
|
40
|
+
"*": "prettier --ignore-unknown --write"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"node-fetch": "^3.3.2"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@eslint-community/eslint-plugin-eslint-comments": "4.5.0",
|
|
47
|
+
"@eslint/js": "9.22.0",
|
|
48
|
+
"@release-it/conventional-changelog": "10.0.0",
|
|
49
|
+
"@types/eslint-plugin-markdown": "2.0.2",
|
|
50
|
+
"@types/node": "22.13.10",
|
|
51
|
+
"@types/node-fetch": "^2.6.11",
|
|
52
|
+
"@vitest/coverage-v8": "3.0.9",
|
|
53
|
+
"@vitest/eslint-plugin": "1.1.38",
|
|
54
|
+
"console-fail-test": "0.5.0",
|
|
55
|
+
"cspell": "8.17.5",
|
|
56
|
+
"eslint": "9.22.0",
|
|
57
|
+
"eslint-plugin-jsdoc": "50.6.8",
|
|
58
|
+
"eslint-plugin-jsonc": "2.20.0",
|
|
59
|
+
"eslint-plugin-markdown": "5.1.0",
|
|
60
|
+
"eslint-plugin-n": "17.16.2",
|
|
61
|
+
"eslint-plugin-package-json": "0.29.0",
|
|
62
|
+
"eslint-plugin-perfectionist": "4.11.0",
|
|
63
|
+
"eslint-plugin-regexp": "2.7.0",
|
|
64
|
+
"eslint-plugin-yml": "1.17.0",
|
|
65
|
+
"husky": "9.1.7",
|
|
66
|
+
"knip": "5.46.0",
|
|
67
|
+
"lint-staged": "15.5.0",
|
|
68
|
+
"markdownlint": "0.37.4",
|
|
69
|
+
"markdownlint-cli": "0.44.0",
|
|
70
|
+
"prettier": "3.5.3",
|
|
71
|
+
"prettier-plugin-curly": "0.3.1",
|
|
72
|
+
"prettier-plugin-packagejson": "2.5.10",
|
|
73
|
+
"prettier-plugin-sh": "0.15.0",
|
|
74
|
+
"release-it": "19.0.3",
|
|
75
|
+
"sentences-per-line": "0.3.0",
|
|
76
|
+
"tsup": "8.4.0",
|
|
77
|
+
"typescript": "5.8.2",
|
|
78
|
+
"typescript-eslint": "8.26.1",
|
|
79
|
+
"vitest": "3.0.9"
|
|
80
|
+
},
|
|
81
|
+
"peerDependencies": {
|
|
82
|
+
"release-it": "^19.0.3"
|
|
83
|
+
},
|
|
84
|
+
"packageManager": "pnpm@10.4.0",
|
|
85
|
+
"engines": {
|
|
86
|
+
"node": ">=18.3.0"
|
|
87
|
+
},
|
|
88
|
+
"publishConfig": {
|
|
89
|
+
"provenance": true
|
|
90
|
+
}
|
|
91
|
+
}
|