Skip to content

Commit

Permalink
CWE-149539 - ISPW/GIT Set commit Build status on CLI Failure
Browse files Browse the repository at this point in the history
CWE-149018 - ISPW/Git - Jenkins Build Process for commits - address
failures
  • Loading branch information
zhouqr2000 authored and katieeluskie committed Jul 24, 2019
1 parent 016e8d4 commit b0192de
Show file tree
Hide file tree
Showing 20 changed files with 3,124 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ release.properties
/.settings/
/work/
/work-old/
/queue.txt
/queue1.txt
/queue2.txt
29 changes: 28 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -235,6 +235,33 @@
<version>1.0.9-SNAPSHOT</version>
</dependency>

<!-- dependency from Stash Notifier -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>display-url-api</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.0</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>2.0</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>

</dependencies>

</project>
88 changes: 88 additions & 0 deletions src/main/java/com/compuware/ispw/git/GitInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.compuware.ispw.git;

import java.util.StringTokenizer;

public class GitInfo
{
private String ref;
private String refId;
private String hash;

public GitInfo(String ref, String refId, String hash)
{
this.ref = ref;
this.refId = refId;
this.hash = hash;
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return ref + "|" + refId + "|" + hash;
}

public static GitInfo parse(String s)
{
StringTokenizer tokenizer = new StringTokenizer(s, "|");
String ref = tokenizer.nextToken();
String refId = tokenizer.nextToken();
String hash = tokenizer.nextToken();

return new GitInfo(ref, refId, hash);
}

/**
* @return the ref
*/
public String getRef()
{
return ref;
}

/**
* @param ref
* the ref to set
*/
public void setRef(String ref)
{
this.ref = ref;
}

/**
* @return the refId
*/
public String getRefId()
{
return refId;
}

/**
* @param refId
* the refId to set
*/
public void setRefId(String refId)
{
this.refId = refId;
}

/**
* @return the hash
*/
public String getHash()
{
return hash;
}

/**
* @param hash
* the hash to set
*/
public void setHash(String hash)
{
this.hash = hash;
}

}
23 changes: 23 additions & 0 deletions src/main/java/com/compuware/ispw/git/GitInfoConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.compuware.ispw.git;

import java.io.IOException;
import java.io.OutputStream;
import com.squareup.tape2.ObjectQueue.Converter;

public class GitInfoConverter implements Converter<GitInfo>
{
public static String UTF_8 = "UTF-8";

@Override
public GitInfo from(byte[] source) throws IOException
{
return GitInfo.parse(new String(source, UTF_8));
}

@Override
public void toStream(GitInfo value, OutputStream sink) throws IOException
{
sink.write(value.toString().getBytes(UTF_8));
}

}
34 changes: 33 additions & 1 deletion src/main/java/com/compuware/ispw/git/GitToIspwPublish.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.http.HttpEntity;
import org.jenkinsci.plugins.stashNotifier.BitbucketNotifier;
import org.jenkinsci.plugins.stashNotifier.StashBuildState;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
Expand Down Expand Up @@ -213,16 +217,44 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

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

BitbucketNotifier notifier = new BitbucketNotifier(logger, build, listener);
URL url = new URL(gitRepoUrl);
String baseUrl = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort();
if(gitRepoUrl.contains("/bitbucket/")) { // handle test environment
baseUrl += "/bitbucket";
}

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

throw new AbortException("Call " + osFile + " exited with value = " + exitValue); //$NON-NLS-1$ //$NON-NLS-2$
}
else
{
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)
{
e.printStackTrace(logger);
}

return true;
}

}

@Extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.compuware.jenkins.common.configuration.HostConnection;
import com.google.common.base.Strings;
import com.google.common.collect.Range;
import com.google.common.collect.Ranges;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
Expand Down Expand Up @@ -625,7 +624,7 @@ public static List<Range<Integer>> parseToRange(String value) {
}

checkArgument(from <= to, "Interval %s should be FROM less than TO", code);
validRanges.add(Ranges.closed(from, to));
validRanges.add(Range.closed(from, to));
}

return validRanges;
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/com/squareup/tape2/FileObjectQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2012 Square, Inc.
package com.squareup.tape2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

final class FileObjectQueue<T> extends ObjectQueue<T> {
/** Backing storage implementation. */
private final QueueFile queueFile;
/** Reusable byte output buffer. */
private final DirectByteArrayOutputStream bytes = new DirectByteArrayOutputStream();
@Private final Converter<T> converter;

FileObjectQueue(QueueFile queueFile, Converter<T> converter) {
this.queueFile = queueFile;
this.converter = converter;
}

@Override public @Nonnull QueueFile file() {
return queueFile;
}

@Override public int size() {
return queueFile.size();
}

@Override public boolean isEmpty() {
return queueFile.isEmpty();
}

@Override public void add(T entry) throws IOException {
bytes.reset();
converter.toStream(entry, bytes);
queueFile.add(bytes.getArray(), 0, bytes.size());
}

@Override public @Nullable T peek() throws IOException {
byte[] bytes = queueFile.peek();
if (bytes == null) return null;
return converter.from(bytes);
}

@Override public void remove() throws IOException {
queueFile.remove();
}

@Override public void remove(int n) throws IOException {
queueFile.remove(n);
}

@Override public void clear() throws IOException {
queueFile.clear();
}

@Override public void close() throws IOException {
queueFile.close();
}

/**
* Returns an iterator over entries in this queue.
*
* <p>The iterator disallows modifications to the queue during iteration. Removing entries from
* the head of the queue is permitted during iteration using {@link Iterator#remove()}.
*
* <p>The iterator may throw an unchecked {@link IOException} during {@link Iterator#next()}
* or {@link Iterator#remove()}.
*/
@Override public Iterator<T> iterator() {
return new QueueFileIterator(queueFile.iterator());
}

@Override public String toString() {
return "FileObjectQueue{"
+ "queueFile=" + queueFile
+ '}';
}

private final class QueueFileIterator implements Iterator<T> {
final Iterator<byte[]> iterator;

@Private QueueFileIterator(Iterator<byte[]> iterator) {
this.iterator = iterator;
}

@Override public boolean hasNext() {
return iterator.hasNext();
}

@Override public T next() {
byte[] data = iterator.next();
try {
return converter.from(data);
} catch (IOException e) {
throw QueueFile.<Error>getSneakyThrowable(e);
}
}

@Override public void remove() {
iterator.remove();
}
}

/** Enables direct access to the internal array. Avoids unnecessary copying. */
private static final class DirectByteArrayOutputStream extends ByteArrayOutputStream {
DirectByteArrayOutputStream() {
}

/**
* Gets a reference to the internal byte array. The {@link #size()} method indicates how many
* bytes contain actual data added since the last {@link #reset()} call.
*/
byte[] getArray() {
return buf;
}
}
}

0 comments on commit b0192de

Please sign in to comment.