release-it-gitea 1.5.0 → 1.7.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/README.en.md CHANGED
@@ -92,7 +92,7 @@ npx release-it
92
92
  "README.md",
93
93
  {
94
94
  "path": "dist/**/*",
95
- "name": "distribution-${version}.zip",
95
+ "name": "distribution-file.zip",
96
96
  "type": "zip",
97
97
  "label": "Distribution Files"
98
98
  }
@@ -138,7 +138,7 @@ Use object format for more detailed configuration:
138
138
  },
139
139
  {
140
140
  "path": "src",
141
- "name": "source-code-${version}.zip",
141
+ "name": "source-code-file.zip",
142
142
  "type": "zip",
143
143
  "label": "Source Code"
144
144
  }
@@ -177,7 +177,7 @@ Use object format for more detailed configuration:
177
177
  "assets": [
178
178
  {
179
179
  "path": "src/**/*",
180
- "name": "source-v${version}.zip",
180
+ "name": "source-vfile.zip",
181
181
  "type": "zip",
182
182
  "label": "Source Code"
183
183
  }
@@ -213,13 +213,13 @@ Use object format for more detailed configuration:
213
213
  "assets": [
214
214
  {
215
215
  "path": "dist/prod/**/*",
216
- "name": "production-build-${version}.zip",
216
+ "name": "production-build-file.zip",
217
217
  "type": "zip",
218
218
  "label": "Production Build"
219
219
  },
220
220
  {
221
221
  "path": "dist/dev/**/*",
222
- "name": "development-build-${version}.zip",
222
+ "name": "development-build-file.zip",
223
223
  "type": "zip",
224
224
  "label": "Development Build"
225
225
  }
@@ -249,7 +249,7 @@ export MY_GITEA_TOKEN=your_gitea_api_token
249
249
 
250
250
  ## Template Variables
251
251
 
252
- The following variables can be used in `releaseTitle`, `releaseNotes`, and asset `name`:
252
+ The following variables can be used in `releaseTitle` and `releaseNotes`:
253
253
 
254
254
  | Variable | Description | Example |
255
255
  | -------------------- | ---------------- | -------------------- |
@@ -270,14 +270,16 @@ The following variables can be used in `releaseTitle`, `releaseNotes`, and asset
270
270
  "assets": [
271
271
  {
272
272
  "path": "dist/**/*",
273
- "name": "${name}-${version}-dist.zip",
273
+ "name": "distribution-files.zip",
274
274
  "type": "zip",
275
- "label": "${name} v${version} Distribution"
275
+ "label": "Distribution Files"
276
276
  }
277
277
  ]
278
278
  }
279
279
  ```
280
280
 
281
+ > **Note**: The `name` and `label` fields in asset configuration do not support template variable substitution and require static strings.
282
+
281
283
  ## Troubleshooting
282
284
 
283
285
  ### Common Issues and Solutions
package/README.md CHANGED
@@ -57,19 +57,19 @@ npx release-it
57
57
 
58
58
  ### 基本配置选项
59
59
 
60
- | 选项 | 类型 | 默认值 | 描述 |
61
- | -------------- | ------- | ---------------------- | -------------------- |
62
- | `host` | string | 当前仓库的 host | Gitea 服务器 URL |
63
- | `owner` | string | 从 git remote 自动检测 | 仓库所有者 |
64
- | `repository` | string | 从 git remote 自动检测 | 仓库名称 |
65
- | `release` | boolean | `true` | 是否创建发布 |
66
- | `releaseTitle` | string | `"v${version}"` | 发布标题模板 |
67
- | `releaseNotes` | string | `"${changelog}"` | 发布说明模板 |
68
- | `prerelease` | boolean | `false` | 是否为预发布 |
69
- | `draft` | boolean | `false` | 是否为草稿 |
70
- | `tokenRef` | string | `"GITEA_TOKEN"` | API token 环境变量名 |
71
- | `timeout` | number | `30000` | 请求超时时间(毫秒) |
72
- | `assets` | array | `[]` | 附加的资源文件 |
60
+ | 选项 | 类型 | 默认值 | 描述 |
61
+ | -------------- | ----------------------------- | ---------------------- | -------------------------------------------------- |
62
+ | `host` | string | 当前仓库的 host | Gitea 服务器 URL |
63
+ | `owner` | string | 从 git remote 自动检测 | 仓库所有者 |
64
+ | `repository` | string | 从 git remote 自动检测 | 仓库名称 |
65
+ | `release` | boolean | `true` | 是否创建发布 |
66
+ | `releaseTitle` | string \| (context) => string | `"v${version}"` | 发布标题模板,支持变量替换和函数回调 |
67
+ | `releaseNotes` | string \| (context) => string | `"${changelog}"` | 发布说明模板,支持变量替换、Markdown格式和函数回调 |
68
+ | `prerelease` | boolean | `false` | 是否为预发布 |
69
+ | `draft` | boolean | `false` | 是否为草稿 |
70
+ | `tokenRef` | string | `"GITEA_TOKEN"` | API token 环境变量名 |
71
+ | `timeout` | number | `30000` | 请求超时时间(毫秒) |
72
+ | `assets` | array | `[]` | 附加的资源文件 |
73
73
 
74
74
  ### 完整配置示例
75
75
 
@@ -92,7 +92,7 @@ npx release-it
92
92
  "README.md",
93
93
  {
94
94
  "path": "dist/**/*",
95
- "name": "distribution-${version}.zip",
95
+ "name": "distribution-file.zip",
96
96
  "type": "zip",
97
97
  "label": "Distribution Files"
98
98
  }
@@ -138,7 +138,7 @@ npx release-it
138
138
  },
139
139
  {
140
140
  "path": "src",
141
- "name": "source-code-${version}.zip",
141
+ "name": "source-code-file.zip",
142
142
  "type": "zip",
143
143
  "label": "Source Code"
144
144
  }
@@ -177,7 +177,7 @@ npx release-it
177
177
  "assets": [
178
178
  {
179
179
  "path": "src/**/*",
180
- "name": "source-v${version}.zip",
180
+ "name": "source-vfile.zip",
181
181
  "type": "zip",
182
182
  "label": "Source Code"
183
183
  }
@@ -213,13 +213,13 @@ npx release-it
213
213
  "assets": [
214
214
  {
215
215
  "path": "dist/prod/**/*",
216
- "name": "production-build-${version}.zip",
216
+ "name": "production-build-file.zip",
217
217
  "type": "zip",
218
218
  "label": "Production Build"
219
219
  },
220
220
  {
221
221
  "path": "dist/dev/**/*",
222
- "name": "development-build-${version}.zip",
222
+ "name": "development-build-file.zip",
223
223
  "type": "zip",
224
224
  "label": "Development Build"
225
225
  }
@@ -249,7 +249,7 @@ export MY_GITEA_TOKEN=your_gitea_api_token
249
249
 
250
250
  ## 模板变量
251
251
 
252
- 在 `releaseTitle`、`releaseNotes` 和附件 `name` 中可以使用以下变量:
252
+ 在 `releaseTitle` `releaseNotes` 中可以使用以下变量:
253
253
 
254
254
  | 变量 | 描述 | 示例 |
255
255
  | -------------------- | ------------ | -------------------- |
@@ -261,6 +261,147 @@ export MY_GITEA_TOKEN=your_gitea_api_token
261
261
  | `${repo.repository}` | 仓库名称 | `my-repo` |
262
262
  | `${branchName}` | 分支名称 | `main` |
263
263
 
264
+ ### 函数回调配置
265
+
266
+ 在 JavaScript 配置文件中,可以为 `releaseTitle` 和 `releaseNotes` 传入函数回调,实现更灵活的配置:
267
+
268
+ ```js
269
+ // .release-it.js
270
+ module.exports = {
271
+ plugins: {
272
+ "release-it-gitea": {
273
+ host: "https://gitea.example.com",
274
+ owner: "your-username",
275
+ repository: "your-repo",
276
+ // 使用函数生成动态的发布标题
277
+ releaseTitle: (context) => {
278
+ const date = new Date().toISOString().split("T")[0];
279
+ return `🚀 ${context.name} v${context.version} (${date})`;
280
+ },
281
+ // 使用函数生成动态的发布说明
282
+ releaseNotes: (context) => {
283
+ const sections = context.changelog.split("\n## ");
284
+ const features = sections.find(
285
+ (s) => s.startsWith("Features") || s.startsWith("新特性"),
286
+ );
287
+ const fixes = sections.find(
288
+ (s) => s.startsWith("Bug Fixes") || s.startsWith("修复"),
289
+ );
290
+
291
+ return `## ${context.name} v${context.version} 发布说明
292
+
293
+ ## ✨ 新特性
294
+ ${features ? "## " + features : "无"}
295
+
296
+ ## 🐛 问题修复
297
+ ${fixes ? "## " + fixes : "无"}
298
+
299
+ ## 📦 安装
300
+ \`\`\`
301
+ npm install ${context.name}@${context.version}
302
+ \`\`\``;
303
+ },
304
+ // 其他配置...
305
+ },
306
+ },
307
+ };
308
+ ```
309
+
310
+ 使用函数回调的优势:
311
+
312
+ - 可以进行复杂的逻辑处理
313
+ - 可以访问完整的 context 对象
314
+ - 可以根据条件动态生成内容
315
+ - 可以整合外部数据或API结果
316
+
317
+ > **注意**: 函数回调仅在使用 JavaScript 配置文件 (如 `.release-it.js` 或 `.release-it.cjs`) 时可用,在 JSON 配置文件中只能使用字符串模板。
318
+
319
+ ### 使用 NPM 包
320
+
321
+ `releaseTitle` 和 `releaseNotes` 还支持通过 `npm:` 前缀引用外部 NPM 包来生成内容:
322
+
323
+ ```json
324
+ {
325
+ "plugins": {
326
+ "release-it-gitea": {
327
+ "releaseTitle": "npm:my-release-notes-generator",
328
+ "releaseNotes": "npm:my-changelog-formatter"
329
+ }
330
+ }
331
+ }
332
+ ```
333
+
334
+ 使用 NPM 包的方式:
335
+
336
+ 1. 创建并发布一个 NPM 包,该包导出以下方法:
337
+
338
+ ```js
339
+ // my-release-notes-generator 包示例
340
+ module.exports = {
341
+ releaseTitle: function (context) {
342
+ return `Release v${context.version} - ${new Date().toLocaleDateString()}`;
343
+ },
344
+ releaseNotes: function (context) {
345
+ // 自定义格式化逻辑
346
+ return `# ${context.name} v${context.version}\n\n${context.changelog}`;
347
+ },
348
+ };
349
+ ```
350
+
351
+ 2. 安装该包:
352
+
353
+ ```bash
354
+ npm install --save-dev my-release-notes-generator
355
+ ```
356
+
357
+ 3. 在配置中引用:
358
+ ```json
359
+ {
360
+ "releaseTitle": "npm:my-release-notes-generator",
361
+ "releaseNotes": "npm:my-release-notes-generator"
362
+ }
363
+ ```
364
+
365
+ 使用 NPM 包的优势:
366
+
367
+ - 可以在多个项目间共享相同的发布标题和说明格式
368
+ - 可以独立于项目维护和更新发布格式
369
+ - 支持更复杂的逻辑和依赖
370
+ - 可以在 JSON 配置中使用,不仅限于 JavaScript 配置
371
+
372
+ ### Context 对象属性
373
+
374
+ 在函数回调中,您可以访问以下 context 对象属性:
375
+
376
+ | 属性 | 类型 | 描述 | 示例值 |
377
+ | ----------------- | ------ | ----------------------------- | -------------------------------- |
378
+ | `version` | string | 当前版本号 | `"1.2.3"` |
379
+ | `latestVersion` | string | 上一个版本号 | `"1.2.2"` |
380
+ | `changelog` | string | 生成的变更日志内容 | `"## Bug Fixes\n\n* 修复..."` |
381
+ | `name` | string | 项目名称 | `"my-project"` |
382
+ | `branchName` | string | 当前分支名 | `"main"` |
383
+ | `releaseUrl` | string | 发布 URL (仅在更新发布后可用) | `"https://gitea.com/.../v1.2.3"` |
384
+ | `repo` | object | 仓库相关信息 | |
385
+ | `repo.host` | string | 仓库主机地址 | `"gitea.example.com"` |
386
+ | `repo.owner` | string | 仓库所有者 | `"username"` |
387
+ | `repo.project` | string | 项目名称 | `"my-repo"` |
388
+ | `repo.protocol` | string | 仓库协议 | `"https"` |
389
+ | `repo.remote` | string | 远程仓库名称 | `"origin"` |
390
+ | `repo.repository` | string | 仓库名称 | `"my-repo"` |
391
+
392
+ **示例:使用 context 属性生成自定义发布标题**
393
+
394
+ ```js
395
+ releaseTitle: (context) => {
396
+ const emoji = context.version.includes("beta")
397
+ ? "🧪"
398
+ : context.version.includes("alpha")
399
+ ? "🚧"
400
+ : "🚀";
401
+ return `${emoji} ${context.name} v${context.version} [${context.branchName}]`;
402
+ };
403
+ ```
404
+
264
405
  ### 模板使用示例
265
406
 
266
407
  ```json
@@ -270,14 +411,16 @@ export MY_GITEA_TOKEN=your_gitea_api_token
270
411
  "assets": [
271
412
  {
272
413
  "path": "dist/**/*",
273
- "name": "${name}-${version}-dist.zip",
414
+ "name": "distribution-files.zip",
274
415
  "type": "zip",
275
- "label": "${name} v${version} Distribution"
416
+ "label": "Distribution Files"
276
417
  }
277
418
  ]
278
419
  }
