Skip to content

Commit

Permalink
Correcting revision logging
Browse files Browse the repository at this point in the history
 * Resetting Jira and GitHub clients before tests. Was having troubles with the cache not being invalidated between tests.
  • Loading branch information
tomasbjerre committed Jan 30, 2016
1 parent da9d387 commit 00b37d1
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 45 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ language: java
jdk:
- oraclejdk8
- oraclejdk7
git:
depth: 9999999
before_script:
- git log --graph --full-history --all --color --date=short --pretty=format:"%Cred%x09%h %Creset%ad%Cgreen%d %Creset %s %C(bold)(%an)%Creset"
script:
- ./gradlew build --stacktrace -i
after_script:
Expand Down
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ Changelog of Git Changelog.
## Next release
### Other changes

[1a9c14ecdaa2a6e](https://github.com/tomasbjerre/git-changelog-lib/commit/1a9c14ecdaa2a6e) Tomas Bjerre *2016-01-30 11:54:00*
[3b8208830a28d67](https://github.com/tomasbjerre/git-changelog-lib/commit/3b8208830a28d67) Tomas Bjerre *2016-01-30 14:46:41*

Correcting revision logging


## 1.20
### Other changes

[c3da07c0286855d](https://github.com/tomasbjerre/git-changelog-lib/commit/c3da07c0286855d) Tomas Bjerre *2016-01-30 11:56:15*

Including first commit

Expand Down Expand Up @@ -331,4 +339,8 @@ Work for 1.0

Initial commit

[a1aa5ff5b625e63](https://github.com/tomasbjerre/git-changelog-lib/commit/a1aa5ff5b625e63) Tomas Bjerre *2015-11-12 05:29:31*

Initial commit


18 changes: 7 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.bmuschko:gradle-nexus-plugin:2.3.1'
classpath 'net.researchgate:gradle-release:2.2.2'
classpath 'gradle.plugin.se.bjurr.gitchangelog:git-changelog-gradle-plugin:1.10'
}
}

Expand All @@ -18,13 +14,6 @@ apply plugin: 'eclipse'
apply plugin: 'signing'
apply plugin: 'com.bmuschko.nexus'
apply plugin: 'net.researchgate.release'
apply plugin: 'se.bjurr.gitchangelog.git-changelog-gradle-plugin'

task gitChangelogTask(type: se.bjurr.gitchangelog.plugin.gradle.GitChangelogTask) {
gitChangelogPlugin.templateContent = new File('changelog.mustache').getText('UTF-8');
gitChangelogPlugin.filePath = "CHANGELOG.md";
gitChangelogPlugin.settingsFile = "changelog.json";
}

group = 'se.bjurr.gitchangelog'

Expand All @@ -45,6 +34,13 @@ dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.slf4j:slf4j-simple:1.7.13'
testCompile 'org.assertj:assertj-core:2.3.0'
testCompile 'com.google.code.gson:gson:2.5'
}

test {
if (System.getProperty('DEBUG', 'false') == 'true') {
jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009'
}
}

eclipse {
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
./gradlew clean gitChangelog build
./gradlew clean build
4 changes: 4 additions & 0 deletions src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,8 @@ private GitChangelogApi() {
private GitChangelogApi(Settings settings) {
this.settings = settings;
}

public Settings getSettings() {
return settings;
}
}
81 changes: 61 additions & 20 deletions src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
package se.bjurr.gitchangelog.internal.git;

import static com.google.common.base.Joiner.on;
import static com.google.common.base.Objects.firstNonNull;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Iterators.getLast;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.transform;
import static org.eclipse.jgit.lib.ObjectId.fromString;
import static org.eclipse.jgit.revwalk.RevSort.REVERSE;
import static se.bjurr.gitchangelog.api.GitChangelogApiConstants.REF_MASTER;
import static se.bjurr.gitchangelog.api.GitChangelogApiConstants.ZERO_COMMIT;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.LogCommand;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import se.bjurr.gitchangelog.api.GitChangelogApiConstants;
import se.bjurr.gitchangelog.internal.git.model.GitCommit;
import se.bjurr.gitchangelog.internal.git.model.GitTag;

import com.google.common.base.Function;
import com.google.common.base.Predicate;

public class GitRepo {
private static final Function<RevCommit, GitCommit> TO_GITCOMMIT = new Function<RevCommit, GitCommit>() {
Expand Down Expand Up @@ -66,26 +72,43 @@ public GitRepo(File repo) {
}
}

/**
*
* @param from
* From, but not including, this commit. Except for the
* {@link GitChangelogApiConstants#ZERO_COMMIT}, it is included.
* @param to
* To and including this commit.
*/
public List<GitCommit> getDiff(ObjectId from, ObjectId to) {
Git git = null;
try {
git = new Git(repository);
LogCommand logCommand = git //
.log();
final List<RevCommit> toList = newArrayList(git.log().add(to).call());
if (from.name().equals(firstCommit().name())) {
logCommand.add(to);
} else {
logCommand.addRange(from, to);
return newArrayList(transform(toList, TO_GITCOMMIT));
}
Iterable<RevCommit> itr = logCommand.call(); //
return newArrayList(transform(itr, TO_GITCOMMIT));

List<RevCommit> fromList = newArrayList(git.log().add(from).call()); //
Iterable<RevCommit> diff = filter(toList, notIn(fromList));

return newArrayList(transform(diff, TO_GITCOMMIT));
} catch (Exception e) {
throw new RuntimeException("References:\n" + on("\n").join(repository.getAllRefs().keySet()), e);
throw new RuntimeException("References:\n" + on("\n").join(getAllRefs().keySet()), e);
} finally {
git.close();
}
}

private Predicate<RevCommit> notIn(final List<RevCommit> fromList) {
return new Predicate<RevCommit>() {
@Override
public boolean apply(RevCommit input) {
return !fromList.contains(input);
}
};
}

public List<GitTag> getTags() {
Git git = null;
try {
Expand Down Expand Up @@ -120,9 +143,9 @@ public List<GitTag> getTags() {

public ObjectId getRef(String fromRef) {
try {
for (Ref foundRef : repository.getAllRefs().values()) {
for (Ref foundRef : getAllRefs().values()) {
if (foundRef.getName().endsWith(fromRef)) {
Ref ref = repository.getAllRefs().get(foundRef.getName());
Ref ref = getAllRefs().get(foundRef.getName());
Git git = null;
try {
git = new Git(repository);
Expand All @@ -137,12 +160,16 @@ public ObjectId getRef(String fromRef) {
}
}
}
throw new RuntimeException(fromRef + " not found in:\n" + on("\n").join(repository.getAllRefs().values()));
throw new RuntimeException(fromRef + " not found in:\n" + toString());
} catch (Exception e) {
throw new RuntimeException("", e);
}
}

private Map<String, Ref> getAllRefs() {
return repository.getAllRefs();
}

public ObjectId getCommit(String fromCommit) {
if (fromCommit.startsWith(ZERO_COMMIT)) {
return firstCommit();
Expand All @@ -151,15 +178,29 @@ public ObjectId getCommit(String fromCommit) {
}

private RevCommit firstCommit() {
RevWalk walk = null;
Git git = null;
try {
walk = new RevWalk(repository);
RevCommit root = walk.parseCommit(repository.resolve(Constants.HEAD));
walk.sort(REVERSE);
walk.markStart(root);
return walk.next();
git = new Git(repository);
AnyObjectId master = getRef(REF_MASTER);
Iterator<RevCommit> itr = git.log().add(master).call().iterator();
return getLast(itr);
} catch (Exception e) {
throw new RuntimeException("First commit not found in " + repository.getDirectory(), e);
} finally {
git.close();
}
}

@Override
public String toString() {
String s = ""//
+ "First commit at: " + firstCommit().name() + "\n"//
+ "Refs: \n";
Map<String, Ref> allRefs = getAllRefs();
for (String k : allRefs.keySet()) {
ObjectId pealed = firstNonNull(allRefs.get(k).getPeeledObjectId(), pealed = allRefs.get(k).getObjectId());
s += "Ref: " + k + " -> " + pealed.name() + "\n";
}
return s;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public String getName() {
public List<GitCommit> getGitCommits() {
return gitCommits;
}

@Override
public String toString() {
return "Tag: " + name + ": " + getGitCommit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ public class GitHubClientFactory {

private static GitHubClient gitHubClient;

public static void reset() {
gitHubClient = null;
}

public static GitHubClient createGitHubClient(String apiUrl) {
if (gitHubClient == null) {
gitHubClient = new GitHubClient(apiUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ public class JiraClientFactory {

private static JiraClient jiraClient;

public static void reset() {
jiraClient = null;
}

public static JiraClient createJiraClient(String apiUrl) {
if (jiraClient == null) {
jiraClient = new JiraClient(apiUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.URL;

import com.google.common.io.Resources;
import com.google.gson.GsonBuilder;

public class GitChangelogApiAsserter {

Expand Down Expand Up @@ -42,9 +43,16 @@ public void rendersTo(String file) throws Exception {
String templatePath = "templates/" + template;

// Test lib with settings
assertEquals("With lib: " + file, expected, gitChangelogApiBuilder()//
GitChangelogApi gitChangelogApiBuilder = gitChangelogApiBuilder()//
.withSettings(settingsFile)//
.withTemplatePath(templatePath)//
.withTemplatePath(templatePath);

String changelog = toJson(gitChangelogApiBuilder.getChangelog());
String settings = toJson(gitChangelogApiBuilder.getSettings());
String templateContent = Resources.toString(getResource(templatePath), UTF_8);

assertEquals("Test:\n" + file + "\nTemplate:\n" + templateContent + "\nChangelog: " + changelog + "\nSettings: "
+ settings, expected, gitChangelogApiBuilder //
.render().trim());

// Test lib
Expand Down Expand Up @@ -72,4 +80,8 @@ public void rendersTo(String file) throws Exception {
.render() //
.trim());
}

private String toJson(Object object) {
return new GsonBuilder().setPrettyPrinting().create().toJson(object);
}
}
20 changes: 17 additions & 3 deletions src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@
import org.junit.Before;
import org.junit.Test;

import se.bjurr.gitchangelog.internal.integrations.github.GitHubClientFactory;
import se.bjurr.gitchangelog.internal.integrations.jira.JiraClientFactory;
import se.bjurr.gitchangelog.internal.integrations.rest.RestClientMock;

import com.google.common.io.Resources;
import com.google.gson.GsonBuilder;

public class GitChangelogApiTest {
private RestClientMock mockedRestClient;

@Before
public void before() throws Exception {
GitHubClientFactory.reset();
JiraClientFactory.reset();

setFakeGitRepo(fakeRepo());
mockedRestClient = new RestClientMock();
mockedRestClient //
Expand All @@ -44,11 +50,15 @@ public void testThatIssuesCanBeRemoved() throws Exception {
URL settingsFile = getResource("settings/git-changelog-test-settings.json").toURI().toURL();
String templatePath = "templates/testIssuesCommits.mustache";

assertEquals(expected, gitChangelogApiBuilder()//
String templateContent = Resources.toString(getResource(templatePath), UTF_8);

GitChangelogApi changelogApiBuilder = gitChangelogApiBuilder()//
.withSettings(settingsFile)//
.withRemoveIssueFromMessageArgument(true) //
.withTemplatePath(templatePath)//
.render().trim());
.withTemplatePath(templatePath);

assertEquals("templateContent:\n" + templateContent + "\nContext:\n" + toJson(changelogApiBuilder.getChangelog()),
expected, changelogApiBuilder.render().trim());
}

@Test(expected = RuntimeException.class)
Expand Down Expand Up @@ -92,4 +102,8 @@ public void testThatCustomVariablesCanBeUsed() throws Exception {
.render() //
.trim());
}

private String toJson(Object object) {
return new GsonBuilder().setPrettyPrinting().create().toJson(object);
}
}
Loading

0 comments on commit 00b37d1

Please # to comment.