Skip to content

Commit

Permalink
Tidy minor code warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
chadlwilson committed Sep 1, 2023
1 parent 73082aa commit 89ad673
Show file tree
Hide file tree
Showing 29 changed files with 50 additions and 60 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pipelines:
materials:
mygit: # this is the name of material, the name can contain only of alphanumeric & underscore characters
# keyword git says about type of material and url at once
git: http://my.example.org/mygit.git
git: https://my.example.org/mygit.git
branch: ci
myupstream: # this name does not matter, but there should be no 2 materials with the same name
# type is optional here, material type is implied based on presence of pipeline and stage fields
Expand Down Expand Up @@ -365,7 +365,7 @@ When `format_version` is `4` (see [above](#format-version)), the order of displa
- Pipelines defined in GoCD's config XML will also default to -1.
- If multiple pipelines have the same `display_order` value, their order relative to each other will be indeterminate.

```json
```yaml
mypipeline1:
group: group1
display_order: 10
Expand Down Expand Up @@ -643,7 +643,7 @@ mygit:
More customized git material is possible:
```yaml
gitMaterial1:
git: "http://my.git.repository.com"
git: "https://my.git.repository.com"
branch: feature12
ignore:
- externals/**/*.*
Expand Down Expand Up @@ -677,7 +677,7 @@ For **GoCD >= 19.4.0 and `format_version: 5` and above**:
You are advised to utilize `username` and `encrypted_password` for passing in material credentials as:
```yaml
gitMaterial1:
git: "http://my.git.repository.com"
git: "https://my.git.repository.com"
branch: feature12
username: my_username
encrypted_password: encrypted_value
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/cd/go/plugin/config/yaml/YamlConfigPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ private GoPluginApiResponse handleParseContentRequest(GoPluginApiRequest request
YamlConfigParser parser = new YamlConfigParser();
Map<String, String> contents = parsed.getParam("contents");
JsonConfigCollection result = new JsonConfigCollection();
contents.forEach((filename, content) -> {
parser.parseStream(result, new ByteArrayInputStream(content.getBytes()), filename);
});
contents.forEach((filename, content) -> parser.parseStream(result, new ByteArrayInputStream(content.getBytes()), filename));
result.updateTargetVersionFromFiles();

return success(gson.toJson(result.getJsonObject()));
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/cd/go/plugin/config/yaml/YamlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

public class YamlUtils {
// http://yaml.org/type/bool.html
private static Pattern truePattern = Pattern.compile(
private static final Pattern PATTERN_TRUE = Pattern.compile(
"y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON");
private static Pattern falsePattern = Pattern.compile(
private static final Pattern PATTERN_FALSE = Pattern.compile(
"n|N|no|No|NO|false|False|FALSE|off|Off|OFF");

public static String dump(Object o) {
Expand Down Expand Up @@ -61,8 +61,8 @@ private static JsonArray getOptionalStringList(Map<String, Object> map, String f
JsonArray jsonArray = new JsonArray();
Object value = map.get(fieldName);
if (value != null) {
List<String> list = (List<String>) value;
if (list.size() == 0)
@SuppressWarnings("unchecked") List<String> list = (List<String>) value;
if (list.isEmpty())
return null;
for (String item : list) {
jsonArray.add(item);
Expand All @@ -86,24 +86,24 @@ public static Boolean getOptionalBoolean(Map<String, Object> map, String fieldNa
if (value instanceof Boolean)
return (Boolean) value;
String boolText = (String) value;
if (truePattern.matcher(boolText).matches())
if (PATTERN_TRUE.matcher(boolText).matches())
return true;
else if (falsePattern.matcher(boolText).matches())
else if (PATTERN_FALSE.matcher(boolText).matches())
return false;
throw new YamlConfigException("Expected boolean value in field " + fieldName + ", got " + boolText);
}
return null;
}

public static String getOptionalString(Map map, String fieldName) {
public static String getOptionalString(Map<String, Object> map, String fieldName) {
Object value = map.get(fieldName);
if (value != null) {
return (String) value;
}
return null;
}

public static Integer getOptionalInteger(Map map, String fieldName) {
public static Integer getOptionalInteger(Map<String, Object> map, String fieldName) {
Object value = map.get(fieldName);
if (value != null) {
if (value instanceof Integer)
Expand All @@ -115,7 +115,7 @@ else if (value instanceof String) {
return null;
}

public static Object getOptionalObject(Map map, String fieldName) {
public static Object getOptionalObject(Map<String, Object> map, String fieldName) {
return map.get(fieldName);
}
}
9 changes: 3 additions & 6 deletions src/main/java/cd/go/plugin/config/yaml/cli/YamlPluginCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
import com.beust.jcommander.ParameterException;
import com.google.gson.JsonObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import static java.lang.String.format;

public class YamlPluginCli {
public static void main(String[] args) {
RootCmd root = new RootCmd();
Expand Down Expand Up @@ -65,19 +62,19 @@ private static String getLocation(String file) {
private static InputStream getFileAsStream(String file) {
InputStream s = null;
try {
s = "-".equals(file) ? System.in : new FileInputStream(new File(file));
s = "-".equals(file) ? System.in : new FileInputStream(file);
} catch (FileNotFoundException e) {
die(1, e.getMessage());
}
return s;
}

private static void echo(String message, Object... items) {
System.out.println(format(message, items));
System.out.printf(message + "%n", items);
}

private static void error(String message, Object... items) {
System.err.println(format(message, items));
System.err.printf(message + "%n", items);
}

private static void die(int exitCode, String message, Object... items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public Map<String, Object> inverseTransform(Map<String, Object> material) {
if (yamlSpecialKeywords.contains(materialProp.getKey()))
continue;
if (materialProp.getValue() instanceof String)
materialdata.put(materialProp.getKey(), (String) materialProp.getValue());
materialdata.put(materialProp.getKey(), materialProp.getValue());
}

if (materialName == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import java.util.Map;

public class RootTransform {
private PipelineTransform pipelineTransform;
private EnvironmentsTransform environmentsTransform;
private final PipelineTransform pipelineTransform;
private final EnvironmentsTransform environmentsTransform;

public RootTransform() {
EnvironmentVariablesTransform environmentVarsTransform = new EnvironmentVariablesTransform();
Expand All @@ -37,12 +37,12 @@ public String inverseTransformPipeline(Map<String, Object> pipeline) {

public JsonConfigCollection transform(Object rootObj, String location) {
JsonConfigCollection partialConfig = new JsonConfigCollection();
Map<String, Object> rootMap = (Map<String, Object>) rootObj;
@SuppressWarnings("unchecked") Map<String, Object> rootMap = (Map<String, Object>) rootObj;
// must obtain format_version first
int formatVersion = 1;
for (Map.Entry<String, Object> pe : rootMap.entrySet()) {
if ("format_version".equalsIgnoreCase(pe.getKey())) {
formatVersion = Integer.valueOf((String) pe.getValue());
formatVersion = Integer.parseInt((String) pe.getValue());
partialConfig.updateFormatVersionFound(formatVersion);
}
}
Expand All @@ -63,7 +63,7 @@ public JsonConfigCollection transform(Object rootObj, String location) {
} else if ("environments".equalsIgnoreCase(pe.getKey())) {
if (pe.getValue() == null)
continue;
Map<String, Object> environments = (Map<String, Object>) pe.getValue();
@SuppressWarnings("unchecked") Map<String, Object> environments = (Map<String, Object>) pe.getValue();
for (Map.Entry<String, Object> env : environments.entrySet()) {
try {
JsonElement jsonEnvironment = environmentsTransform.transform(env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public JsonObject transform(Map.Entry<String, Object> entry) {

public Map<String, Object> inverseTransform(Map<String, Object> stage) {
if (stage == null) {
return stage;
return null;
}
String stageName = (String) stage.get(JSON_STAGE_NAME_FIELD);
Map<String, Object> inverseStage = new LinkedTreeMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public class JsonConfigCollectionTest {
private JsonObject pipe1;
private JsonObject pipe2;
private JsonObject devEnv;
private JsonObject pipeInGroup;

@BeforeEach
public void SetUp() {
public void setUp() {
jsonCollection = new JsonConfigCollection();

pipe1 = new JsonObject();
Expand All @@ -27,10 +26,6 @@ public void SetUp() {
pipe2 = new JsonObject();
pipe2.addProperty("name", "pipe2");

pipeInGroup = new JsonObject();
pipeInGroup.addProperty("name", "pipe3");
pipeInGroup.addProperty("group", "mygroup");

devEnv = new JsonObject();
devEnv.addProperty("name", "dev");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void shouldInverseTRansformCompletePluggable() throws IOException {
public void inverseTransform_shouldGenerateARandomMaterialNameInAbsenceOfName() {
Map<String, Object> material = parser.inverseTransform(readJsonGson("parts/materials/material_without_name.git.json"));

String materialName = material.keySet().stream().findFirst().get();
String materialName = material.keySet().stream().findFirst().orElseThrow();
assertThat(materialName, containsString("git-"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void inverseTransform_shouldHandleMultipleMaterialsWithoutNames() {

Map<String, Object> pipeline = parser.inverseTransform(readJsonGson("parts/pipeline_with_multiple_materials.json"));

assertThat(((Map)((Map)pipeline.get("pipe1")).get("materials")).size(), is(2));
assertThat(((Map<?, ?>)((Map<?, ?>)pipeline.get("pipe1")).get("materials")).size(), is(2));
}

private void testTransform(String caseFile) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/examples.out/aliases.gocd.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"materials": [{
"name": "mygit",
"type": "git",
"url": "http://my.example.org/mygit.git",
"url": "https://my.example.org/mygit.git",
"branch": "ci"
}],
"stages": [{
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/examples.out/format-version-10.gocd.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{
"name": "mygit",
"type": "git",
"url": "http://my.example.org/mygit.git",
"url": "https://my.example.org/mygit.git",
"filter": {
"includes": [
"externals",
Expand Down Expand Up @@ -45,7 +45,7 @@
{
"name": "mygit",
"type": "git",
"url": "http://my.example.org/mygit.git",
"url": "https://my.example.org/mygit.git",
"filter": {
"includes": [
"externals",
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/examples.out/format-version-9.gocd.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{
"name": "mygit",
"type": "git",
"url": "http://my.example.org/mygit.git",
"url": "https://my.example.org/mygit.git",
"filter": {
"whitelist": [
"externals",
Expand Down Expand Up @@ -45,7 +45,7 @@
{
"name": "mygit",
"type": "git",
"url": "http://my.example.org/mygit.git",
"url": "https://my.example.org/mygit.git",
"filter" : {
"whitelist": [
"externals",
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/examples.out/rich.gocd.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{
"name": "mygit",
"type": "git",
"url": "http://my.example.org/mygit.git",
"url": "https://my.example.org/mygit.git",
"branch": "ci"
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/examples.out/simple.gocd.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{
"name": "mygit",
"type": "git",
"url": "http://my.example.org/mygit.git"
"url": "https://my.example.org/mygit.git"
}
],
"stages": [
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/examples/aliases.gocd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pipelines:
group: aliases
materials:
mygit:
git: http://my.example.org/mygit.git
git: https://my.example.org/mygit.git
branch: ci
stages:
- prepare:
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/examples/format-version-10.gocd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pipelines:
group: simple
materials:
mygit:
git: http://my.example.org/mygit.git
git: https://my.example.org/mygit.git
# whitelist is still allowed (backwards compatibility) and transforms to includes because format_version is 10
whitelist:
- externals
Expand All @@ -22,7 +22,7 @@ pipelines:
group: simple
materials:
mygit:
git: http://my.example.org/mygit.git
git: https://my.example.org/mygit.git
# includes is allowed and transforms to includes because format_version is 10
includes:
- externals
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/examples/format-version-9.gocd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pipelines:
group: simple
materials:
mygit:
git: http://my.example.org/mygit.git
git: https://my.example.org/mygit.git
# whitelist is still allowed and transforms to whitelist because format_version is 9
whitelist:
- externals
Expand All @@ -22,7 +22,7 @@ pipelines:
group: simple
materials:
mygit:
git: http://my.example.org/mygit.git
git: https://my.example.org/mygit.git
# includes is allowed and transforms to whitelist because format_version is 9
includes:
- externals
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/examples/rich.gocd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pipelines: # tells plugin that here are pipelines by name
materials:
mygit: # this is the name of material
# says about type of material and url at once
git: http://my.example.org/mygit.git
git: https://my.example.org/mygit.git
branch: ci
upstream:
# type is optional here, material type is implied based on pipeline and stage fields
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/examples/simple-invalid.gocd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pipelines:
materials:
# materials should be a hash - bogus !
- mygit:
git: http://my.example.org/mygit.git
git: https://my.example.org/mygit.git
stages:
- build: # name of stage
jobs:
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/examples/simple.gocd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pipelines:
materials:
mygit: # this is the name of material
# says about type of material and url at once
git: http://my.example.org/mygit.git
git: https://my.example.org/mygit.git
stages:
- build: # name of stage
jobs:
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/parts/materials/complete10.git.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gitMaterial1",
"type": "git",
"url": "http://my.git.repository.com",
"url": "https://my.git.repository.com",
"branch": "feature12",
"username": "username",
"encrypted_password": "some encrypted value",
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/parts/materials/complete10.git.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gitMaterial1:
git: "http://my.git.repository.com"
git: "https://my.git.repository.com"
username: "username"
encrypted_password: "some encrypted value"
branch: "feature12"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gitMaterial1",
"type": "git",
"url": "http://my.git.repository.com",
"url": "https://my.git.repository.com",
"branch": "feature12",
"username": "username",
"encrypted_password": "some encrypted value",
Expand Down
Loading

0 comments on commit 89ad673

Please sign in to comment.