Skip to content

Commit

Permalink
Merge branch 'master' into sb_bazelMulipleRuleTypesOffMaster
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Billings committed Jul 13, 2020
2 parents 43a6318 + 055a88a commit 4231bdb
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 53 deletions.
Expand Up @@ -23,31 +23,24 @@
package com.synopsys.integration.detectable.detectables.go.gomod;

import java.io.File;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.synopsys.integration.blackduck.service.BlackDuckServicesFactory;
import com.synopsys.integration.detectable.Extraction;
import com.synopsys.integration.detectable.detectable.codelocation.CodeLocation;
import com.synopsys.integration.detectable.detectable.exception.DetectableException;
import com.synopsys.integration.detectable.detectable.executable.ExecutableOutput;
import com.synopsys.integration.detectable.detectable.executable.ExecutableRunner;
import com.synopsys.integration.detectable.detectable.executable.ExecutableRunnerException;
import com.synopsys.integration.detectable.detectables.go.gomod.model.GoListUJsonData;
import com.synopsys.integration.detectable.detectables.go.gomod.model.ReplaceData;

public class GoModCliExtractor {
private final ExecutableRunner executableRunner;
private final GoModGraphParser goModGraphParser;
private final Gson gson = BlackDuckServicesFactory.createDefaultGsonBuilder().setPrettyPrinting().setLenient().create();
private final Map<String, String> replacementData = new HashMap<>();

private ReplacementDataExtractor replacementDataExtractor = new ReplacementDataExtractor(gson);

public GoModCliExtractor(final ExecutableRunner executableRunner, final GoModGraphParser goModGraphParser) {
this.executableRunner = executableRunner;
Expand Down Expand Up @@ -78,43 +71,18 @@ private List<String> execute(final File directory, final File goExe, final Strin

private List<String> modGraphOutputWithReplacements(File directory, File goExe, List<String> listUJsonOutput) throws ExecutableRunnerException, DetectableException {
final List<String> modGraphOutput = execute(directory, goExe, "Querying for the go mod graph failed:", "mod", "graph");
String jsonString = convertOutputToJsonString(listUJsonOutput);

Type goListUJsonEntryType = new TypeToken<List<GoListUJsonData>>() {}.getType();
List<GoListUJsonData> data = gson.fromJson(jsonString, goListUJsonEntryType);

for (final GoListUJsonData entry : data) {
ReplaceData replace = entry.getReplace();
if (replace != null) {
String path = entry.getPath();
String originalVersion = entry.getVersion();
String replaceVersion = replace.getVersion();
replacementData.put(String.format("%s@%s", path, originalVersion), String.format("%s@%s", path, replaceVersion));
}
}
Map<String,String> replacementData = replacementDataExtractor.extractReplacementData(listUJsonOutput);

for (String line : modGraphOutput) {
for (String original : replacementData.keySet()) {
String newLine = line.replace(original, replacementData.get(original));
int indexOfLine = modGraphOutput.indexOf(line);
modGraphOutput.set(indexOfLine, newLine);
if (!line.equals(newLine)) {
modGraphOutput.set(indexOfLine, newLine);
}
}
}
return modGraphOutput;
}

private String convertOutputToJsonString(List<String> listUJsonOutput) {
// go list -u -json does not provide data in a format that can be consumed by gson
Collections.replaceAll(listUJsonOutput, "}", "},");
String goModGraphAsString = String.join(System.lineSeparator(), listUJsonOutput);
int lastCloseBrace = goModGraphAsString.lastIndexOf("},");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[" + System.lineSeparator());
stringBuilder.append(goModGraphAsString.substring(0, lastCloseBrace));
stringBuilder.append("}");
stringBuilder.append(goModGraphAsString.substring(lastCloseBrace + 2));
stringBuilder.append(System.lineSeparator() + "]");

return stringBuilder.toString();
}
}
@@ -0,0 +1,72 @@
/**
* detectable
*
* Copyright (c) 2020 Synopsys, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.detectable.detectables.go.gomod;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.stream.MalformedJsonException;
import com.synopsys.integration.detectable.detectable.exception.DetectableException;
import com.synopsys.integration.detectable.detectables.go.gomod.model.GoListUJsonData;
import com.synopsys.integration.detectable.detectables.go.gomod.model.ReplaceData;

public class ReplacementDataExtractor {

private Gson gson;

public ReplacementDataExtractor(Gson gson) {
this.gson = gson;
}

public Map<String, String> extractReplacementData(List<String> listUJsonOutput) throws DetectableException {
// Similar to Extractor A in that we're going to delegate parsing to Gson, but we're only going to convert elements one at a time
Map<String, String> replacementData = new HashMap<>();
StringBuilder rawEntry = new StringBuilder();
for (String line : listUJsonOutput) {
rawEntry.append(line);
if (line.startsWith("}")) {
try {
GoListUJsonData data = gson.fromJson(rawEntry.toString(), GoListUJsonData.class);

ReplaceData replace = data.getReplace();
if (replace != null) {
String path = data.getPath();
String originalVersion = data.getVersion();
String replaceVersion = replace.getVersion();
replacementData.put(String.format("%s@%s", path, originalVersion), String.format("%s@%s", path, replaceVersion));
}

// Reset to accumulate a new entry
rawEntry = new StringBuilder();
} catch (JsonSyntaxException e) {
throw new DetectableException(e.getMessage());
}
}
}
return replacementData;
}
}
Expand Up @@ -34,23 +34,23 @@ protected void setup() throws IOException {
addExecutableOutput(goListOutput, "go", "list", "-m");

ExecutableOutput goListUJsonOutput = createStandardOutput(
"{",
"\t\"Path\": \"github.com/codegangsta/negroni\",",
"\t\"Version\": \"v1.0.0\"",
"}",
"{\n",
"\t\"Path\": \"github.com/codegangsta/negroni\",\n",
"\t\"Version\": \"v1.0.0\"\n",
"}\n",
"",
"{",
"\t\"Path\": \"github.com/sirupsen/logrus\",",
"\t\"Version\": \"v1.1.1\",",
"\t\"Replace\": {",
"\t\t\"Path\": \"github.com/sirupsen/logrus\",",
"\t\t\"Version\": \"v2.0.0\"",
"\t}",
"}",
"{\n",
"\t\"Path\": \"github.com/sirupsen/logrus\",\n",
"\t\"Version\": \"v1.1.1\",\n",
"\t\"Replace\": {\n",
"\t\t\"Path\": \"github.com/sirupsen/logrus\",\n",
"\t\t\"Version\": \"v2.0.0\"\n",
"\t}\n",
"}\n",
"",
"{",
"\t\"Path\": \"github.com/davecgh/go-spew\",",
"\t\"Version\": \"v1.1.1\"",
"{\n",
"\t\"Path\": \"github.com/davecgh/go-spew\",\n",
"\t\"Version\": \"v1.1.1\"\n",
"}"
);
addExecutableOutput(goListUJsonOutput, "go", "list", "-m", "-u", "-json", "all");
Expand Down
@@ -0,0 +1,43 @@
package com.synopsys.integration.detectable.detectables.go.unit;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.google.gson.GsonBuilder;
import com.synopsys.integration.detectable.detectable.exception.DetectableException;
import com.synopsys.integration.detectable.detectables.go.gomod.ReplacementDataExtractor;

public class ReplacementDataExtractorTest {

private ReplacementDataExtractor replacementDataExtractor = new ReplacementDataExtractor(new GsonBuilder().setPrettyPrinting().create());

@Test
public void extractorThrowsExceptionIfVersionFormatIsOdd() {
List<String> input = Arrays.asList(
"{\n",
"\t\"Path\": \"github.com/sirupsen/logrus\",\n",
"\t\"Version\": \"v1.1.1\",\n",
"\t\"Replace\": {\n",
"\t\t\"Path\": \"github.com/sirupsen/logrus\",\n",
"\t\t\"Version\": \"v1.1.1\n",
"\t}\n",
"}\n"
);

boolean threwException = false;

try {
replacementDataExtractor.extractReplacementData(input);
} catch (DetectableException e) {
threwException = true;
}

Assertions.assertTrue(threwException);
}

}

0 comments on commit 4231bdb

Please sign in to comment.