Last active
September 18, 2019 15:01
-
-
Save computerlove/957e11d801281c34a516106e0439eb76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package no.marvin; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class Main { | |
private static final Pattern jiraPattern = Pattern.compile(".*nvdb(\\s|-)(\\d+).*", Pattern.CASE_INSENSITIVE); | |
enum Project { | |
API_V2("/home/marlil/src/vegvesenet/nvdb-rest-api", "V2", "NVDB-Lese-API"), | |
API_V3("/home/marlil/src/vegvesenet/nvdb-rest-api", "master", "NVDB-Lese-API"), | |
READER_V2("/home/marlil/src/vegvesenet/nvdb-reader", "V2", "NVDB-Reader"), | |
READER_V3("/home/marlil/src/vegvesenet/nvdb-reader", "master", "NVDB-Reader"), | |
VEGKART_V3("/home/marlil/src/vegvesenet/nvdb-vegkart", "v3", "NVDB-Vegkart"); | |
final String repo; | |
final String branch; | |
final String filter; | |
Project(String repo, String branch, String filter) { | |
this.repo = repo; | |
this.branch = branch; | |
this.filter = filter; | |
} | |
} | |
public static void main(String[] args) throws IOException, InterruptedException { | |
if(args.length != 1) { | |
System.out.println("Mulige parametre: " + | |
Stream.of(Project.values()).map(Enum::name).collect(Collectors.joining(","))); | |
System.exit(0); | |
} | |
Project project = Project.valueOf(args[0]); | |
File workingDir = new File(project.repo); | |
File tagsFile = File.createTempFile("tags", project.branch); | |
new ProcessBuilder("git", "tag", "--merged", project.branch) | |
.directory(workingDir) | |
.redirectOutput(ProcessBuilder.Redirect.to(tagsFile)) | |
.start() | |
.waitFor(); | |
var tags = Files.lines(tagsFile.toPath()) | |
.filter(t -> t.startsWith(project.filter)) | |
.filter(t -> !t.endsWith("-alpha")) | |
.map(t -> parseTag(t, workingDir)) | |
.sorted() | |
.collect(Collectors.toList()); | |
tagsFile.delete(); | |
var tag = tags.remove(0); | |
for (Tag tag2 : tags) { | |
System.out.println(tag + " -> " + tag2 + " - " + tag2.timestamp.format(DateTimeFormatter.ISO_DATE)); | |
File tempFile = File.createTempFile(tag.name, tag2.name); | |
new ProcessBuilder("git", "log", "--pretty=oneline", tag + "..." + tag2) | |
.directory(workingDir) | |
.redirectOutput(ProcessBuilder.Redirect.to(tempFile)) | |
.start() | |
.waitFor(); | |
Files.lines(tempFile.toPath()) | |
.map(MatcherProxy::m) | |
.filter(MatcherProxy::matches) | |
.map(MatcherProxy::jiraIssue) | |
.distinct() | |
.sorted() | |
.forEach(System.out::println); | |
tempFile.delete(); | |
tag = tag2; | |
} | |
} | |
private static Tag parseTag(String t, File workingDir) { | |
try { | |
var tagTime = getTagTime(workingDir, t) | |
.replace(" +0100", "") | |
.replace(" +0200", "") | |
.replace(" ", "T"); | |
return new Tag(t, LocalDateTime.parse(tagTime)); | |
} catch (IOException | InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private static String getTagTime(File workingDir, String tag) throws IOException, InterruptedException { | |
// git log -1 --format=%ai T | |
File tempFile = File.createTempFile("time", tag); | |
new ProcessBuilder("git", "log", "-1", "--format=%ai", tag) | |
.directory(workingDir) | |
.redirectOutput(ProcessBuilder.Redirect.to(tempFile)) | |
.start() | |
.waitFor(); | |
return Files.readAllLines(tempFile.toPath()).get(0); | |
} | |
private static class MatcherProxy { | |
private final Matcher matcher; | |
private MatcherProxy(Matcher matcher) { | |
this.matcher = matcher; | |
} | |
static MatcherProxy m(String line) { | |
return new MatcherProxy(jiraPattern.matcher(line)); | |
} | |
public boolean matches() { | |
return matcher.matches(); | |
} | |
public String jiraIssue() { | |
return "NVDB-" + matcher.group(2); | |
} | |
} | |
private static class Tag implements Comparable<Tag>{ | |
final String name; | |
final LocalDateTime timestamp; | |
private Tag(String name, LocalDateTime timestamp) { | |
this.name = name; | |
this.timestamp = timestamp; | |
} | |
@Override | |
public int compareTo(Tag o) { | |
return timestamp.compareTo(o.timestamp); | |
} | |
@Override | |
public String toString() { | |
return name; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment