-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
276 lines (230 loc) · 10.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import fs from 'fs';
import readline from 'readline';
import core from '@actions/core';
import * as github from '@actions/github';
import { cppcheckParser } from './parsers/cppcheckParser.js';
import { pcLintParser } from './parsers/pclintParser.js';
import { parseRules } from './rules/parseRules.js';
import { parseSuppressions } from './rules/parseSuppressions.js';
import { generatePdfReport } from './pdf/generate-pdf-report.js';
import { uploadFile } from "./storage/uploadToStorage.js";
const parsers = {
'Cppcheck': cppcheckParser,
'PC-lint': pcLintParser
};
async function updateMISRAComment(octokit, context, newCommentBody) {
const { owner, repo } = context.repo;
if (!context.payload.pull_request) {
return;
}
const pullRequestNumber = context.payload.pull_request.number;
// Fetch all comments on the pull request
const { data: comments } = await octokit.rest.issues.listComments({
owner,
repo,
issue_number: pullRequestNumber,
});
// Find the comment with the hidden tag
const existingComment = comments.find(comment => comment.body.includes('<!-- MISRA C REPORT -->'));
if (existingComment) {
// Update the existing comment
await octokit.rest.issues.updateComment({
owner,
repo,
comment_id: existingComment.id,
body: newCommentBody,
});
console.log('Updated the existing MISRA C report comment.');
} else {
// Create a new comment if no existing comment was found
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: pullRequestNumber,
body: newCommentBody,
});
console.log('Created a new MISRA C report comment.');
}
}
function constructFileUrl(filePath, lineNumber, githubContext) {
const owner = githubContext.payload.repository.owner.login;
const repo = githubContext.payload.repository.name;
// Initialize with commit SHA; it's always available and serves as a good fallback
let ref = process.env.GITHUB_SHA;
// If in the context of a pull request, prefer using the PR's branch name
if (githubContext.eventName === 'pull_request' && githubContext.payload.pull_request) {
ref = githubContext.payload.pull_request.head.ref;
}
const basePath = process.env.GITHUB_WORKSPACE;
// Ensure filePath is relative to the repository root
const relativePath = filePath.startsWith(basePath) ? filePath.substring(basePath.length + 1) : filePath;
return `https://github.com/${owner}/${repo}/blob/${ref}/${relativePath}#L${lineNumber}`;
}
function getCurrentDateFormatted() {
const now = new Date();
const year = now.getFullYear();
const month = (now.getMonth() + 1).toString().padStart(2, '0');
const day = now.getDate().toString().padStart(2, '0');
return `${year}-${month}-${day}`;
}
const parser = core.getInput('parser');
const filePathRules = core.getInput('rules');
const filePathSuppressions = core.getInput('suppressions');
const resultPath = core.getInput('results');
const project = core.getInput('project');
const filesPath = core.getInput('files');
const outputFile = '/tmp/report.pdf';
const toComplianceTable = (rules, results, suppresions) => {
return rules.map(rule => {
const compliant = !results.find(result => result.directive() === rule.directive());
const isSuppressed = suppresions.includes(rule.directive());
return {
directive: 'Directive ' + rule.directive(),
category: rule.category(),
recategorization: '', // TODO: Implement recategorization
compliance: isSuppressed ? 'DEVIATION': (compliant ? 'COMPLIANT' : 'NON-COMPLIANT')
};
});
}
async function parseFileList(fileList) {
const fileStream = fs.createReadStream(fileList);
const lines = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
const parsedFiles = []; // Initialize an empty array to store the results
// Await the processing of each line
for await (const line of lines) {
const [moduleName, path] = line.split('::');
const relativePath = path.replace(process.env.GITHUB_WORKSPACE, '');
parsedFiles.push(relativePath); // Add the processed line to the array
}
return parsedFiles;
}
async function uploadReportToGoogleCloud(hash) {
// Example usage
const bucketName = 'misra-c'; // TODO: Hardcoded for now. Make this an input?
const destination = `MISRA_c_compliance_report_${hash}.pdf`;
return uploadFile(bucketName, outputFile, destination);
}
async function run() {
try {
const rules = await parseRules(filePathRules);
const suppressions = await parseSuppressions(filePathSuppressions);
const results = await parsers[parser](resultPath);
const files = await parseFileList(filesPath);
const complianceTable = toComplianceTable(rules, results, suppressions);
const deviationsByCategory = complianceTable.reduce((acc, result) => {
if (acc[result.category]) {
if (result.compliance !== 'DEVIATION') return acc;
acc[result.category]++;
} else {
acc[result.category] = 0;
if (result.compliance === 'DEVIATION')
acc[result.category]++;
return acc;
}
return acc;
}, {});
const ruleViolations = results.reduce((acc, result) => {
const category = rules.find(rule => rule.directive() === result.directive()).category();
if (acc[category]) {
acc[category]++;
} else {
acc[category] = 1;
return acc;
}
return acc;
}, { Advisory: 0, Required: 0, Mandatory: 0 });
await generatePdfReport({
rules: complianceTable,
files,
project,
commit: process.env.GITHUB_SHA,
date: getCurrentDateFormatted(),
guidelines: 'MISRA C 2012', // TODO: make this configurable
checkingTool: parser,
compliance: results.length > 0 ? 'Non-Compliant' : 'Compliant',
outputFile,
violationsByCategory: ruleViolations,
deviationsByCategory
});
const github_token = core.getInput('GITHUB_TOKEN');
const octokit = github.getOctokit(github_token);
const context = github.context;
let message = "\<\!-- MISRA C REPORT --\>\n";
message += '\n';
message += '<p align="left">\n';
message += ' <img src="https://storage.googleapis.com/biglup/misra_c.svg" width="100" alt="MISRA C Logo">\n';
message += '</p>\n';
message += '\n';
message += '# MISRA Guideline Compliance Summary\n';
message += '\n';
message += '<table border="0">\n';
message += ' <tr>\n';
message += ' <td><b>Commit:</b></td>\n';
message += ` <td><code>${process.env.GITHUB_SHA}</code></td>\n`;
message += ' </tr>\n';
message += ' <tr>\n';
message += ' <td><b>Date:</b></td>\n';
message += ` <td>${getCurrentDateFormatted()}</td>\n`;
message += ' </tr>\n';
message += ' <tr>\n';
message += ' <td><b>Guidelines:</b></td>\n';
message += ' <td>MISRA C 2012</td>\n'; // TODO: make this configurable
message += ' </tr>\n';
message += ' <tr>\n';
message += ' <td><b>Checking Tool:</b></td>\n';
message += ` <td>${parser}</td>\n`;
message += ' </tr>\n';
message += ' <tr>\n';
message += ' <td><b>Result:</b></td>\n';
message += results.length > 0 ? ' <td>Non-Compliant ❌</td>\n' : ' <td>Compliant ✅</td>\n';
message += ' </tr>\n';
message += '</table>\n';
if (results.length === 0) {
message += '\n';
const uploadedReport = await uploadReportToGoogleCloud(process.env.GITHUB_SHA);
message += '## 🎉 No MISRA C 2012 Violations Found!\n';
message += `You can download the complete report from: [MISRA C Report](${uploadedReport})\n`;
await updateMISRAComment(octokit, context, message);
return;
}
message += '\n';
message += '## Violations Summary\n';
message += '<table>\n';
message += ' <tr>\n';
message += ' <th>Category</th>\n';
message += ' <th>Violations</th>\n';
message += ' </tr>\n';
for (const [category, count] of Object.entries(ruleViolations)) {
message += ' <tr>\n';
message += ` <td>${category}</td>\n`;
message += ` <td>${count}</td>\n`;
message += ' </tr>\n';
}
message += '</table><br/>\n';
message += '<details>\n';
message += ` <summary>Click here to toggle the visibility of the <b>${results.length}</b> violations</summary>\n`;
message += `\n`;
message += `| File | Directive | Category | Rationale |\n`;
message += `| --- | --- | --- | --- |\n`;
for (const result of results) {
const rule = rules.find(rule => rule.directive() === result.directive());
if (rule && !suppressions.includes(rule.directive())) {
const fileUrl = constructFileUrl(result.file(), result.lineNumber(), github.context);
const fileName = result.file().split('/').pop();
const markdownLink = `[${fileName}:${result.lineNumber()}](${fileUrl})`;
message += `| ${markdownLink} | ${result.directive()} | ${rule.category()} | ${result.rationale()} |\n`;
}
}
message += `</details>\n`;
const uploadedReport = await uploadReportToGoogleCloud(process.env.GITHUB_SHA);
message += `\n`;
message += `You can download the complete report from: [MISRA C Report](${uploadedReport})\n`;
await updateMISRAComment(octokit, context, message);
} catch (error) {
core.setFailed(error.message);
}
}
run().catch(core.setFailed);