Skip to content

Commit

Permalink
Merge pull request #8 from jenkinsci/devel
Browse files Browse the repository at this point in the history
bug fix and refactor and tests
  • Loading branch information
Patrick Huang authored Mar 9, 2017
2 parents ba58864 + 9c3b204 commit 7eb2f76
Show file tree
Hide file tree
Showing 10 changed files with 437 additions and 77 deletions.
7 changes: 3 additions & 4 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ boolean onJenkinsCI = env.JENKINS_URL &&

if (onJenkinsCI) {
/* running on ci.jenkins.io, we will have `buildPlugin` step provided by: https://github.com/jenkins-infra/pipeline-library */
buildPlugin()
buildPlugin(['platforms': ['linux']])
return
}

Expand Down Expand Up @@ -59,12 +59,11 @@ try {

// notify if compile+unit test successful
notify.testResults(null)
archive "**/${hpiFiles},**/target/site/jacoco/**"
archive "**/${hpiFiles}"
}

stage('Report') {
// this is not working properly yet
// step([$class: 'JacocoPublisher'])
step([$class: 'JacocoPublisher'])
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://ci.jenkins.io/buildStatus/icon?job=Plugins/zanata-plugin/master)](https://ci.jenkins.io/job/Plugins/job/zanata-plugin/job/master/)
master branch: [![Build Status](https://ci.jenkins.io/buildStatus/icon?job=Plugins/zanata-plugin/master)](https://ci.jenkins.io/job/Plugins/job/zanata-plugin/job/master/)

### A Jenkins plugin to synchronize localization resources between SCM repository and Zanata

Expand Down
19 changes: 17 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<packaging>hpi</packaging>

<properties>
<coverage.limit>0.64</coverage.limit>
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
<jenkins.version>1.625.3</jenkins.version>
<!-- Java Level to use. Java 7 required when using core >= 1.612 -->
Expand Down Expand Up @@ -232,13 +233,27 @@
</repository>
</repositories>
</profile>

<profile>
<id>check-coverage</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<!-- jacoco gives different coverage result on windows. Plus we have tests that will fail on windows -->
<coverage.limit>0</coverage.limit>
</properties>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>default-prepare-agent</id>
Expand All @@ -264,9 +279,9 @@
<element>BUNDLE</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.60</minimum>
<minimum>${coverage.limit}</minimum>
</limit>
</limits>
</rule>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
*/
public interface ZanataSyncService extends Serializable {

PullOptions getPullOptions();

PushOptions getPushOptions();

void pushToZanata(Path repoBase) throws ZanataSyncException;

void pullFromZanata(Path repoBase) throws ZanataSyncException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;

import org.jenkinsci.plugins.zanata.cli.HasSyncJobDetail;
import org.jenkinsci.plugins.zanata.cli.service.PullService;
import org.jenkinsci.plugins.zanata.cli.service.PushService;
import org.jenkinsci.plugins.zanata.cli.service.ZanataSyncService;
import org.jenkinsci.plugins.zanata.cli.util.PushPullOptionsUtil;
import org.jenkinsci.plugins.zanata.exception.ZanataSyncException;
Expand All @@ -41,6 +44,7 @@
import org.zanata.client.commands.push.PushOptions;
import org.zanata.client.commands.push.PushOptionsImpl;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
Expand All @@ -54,51 +58,71 @@ public class ZanataSyncServiceImpl implements ZanataSyncService {
LoggerFactory.getLogger(ZanataSyncServiceImpl.class);
private static final long serialVersionUID = 1L;

private final transient PullOptions pullOptions;
private final transient PushOptions pushOptions;

private final PushServiceImpl pushService = new PushServiceImpl();
private final PullServiceImpl pullService = new PullServiceImpl();
private final PushService pushService;
private final PullService pullService;
private final Set<String> projectConfigs;

public ZanataSyncServiceImpl(HasSyncJobDetail jobDetail) {
String zanataUrl = jobDetail.getZanataURL();
private final String zanataUrl;
private final String username;
private final String apiKey;
private final String localeId;
private final String pushToZanataOption;

@VisibleForTesting
protected ZanataSyncServiceImpl(PullService pullService,
PushService pushService, HasSyncJobDetail jobDetail) {
this.pullService = pullService;
this.pushService = pushService;
zanataUrl = jobDetail.getZanataURL();
String syncToZanataOption = jobDetail.getSyncOption();
String pushToZanataOption = Strings.emptyToNull(syncToZanataOption);
String username = jobDetail.getZanataUsername();
String apiKey = jobDetail.getZanataSecret();
pushToZanataOption = Strings.emptyToNull(syncToZanataOption);
username = jobDetail.getZanataUsername();
apiKey = jobDetail.getZanataSecret();
projectConfigs = getProjectConfigs(jobDetail.getZanataProjectConfigs());

String localeId = jobDetail.getZanataLocaleIds();
PullOptionsImpl pullOptions = new PullOptionsImpl();
localeId = jobDetail.getZanataLocaleIds();


// if project id is given from webhook, only handle this project
// String projectId = jobDetail.getProject();
// if (!Strings.isNullOrEmpty(projectId)) {
// pullOptions.setProj(projectId);
// pushOptions.setProj(projectId);
// }
}

public ZanataSyncServiceImpl(HasSyncJobDetail jobDetail) {
this(new PullServiceImpl(), new PushServiceImpl(), jobDetail);
}

private PushOptionsImpl newPushOptionsFromJobConfig() {
PushOptionsImpl pushOptions = new PushOptionsImpl();
pullOptions.setInteractiveMode(false);
pushOptions.setInteractiveMode(false);
pullOptions.setUsername(username);
pullOptions.setKey(apiKey);
pushOptions.setUsername(username);
pushOptions.setKey(apiKey);
pushOptions.setPushType(pushToZanataOption);
// TODO until https://zanata.atlassian.net/browse/ZNTA-1427 is fixed we can't trust etag cache
pullOptions.setUseCache(false);
// if localeId is given, only handle this locale
if (!Strings.isNullOrEmpty(localeId)) {
pushOptions.setLocales(localeId);
}
overrideURLIfSpecified(pushOptions, zanataUrl);
// this.pushOptions.setLogHttp(true);
return pushOptions;
}

this.pushOptions = pushOptions;
this.pullOptions = pullOptions;
// this.pushOptions.setLogHttp(true);
// this.pullOptions.setLogHttp(true);
private PullOptionsImpl newPullOptionsFromJobConfig() {
PullOptionsImpl pullOptions = new PullOptionsImpl();
pullOptions.setInteractiveMode(false);
pullOptions.setUsername(username);
pullOptions.setKey(apiKey);
// if localeId is given, only handle this locale
if (!Strings.isNullOrEmpty(localeId)) {
pullOptions.setLocales(localeId);
pushOptions.setLocales(localeId);
}
// if project id is given, only handle this project
// String projectId = jobDetail.getProject();
// if (!Strings.isNullOrEmpty(projectId)) {
// pullOptions.setProj(projectId);
// pushOptions.setProj(projectId);
// }
overrideURLIfSpecified(getPushOptions(), zanataUrl);
overrideURLIfSpecified(getPullOptions(), zanataUrl);
overrideURLIfSpecified(pullOptions, zanataUrl);
// TODO https://zanata.atlassian.net/browse/ZNTA-1427 is fixed, we should set cacheDir to workspace root
pullOptions.setUseCache(false);
// this.pullOptions.setLogHttp(true);
return pullOptions;
}

private static Set<String> getProjectConfigs(String projectConfigs) {
Expand All @@ -110,43 +134,43 @@ private static Set<String> getProjectConfigs(String projectConfigs) {
.split(projectConfigs));
}

@Override
public PullOptions getPullOptions() {
return pullOptions;
}

@Override
public PushOptions getPushOptions() {
return pushOptions;
}

@Override
public void pushToZanata(Path repoBase) throws ZanataSyncException {
String project = getPushOptions().getProj();
if (projectConfigs.isEmpty()) {
Set<File> projectConfigs = findProjectConfigsOrThrow(repoBase);
for (File config : projectConfigs) {
PushOptionsImpl opts = newPushOptionsFromJobConfig();
String project = opts.getProj();

PushPullOptionsUtil
.applyProjectConfig(getPushOptions(), config);
log.info("{} - {}", getPushOptions());
pushIfProjectIdMatchesConfig(project, config);
.applyProjectConfig(opts, config);
pushIfProjectIdMatchesConfig(opts, project, config);
}
} else {
for (String projectConfig : projectConfigs) {
Path absPath = Paths.get(repoBase.toString(), projectConfig);
PushPullOptionsUtil.applyProjectConfig(getPushOptions(), absPath.toFile());
pushIfProjectIdMatchesConfig(project, absPath.toFile());
if (Files.exists(absPath)) {
PushOptionsImpl opts = newPushOptionsFromJobConfig();
String project = opts.getProj();

PushPullOptionsUtil.applyProjectConfig(
opts, absPath.toFile());
pushIfProjectIdMatchesConfig(opts, project, absPath.toFile());
} else {
log.warn("{} does not exist! Ignored!", projectConfig);
}
}
}
}

private void pushIfProjectIdMatchesConfig(String project, File config) {
if (Strings.isNullOrEmpty(project) || Objects.equals(getPushOptions().getProj(), project)) {
pushService.pushToZanata(getPushOptions());
private void pushIfProjectIdMatchesConfig(PushOptions opts, String project,
File config) {
if (Strings.isNullOrEmpty(project) || Objects.equals(opts.getProj(), project)) {
pushService.pushToZanata(opts);
} else if (!Strings.isNullOrEmpty(project)) {
log.warn(
"project id is provided as {}. Skip {} which has project set to {}",
config, getPushOptions().getProj());
config, opts.getProj());
}
}

Expand Down Expand Up @@ -176,31 +200,42 @@ private Set<File> findProjectConfigsOrThrow(Path repoBase) {

@Override
public void pullFromZanata(Path repoBase) throws ZanataSyncException {
String project = getPullOptions().getProj();
if (projectConfigs.isEmpty()) {
Set<File> projectConfigs =
findProjectConfigsOrThrow(repoBase);
for (File config : projectConfigs) {
PullOptionsImpl opts = newPullOptionsFromJobConfig();
String project = opts.getProj();

PushPullOptionsUtil
.applyProjectConfig(getPullOptions(), config);
pullIfProjectIdMatchesConfig(project, config);
.applyProjectConfig(opts, config);
pullIfProjectIdMatchesConfig(opts, project, config);
}
} else {
for (String projectConfig : projectConfigs) {
Path absPath = Paths.get(repoBase.toString(), projectConfig);
PushPullOptionsUtil.applyProjectConfig(getPullOptions(), absPath.toFile());
pullIfProjectIdMatchesConfig(project, absPath.toFile());
if (Files.exists(absPath)) {
PullOptionsImpl opts = newPullOptionsFromJobConfig();
String project = opts.getProj();

opts = PushPullOptionsUtil.applyProjectConfig(opts,
absPath.toFile());
pullIfProjectIdMatchesConfig(opts, project, absPath.toFile());
} else {
log.warn("{} does not exist! Ignored!", projectConfig);
}
}
}
}

private void pullIfProjectIdMatchesConfig(String project, File config) {
if (Strings.isNullOrEmpty(project) || Objects.equals(getPushOptions().getProj(), project)) {
pullService.pullFromZanata(getPullOptions());
private void pullIfProjectIdMatchesConfig(PullOptions opts,
String project, File config) {
if (Strings.isNullOrEmpty(project) || Objects.equals(opts.getProj(), project)) {
pullService.pullFromZanata(opts);
} else if (!Strings.isNullOrEmpty(project)) {
log.warn(
"project id is provided as {}. Skip {} which has project set to {}",
config, getPushOptions().getProj());
config, opts.getProj());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ public final class PushPullOptionsUtil {
public static <O extends PushPullOptions> O applyProjectConfig(O options,
File projectConfig) {
options.setProjectConfig(projectConfig);
// unset previous values so that we can reload them from project config
options.setSrcDir(null);
options.setTransDir(null);
options.setProj(null);
options.setProjectVersion(null);
options.setProjectType(null);

try {
// here we must take it step by step due to an issue http://stackoverflow.com/questions/41253028/how-to-make-jenkins-plugin-aware-of-spi
Expand All @@ -169,6 +163,7 @@ public static <O extends PushPullOptions> O applyProjectConfig(O options,
options.setTransDir(
new File(baseDir, options.getTransDir() != null ?
options.getTransDir().getPath() : "."));
// TODO maybe we should allow this as long as user can configure their jenkins box to have the commands available?
// disable commandhook
if (!options.getCommandHooks().isEmpty()) {
throw new ZanataSyncException(
Expand Down
Loading

0 comments on commit 7eb2f76

Please sign in to comment.