job51-gitlab-cr-node-skill-prompt-optimize 1.0.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.
@@ -0,0 +1,119 @@
1
+ // 详细测试Claude命令能否识别传入的文件路径
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const { spawn } = require('child_process');
5
+
6
+ console.log('=== 测试Claude命令的文件路径识别能力 ===\n');
7
+
8
+ async function testPathRecognition() {
9
+ // 1. 创建一个临时文件
10
+ const tempFileName = `./test-path-recognition-${Date.now()}.txt`;
11
+ const sampleContent = 'This is a test file to verify path recognition.';
12
+
13
+ console.log(`1. 创建测试文件: ${tempFileName}`);
14
+ fs.writeFileSync(tempFileName, sampleContent);
15
+
16
+ // 2. 验证文件创建成功
17
+ const exists = fs.existsSync(tempFileName);
18
+ console.log(`2. 文件创建成功: ${exists}`);
19
+
20
+ // 3. 获取各种路径形式
21
+ const relativePath = tempFileName; // ./test-file.txt
22
+ const absolutePath = path.resolve(tempFileName); // /full/path/to/test-file.txt
23
+ const simpleName = path.basename(tempFileName); // test-file.txt
24
+
25
+ console.log(`3. 相对路径: ${relativePath}`);
26
+ console.log(` 绝对路径: ${absolutePath}`);
27
+ console.log(` 简单文件名: ${simpleName}`);
28
+
29
+ // 4. 检查各种路径形式在当前目录下是否可访问
30
+ console.log(`5. 当前工作目录: ${process.cwd()}`);
31
+ console.log(` 相对路径可访问: ${fs.existsSync(relativePath)}`);
32
+ console.log(` 绝对路径可访问: ${fs.existsSync(absolutePath)}`);
33
+ console.log(` 简单文件名可访问: ${fs.existsSync(simpleName)}`);
34
+
35
+ // 5. 测试runClaudeCommand函数的实际行为
36
+ console.log('\n6. 模拟runClaudeCommand函数的行为...');
37
+
38
+ // 在index.js中实际使用的代码流程
39
+ const prompt = '/simple-code-review' + ' ' + relativePath;
40
+ console.log(` 构造的prompt: ${prompt}`);
41
+
42
+ // 这对应index.js第523行开始的runClaudeCommand函数
43
+ console.log(` 实际执行: claude -p "${prompt}"`);
44
+ console.log(` 工作目录设置为: ${__dirname} (index.js所在目录)`);
45
+
46
+ // 检查在__dirname下该文件是否存在
47
+ const originalCwd = process.cwd();
48
+ try {
49
+ process.chdir(__dirname);
50
+ console.log(` 切换到__dirname: ${__dirname}`);
51
+ console.log(` 在__dirname下相对路径可访问: ${fs.existsSync(relativePath)}`);
52
+ console.log(` 在__dirname下绝对路径可访问: ${fs.existsSync(absolutePath)}`);
53
+ } finally {
54
+ process.chdir(originalCwd);
55
+ }
56
+
57
+ // 6. 分析index.js中的runClaudeCommand实现
58
+ console.log('\n7. index.js中的runClaudeCommand函数分析:');
59
+ console.log(' spawn(\'claude\', [\'-p\', promptContent], {');
60
+ console.log(' shell: true,');
61
+ console.log(' cwd: __dirname, <- 这很重要!');
62
+ console.log(' env: process.env,');
63
+ console.log(' stdio: [\'ignore\', \'pipe\', \'pipe\']');
64
+ console.log(' });');
65
+ console.log('');
66
+ console.log(' 关键点: cwd被设置为__dirname,这意味着Claude命令将在');
67
+ console.log(' index.js文件所在的目录中执行,而不是当前工作目录。');
68
+
69
+ // 7. 验证__dirname和process.cwd()的关系
70
+ console.log(`\n8. 目录关系验证:`);
71
+ console.log(` __dirname (脚本目录): ${__dirname}`);
72
+ console.log(` process.cwd() (当前工作目录): ${process.cwd()}`);
73
+ console.log(` 两者是否相同: ${__dirname === process.cwd()}`);
74
+
75
+ // 8. 针对index.js的精确场景进行测试
76
+ console.log('\n9. 模拟index.js中的精确场景:');
77
+
78
+ // 模拟第118-120行的代码
79
+ const blockIndex = 0;
80
+ const tmpFileNameFromIndex = `./temp-diff-block-${Date.now()}-${blockIndex}.diff`;
81
+ console.log(` 模拟创建临时文件: ${tmpFileNameFromIndex}`);
82
+
83
+ // 模拟第124行
84
+ fs.writeFileSync(tmpFileNameFromIndex, 'Sample diff content for testing');
85
+ console.log(` 文件写入成功`);
86
+
87
+ // 模拟第198行
88
+ const promptFromIndex = '/simple-code-review' + ' ' + tmpFileNameFromIndex;
89
+ console.log(` 模拟prompt: ${promptFromIndex}`);
90
+
91
+ // 检查在index.js所在目录中是否可以访问该文件
92
+ const fileAccessibleInScriptDir = fs.existsSync(path.join(__dirname, tmpFileNameFromIndex));
93
+ console.log(` 在脚本目录中可访问 (使用join): ${fileAccessibleInScriptDir}`);
94
+
95
+ // 清理测试文件
96
+ try {
97
+ if (fs.existsSync(tempFileName)) {
98
+ fs.unlinkSync(tempFileName);
99
+ console.log(`\n10. 清理测试文件: ${tempFileName}`);
100
+ }
101
+
102
+ if (fs.existsSync(tmpFileNameFromIndex)) {
103
+ fs.unlinkSync(tmpFileNameFromIndex);
104
+ console.log(` 清理临时文件: ${tmpFileNameFromIndex}`);
105
+ }
106
+ } catch (error) {
107
+ console.error(`清理文件时出错: ${error.message}`);
108
+ }
109
+
110
+ console.log('\n=== 结论 ===');
111
+ console.log('1. 在index.js的runClaudeCommand中,cwd设置为__dirname');
112
+ console.log('2. 临时文件使用相对路径创建,也位于__dirname目录中');
113
+ console.log('3. 因此Claude命令应该能够在设置的cwd中找到该文件');
114
+ console.log('4. 如果在GitLab CI中失败,可能是由于Claude命令本身不存在或');
115
+ console.log(' 权限/环境问题,而不是文件路径问题');
116
+ console.log('5. 但路径问题仍可能存在,因为如果工作流程改变,可能会产生差异');
117
+ }
118
+
119
+ testPathRecognition().catch(console.error);
package/utils.js ADDED
@@ -0,0 +1,67 @@
1
+ /**
2
+ * 工具函数集合
3
+ */
4
+
5
+ const axios = require('axios');
6
+
7
+ // 调试日志函数
8
+ function debugLog(message) {
9
+ console.log('[DEBUG]', new Date().toISOString(), message);
10
+ }
11
+
12
+ /**
13
+ * 提取REPORT标签之间的内容
14
+ * @param {string} text 包含REPORT标签的文本
15
+ * @returns {string} 提取后的内容
16
+ */
17
+ function extractReportContent(text) {
18
+ const reportRegex = /<REPORT>([\s\S]*?)<\/REPORT>/;
19
+ const match = text.match(reportRegex);
20
+ return match ? match[1].trim() : text;
21
+ }
22
+
23
+ // 创建一个可配置的GitLab API客户端
24
+ class GitLabAPIClient {
25
+ constructor(gitlabToken, gitlabUrl = null) {
26
+ this.gitlabToken = gitlabToken;
27
+ this.gitlabUrl = gitlabUrl || 'https://gitdev.51job.com';
28
+ this.axiosInstance = axios.create({
29
+ headers: {
30
+ 'PRIVATE-TOKEN': this.gitlabToken,
31
+ },
32
+ });
33
+ }
34
+
35
+ /**
36
+ * 通用的GitLab API调用函数
37
+ * @param {string} endpoint API端点
38
+ * @param {Object} options 额外选项(method, data等)
39
+ * @returns {Promise} API调用结果
40
+ */
41
+ async callGitLabAPI(endpoint, options = {}) {
42
+ const { method = 'GET', data = null, headers = {} } = options;
43
+
44
+ try {
45
+ const response = await this.axiosInstance({
46
+ method,
47
+ url: `${this.gitlabUrl}${endpoint}`,
48
+ headers: {
49
+ 'Content-Type': 'application/json',
50
+ ...headers
51
+ },
52
+ data
53
+ });
54
+
55
+ return response.data;
56
+ } catch (error) {
57
+ console.error(`GitLab API调用失败: ${method} ${endpoint}`, error.message);
58
+ throw error;
59
+ }
60
+ }
61
+ }
62
+
63
+ module.exports = {
64
+ GitLabAPIClient,
65
+ debugLog,
66
+ extractReportContent
67
+ };