Skip to content

Commit

Permalink
Pipeline compatible and other miscs
Browse files Browse the repository at this point in the history
  • Loading branch information
yshao committed Jan 19, 2017
1 parent 6e940b5 commit 44c05cd
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>zanata</artifactId>
<version>0.2</version>
<version>0.3</version>
<packaging>hpi</packaging>

<properties>
Expand Down
111 changes: 76 additions & 35 deletions src/main/java/org/jenkinsci/plugins/zanata/ZanataBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ of this software and associated documentation files (the "Software"), to deal
import hudson.util.FormValidation;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.Build;
import hudson.model.TaskListener;
import hudson.model.*;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
import jenkins.tasks.SimpleBuildStep;
Expand All @@ -47,6 +49,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.io.File;
import java.io.Writer;
import java.io.*;
import java.util.*;
import hudson.EnvVars;
import hudson.Launcher.*;
import hudson.Proc;
Expand All @@ -55,20 +58,16 @@ of this software and associated documentation files (the "Software"), to deal
public class ZanataBuilder extends Builder implements SimpleBuildStep {

private final String projFile;
private final String commandG2Z;
private final String commandZ2G;
private final boolean syncG2zanata;
private final boolean syncZ2git;


// Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
@DataBoundConstructor
public ZanataBuilder(String projFile, String commandG2Z, boolean syncG2zanata, String commandZ2G, boolean syncZ2git) {
public ZanataBuilder(String projFile, boolean syncG2zanata, boolean syncZ2git) {
this.projFile = projFile;
this.syncG2zanata = syncG2zanata;
this.syncZ2git = syncZ2git;
this.commandG2Z = commandG2Z;
this.commandZ2G = commandZ2G;
}

/**
Expand All @@ -79,14 +78,6 @@ public String getProjFile() {
return projFile;
}

public String getCommandG2Z() {
return commandG2Z;
}

public String getCommandZ2G() {
return commandZ2G;
}

public boolean getSyncG2zanata() {
return syncG2zanata;
}
Expand All @@ -98,25 +89,31 @@ public boolean getSyncZ2git() {
@Override
public void perform(Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener) {

listener.getLogger().println("Running Zanata Sync, project file: " + projFile);
String commandG2Z;
String commandZ2G;

listener.getLogger().println("Running Zanata Sync, project file: " + projFile);

if (syncG2zanata) {
commandG2Z = getDescriptor().getCommandG2Z();

listener.getLogger().println("Git to Zanata sync is enabled, running command:");
listener.getLogger().println(commandG2Z + "\n");

if (runShellCommandInBuild(commandG2Z, listener, build)){
if (runShellCommandInBuild(commandG2Z, listener, build, workspace)){
listener.getLogger().println("Git to Zanata sync finished.\n");
}

};


if (syncZ2git) {
commandZ2G = getDescriptor().getCommandZ2G();

listener.getLogger().println("Zanata to Git sync is enabled, running command:");
listener.getLogger().println(commandZ2G + "\n");

if (runShellCommandInBuild(commandZ2G, listener, build)){
if (runShellCommandInBuild(commandZ2G, listener, build, workspace)){
listener.getLogger().println("Zanata to Git sync finished.\n");
}
};
Expand All @@ -129,15 +126,23 @@ public void perform(Run<?,?> build, FilePath workspace, Launcher launcher, TaskL
*/
}

private boolean runShellCommandInBuild(String command, TaskListener listener, Run<?,?> builder){
private boolean runShellCommandInBuild(String command, TaskListener listener, Run<?,?> builder, FilePath workspace){

try {

EnvVars envs = builder.getEnvironment(listener);
EnvVars jenkinsEnvs = builder.getEnvironment(listener);
Map<String, String> sysEnvs = System.getenv();

Map<String, String> allEnvs = new HashMap<String, String> ();
allEnvs.putAll(sysEnvs);
allEnvs.putAll(jenkinsEnvs);

listener.getLogger().println("workspace: " + workspace.toURI());

Process pg = Runtime.getRuntime().exec(new String[]{"bash","-c",command},
envs.toString().split(", "),
new File(envs.get("WORKSPACE")));
allEnvs.toString().split(", "),
new File(workspace.toURI()));


try (BufferedReader in = new BufferedReader(
new InputStreamReader(pg.getInputStream(),"UTF8"));) {
Expand All @@ -152,6 +157,22 @@ private boolean runShellCommandInBuild(String command, TaskListener listener, Ru
e.printStackTrace();
return false;
}
try (BufferedReader in = new BufferedReader(
new InputStreamReader(pg.getErrorStream(),"UTF8"));) {
String line = null;
while ((line = in.readLine()) != null)
{ System.out.println(line);
listener.getLogger().println(line);
}
in.close();
} catch (IOException e) {
listener.getLogger().println("Can't generate error message of command:" + command);
e.printStackTrace();
return false;
}
pg.waitFor();
listener.getLogger().println("Run command return: " + Integer.toString(pg.exitValue()));

} catch (IOException e) {
listener.getLogger().println("Can't run command:" + command);
e.printStackTrace();
Expand All @@ -161,8 +182,8 @@ private boolean runShellCommandInBuild(String command, TaskListener listener, Ru
e.printStackTrace();
return false;
}
return true;

return true;
}

// Overridden for better type safety.
Expand Down Expand Up @@ -192,6 +213,8 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
*/
private String iniLocation;
private String iniContents;
private String commandG2Z;
private String commandZ2G;


/**
Expand All @@ -204,26 +227,23 @@ public DescriptorImpl() {


public FormValidation doCheckProjFile(@QueryParameter String value,
@QueryParameter String commandG2Z,
@QueryParameter boolean syncG2zanata,
@QueryParameter String commandZ2G,
@QueryParameter boolean syncZ2git)
throws IOException, ServletException {

if (value.length() == 0)
return FormValidation.error("Please set a project name such as zanata.xml");

System.out.println("Project File is : " + value);
System.out.println("Project G2Z is : " + commandG2Z);
System.out.println(syncG2zanata);
System.out.println("Project Z2G is : " + commandZ2G);
System.out.println(syncZ2git);

save ();
return FormValidation.ok();
}


@Override
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
// Indicates that this builder can be used with all kinds of project types
return true;
Expand All @@ -232,6 +252,7 @@ public boolean isApplicable(Class<? extends AbstractProject> aClass) {
/**
* This human readable name is used in the configuration screen.
*/
@Override
public String getDisplayName() {
return "Zanata Localization Sync";
}
Expand All @@ -246,8 +267,21 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc

iniLocation = formData.getString("iniLocation");
iniContents = formData.getString("iniContents");
commandG2Z = formData.getString("commandG2Z");
commandZ2G = formData.getString("commandZ2G");

String iniFullPath;

if (System.getProperty("JENKINS_HOME") != null) {
iniFullPath = System.getProperty("JENKINS_HOME") + "/" + iniLocation;
} else {
if (System.getProperty("user.home") != null) {
iniFullPath = System.getProperty("user.home") + "/" + iniLocation;
} else {
iniFullPath = "/var/lib/jenkins" + "/" + iniLocation;
}
}

String iniFullPath = System.getProperty("JENKINS_HOME") + "/" + iniLocation;
File file = new File(iniFullPath);

try {
Expand All @@ -266,16 +300,17 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
e.printStackTrace();
}

try (Writer fstream = new OutputStreamWriter(new FileOutputStream(iniFullPath), "UTF8");) {
fstream.write(iniContents);
fstream.flush();
fstream.close();
System.out.println("File written Succesfully: " + iniFullPath);
} catch (IOException e) {
System.out.println("File write NOT Succesfully: " + iniFullPath);
e.printStackTrace();
if (file.exists()) {
try (Writer fstream = new OutputStreamWriter(new FileOutputStream(iniFullPath), "UTF8");) {
fstream.write(iniContents);
fstream.flush();
fstream.close();
System.out.println("File written Succesfully: " + iniFullPath);
} catch (IOException e) {
System.out.println("File write NOT Succesfully: " + iniFullPath);
e.printStackTrace();
}
}

save();
return super.configure(req,formData);
}
Expand All @@ -292,6 +327,12 @@ public String getIniLocation() {
public String getIniContents() {
return iniContents;
}
public String getCommandG2Z() {
return commandG2Z;
}
public String getCommandZ2G() {
return commandZ2G;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@
<f:entry title="Project File" field="projFile">
<f:textbox default="zanata.xml"></f:textbox>
</f:entry>
<f:entry title="Command to sync from source Git repo to Zanata" field="commandG2Z">
<f:textarea default="find . -type f -not -path &quot;*/target/*&quot; -name 'zanata.xml' \&#10; -execdir pwd \; \&#10; -execdir ls \; \&#10; -execdir echo 'Running zanata-cli -B stats\n' \; \&#10; -execdir zanata-cli -B stats \; \&#10; -execdir echo 'Running zanata-cli -B push\n' \; \&#10; -execdir zanata-cli -B push \; \&#10; -execdir echo 'Running zanata-cli -B pull\n' \; \&#10; -execdir zanata-cli -B pull \;"></f:textarea>
</f:entry>
<f:entry title="Enable" field="syncG2zanata"
description="Check to sync from Git Repo to Zanata">
<f:checkbox/>
</f:entry>
<f:entry title="Command to sync from Zanata to source Git repo" field="commandZ2G">
<f:textarea default="find . -type f -not -path &quot;*/target/*&quot; -name 'zanata.xml' \&#10; -execdir echo &quot;=== Commiting new translation $BUILD_TAG...\n&quot; \; \&#10; -execdir pwd \; \&#10; -execdir ls \; \&#10; -execdir echo '=== Git add...\n' \; \&#10; -execdir git add . \; \&#10; -execdir echo '=== Git commit ...\n' \; \&#10; -execdir git commit -m &quot;$BUILD_TAG&quot; \; \&#10; -execdir echo &quot;=== Finished commit preparation - $BUILD_TAG.\n&quot; \;"></f:textarea>
</f:entry>
<f:entry title="Enable" field="syncZ2git"
description="Check to prepare commit of translation from Zanata to Git, you need to push in After Build.">
<f:checkbox/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@
description="The contents of zanata.ini">
<f:textarea></f:textarea>
</f:entry>
<f:entry title="Command to sync from source Git repo to Zanata" field="commandG2Z">
<f:textarea default="find . -type f -not -path &quot;*/target/*&quot; -name 'zanata.xml' \&#10; -execdir pwd \; \&#10; -execdir ls \; \&#10; -execdir echo 'Running zanata-cli -B stats\n' \; \&#10; -execdir zanata-cli -B stats \; \&#10; -execdir echo 'Running zanata-cli -B push\n' \; \&#10; -execdir zanata-cli -B push \; \&#10; -execdir echo 'Running zanata-cli -B pull\n' \; \&#10; -execdir zanata-cli -B pull \;"></f:textarea>
</f:entry>
<f:entry title="Command to sync from Zanata to source Git repo" field="commandZ2G">
<f:textarea default="find . -type f -not -path &quot;*/target/*&quot; -name 'zanata.xml' \&#10; -execdir echo &quot;=== Commiting new translation $BUILD_TAG...\n&quot; \; \&#10; -execdir pwd \; \&#10; -execdir ls \; \&#10; -execdir echo '=== Git add...\n' \; \&#10; -execdir git add . \; \&#10; -execdir echo '=== Git commit ...\n' \; \&#10; -execdir git commit -m &quot;$BUILD_TAG&quot; \; \&#10; -execdir echo &quot;=== Finished commit preparation - $BUILD_TAG.\n&quot; \;"></f:textarea>
</f:entry>
</f:section>
</j:jelly>

0 comments on commit 44c05cd

Please sign in to comment.