Navigation Menu

Skip to content

Commit

Permalink
JENKINS-33831
Browse files Browse the repository at this point in the history
  • Loading branch information
klimas7 committed Mar 25, 2016
1 parent aee8c75 commit 129d4af
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 65 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Expand Up @@ -81,6 +81,16 @@
<version>1.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>

<scm>
Expand Down
Expand Up @@ -56,17 +56,14 @@ public class GitParameterDefinition extends ParameterDefinition implements Compa
public static final String PARAMETER_TYPE_REVISION = "PT_REVISION";
public static final String PARAMETER_TYPE_BRANCH = "PT_BRANCH";
public static final String PARAMETER_TYPE_TAG_BRANCH = "PT_BRANCH_TAG";

private final UUID uuid;
private static final Logger LOGGER = Logger.getLogger(GitParameterDefinition.class.getName());

private final UUID uuid;
private String type;
private String branch;
private String tagFilter;
private String branchfilter;

private SortMode sortMode;

private String errorMessage;
private String defaultValue;
private Boolean quickFilterEnabled;
Expand Down Expand Up @@ -168,7 +165,7 @@ public String getTagFilter() {
}

public void setTagFilter(String tagFilter) {
if (isNullOrWhitespace(tagFilter)) {
if (StringUtils.isEmpty(StringUtils.trim(tagFilter))) {
tagFilter = "*";
}
this.tagFilter = tagFilter;
Expand All @@ -187,10 +184,10 @@ public String getBranchfilter() {
}

public void setBranchfilter(String branchFilter) {
if (isNullOrWhitespace(branchFilter)) {
if (StringUtils.isEmpty(StringUtils.trim(branchFilter))) {
branchFilter = "*";
}
// Accept "*" as a wilcard

if (!"*".equals(branchFilter)) {
try {
Pattern.compile(branchFilter);
Expand Down Expand Up @@ -239,34 +236,6 @@ public int compareTo(GitParameterDefinition pd) {
return -1;
}

public String prettyRevisionInfo(GitClient newgit, Revision r) {
List<String> test3 = null;
try {
test3 = newgit.showRevision(r.getSha1());
} catch (GitException e1) {
LOGGER.log(Level.SEVERE, "Unexpected error ", e1);
return "";
} catch (InterruptedException e1) {
LOGGER.log(Level.SEVERE, "Unexpected error ", e1);
return "";
}
String[] authorDate = test3.get(3).split(">");
String author = authorDate[0].replaceFirst("author ", "").replaceFirst("committer ", "") + ">";
String goodDate = null;
try {
String totmp = authorDate[1].trim().split("\\+")[0].trim();
long timestamp = Long.parseLong(totmp, 10) * 1000;
Date date = new Date();
date.setTime(timestamp);

goodDate = new SimpleDateFormat("yyyy:MM:dd HH:mm").format(date);

} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Unexpected error ", e);
}
return r.getSha1String().substring(0, 8) + " " + goodDate + " " + author;
}

public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSCM git) throws IOException, InterruptedException {

Map<String, String> paramList = new LinkedHashMap<String, String>();
Expand Down Expand Up @@ -319,17 +288,11 @@ public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSC

LOGGER.finest("Took " + (time + System.currentTimeMillis()) + "ms to fetch");
if (type.equalsIgnoreCase(PARAMETER_TYPE_REVISION)) {
List<ObjectId> oid;

if (this.branch != null && !this.branch.isEmpty()) {
oid = newgit.revList(this.branch);
} else {
oid = newgit.revListAll();
}
RevisionInfoFactory revisionInfoFactory = new RevisionInfoFactory(newgit,branch);
List<RevisionInfo> revisions = revisionInfoFactory.getRevisions();

for (ObjectId noid : oid) {
Revision r = new Revision(noid);
paramList.put(r.getSha1String(), prettyRevisionInfo(newgit, r));
for (RevisionInfo revision : revisions) {
paramList.put(revision.getSha1(), revision.getRevisionInfo());
}
}
if (type.equalsIgnoreCase(PARAMETER_TYPE_TAG) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) {
Expand Down Expand Up @@ -388,10 +351,10 @@ public ArrayList<String> sortByName(Set<String> set) {

ArrayList<String> tags = new ArrayList<String>(set);

if (!this.getSortMode().getIsUsingSmartSort()) {
Collections.sort(tags);
} else {
if (sortMode.getIsUsingSmartSort()) {
Collections.sort(tags, new SmartNumberStringComparer());
} else {
Collections.sort(tags);
}

return tags;
Expand All @@ -401,21 +364,6 @@ public String getErrorMessage() {
return errorMessage;
}

private static boolean isNullOrWhitespace(String s) {
return s == null || isWhitespace(s);

}

private static boolean isWhitespace(String s) {
int length = s.length();
for (int i = 0; i < length; i++) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}

public String getDivUUID() {
StringBuilder randomSelectName = new StringBuilder();
randomSelectName.append(getName()).append("-").append(uuid);
Expand Down
Expand Up @@ -13,6 +13,4 @@ public class GitParameterValue extends StringParameterValue {
public GitParameterValue(String name, String value) {
super(name, value);
}


}
@@ -0,0 +1,19 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter;

public class RevisionInfo {
private String sha1;
private String revisionInfo;

public RevisionInfo(String sha1, String revisionInfo){
this.sha1 = sha1;
this.revisionInfo = revisionInfo;
}

public String getSha1() {
return sha1;
}

public String getRevisionInfo() {
return revisionInfo;
}
}
@@ -0,0 +1,81 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter;

import hudson.plugins.git.GitException;
import hudson.plugins.git.Revision;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RevisionInfoFactory {

private static final Logger LOGGER = Logger.getLogger(RevisionInfoFactory.class.getName());
public static final Pattern AUTHOR_LINE_PATTERN = Pattern.compile("author (.* <.*@.*>) (\\d{10}) ([\\+-]\\d{4})");

private GitClient gitClient;
private String branch;

public RevisionInfoFactory(GitClient gitClient, String branch) {
this.gitClient = gitClient;
this.branch = branch;
}

public List<RevisionInfo> getRevisions() throws InterruptedException {
List<ObjectId> objectIds;

if (StringUtils.isEmpty(branch)) {
objectIds = gitClient.revListAll();
} else {
objectIds = gitClient.revList(branch);
}

ArrayList<RevisionInfo> revisionInfoList = new ArrayList<RevisionInfo>(objectIds.size());
for (ObjectId objectId : objectIds) {
Revision revision = new Revision(objectId);
revisionInfoList.add(new RevisionInfo(revision.getSha1String(), prettyRevisionInfo(revision)));
}

return revisionInfoList;
}

private String prettyRevisionInfo(Revision revision) {
List<String> raw = null;
try {
raw = gitClient.showRevision(revision.getSha1());
} catch (GitException e1) {
LOGGER.log(Level.SEVERE, "Unexpected error ", e1);
return "";
} catch (InterruptedException e1) {
LOGGER.log(Level.SEVERE, "Unexpected error ", e1);
return "";
}

String authorLine = getAuthorLine(raw);
Matcher matcher = AUTHOR_LINE_PATTERN.matcher(authorLine);
if (matcher.find()) {
String author = matcher.group(1);
String timestamp = matcher.group(2);
DateTime date = new DateTime(Long.parseLong(timestamp) * 1000); //Convert UNIX timestamp to date
return revision.getSha1String().substring(0, 8) + " " + date.toString("yyyy:MM:dd HH:mm") + " " + author;
} else {
LOGGER.log(Level.WARNING, "Not find author pattern " + authorLine);
return "";
}
}

private String getAuthorLine(List<String> rows) {
for (String row : rows) {
if (StringUtils.isNotEmpty(row) && row.toLowerCase().startsWith("author")) {
return row;
}
}
return "";
}
}

0 comments on commit 129d4af

Please sign in to comment.