Skip to content

Commit

Permalink
initial copybara config file generator
Browse files Browse the repository at this point in the history
generate config file by running Unit Test

Signed-off-by: Robert Gänzle <gaenzlrt@studi.informatik.uni-stuttgart.de>
  • Loading branch information
gaenzlrt committed Jul 11, 2018
1 parent ab82e4f commit 9e8c7b9
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 0 deletions.
152 changes: 152 additions & 0 deletions org.eclipse.winery.tools.copybaragenerator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright (c) 2018 Contributors to the Eclipse Foundation
~
~ See the NOTICE file(s) distributed with this work for additional
~ information regarding copyright ownership.
~
~ This program and the accompanying materials are made available under the
~ terms of the Eclipse Public License 2.0 which is available at
~ http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
~ which is available at https://www.apache.org/licenses/LICENSE-2.0.
~
~ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.winery</groupId>
<artifactId>winery</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<artifactId>org.eclipse.winery.tools.copybaragenerator</artifactId>
<properties>
<checkstyle.config.location>../checkstyle.xml</checkstyle.config.location>
<main.basedir>${project.parent.basedir}</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.winery</groupId>
<artifactId>org.eclipse.winery.repository</artifactId>
<version>2.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${ch.qos.logback.logback-classic.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>.</directory>
<filtering>true</filtering>
<includes>
<include>about.html</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>winery.properties</include>
<include>logback.xml</include>
<include>logback-test.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>7.6</version>
</dependency>
<dependency>
<groupId>com.thomasjensen.checkstyle.addons</groupId>
<artifactId>checkstyle-addons</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<finalName>winery</finalName>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/********************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/

package org.eclipse.winery.tools.copybaragenerator;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.SortedSet;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import org.eclipse.winery.common.RepositoryFileReference;
import org.eclipse.winery.common.ids.definitions.DefinitionsChildId;
import org.eclipse.winery.repository.Constants;
import org.eclipse.winery.repository.backend.BackendUtils;
import org.eclipse.winery.repository.backend.IRepository;
import org.eclipse.winery.repository.backend.RepositoryFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CopybaraGenerator {

private static final Logger LOGGER = LoggerFactory.getLogger(CopybaraGenerator.class);

private final IRepository repository;

public CopybaraGenerator() {
this.repository = RepositoryFactory.getRepository();
}

public CopybaraGenerator(IRepository repository) {
this.repository = repository;
}

public String generateOriginFilesConfig() {
SortedSet<DefinitionsChildId> allDefinitionsChildIds = repository.getAllDefinitionsChildIds();
return allDefinitionsChildIds.stream()
.filter(id -> {
RepositoryFileReference repositoryFileReference = new RepositoryFileReference(id, Constants.LICENSE_FILE_NAME);
if (!repository.exists(repositoryFileReference)) {
return false;
}
try (InputStream inputStream = repository.newInputStream(repositoryFileReference)) {
String licenceString = "Apache License";
// We read 80 bytes, because the string "Apache License" could be centered
byte[] firstBytes = new byte[80];
int readBytes = inputStream.read(firstBytes);
String firstBytesAsString = new String(firstBytes, 0, readBytes);
// trim -> remove whitespaces before
// We might have read more than one line (because of 80 bytes maximum), therefore, we just check whether the string begins with "Apache License"
return (firstBytesAsString.trim().startsWith(licenceString));
} catch (IOException e) {
LOGGER.error("Could not create input stream for {}", repositoryFileReference.toString(), e);
return false;
}
})
.map(id -> BackendUtils.getPathInsideRepo(id))
.collect(Collectors.joining("**\",\n \"", "origin_files = glob([\"", "**\"]),"));
}

public String generateCopybaraConfigFile() {
StringJoiner copybaraConfig = new StringJoiner("\n");
copybaraConfig.add("urlOrigin = \"git@github.com:OpenTOSCA/tosca-definitions-internal.git\"");
copybaraConfig.add("urlDestination = \"file:///tmp/copybara/tosca-definitions-public\"");
copybaraConfig.add("core.workflow(");
copybaraConfig.add(" name = \"default\",");
copybaraConfig.add(" origin = git.origin(");
copybaraConfig.add(" url = urlOrigin,");
copybaraConfig.add(" ref = \"master\",");
copybaraConfig.add(" ),");
copybaraConfig.add(" destination = git.destination(");
copybaraConfig.add(" url = urlDestination,");
copybaraConfig.add(" fetch = \"master\",");
copybaraConfig.add(" push = \"master\",");
copybaraConfig.add(" ),");
copybaraConfig.add(" authoring = authoring.pass_thru(\"OpenTOSCA Bot <opentosca@iaas.uni-stuttgart.de>\"),");
copybaraConfig.add(" " + generateOriginFilesConfig());
copybaraConfig.add(" destination_files = glob([\"**\"], exclude = [\"README_INTERNAL.md\"]),");
copybaraConfig.add(")");
return copybaraConfig.toString();
}

public void generateCopybaraConfigFile(Path targetFile) throws IOException {
String copyBaraConfig = generateCopybaraConfigFile();
Files.write(targetFile, copyBaraConfig.getBytes());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/********************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/

package org.eclipse.winery.tools.copybaragenerator;

import org.eclipse.winery.repository.TestWithGitBackedRepository;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CopybaraGeneratorTest extends TestWithGitBackedRepository {

@Test
public void generatedConfigHasCorrectStrings() throws Exception {
// we use the commit 4b218e6 instead of origin/plain, because the number of definitions changes fast
this.setRevisionTo("4b218e6");
CopybaraGenerator copybaraGenerator = new CopybaraGenerator();
String config = copybaraGenerator.generateCopybaraConfigFile();
assertEquals("urlOrigin = \"git@github.com:OpenTOSCA/tosca-definitions-internal.git\"\n" +
"urlDestination = \"file:///tmp/copybara/tosca-definitions-public\"\n" +
"core.workflow(\n" +
" name = \"default\",\n" +
" origin = git.origin(\n" +
" url = urlOrigin,\n" +
" ref = \"master\",\n" +
" ),\n" +
" destination = git.destination(\n" +
" url = urlDestination,\n" +
" fetch = \"master\",\n" +
" push = \"master\",\n" +
" ),\n" +
" authoring = authoring.pass_thru(\"OpenTOSCA Bot <opentosca@iaas.uni-stuttgart.de>\"),\n" +
" origin_files = glob([\"artifacttemplates/http%3A%2F%2Fplain.winery.opentosca.org%2Fartifacttemplates/ArtifactTemplateWithFilesAndSources-ArtifactTypeWithoutProperties/**\",\n" +
" \"artifacttemplates/http%3A%2F%2Fplain.winery.opentosca.org%2Fartifacttemplates/ArtifactTemplateWithoutAnyFiles-ArtifactTypeWithoutProperties/**\"]),\n" +
" destination_files = glob([\"**\"], exclude = [\"README_INTERNAL.md\"]),\n" +
")", config);
}

@Test
public void generatedOriginFilesConfigHasCorrectStrings() throws Exception {
// we use the commit 4b218e6 instead of origin/plain, because the number of definitions changes fast
this.setRevisionTo("4b218e6");
CopybaraGenerator copybaraGenerator = new CopybaraGenerator();
String config = copybaraGenerator.generateOriginFilesConfig();
assertEquals("origin_files = glob([\"artifacttemplates/http%3A%2F%2Fplain.winery.opentosca.org%2Fartifacttemplates/ArtifactTemplateWithFilesAndSources-ArtifactTypeWithoutProperties/**\",\n" +
" \"artifacttemplates/http%3A%2F%2Fplain.winery.opentosca.org%2Fartifacttemplates/ArtifactTemplateWithoutAnyFiles-ArtifactTypeWithoutProperties/**\"]),", config);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<module>org.eclipse.winery.compliance</module>
<module>org.eclipse.winery.generators.ia</module>
<module>org.eclipse.winery.highlevelrestapi</module>
<module>org.eclipse.winery.tools.copybaragenerator</module>
</modules>
<build>
<plugins>
Expand Down

0 comments on commit 9e8c7b9

Please sign in to comment.