/src/main/java/net/uaznia/lukanus/hudson/plugins/gitparameter/RevisionInfoFactory.java

https://github.com/jenkinsci/git-parameter-plugin · Java · 119 lines · 100 code · 19 blank · 0 comment · 14 complexity · 8cce9edd7661de8a3e1f72b158e2a3e7 MD5 · raw file

  1. package net.uaznia.lukanus.hudson.plugins.gitparameter;
  2. import static java.lang.Long.parseLong;
  3. import hudson.plugins.git.GitException;
  4. import hudson.plugins.git.Revision;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.eclipse.jgit.lib.ObjectId;
  7. import org.jenkinsci.plugins.gitclient.GitClient;
  8. import java.time.Instant;
  9. import java.time.LocalDateTime;
  10. import java.time.ZoneId;
  11. import java.time.format.DateTimeFormatter;
  12. import java.util.*;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17. public class RevisionInfoFactory {
  18. private static final Logger LOGGER = Logger.getLogger(RevisionInfoFactory.class.getName());
  19. private static final Pattern AUTHOR_LINE_PATTERN = Pattern.compile("author (.* <.*@.*>) (\\d{10}) ([+-]\\d{4})");
  20. private static final Pattern AUTHOR_LINE_PATTERN_GENERAL_DATE = Pattern.compile("author (.* <.*@.*>) (.*)");
  21. private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
  22. private static final String COMMIT_MESSAGE_PREFIX = " ";
  23. private static final int MAX_COMMIT_MESSAGE_LENGTH = 150;
  24. private final GitClient gitClient;
  25. private final String branch;
  26. public RevisionInfoFactory(GitClient gitClient, String branch) {
  27. this.gitClient = gitClient;
  28. this.branch = branch;
  29. }
  30. public List<RevisionInfo> getRevisions() throws InterruptedException {
  31. List<ObjectId> objectIds;
  32. if (StringUtils.isEmpty(branch)) {
  33. objectIds = gitClient.revListAll();
  34. } else {
  35. objectIds = gitClient.revList(branch);
  36. }
  37. ArrayList<RevisionInfo> revisionInfoList = new ArrayList<>(objectIds.size());
  38. for (ObjectId objectId : objectIds) {
  39. Revision revision = new Revision(objectId);
  40. revisionInfoList.add(new RevisionInfo(revision.getSha1String(), prettyRevisionInfo(revision)));
  41. }
  42. return revisionInfoList;
  43. }
  44. private String prettyRevisionInfo(Revision revision) {
  45. String shortSha1 = revision.getSha1String().substring(0, 8);
  46. List<String> raw;
  47. try {
  48. raw = gitClient.showRevision(revision.getSha1());
  49. } catch (GitException | InterruptedException e1) {
  50. LOGGER.log(Level.SEVERE, Messages.GitParameterDefinition_unexpectedError(), e1);
  51. return shortSha1;
  52. }
  53. String commitMessage = trimMessage(getCommitMessage(raw));
  54. String authorLine = getAuthorLine(raw);
  55. Matcher matcher = AUTHOR_LINE_PATTERN.matcher(authorLine);
  56. if (matcher.find()) {
  57. String author = matcher.group(1);
  58. String timestamp = matcher.group(2);
  59. String zone = matcher.group(3);
  60. LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(parseLong(timestamp) * 1000), ZoneId.of(zone));
  61. String stringDate = date.format(DATE_FORMAT);
  62. return StringUtils.join(new Object[]{shortSha1, stringDate, author, commitMessage}, " ").trim();
  63. }
  64. matcher = AUTHOR_LINE_PATTERN_GENERAL_DATE.matcher(authorLine);
  65. if (matcher.find()) {
  66. String author = matcher.group(1);
  67. String date = matcher.group(2);
  68. return StringUtils.join(new Object[]{shortSha1, date, author, commitMessage}, " ").trim();
  69. }
  70. LOGGER.log(Level.WARNING, Messages.GitParameterDefinition_notFindAuthorPattern(authorLine));
  71. return shortSha1;
  72. }
  73. private String getAuthorLine(List<String> rows) {
  74. for (String row : rows) {
  75. if (StringUtils.isNotEmpty(row) && row.toLowerCase().startsWith("author")) {
  76. return row;
  77. }
  78. }
  79. return "";
  80. }
  81. private String getCommitMessage(List<String> rows) {
  82. Set<String> commitMessages = new LinkedHashSet<>();
  83. for (String row : rows) {
  84. if (row.startsWith(COMMIT_MESSAGE_PREFIX) && row.trim().length() > 0) {
  85. commitMessages.add(row.trim());
  86. }
  87. }
  88. return StringUtils.join(commitMessages, " ");
  89. }
  90. private String trimMessage(String commitMessage) {
  91. if (commitMessage.length() > MAX_COMMIT_MESSAGE_LENGTH) {
  92. int lastSpace = commitMessage.lastIndexOf(" ", MAX_COMMIT_MESSAGE_LENGTH);
  93. if (lastSpace == -1) {
  94. lastSpace = MAX_COMMIT_MESSAGE_LENGTH;
  95. }
  96. return commitMessage.substring(0, lastSpace) + " ...";
  97. }
  98. return commitMessage;
  99. }
  100. }