@sun-asterisk/sunlint 1.3.20 â 1.3.21
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/core/file-targeting-service.js +66 -15
- package/core/git-utils.js +121 -11
- package/core/github-annotate-service.js +72 -19
- package/core/output-service.js +2 -2
- package/package.json +1 -1
|
@@ -12,6 +12,7 @@ const { minimatch } = require('minimatch');
|
|
|
12
12
|
class FileTargetingService {
|
|
13
13
|
constructor() {
|
|
14
14
|
this.supportedLanguages = ['typescript', 'javascript', 'dart', 'kotlin', 'java', 'swift'];
|
|
15
|
+
this.GitUtils = require('./git-utils');
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
/**
|
|
@@ -22,35 +23,43 @@ class FileTargetingService {
|
|
|
22
23
|
try {
|
|
23
24
|
const startTime = Date.now();
|
|
24
25
|
const metadata = config._metadata;
|
|
25
|
-
|
|
26
|
+
|
|
26
27
|
if (cliOptions.verbose) {
|
|
27
28
|
console.log(chalk.cyan(`đ File Targeting: ${this.getTargetingMode(metadata)}`));
|
|
28
29
|
if (metadata?.shouldBypassProjectDiscovery) {
|
|
29
30
|
console.log(chalk.blue(`đ¯ Optimized targeting for ${metadata.analysisScope}`));
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
+
|
|
33
34
|
let allFiles = [];
|
|
34
|
-
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
allFiles = await this.
|
|
35
|
+
|
|
36
|
+
// Handle --changed-files option (git diff mode)
|
|
37
|
+
if (cliOptions.changedFiles) {
|
|
38
|
+
if (cliOptions.verbose) {
|
|
39
|
+
console.log(chalk.cyan('đ Using --changed-files mode (git diff)'));
|
|
40
|
+
}
|
|
41
|
+
allFiles = await this.getGitChangedFiles(cliOptions);
|
|
41
42
|
} else {
|
|
42
|
-
|
|
43
|
+
// Smart project-level optimization
|
|
44
|
+
const optimizedPaths = this.optimizeProjectPaths(inputPaths, cliOptions);
|
|
45
|
+
|
|
46
|
+
// Use enhanced targeting based on metadata
|
|
47
|
+
if (metadata?.shouldBypassProjectDiscovery) {
|
|
48
|
+
allFiles = await this.collectTargetedFiles(optimizedPaths, config, cliOptions);
|
|
49
|
+
} else {
|
|
50
|
+
allFiles = await this.collectProjectFiles(optimizedPaths, config, cliOptions);
|
|
51
|
+
}
|
|
43
52
|
}
|
|
44
53
|
|
|
45
54
|
// Apply filtering logic
|
|
46
55
|
const targetFiles = this.applyFiltering(allFiles, config, cliOptions);
|
|
47
|
-
|
|
56
|
+
|
|
48
57
|
const duration = Date.now() - startTime;
|
|
49
|
-
|
|
58
|
+
|
|
50
59
|
if (cliOptions.verbose) {
|
|
51
60
|
console.log(chalk.green(`â
File targeting completed in ${duration}ms (${targetFiles.length} files)`));
|
|
52
61
|
}
|
|
53
|
-
|
|
62
|
+
|
|
54
63
|
return {
|
|
55
64
|
files: targetFiles,
|
|
56
65
|
stats: this.generateStats(targetFiles, config),
|
|
@@ -62,6 +71,41 @@ class FileTargetingService {
|
|
|
62
71
|
}
|
|
63
72
|
}
|
|
64
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Get files changed in git (for --changed-files option)
|
|
76
|
+
*/
|
|
77
|
+
async getGitChangedFiles(cliOptions) {
|
|
78
|
+
try {
|
|
79
|
+
const baseRef = cliOptions.diffBase || null; // null = auto-detect
|
|
80
|
+
const changedFiles = this.GitUtils.getChangedFiles(baseRef);
|
|
81
|
+
|
|
82
|
+
if (cliOptions.verbose) {
|
|
83
|
+
const detectedBase = baseRef || this.GitUtils.getSmartBaseRef();
|
|
84
|
+
console.log(chalk.blue(`âšī¸ Using base ref: ${detectedBase}`));
|
|
85
|
+
console.log(chalk.blue(`âšī¸ Found ${changedFiles.length} changed file(s)`));
|
|
86
|
+
|
|
87
|
+
if (changedFiles.length > 0 && changedFiles.length <= 10) {
|
|
88
|
+
console.log(chalk.gray(' Changed files:'));
|
|
89
|
+
changedFiles.forEach(f => {
|
|
90
|
+
console.log(chalk.gray(` - ${path.relative(process.cwd(), f)}`));
|
|
91
|
+
});
|
|
92
|
+
} else if (changedFiles.length > 10) {
|
|
93
|
+
console.log(chalk.gray(` First 10 changed files:`));
|
|
94
|
+
changedFiles.slice(0, 10).forEach(f => {
|
|
95
|
+
console.log(chalk.gray(` - ${path.relative(process.cwd(), f)}`));
|
|
96
|
+
});
|
|
97
|
+
console.log(chalk.gray(` ... and ${changedFiles.length - 10} more`));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return changedFiles;
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.error(chalk.yellow(`â ī¸ Failed to get changed files: ${error.message}`));
|
|
104
|
+
console.error(chalk.yellow('âšī¸ Falling back to all files'));
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
65
109
|
/**
|
|
66
110
|
* Get targeting mode description
|
|
67
111
|
*/
|
|
@@ -293,15 +337,22 @@ class FileTargetingService {
|
|
|
293
337
|
if (debug) console.log(`đ [DEBUG] applyFiltering: start with ${filteredFiles.length} files`);
|
|
294
338
|
if (debug) console.log(`đ [DEBUG] config.include:`, config.include);
|
|
295
339
|
if (debug) console.log(`đ [DEBUG] cliOptions.include:`, cliOptions.include);
|
|
340
|
+
if (debug) console.log(`đ [DEBUG] cliOptions.changedFiles:`, cliOptions.changedFiles);
|
|
341
|
+
|
|
342
|
+
// IMPORTANT: When using --changed-files, skip include patterns
|
|
343
|
+
// Git already filtered the files, we only need to apply excludes
|
|
344
|
+
const skipIncludePatterns = cliOptions.changedFiles;
|
|
296
345
|
|
|
297
346
|
// 1. Apply config include patterns first (medium priority)
|
|
298
|
-
if (config.include && config.include.length > 0) {
|
|
347
|
+
if (!skipIncludePatterns && config.include && config.include.length > 0) {
|
|
299
348
|
filteredFiles = this.applyIncludePatterns(filteredFiles, config.include, debug);
|
|
300
349
|
if (debug) console.log(`đ [DEBUG] After config include: ${filteredFiles.length} files`);
|
|
350
|
+
} else if (skipIncludePatterns && debug) {
|
|
351
|
+
console.log(`đ [DEBUG] Skipping config include patterns (--changed-files mode)`);
|
|
301
352
|
}
|
|
302
353
|
|
|
303
354
|
// 2. Apply CLI include overrides (highest priority - completely overrides config)
|
|
304
|
-
if (cliOptions.include) {
|
|
355
|
+
if (!skipIncludePatterns && cliOptions.include) {
|
|
305
356
|
// CLI include completely replaces config include - start fresh from all files
|
|
306
357
|
filteredFiles = this.applyIncludePatterns([...files], cliOptions.include, debug);
|
|
307
358
|
}
|
package/core/git-utils.js
CHANGED
|
@@ -21,13 +21,98 @@ class GitUtils {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Detect if running in PR context (GitHub Actions, GitLab CI, etc.)
|
|
26
|
+
* @returns {Object|null} PR context info or null
|
|
27
|
+
*/
|
|
28
|
+
static detectPRContext() {
|
|
29
|
+
// GitHub Actions
|
|
30
|
+
if (process.env.GITHUB_EVENT_NAME === 'pull_request' ||
|
|
31
|
+
process.env.GITHUB_EVENT_NAME === 'pull_request_target') {
|
|
32
|
+
return {
|
|
33
|
+
provider: 'github',
|
|
34
|
+
baseBranch: process.env.GITHUB_BASE_REF,
|
|
35
|
+
headBranch: process.env.GITHUB_HEAD_REF,
|
|
36
|
+
prNumber: process.env.GITHUB_REF ? process.env.GITHUB_REF.match(/refs\/pull\/(\d+)\/merge/)?.[1] : null
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// GitLab CI
|
|
41
|
+
if (process.env.CI_MERGE_REQUEST_ID) {
|
|
42
|
+
return {
|
|
43
|
+
provider: 'gitlab',
|
|
44
|
+
baseBranch: process.env.CI_MERGE_REQUEST_TARGET_BRANCH_NAME,
|
|
45
|
+
headBranch: process.env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME,
|
|
46
|
+
prNumber: process.env.CI_MERGE_REQUEST_IID
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get smart base reference for diff
|
|
55
|
+
* Auto-detects PR context or falls back to common base branches
|
|
56
|
+
* @param {string} cwd - Working directory
|
|
57
|
+
* @returns {string} Base reference for git diff
|
|
58
|
+
*/
|
|
59
|
+
static getSmartBaseRef(cwd = process.cwd()) {
|
|
60
|
+
if (!this.isGitRepository(cwd)) {
|
|
61
|
+
throw new Error('Not a git repository');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Check PR context first
|
|
65
|
+
const prContext = this.detectPRContext();
|
|
66
|
+
if (prContext && prContext.baseBranch) {
|
|
67
|
+
const candidates = [
|
|
68
|
+
`origin/${prContext.baseBranch}`,
|
|
69
|
+
`upstream/${prContext.baseBranch}`,
|
|
70
|
+
prContext.baseBranch
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
for (const candidate of candidates) {
|
|
74
|
+
try {
|
|
75
|
+
execSync(`git rev-parse --verify ${candidate}`, { cwd, stdio: 'ignore' });
|
|
76
|
+
return candidate;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
// Continue to next candidate
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Fallback to common base branches
|
|
84
|
+
const fallbackBranches = [
|
|
85
|
+
'origin/main',
|
|
86
|
+
'origin/master',
|
|
87
|
+
'origin/develop',
|
|
88
|
+
'upstream/main',
|
|
89
|
+
'upstream/master',
|
|
90
|
+
'main',
|
|
91
|
+
'master',
|
|
92
|
+
'develop'
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
for (const branch of fallbackBranches) {
|
|
96
|
+
try {
|
|
97
|
+
execSync(`git rev-parse --verify ${branch}`, { cwd, stdio: 'ignore' });
|
|
98
|
+
return branch;
|
|
99
|
+
} catch (error) {
|
|
100
|
+
// Continue to next candidate
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Last resort: use HEAD (uncommitted changes only)
|
|
105
|
+
return 'HEAD';
|
|
106
|
+
}
|
|
107
|
+
|
|
24
108
|
/**
|
|
25
109
|
* Get list of changed files compared to base reference
|
|
26
|
-
* @param {string} baseRef - Base git reference (e.g., 'origin/main')
|
|
110
|
+
* @param {string|null} baseRef - Base git reference (e.g., 'origin/main'). If null, auto-detect.
|
|
27
111
|
* @param {string} cwd - Working directory
|
|
112
|
+
* @param {boolean} includeUncommitted - Include uncommitted changes
|
|
28
113
|
* @returns {string[]} Array of changed file paths
|
|
29
114
|
*/
|
|
30
|
-
static getChangedFiles(baseRef =
|
|
115
|
+
static getChangedFiles(baseRef = null, cwd = process.cwd(), includeUncommitted = true) {
|
|
31
116
|
if (!this.isGitRepository(cwd)) {
|
|
32
117
|
throw new Error('Not a git repository');
|
|
33
118
|
}
|
|
@@ -35,15 +120,40 @@ class GitUtils {
|
|
|
35
120
|
try {
|
|
36
121
|
// Get git root directory
|
|
37
122
|
const gitRoot = execSync('git rev-parse --show-toplevel', { cwd, encoding: 'utf8' }).trim();
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
123
|
+
|
|
124
|
+
// Auto-detect base ref if not provided
|
|
125
|
+
const actualBaseRef = baseRef || this.getSmartBaseRef(cwd);
|
|
126
|
+
|
|
127
|
+
const allFiles = new Set();
|
|
128
|
+
|
|
129
|
+
// Get committed changes
|
|
130
|
+
if (actualBaseRef !== 'HEAD') {
|
|
131
|
+
// Use two-dot diff for branch comparison (what's new in this branch)
|
|
132
|
+
const command = `git diff --name-only ${actualBaseRef}..HEAD`;
|
|
133
|
+
const output = execSync(command, { cwd: gitRoot, encoding: 'utf8' });
|
|
134
|
+
|
|
135
|
+
output
|
|
136
|
+
.split('\n')
|
|
137
|
+
.filter(file => file.trim() !== '')
|
|
138
|
+
.forEach(file => allFiles.add(path.resolve(gitRoot, file)));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Get uncommitted changes if requested
|
|
142
|
+
if (includeUncommitted) {
|
|
143
|
+
const uncommittedCommand = 'git diff --name-only HEAD';
|
|
144
|
+
try {
|
|
145
|
+
const uncommittedOutput = execSync(uncommittedCommand, { cwd: gitRoot, encoding: 'utf8' });
|
|
146
|
+
uncommittedOutput
|
|
147
|
+
.split('\n')
|
|
148
|
+
.filter(file => file.trim() !== '')
|
|
149
|
+
.forEach(file => allFiles.add(path.resolve(gitRoot, file)));
|
|
150
|
+
} catch (error) {
|
|
151
|
+
// Ignore errors for uncommitted changes (might be empty)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Filter to only existing files
|
|
156
|
+
return Array.from(allFiles).filter(file => fs.existsSync(file));
|
|
47
157
|
} catch (error) {
|
|
48
158
|
throw new Error(`Failed to get changed files: ${error.message}`);
|
|
49
159
|
}
|
|
@@ -182,14 +182,56 @@ function readJsonFile(jsonFile) {
|
|
|
182
182
|
}
|
|
183
183
|
}
|
|
184
184
|
|
|
185
|
+
/**
|
|
186
|
+
* Get git root directory
|
|
187
|
+
* @param {string} cwd - Current working directory
|
|
188
|
+
* @returns {string} Git root path
|
|
189
|
+
*/
|
|
190
|
+
function getGitRoot(cwd = process.cwd()) {
|
|
191
|
+
try {
|
|
192
|
+
const { execSync } = require('child_process');
|
|
193
|
+
const gitRoot = execSync('git rev-parse --show-toplevel', {
|
|
194
|
+
cwd,
|
|
195
|
+
encoding: 'utf8'
|
|
196
|
+
}).trim();
|
|
197
|
+
return gitRoot;
|
|
198
|
+
} catch (error) {
|
|
199
|
+
logger.warn('Not a git repository, using cwd as root');
|
|
200
|
+
return cwd;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Normalize path to be relative from git root
|
|
206
|
+
* @param {string} filePath - File path (absolute or relative)
|
|
207
|
+
* @param {string} gitRoot - Git root directory
|
|
208
|
+
* @returns {string} Normalized relative path
|
|
209
|
+
*/
|
|
210
|
+
function normalizePathFromGitRoot(filePath, gitRoot) {
|
|
211
|
+
let normalized = filePath;
|
|
212
|
+
|
|
213
|
+
// Convert absolute path to relative from git root
|
|
214
|
+
if (filePath.startsWith(gitRoot)) {
|
|
215
|
+
normalized = filePath.slice(gitRoot.length);
|
|
216
|
+
if (normalized.startsWith('/') || normalized.startsWith('\\')) {
|
|
217
|
+
normalized = normalized.slice(1);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Normalize path separators to forward slash
|
|
222
|
+
normalized = normalized.replace(/\\/g, '/');
|
|
223
|
+
|
|
224
|
+
return normalized;
|
|
225
|
+
}
|
|
226
|
+
|
|
185
227
|
/**
|
|
186
228
|
* Parse violations from JSON data
|
|
187
229
|
* @param {Array|Object} raw - Raw JSON data
|
|
230
|
+
* @param {string} gitRoot - Git root directory for path normalization
|
|
188
231
|
* @returns {Array} Array of violation objects
|
|
189
232
|
*/
|
|
190
|
-
function parseViolations(raw) {
|
|
233
|
+
function parseViolations(raw, gitRoot) {
|
|
191
234
|
const violations = [];
|
|
192
|
-
const cwd = process.env.GITHUB_WORKSPACE || process.cwd();
|
|
193
235
|
|
|
194
236
|
if (Array.isArray(raw)) {
|
|
195
237
|
for (const fileObj of raw) {
|
|
@@ -203,18 +245,8 @@ function parseViolations(raw) {
|
|
|
203
245
|
continue;
|
|
204
246
|
}
|
|
205
247
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
// Convert absolute path to relative
|
|
209
|
-
if (relPath.startsWith(cwd)) {
|
|
210
|
-
relPath = relPath.slice(cwd.length);
|
|
211
|
-
if (relPath.startsWith('/') || relPath.startsWith('\\')) {
|
|
212
|
-
relPath = relPath.slice(1);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// Normalize path separators
|
|
217
|
-
relPath = relPath.replace(/\\/g, '/');
|
|
248
|
+
// Normalize path relative to git root (same as GitHub API)
|
|
249
|
+
const relPath = normalizePathFromGitRoot(fileObj.filePath, gitRoot);
|
|
218
250
|
|
|
219
251
|
for (const msg of fileObj.messages) {
|
|
220
252
|
if (!msg || typeof msg !== 'object') {
|
|
@@ -243,7 +275,12 @@ function parseViolations(raw) {
|
|
|
243
275
|
if (!Array.isArray(rawViolations)) {
|
|
244
276
|
throw new Error('violations property must be an array');
|
|
245
277
|
}
|
|
246
|
-
|
|
278
|
+
|
|
279
|
+
// Normalize paths for raw violations too
|
|
280
|
+
violations.push(...rawViolations.map(v => ({
|
|
281
|
+
...v,
|
|
282
|
+
file: normalizePathFromGitRoot(v.file, gitRoot)
|
|
283
|
+
})));
|
|
247
284
|
} else {
|
|
248
285
|
throw new Error('JSON data must be an array or object with violations property');
|
|
249
286
|
}
|
|
@@ -521,9 +558,13 @@ async function annotate({
|
|
|
521
558
|
logger.info('Reading result file', { jsonFile });
|
|
522
559
|
const raw = readJsonFile(jsonFile);
|
|
523
560
|
|
|
524
|
-
// Step
|
|
561
|
+
// Step 2.5: Get git root for path normalization
|
|
562
|
+
const gitRoot = getGitRoot();
|
|
563
|
+
logger.debug('Git root directory', { gitRoot });
|
|
564
|
+
|
|
565
|
+
// Step 3: Parse violations with git root normalization
|
|
525
566
|
logger.info('Parsing violations');
|
|
526
|
-
const violations = parseViolations(raw);
|
|
567
|
+
const violations = parseViolations(raw, gitRoot);
|
|
527
568
|
|
|
528
569
|
if (violations.length === 0) {
|
|
529
570
|
logger.info('No violations found');
|
|
@@ -607,6 +648,14 @@ async function annotate({
|
|
|
607
648
|
let linesSkipped = 0;
|
|
608
649
|
let renamedFilesHandled = 0;
|
|
609
650
|
|
|
651
|
+
// Debug: Log sample paths for comparison
|
|
652
|
+
logger.debug('Path comparison debug:', {
|
|
653
|
+
sampleViolationFiles: violations.slice(0, 3).map(v => v.file),
|
|
654
|
+
samplePRFiles: Array.from(prFilesInfo.keys()).slice(0, 3),
|
|
655
|
+
totalViolations: violations.length,
|
|
656
|
+
totalPRFiles: prFilesInfo.size
|
|
657
|
+
});
|
|
658
|
+
|
|
610
659
|
for (const v of violations) {
|
|
611
660
|
let targetFile = v.file;
|
|
612
661
|
let fileInfo = prFilesInfo.get(targetFile);
|
|
@@ -813,9 +862,13 @@ async function postSummaryComment({
|
|
|
813
862
|
logger.info('Reading result file', { jsonFile });
|
|
814
863
|
const raw = readJsonFile(jsonFile);
|
|
815
864
|
|
|
816
|
-
// Step
|
|
865
|
+
// Step 2.5: Get git root for path normalization
|
|
866
|
+
const gitRoot = getGitRoot();
|
|
867
|
+
logger.debug('Git root directory', { gitRoot });
|
|
868
|
+
|
|
869
|
+
// Step 3: Parse violations with git root normalization
|
|
817
870
|
logger.info('Parsing violations for summary');
|
|
818
|
-
const violations = parseViolations(raw);
|
|
871
|
+
const violations = parseViolations(raw, gitRoot);
|
|
819
872
|
|
|
820
873
|
// Step 4: Initialize Octokit
|
|
821
874
|
const token = githubToken || process.env.GITHUB_TOKEN;
|
package/core/output-service.js
CHANGED
|
@@ -28,9 +28,9 @@ class OutputService {
|
|
|
28
28
|
try {
|
|
29
29
|
const packageJsonPath = path.join(__dirname, '..', 'package.json');
|
|
30
30
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
31
|
-
return packageJson.version || '1.3.
|
|
31
|
+
return packageJson.version || '1.3.21';
|
|
32
32
|
} catch (error) {
|
|
33
|
-
return '1.3.
|
|
33
|
+
return '1.3.21'; // Fallback version
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
package/package.json
CHANGED