ai-worklog 1.0.0 → 1.0.2

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/bin/index.js CHANGED
@@ -132,8 +132,9 @@ async function setupGitlabConfig() {
132
132
  } catch {}
133
133
  }
134
134
 
135
- // 尝试从 glab 配置自动读取
136
135
  let token = '', username = '';
136
+
137
+ // 来源1:glab 配置文件
137
138
  const glabPaths = [
138
139
  path.join(os.homedir(), 'Library', 'Application Support', 'glab-cli', 'config.yml'),
139
140
  path.join(os.homedir(), '.config', 'glab-cli', 'config.yml'),
@@ -150,11 +151,51 @@ async function setupGitlabConfig() {
150
151
  if (token) { console.log(' ✓ 从 glab 配置自动读取了 Token'); break; }
151
152
  }
152
153
 
153
- // 未找到则手动输入
154
+ // 来源2:git credential helper(适合配置了 HTTPS 凭据的用户)
154
155
  if (!token) {
155
- console.log(`\n 需要 GitLab Personal Access Token(api + write_repository 权限)`);
156
- console.log(` 获取地址: https://${GITLAB_HOST}/-/user_settings/personal_access_tokens\n`);
157
- token = await prompt(' GitLab Token: ');
156
+ try {
157
+ const cr = spawnSync('git', ['credential', 'fill'], {
158
+ input: `protocol=https\nhost=${GITLAB_HOST}\n\n`,
159
+ encoding: 'utf8', timeout: 3000,
160
+ });
161
+ const passMatch = cr.stdout.match(/password=(.+)/);
162
+ const userMatch = cr.stdout.match(/username=(.+)/);
163
+ if (passMatch) { token = passMatch[1].trim(); console.log(' ✓ 从 git credential 自动读取了 Token'); }
164
+ if (userMatch && !username) username = userMatch[1].trim();
165
+ } catch {}
166
+ }
167
+
168
+ // 来源3:环境变量
169
+ if (!token && process.env.GITLAB_TOKEN) {
170
+ token = process.env.GITLAB_TOKEN;
171
+ console.log(' ✓ 从环境变量 GITLAB_TOKEN 读取了 Token');
172
+ }
173
+
174
+ // 都没找到:自动打开浏览器到预填好的 Token 创建页,提示粘贴
175
+ if (!token) {
176
+ const tokenUrl = `https://${GITLAB_HOST}/-/user_settings/personal_access_tokens?name=ai-worklog&scopes=api,write_repository`;
177
+ console.log('\n 未检测到 GitLab Token,正在打开浏览器...');
178
+ console.log(` 请在页面中点击「Create personal access token」,复制后粘贴到下方\n`);
179
+ spawnSync('open', [tokenUrl]); // macOS
180
+ spawnSync('xdg-open', [tokenUrl]); // Linux(忽略失败)
181
+ token = await prompt(' 粘贴 Token: ');
182
+ }
183
+
184
+ // 用户名:通过 API 自动获取,避免手动输入
185
+ if (!username && token) {
186
+ try {
187
+ const res = await apiRequest({
188
+ hostname: GITLAB_HOST,
189
+ path: '/api/v4/user',
190
+ method: 'GET',
191
+ headers: { 'PRIVATE-TOKEN': token },
192
+ rejectUnauthorized: false,
193
+ });
194
+ if (res.body && res.body.username) {
195
+ username = res.body.username;
196
+ console.log(` ✓ 自动获取用户名: ${username}`);
197
+ }
198
+ } catch {}
158
199
  }
159
200
  if (!username) {
160
201
  username = await prompt(' GitLab 用户名: ');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-worklog",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "AI 对话工作日志自动收集工具 —— 从 Claude Code / Codex 对话记录生成每日工作日志并推送到 GitLab",
5
5
  "bin": {
6
6
  "ai-worklog": "./bin/index.js"
@@ -507,10 +507,11 @@ def git_commit_and_push(log_file: Path, target_date: str, push: bool = True) ->
507
507
  r = run(["git", "commit", "-m", commit_msg], check_err=False)
508
508
  if r.returncode != 0:
509
509
  if "nothing to commit" in r.stdout + r.stderr:
510
- print("Git: 无变更,跳过提交")
510
+ print("Git: 无变更,跳过提交(已在初始提交中)")
511
511
  else:
512
512
  print(f"Git commit 失败: {r.stderr.strip()}", file=sys.stderr)
513
- return
513
+ return
514
+ # nothing to commit 不代表不需要 push,继续走推送流程
514
515
 
515
516
  # ── 5. 推送 ────────────────────────────────────────────────────────────────
516
517
  if push:
@@ -520,6 +521,8 @@ def git_commit_and_push(log_file: Path, target_date: str, push: bool = True) ->
520
521
  r = run(["git", "push"])
521
522
  if r.returncode == 0:
522
523
  print("Git: 推送成功 ✓")
524
+ else:
525
+ print(f"Git push 失败: {r.stderr.strip()}", file=sys.stderr)
523
526
  else:
524
527
  print("Git: 已跳过 push(--no-push 模式)")
525
528