release-it-gitea 1.6.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.
Files changed (3) hide show
  1. package/README.md +154 -13
  2. package/lib/index.js +30 -2
  3. package/package.json +1 -1
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
 
@@ -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
package/lib/index.js CHANGED
@@ -351,7 +351,21 @@ class GiteaPlugin extends Plugin {
351
351
  getReleaseNotes() {
352
352
  const releaseNotes = this.giteaConfig.releaseNotes;
353
353
  if (typeof releaseNotes === "string") {
354
- return this.interpolate(releaseNotes);
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
+ }
355
369
  } else if (typeof releaseNotes === "function") {
356
370
  return releaseNotes(this.config.getContext());
357
371
  }
@@ -360,7 +374,21 @@ class GiteaPlugin extends Plugin {
360
374
  getReleaseTitle() {
361
375
  const releaseTitle = this.giteaConfig.releaseTitle;
362
376
  if (typeof releaseTitle === "string") {
363
- return this.interpolate(releaseTitle);
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
+ }
364
392
  } else if (typeof releaseTitle === "function") {
365
393
  return releaseTitle(this.config.getContext());
366
394
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "release-it-gitea",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "release-it gitea plugin",
5
5
  "keywords": [
6
6
  "gitea",