Skip to content

Commit

Permalink
added support for republish failed commits. Reuse CLI code in both
Browse files Browse the repository at this point in the history
freestyle and pipeline build.
  • Loading branch information
zhouqr2000 committed Jul 24, 2019
1 parent c3f84d3 commit 466bef8
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 255 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@
<version>19.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jenkins-ci.plugins/matrix-project -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
<version>1.4</version>
</dependency>


</dependencies>

</project>
150 changes: 57 additions & 93 deletions src/main/java/com/compuware/ispw/git/CliExecutor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.compuware.ispw.git;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
Expand All @@ -15,7 +14,6 @@
import com.compuware.jenkins.common.utils.ArgumentUtils;
import com.compuware.jenkins.common.utils.CommonConstants;
import com.squareup.tape2.ObjectQueue;
import com.squareup.tape2.QueueFile;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.FilePath;
Expand All @@ -25,6 +23,8 @@
import hudson.util.ArgumentListBuilder;

/**
*
* A reusable CLI executor for both freestyle and pipeline Jenkins build
*
* @author Sam Zhou
*
Expand All @@ -34,107 +34,71 @@ public class CliExecutor
private PrintStream logger;
private Run<?, ?> run;
private TaskListener listener;
private EnvVars env;
private EnvVars envVars;
private Launcher launcher;

private String targetFolder;
private String topazCliWorkspace;

private CpwrGlobalConfiguration globalConfig;
private String connectionId;
private String credentialsId;

private String host;
private String port;
private String protocol;
private String codePage;
private String timeout;

private String userId;
private String password;

private String gitRepoUrl;
private StandardUsernamePasswordCredentials gitCredentials;
private String gitUserId;
private String gitPassword;
private String targetFolder;
private String topazCliWorkspace;

private String cliScriptFileRemote;

private FilePath workDir;

private String ref;
private String refId;
private String hash;

private String stream;
private String app;
private String ispwLevel;
private ObjectQueue<GitInfo> objectQueue;

private String runtimeConfig;

public CliExecutor(PrintStream logger, Run<?, ?> run, TaskListener listener, Launcher launcher, EnvVars env,
String targetFolder, String topazCliWorkspace, CpwrGlobalConfiguration globalConfig, String connectionId,
String credentialsId, String gitRepoUrl, String gitCredentialsId, String cliScriptFileRemote, FilePath workDir,
String ref, String refId, String hash, String stream, String app, String ispwLevel, String runtimeConfig)
public CliExecutor(PrintStream logger, Run<?, ?> run, TaskListener listener, Launcher launcher, EnvVars envVars,
String targetFolder, String topazCliWorkspace, CpwrGlobalConfiguration globalConfig, String cliScriptFileRemote,
FilePath workDir, ObjectQueue<GitInfo> objectQueue)
{
this.logger = logger;
this.run = run;
this.listener = listener;
this.env = env;
this.envVars = envVars;
this.launcher = launcher;

this.globalConfig = globalConfig;
this.connectionId = connectionId;
this.credentialsId = credentialsId;

this.targetFolder = targetFolder;
this.topazCliWorkspace = topazCliWorkspace;

this.cliScriptFileRemote = cliScriptFileRemote;

this.workDir = workDir;

this.objectQueue = objectQueue;

}

public boolean execute(boolean bitbucketNotify, String connectionId, String credentialsId, String runtimeConfig,
String stream, String app, String ispwLevel, String gitRepoUrl, String gitCredentialsId, String ref, String refId,
String hash) throws InterruptedException, IOException
{
HostConnection connection = globalConfig.getHostConnection(connectionId);
this.host = ArgumentUtils.escapeForScript(connection.getHost());
this.port = ArgumentUtils.escapeForScript(connection.getPort());
this.protocol = connection.getProtocol();
this.codePage = connection.getCodePage();
this.timeout = ArgumentUtils.escapeForScript(connection.getTimeout());
String host = ArgumentUtils.escapeForScript(connection.getHost());
String port = ArgumentUtils.escapeForScript(connection.getPort());
String protocol = connection.getProtocol();
String codePage = connection.getCodePage();
String timeout = ArgumentUtils.escapeForScript(connection.getTimeout());

StandardUsernamePasswordCredentials credentials = globalConfig.getLoginInformation(run.getParent(), credentialsId);
this.userId = ArgumentUtils.escapeForScript(credentials.getUsername());
this.password = ArgumentUtils.escapeForScript(credentials.getPassword().getPlainText());

String userId = ArgumentUtils.escapeForScript(credentials.getUsername());
String password = ArgumentUtils.escapeForScript(credentials.getPassword().getPlainText());
if (RestApiUtils.isIspwDebugMode())
{
logger.println("host=" + host + ", port=" + port + ", protocol=" + protocol + ", codePage=" + codePage
+ ", timeout=" + timeout + ", userId=" + userId + ", password=" + password);
}

this.gitRepoUrl = gitRepoUrl;
this.gitCredentials = globalConfig.getLoginInformation(run.getParent(), gitCredentialsId);
this.gitUserId = ArgumentUtils.escapeForScript(gitCredentials.getUsername());
this.gitPassword = ArgumentUtils.escapeForScript(gitCredentials.getPassword().getPlainText());

StandardUsernamePasswordCredentials gitCredentials = globalConfig.getLoginInformation(run.getParent(),
gitCredentialsId);
String gitUserId = ArgumentUtils.escapeForScript(gitCredentials.getUsername());
String gitPassword = ArgumentUtils.escapeForScript(gitCredentials.getPassword().getPlainText());
if (RestApiUtils.isIspwDebugMode())
{
logger.println("gitRepoUrl=" + gitRepoUrl + ", gitUserId=" + gitUserId + ", gitPassword=" + gitPassword);
}

this.cliScriptFileRemote = cliScriptFileRemote;

this.workDir = workDir;

this.ref = ref;
this.refId = refId;
this.hash = hash;

this.stream = stream;
this.app = app;
this.ispwLevel = ispwLevel;

this.runtimeConfig = runtimeConfig;
}

public boolean execute() throws InterruptedException, IOException
{

ArgumentListBuilder args = new ArgumentListBuilder();
// build the list of arguments to pass to the CLI

Expand Down Expand Up @@ -182,7 +146,7 @@ public boolean execute() throws InterruptedException, IOException
logger.println("Shell script: " + args.toString());

// invoke the CLI (execute the batch/shell script)
int exitValue = launcher.launch().cmds(args).envs(env).stdout(logger).pwd(workDir).join();
int exitValue = launcher.launch().cmds(args).envs(envVars).stdout(logger).pwd(workDir).join();

BitbucketNotifier notifier = new BitbucketNotifier(logger, run, listener);
URL url = new URL(gitRepoUrl);
Expand All @@ -198,25 +162,22 @@ public boolean execute() throws InterruptedException, IOException

if (exitValue != 0)
{
try
if (bitbucketNotify)
{
logger.println("Notify bitbucket success at: " + baseUrl);
notifier.notifyStash(baseUrl, gitCredentials, hash, StashBuildState.FAILED, null);
try
{
logger.println("Notify bitbucket success at: " + baseUrl);
notifier.notifyStash(baseUrl, gitCredentials, hash, StashBuildState.FAILED, null);
}
catch (Exception e)
{
e.printStackTrace(logger);
}
}
catch (Exception e)
{
e.printStackTrace(logger);
}

File file = new File(run.getRootDir(), "../" + GitToIspwPublish.FILE_QUEUE);
logger.println("queue file path = " + file.toString());

QueueFile queueFile = new QueueFile.Builder(file).build();
GitInfoConverter converter = new GitInfoConverter();
ObjectQueue<GitInfo> objectQueue = ObjectQueue.create(queueFile, converter);

GitInfo newGitInfo = new GitInfo(ref, refId, hash);
if(!objectQueue.asList().contains(newGitInfo)) {
if (objectQueue != null && !objectQueue.asList().contains(newGitInfo))
{
objectQueue.add(newGitInfo);
}

Expand All @@ -229,15 +190,18 @@ public boolean execute() throws InterruptedException, IOException
{
logger.println("Call " + osFile + " exited with value = " + exitValue); //$NON-NLS-1$ //$NON-NLS-2$

try
{
logger.println("Notify bitbucket success at: " + baseUrl);

notifier.notifyStash(baseUrl, gitCredentials, hash, StashBuildState.SUCCESSFUL, null);
}
catch (Exception e)
if (bitbucketNotify)
{
e.printStackTrace(logger);
try
{
logger.println("Notify bitbucket success at: " + baseUrl);

notifier.notifyStash(baseUrl, gitCredentials, hash, StashBuildState.SUCCESSFUL, null);
}
catch (Exception e)
{
e.printStackTrace(logger);
}
}

return true;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/compuware/ispw/git/GitToIspwConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public class GitToIspwConstants
{
public static String FILE_QUEUE = "file_queue.txt";

public static String VAR_REF_ID = "refId"; //$NON-NLS-1$
public static String VAR_REF = "ref"; //$NON-NLS-1$
public static String VAR_HASH = "hash"; //$NON-NLS-1$
Expand Down
46 changes: 23 additions & 23 deletions src/main/java/com/compuware/ispw/git/GitToIspwPublish.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ public class GitToIspwPublish extends Builder

// Branch mapping
private String branchMapping = DescriptorImpl.branchMapping;

public static String FILE_QUEUE = "file_queue.txt";


@DataBoundConstructor
public GitToIspwPublish()
{
Expand All @@ -75,40 +73,43 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
PrintStream logger = listener.getLogger();

EnvVars envVars = build.getEnvironment(listener);

String hash = envVars.get(GitToIspwConstants.VAR_HASH, GitToIspwConstants.VAR_HASH);
String ref = envVars.get(GitToIspwConstants.VAR_REF, GitToIspwConstants.VAR_REF);
String refId = envVars.get(GitToIspwConstants.VAR_REF_ID, GitToIspwConstants.VAR_REF_ID);

File file = new File(build.getRootDir(), "../"+FILE_QUEUE);
logger.println("queue file path = "+file.toString());
File file = new File(build.getRootDir(), "../" + GitToIspwConstants.FILE_QUEUE);
logger.println("commits queue file path = " + file.toString());

QueueFile queueFile = new QueueFile.Builder(file).build();
GitInfoConverter converter = new GitInfoConverter();
ObjectQueue<GitInfo> objectQueue = ObjectQueue.create(queueFile, converter);

boolean newCommit = true;
List<GitInfo> gitInfos = new ArrayList<GitInfo>();
if (hash.equals(GitToIspwConstants.VAR_HASH) || ref.equals(GitToIspwConstants.VAR_REF)
|| refId.equals(GitToIspwConstants.VAR_REF_ID))
{
logger.println("hash, ref, refId must be presented in order for the build to work, reading from file queue if any ...");

logger.println(
"hash, ref, refId must be presented in order for the build to work, reading from file queue if any ...");

GitInfo gitInfo = objectQueue.peek();
if (gitInfo != null)
{
newCommit = false;
gitInfos = objectQueue.asList();
logger.println("Republish old failed commits...");
} else {
logger.println("file queue is empty, do nothing");
logger.println("Re-push failed commits to ISPW...");
}
else
{
logger.println("No failed commits, do nothing...");
return true;
}
}
else
{
logger.println("New commit - hash=" + hash + ", ref=" + ref + ", refId=" + refId);

newCommit = true;
gitInfos.add(new GitInfo(ref, refId, hash));
}
Expand All @@ -134,8 +135,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
String topazCliWorkspace = build.getWorkspace().getRemote() + remoteFileSeparator + CommonConstants.TOPAZ_CLI_WORKSPACE;
logger.println("TopazCliWorkspace: " + topazCliWorkspace); //$NON-NLS-1$
logger.println("targetFolder: " + targetFolder);

EnvVars env = build.getEnvironment(listener);

FilePath workDir = new FilePath(vChannel, build.getWorkspace().getRemote());

for (GitInfo gitInfo : gitInfos)
Expand Down Expand Up @@ -174,11 +174,11 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
logger.println("debugMsg=" + debugMsg);
}

CliExecutor cliExecutor = new CliExecutor(logger, build, listener, launcher, env, targetFolder, topazCliWorkspace,
globalConfig, connectionId, credentialsId, gitRepoUrl, gitCredentialsId, cliScriptFileRemote, workDir, ref,
refId, hash, stream, app, ispwLevel, runtimeConfig);
CliExecutor cliExecutor = new CliExecutor(logger, build, listener, launcher, envVars, targetFolder,
topazCliWorkspace, globalConfig, cliScriptFileRemote, workDir, objectQueue);
boolean success = cliExecutor.execute(true, connectionId, credentialsId, runtimeConfig, stream, app, ispwLevel,
gitRepoUrl, gitCredentialsId, ref, refId, hash);

boolean success = cliExecutor.execute();
if (success)
{
if (!newCommit)
Expand All @@ -191,7 +191,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
return false;
}
}

return true;
}

Expand All @@ -212,8 +212,8 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Builder>

// Branch mapping
public static final String branchMapping = "#The following comments show how to use the 'Branch Mapping' field.\n"
+ "#Click on the help button to the right of the screen for more details on how to populate this field\n" + "#\n"
+ "#*/dev1/ => DEV1, per-commit\n" + "#*/dev2/ => DEV2, per-branch\n"
+ "#Click on the help button to the right of the screen for more details on how to populate this field\n"
+ "#\n" + "#*/dev1/ => DEV1, per-commit\n" + "#*/dev2/ => DEV2, per-branch\n"
+ "#*/dev3/ => DEV3, custom, a description\n";
public static final String containerDesc = StringUtils.EMPTY;
public static final String containerPref = StringUtils.EMPTY;
Expand Down

0 comments on commit 466bef8

Please sign in to comment.