279
420
  ```
280
421
 
422
+ > **注意**: 附件配置中的 `name` 和 `label` 字段不支持模板变量替换,需要使用静态字符串。
423
+
281
424
  ## 故障排除
282
425
 
283
426
  ### 常见问题及解决方案
package/lib/global.d.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  declare module "release-it" {
2
2
  interface Config {
3
- getContext(path?: string): unknown;
3
+ getContext(): Context;
4
+ getContext(path: string): unknown;
4
5
  setContext(path: string, value: unknown): void;
5
6
  }
6
7
 
@@ -83,10 +84,10 @@ interface GiteaConfig {
83
84
  release?: boolean;
84
85
 
85
86
  /** 发布标题的模板字符串,支持变量替换 */
86
- releaseTitle?: string;
87
+ releaseTitle?: ((context: Context) => string) | string;
87
88
 
88
89
  /** 发布说明的模板字符串,支持变量替换 */
89
- releaseNotes?: string;
90
+ releaseNotes?: ((context: Context) => string) | string;
90
91
 
91
92
  /** 是否标记为预发布版本 */
92
93
  prerelease?: boolean;
package/lib/index.d.ts CHANGED
@@ -98,6 +98,13 @@ declare class GiteaPlugin extends Plugin {
98
98
  * @param config 附件配置
99
99
  */
100
100
  private processAsset;
101
+ /**
102
+ * 处理单个附件配置
103
+ * @param releaseId 发布 ID
104
+ * @param config 附件配置
105
+ */
106
+ private getReleaseNotes;
107
+ private getReleaseTitle;
101
108
  /**
102
109
  * 执行发布操作,创建或更新 Gitea 发布.
103
110
  * @throws 当发布创建失败时抛出错误
package/lib/index.js CHANGED
@@ -343,6 +343,57 @@ class GiteaPlugin extends Plugin {
343
343
  }
344
344
  }
345
345
  }
346
+ /**
347
+ * 处理单个附件配置
348
+ * @param releaseId 发布 ID
349
+ * @param config 附件配置
350
+ */
351
+ getReleaseNotes() {
352
+ const releaseNotes = this.giteaConfig.releaseNotes;
353
+ if (typeof releaseNotes === "string") {
354
+ if (releaseNotes.startsWith("npm:")) {
355
+ const npmPackage = releaseNotes.slice(4);
356
+ try {
357
+ const npmHandler = require(npmPackage);
358
+ if (typeof npmHandler !== "function") {
359
+ throw new Error(`${npmPackage} not found npm`);
360
+ }
361
+ return npmHandler.releaseNotes(this.config.getContext());
362
+ } catch (error) {
363
+ console.error(error);
364
+ throw new Error(`${npmPackage} not found npm`);
365
+ }
366
+ } else {
367
+ return this.interpolate(releaseNotes);
368
+ }
369
+ } else if (typeof releaseNotes === "function") {
370
+ return releaseNotes(this.config.getContext());
371
+ }
372
+ return this.config.getContext("changelog");
373
+ }
374
+ getReleaseTitle() {
375
+ const releaseTitle = this.giteaConfig.releaseTitle;
376
+ if (typeof releaseTitle === "string") {
377
+ if (releaseTitle.startsWith("npm:")) {
378
+ const npmPackage = releaseTitle.slice(4);
379
+ try {
380
+ const npmHandler = require(npmPackage);
381
+ if (typeof npmHandler !== "function") {
382
+ throw new Error(`${npmPackage} not found npm`);
383
+ }
384
+ return npmHandler.releaseTitle(this.config.getContext());
385
+ } catch (error) {
386
+ console.error(error);
387
+ throw new Error(`${npmPackage} not found npm`);
388
+ }
389
+ } else {
390
+ return this.interpolate(releaseTitle);
391
+ }
392
+ } else if (typeof releaseTitle === "function") {
393
+ return releaseTitle(this.config.getContext());
394
+ }
395
+ return this.config.getContext("version");
396
+ }
346
397
  /**
347
398
  * 执行发布操作,创建或更新 Gitea 发布.
348
399
  * @throws 当发布创建失败时抛出错误
@@ -353,12 +404,8 @@ class GiteaPlugin extends Plugin {
353
404
  return;
354
405
  }
355
406
  const tagName = this.config.getContext("tagName");
356
- const releaseTitle = this.interpolate(
357
- this.giteaConfig.releaseTitle ?? "v${version}"
358
- );
359
- const releaseNotes = this.interpolate(
360
- this.giteaConfig.releaseNotes ?? "${changelog}"
361
- );
407
+ const releaseTitle = this.getReleaseTitle();
408
+ const releaseNotes = this.getReleaseNotes();
362
409
  this.log.info(`\u51C6\u5907\u521B\u5EFA Gitea \u53D1\u5E03: ${releaseTitle}`);
363
410
  const releaseData = {
364
411
  body: releaseNotes,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "release-it-gitea",
3
- "version": "1.5.0",
3
+ "version": "1.7.0",
4
4
  "description": "release-it gitea plugin",
5
5
  "keywords": [
6
6
  "gitea",