-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
169 lines (159 loc) · 7.29 KB
/
false-positive-approvals.yml
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
name: False Positive Approvals
on:
issue_comment:
types: [ created ]
permissions: {}
jobs:
update_suppression:
permissions:
contents: write # to push changes in repo (jamesives/github-pages-deploy-action)
issues: write
name: Update Suppression Rules
if: ${{ !github.event.issue.pull_request &&
contains(github.event.issue.labels.*.name, 'FP Report') &&
contains(github.event.comment.body,'approved') &&
(github.event.comment.user.login == 'jeremylong' ||
github.event.comment.user.login == 'aikebah' ||
github.event.comment.user.login == 'nhumblot' ||
github.event.comment.user.login == 'chadlwilson') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: generatedSuppressions
- uses: actions/[email protected]
- run: |
npm install [email protected]
npm install fs
- name: Commit Suppression Rule
id: fp-ops-commit
uses: actions/[email protected]
with:
script: |
const { execSync } = require("child_process");
const { XMLParser } = require("fast-xml-parser");
const fs = require('fs');
console.log('evaluating issue #' + context.issue.number);
const { data: comments } = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.issue.owner,
repo: context.issue.repo,
})
console.log('author0: ' + comments[0].user.login);
console.log('comments0: ' + comments[0].body);
let botComments = comments.filter(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes('<suppress base="true">') &&
comment.body.includes('Suppression rule:')
)
console.log('bot comments: ' + botComments);
if (botComments.length<=0) {
console.log('Suppression rule from github-actions not found');
core.setOutput('publish', 'false');
//using core.setFailed() ended execution and the check for failure below (to post a message did not work)
core.setOutput('failed', 'true');
return;
}
let lastComment = botComments[botComments.length-1];
let data = lastComment.body.split('```');
let suppression = data[data.length-2].substring(3).trim();
console.log("suppression rule: " + suppression);
const options = {
ignoreAttributes : true
};
const parser = new XMLParser(options);
let proposedRule = parser.parse(suppression);
let commitComment = proposedRule.suppress.notes.trim();
commitComment = commitComment.replace(/[[\]{}`'";|\/\\]/g, '');
console.log("commit comment: " + commitComment);
//validate we haven't already suppressed this one.
const generatedSuppressions = fs.readFileSync('generatedSuppressions.xml', 'utf8');
let found = false;
let previousReportNotes = '';
if (!(generatedSuppressions && generatedSuppressions.trim() === '')) {
let rules = parser.parse(generatedSuppressions);
for (r of rules.suppress) {
if (proposedRule.suppress.packageUrl === r.packageUrl &&
proposedRule.suppress.cpe === r.cpe) {
found = true;
previousReportNotes = r.notes.trim();
break;
}
}
}
if (found) {
let prevIssue = previousReportNotes.split('#')[1].trim();
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Suppress already exists in `generatedSuppressions` branch. See issue #' + prevIssue + '.',
});
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['duplicate']
});
github.rest.issues.update({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed',
});
core.setOutput('publish', 'false');
} else {
fs.appendFileSync('generatedSuppressions.xml', '\n' + suppression.trim(), function (err) {
if (err) throw err;
console.log('Suppression rule added');
});
if (!fs.existsSync('./suppressions')){
fs.mkdirSync('./suppressions');
}
fs.appendFileSync('suppressions/publishedSuppressions.xml', '<?xml version="1.0" encoding="UTF-8"?>\n<suppressions xmlns="https://jeremylong.github.io/DependencyCheck/dependency-suppression.1.3.xsd">' + generatedSuppressions + '\n' + suppression.trim() + '\n</suppressions>', function (err) {
if (err) throw err;
console.log('publishedSuppressions.xml created');
});
fs.appendFileSync('.git/config', `
[user]
name = github-actions[bot]
email = 41898282+github-actions[bot]@users.noreply.github.com
`, err => {
if (err) throw err;
});
execSync('git commit -m "fix(fp): ' + commitComment + '" generatedSuppressions.xml;git push origin generatedSuppressions', (err, stdout, stderr) => {
if (err) throw err;
console.log('committed suppression rule');
});
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Suppress rule has been added to the `generatedSuppressions` branch.',
});
github.rest.issues.update({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed',
});
core.setOutput('publish', 'true');
}
- name: Publish Updated Suppressions
if: ${{ steps.fp-ops-commit.outputs.publish == 'true' }}
uses: JamesIves/[email protected]
with:
branch: gh-pages
folder: suppressions
target-folder: suppressions
- name: Message failure
if: ${{ failure() || steps.fp-ops-commit.outputs.failed }}
uses: actions/[email protected]
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Failed to automatically generate and publish the suppression rule!\n\n' + 'Link to action run: ' + context.serverUrl + '/' + context.repo.owner + '/' + context.repo.repo + '/actions/runs/' + context.runId,
});