@@ -102,17 +102,8 @@ namespace {
102
102
}
103
103
104
104
void FileSarif::addCodeFlowWithoutExternal (json &result, const fs::path &projectRoot) {
105
- json resultWithAbs = result;
106
- json &locationsWithAbs = resultWithAbs.at (" codeFlows" ).at (0 ).at (" threadFlows" ).at (0 );
107
- deleteExternalFilesFromResult (locationsWithAbs,
108
- projectRoot, true );
109
- result = resultWithAbs;
110
- json &locations = result.at (" codeFlows" ).at (0 ).at (" threadFlows" ).at (0 );
111
- deleteExternalFilesFromResult (locations,
105
+ deleteExternalFilesFromResult (result.at (" codeFlows" ).at (0 ).at (" threadFlows" ).at (0 ),
112
106
projectRoot, false );
113
- if (locationsWithAbs.at (" locations" ).size () != locations.at (" locations" ).size ()) {
114
- result.at (" codeFlows" ).push_back (resultWithAbs.at (" codeFlows" ).at (0 ));
115
- }
116
107
}
117
108
118
109
void FileSarif::deleteExternalFilesFromResult (json &result, const fs::path &projectRoot, bool leaveFromLib) {
@@ -125,24 +116,33 @@ namespace {
125
116
}
126
117
auto it = std::remove_if (result.at (" locations" ).begin (), result.at (" locations" ).end (),
127
118
[&](json &location) {
128
- if (leaveFromLib) {
129
- auto opt = isRelevant (
130
- (std::string) getUriFromLocation (location.at (" location" )));
131
- if (!opt.has_value ()) {
132
- return true ;
133
- }
134
- getUriFromLocation (location.at (" location" )) = opt.value ().string ();
135
- return false ;
136
- } else {
137
- return StringUtils::startsWith (
138
- (std::string) getUriFromLocation (location.at (" location" )), " /" );
139
- }
119
+ return !fs::exists (projectRoot / (std::string)getUriFromLocation (location.at (" location" )));
140
120
});
141
121
result.at (" locations" ).erase (it, result.at (" locations" ).end ());
142
122
}
143
123
144
- void FileSarif::addResultToSarif (const json &result) {
145
- sarifJson.at (" runs" ).at (0 ).at (" results" ).push_back (result);
124
+ namespace {
125
+ [[nodiscard]] int getErrorLineFromResult (const json& result) {
126
+ return result.at (" locations" ).at (0 ).at (" physicalLocation" )
127
+ .at (" region" ).at (" startLine" );
128
+ }
129
+
130
+ [[nodiscard]] std::string getErrorFileFromResult (const json& result) {
131
+ return result.at (" locations" ).at (0 ).at (" physicalLocation" )
132
+ .at (" artifactLocation" ).at (" uri" );
133
+ }
134
+ }
135
+
136
+ void FileSarif::addResultToSarif (const json &newResult) {
137
+ json &all = sarifJson.at (" runs" ).at (0 ).at (" results" );
138
+ for (json &result : all) {
139
+ if (getErrorFileFromResult (result) == getErrorFileFromResult (newResult) &&
140
+ getErrorLineFromResult (result) == getErrorLineFromResult (newResult)) {
141
+ result.at (" codeFlows" ).push_back (newResult.at (" codeFlows" ).at (0 ));
142
+ return ;
143
+ }
144
+ }
145
+ sarifJson.at (" runs" ).at (0 ).at (" results" ).push_back (newResult);
146
146
}
147
147
148
148
}
0 commit comments