Skip to content

Commit

Permalink
refactor(git-parse): Moved GitConfigNode transformation to GitConfig …
Browse files Browse the repository at this point in the history
…to GitConfigNodeTransformer. Added license headers.
  • Loading branch information
JakeMathews committed Apr 9, 2020
1 parent 9a95b48 commit 56e38b3
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 33 deletions.
Expand Up @@ -34,6 +34,7 @@
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfig;
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigNode;
import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigExtractor;
import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigNodeTransformer;
import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitFileParser;
import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.log.IntLogger;
Expand All @@ -45,10 +46,12 @@ public class GitParseExtractor {

private final GitFileParser gitFileParser;
private final GitConfigExtractor gitConfigExtractor;
private final GitConfigNodeTransformer gitConfigNodeTransformer;

public GitParseExtractor(final GitFileParser gitFileParser, final GitConfigExtractor gitConfigExtractor) {
public GitParseExtractor(final GitFileParser gitFileParser, final GitConfigExtractor gitConfigExtractor, final GitConfigNodeTransformer gitConfigNodeTransformer) {
this.gitFileParser = gitFileParser;
this.gitConfigExtractor = gitConfigExtractor;
this.gitConfigNodeTransformer = gitConfigNodeTransformer;
}

public final Extraction extract(final File gitConfigFile, final File gitHeadFile) {
Expand All @@ -58,7 +61,7 @@ public final Extraction extract(final File gitConfigFile, final File gitHeadFile

final List<String> configFileContent = FileUtils.readLines(gitConfigFile, StandardCharsets.UTF_8);
final List<GitConfigNode> gitConfigNodes = gitFileParser.parseGitConfig(configFileContent);
final GitConfig gitConfig = GitConfig.fromGitConfigNodes(gitConfigNodes);
final GitConfig gitConfig = gitConfigNodeTransformer.createGitConfig(gitConfigNodes);

final NameVersion projectNameVersion = gitConfigExtractor.extractProjectInfo(gitConfig, gitHead);

Expand Down
@@ -1,38 +1,35 @@
/**
* 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.git.parsing.model;

import java.util.List;
import java.util.stream.Collectors;

// A List<GitConfigNode> is a rough parsing result. This class provides a better API.
public class GitConfig {
// Only including remote and branch nodes since the core node is not being used.
private final List<GitConfigRemote> gitConfigRemotes;
private final List<GitConfigBranch> gitConfigBranches;

public static GitConfig fromGitConfigNodes(final List<GitConfigNode> gitConfigNodes) {
final List<GitConfigRemote> gitConfigRemotes = gitConfigNodes.stream()
.filter(node -> node.getType().equals("remote"))
.map(node -> {
String remoteNodeName = node.getName().orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a name."));
String remoteNodeUrl = node.getProperty("url").orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a url field."));
String remoteNodeFetch = node.getProperty("fetch").orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a fetch field."));
return new GitConfigRemote(remoteNodeName, remoteNodeUrl, remoteNodeFetch);
})
.collect(Collectors.toList());

final List<GitConfigBranch> gitConfigBranches = gitConfigNodes.stream()
.filter(node -> node.getType().equals("branch"))
.map(node -> {
String remoteNodeName = node.getName().orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a name."));
String remoteNodeRemote = node.getProperty("remote").orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a remote field."));
String remoteNodeMerge = node.getProperty("merge").orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a fetch field."));
return new GitConfigBranch(remoteNodeName, remoteNodeRemote, remoteNodeMerge);
})
.collect(Collectors.toList());

return new GitConfig(gitConfigRemotes, gitConfigBranches);
}

public GitConfig(final List<GitConfigRemote> gitConfigRemotes, final List<GitConfigBranch> gitConfigBranches) {
this.gitConfigRemotes = gitConfigRemotes;
this.gitConfigBranches = gitConfigBranches;
Expand Down
@@ -1,3 +1,25 @@
/**
* 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.git.parsing.model;

import org.jetbrains.annotations.NotNull;
Expand Down
@@ -1,3 +1,25 @@
/**
* 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.git.parsing.model;

import org.jetbrains.annotations.NotNull;
Expand Down
@@ -0,0 +1,57 @@
/**
* 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.git.parsing.parse;

import java.util.List;
import java.util.stream.Collectors;

import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfig;
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigBranch;
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigNode;
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigRemote;

public class GitConfigNodeTransformer {
public GitConfig createGitConfig(final List<GitConfigNode> gitConfigNodes) {
final List<GitConfigRemote> gitConfigRemotes = gitConfigNodes.stream()
.filter(node -> node.getType().equals("remote"))
.map(node -> {
String remoteNodeName = node.getName().orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a name."));
String remoteNodeUrl = node.getProperty("url").orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a url field."));
String remoteNodeFetch = node.getProperty("fetch").orElseThrow(() -> new IllegalArgumentException("Expected remote node to have a fetch field."));
return new GitConfigRemote(remoteNodeName, remoteNodeUrl, remoteNodeFetch);
})
.collect(Collectors.toList());

final List<GitConfigBranch> gitConfigBranches = gitConfigNodes.stream()
.filter(node -> node.getType().equals("branch"))
.map(node -> {
String remoteNodeName = node.getName().orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a name."));
String remoteNodeRemote = node.getProperty("remote").orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a remote field."));
String remoteNodeMerge = node.getProperty("merge").orElseThrow(() -> new IllegalArgumentException("Expected branch node to have a fetch field."));
return new GitConfigBranch(remoteNodeName, remoteNodeRemote, remoteNodeMerge);
})
.collect(Collectors.toList());

return new GitConfig(gitConfigRemotes, gitConfigBranches);
}
}
Expand Up @@ -104,6 +104,7 @@
import com.synopsys.integration.detectable.detectables.git.parsing.GitParseDetectable;
import com.synopsys.integration.detectable.detectables.git.parsing.GitParseExtractor;
import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigExtractor;
import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigNodeTransformer;
import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitFileParser;
import com.synopsys.integration.detectable.detectables.go.godep.GoDepExtractor;
import com.synopsys.integration.detectable.detectables.go.godep.GoDepLockDetectable;
Expand Down Expand Up @@ -447,8 +448,12 @@ private GitConfigExtractor gitConfigExtractor() {
return new GitConfigExtractor(gitUrlParser());
}

private GitConfigNodeTransformer gitConfigNodeTransformer() {
return new GitConfigNodeTransformer();
}

private GitParseExtractor gitParseExtractor() {
return new GitParseExtractor(gitFileParser(), gitConfigExtractor());
return new GitParseExtractor(gitFileParser(), gitConfigExtractor(), gitConfigNodeTransformer());
}

private GitUrlParser gitUrlParser() {
Expand Down
Expand Up @@ -8,12 +8,13 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class GitConfigTest {
import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigNodeTransformer;

class GitConfigNodeTransformerTest {

@Test
void fromGitConfigNodes() {
void createGitConfig() {
//#region Create GitConfigNodes

// The core node should be ignored by the transformation.
final Map<String, String> coreProperties = new HashMap<>();
coreProperties.put("repositoryformatversion", "0");
Expand All @@ -40,10 +41,10 @@ void fromGitConfigNodes() {
gitConfigNodes.add(remoteNode);
gitConfigNodes.add(branchNode);
gitConfigNodes.add(anotherBranch);

//#endregion Create GitConfigNodes

final GitConfig gitConfig = GitConfig.fromGitConfigNodes(gitConfigNodes);
final GitConfigNodeTransformer gitConfigNodeTransformer = new GitConfigNodeTransformer();
final GitConfig gitConfig = gitConfigNodeTransformer.createGitConfig(gitConfigNodes);

Assertions.assertEquals(1, gitConfig.getGitConfigRemotes().size());

Expand Down

0 comments on commit 56e38b3

Please sign in to comment.