codeant-cli 0.1.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,127 @@
1
+ import GitDiffHelper from './gitDiffHelper.js';
2
+
3
+ /**
4
+ * Transforms git diff data into the format expected by the secrets detection API
5
+ *
6
+ * API Input Format:
7
+ * {
8
+ * "files": [
9
+ * {
10
+ * "file_path": str,
11
+ * "code": str,
12
+ * "diffs": [{ "start_line": int, "end_line": int }]
13
+ * }
14
+ * ]
15
+ * }
16
+ */
17
+ class SecretsApiHelper {
18
+ constructor(workspacePath) {
19
+ this.workspacePath = workspacePath;
20
+ this.gitHelper = new GitDiffHelper(workspacePath);
21
+ }
22
+
23
+ async init() {
24
+ await this.gitHelper.init();
25
+ }
26
+
27
+ /**
28
+ * Get staged files formatted for the secrets API
29
+ * This is used for pre-commit hooks
30
+ */
31
+ async getStagedFilesForApi() {
32
+ const diffs = await this.gitHelper.getDiffBasedOnReviewConfig({ type: 'staged-only' });
33
+ return this._transformDiffsToApiFormat(diffs);
34
+ }
35
+
36
+ /**
37
+ * Get all changed files formatted for the secrets API
38
+ */
39
+ async getChangedFilesForApi() {
40
+ const diffs = await this.gitHelper.getDiffBasedOnReviewConfig({ type: 'branch-diff' });
41
+ return this._transformDiffsToApiFormat(diffs);
42
+ }
43
+
44
+ /**
45
+ * Get uncommitted files formatted for the secrets API
46
+ */
47
+ async getUncommittedFilesForApi() {
48
+ const diffs = await this.gitHelper.getDiffBasedOnReviewConfig({ type: 'uncommitted' });
49
+ return this._transformDiffsToApiFormat(diffs);
50
+ }
51
+
52
+ /**
53
+ * Get last commit files formatted for the secrets API
54
+ */
55
+ async getLastCommitFilesForApi() {
56
+ const diffs = await this.gitHelper.getDiffBasedOnReviewConfig({ type: 'last-commit' });
57
+ return this._transformDiffsToApiFormat(diffs);
58
+ }
59
+
60
+ /**
61
+ * Transform diff info array to API format
62
+ * Groups by file and extracts diff ranges
63
+ */
64
+ _transformDiffsToApiFormat(diffs) {
65
+ // Group diffs by filename
66
+ const fileMap = new Map();
67
+
68
+ for (const diff of diffs) {
69
+ const filePath = diff.filename_str;
70
+
71
+ if (!fileMap.has(filePath)) {
72
+ fileMap.set(filePath, {
73
+ file_path: filePath,
74
+ code: diff.head_file_str || '',
75
+ diffs: []
76
+ });
77
+ }
78
+
79
+ // Add diff range if available
80
+ if (diff.start_line_str && diff.end_line_str) {
81
+ const startLine = parseInt(diff.start_line_str, 10);
82
+ const endLine = parseInt(diff.end_line_str, 10);
83
+
84
+ if (!isNaN(startLine) && !isNaN(endLine)) {
85
+ fileMap.get(filePath).diffs.push({
86
+ start_line: startLine,
87
+ end_line: endLine
88
+ });
89
+ }
90
+ }
91
+ }
92
+
93
+ return Array.from(fileMap.values());
94
+ }
95
+
96
+ /**
97
+ * Build the complete request body for the secrets API
98
+ */
99
+ async buildSecretsApiRequest(type = 'staged-only') {
100
+ let files;
101
+
102
+ switch (type) {
103
+ case 'staged-only':
104
+ files = await this.getStagedFilesForApi();
105
+ break;
106
+ case 'branch-diff':
107
+ files = await this.getChangedFilesForApi();
108
+ break;
109
+ case 'uncommitted':
110
+ files = await this.getUncommittedFilesForApi();
111
+ break;
112
+ case 'last-commit':
113
+ files = await this.getLastCommitFilesForApi();
114
+ break;
115
+ default:
116
+ files = await this.getStagedFilesForApi();
117
+ }
118
+
119
+ return { files };
120
+ }
121
+
122
+ getGitRoot() {
123
+ return this.gitHelper.getGitRoot();
124
+ }
125
+ }
126
+
127
+ export default SecretsApiHelper;