ai-git-tools 2.0.12 → 2.0.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-git-tools",
3
- "version": "2.0.12",
3
+ "version": "2.0.13",
4
4
  "description": "AI-powered Git automation tools for commit messages and PR generation",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -22,6 +22,8 @@ export async function prCommand() {
22
22
  // 載入配置(使用 scripts/ 的配置載入邏輯)
23
23
  const config = await loadConfig();
24
24
 
25
+ console.log('📋 配置:', config);
26
+
25
27
  if (config.output.verbose) {
26
28
  console.log('📋 使用配置:');
27
29
  console.log(` AI Model: ${config.ai.model}`);
@@ -8,8 +8,40 @@ import { colors } from '../utils/constants.js';
8
8
  */
9
9
  export class GitHubAPI {
10
10
  constructor(config = {}) {
11
- // 如果沒有提供 orgName 或為空字串,則不設置默認值
12
- this.orgName = config.orgName || null;
11
+ // 如果沒有提供 orgName 或為空字串,則自動從 git remote 偵測
12
+ this.orgName = config.orgName || this.detectOrgFromRemote();
13
+ }
14
+
15
+ /**
16
+ * 從 git remote URL 自動偵測組織名稱
17
+ */
18
+ detectOrgFromRemote() {
19
+ try {
20
+ const remoteUrl = execSync('git remote get-url origin', {
21
+ encoding: 'utf-8',
22
+ stdio: ['pipe', 'pipe', 'pipe'],
23
+ }).trim();
24
+
25
+ // 解析 HTTPS 格式: https://github.com/org/repo.git
26
+ const httpsMatch = remoteUrl.match(/github\.com\/([^/]+)\//);
27
+ if (httpsMatch) {
28
+ log.info(`自動偵測組織名稱: ${httpsMatch[1]}\n`);
29
+ return httpsMatch[1];
30
+ }
31
+
32
+ // 解析 SSH 格式: git@github.com:org/repo.git
33
+ const sshMatch = remoteUrl.match(/github\.com:([^/]+)\//);
34
+ if (sshMatch) {
35
+ log.info(`自動偵測組織名稱: ${sshMatch[1]}\n`);
36
+ return sshMatch[1];
37
+ }
38
+
39
+ log.warning('無法從 git remote 偵測組織名稱');
40
+ return null;
41
+ } catch (error) {
42
+ log.warning('無法從 git remote 偵測組織名稱');
43
+ return null;
44
+ }
13
45
  }
14
46
 
15
47
  /**
@@ -83,8 +115,8 @@ export class GitHubAPI {
83
115
  async fetchTeams(orgName = this.orgName) {
84
116
  // 如果沒有 orgName,直接返回空結果
85
117
  if (!orgName || orgName.trim() === '') {
86
- log.warning('未設置 GitHub 組織名稱');
87
- log.info('請在 .ai-pr-config.mjs 中設置 github.orgName 或使用 --org 參數\n');
118
+ log.warning('未設置 GitHub 組織名稱,且無法自動偵測');
119
+ log.info('請在 .ai-pr-config.js 中設置 github.orgName 或使用 --org 參數\n');
88
120
  return { teams: {}, members: [] };
89
121
  }
90
122
 
@@ -219,11 +251,15 @@ export class GitHubAPI {
219
251
  // 創建新 PR
220
252
  log.step('正在創建 Pull Request...');
221
253
 
222
- // 使用完整的分支格式(包含組織名稱)以避免 GitHub API 錯誤
223
- const fullHeadBranch = headBranch.includes(':')
224
- ? headBranch
225
- : `${this.orgName}:${headBranch}`;
226
- let createCmd = `gh pr create --base "${baseBranch}" --head "${fullHeadBranch}" --title "${escapedTitle}" --body-file "${bodyFile}"`;
254
+ // 如果有 orgName 且是組織 repo,使用完整的分支格式
255
+ // 如果沒有 orgName,直接使用分支名稱(gh CLI 會自動處理)
256
+ let headRef = headBranch;
257
+ if (headBranch.includes(':')) {
258
+ headRef = headBranch;
259
+ } else if (this.orgName) {
260
+ headRef = `${this.orgName}:${headBranch}`;
261
+ }
262
+ let createCmd = `gh pr create --base "${baseBranch}" --head "${headRef}" --title "${escapedTitle}" --body-file "${bodyFile}"`;
227
263
 
228
264
  if (draftFlag) {
229
265
  createCmd += ` ${draftFlag}`;