Skip to content

Commit

Permalink
Gathering repo provider information #49
Browse files Browse the repository at this point in the history
 * Getting ownerName and repoName from clone URL.
 * Setting GitLab server and GitHub API from clone URL.
  • Loading branch information
tomasbjerre committed Jul 23, 2017
1 parent 3c0bf2f commit dd84970
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ env:
- secure: "f78upMWhPViLh2W9jLjkjikZgSJCd9LuJVZF227JFSLGV8+DC0YbHQi6pYmpejhTT9H1OTqXyQ5c16QEQzutx6O63iyWdvWtIHtuct66jWPUp1NL3EAlK9F4NdG/9sbdEcElgrQxEArjYB5w+UZ+1uJucc1oMqIWbWQ089tvdx1VYZYRdP3pdLOoqgAqi4/GOxj8vRO9lIY7kc9IhEDDAfzNrGV8/LKao9f1pEOI+XcVDMOovRcstkMJZ5kqAep7xZTURGScKE6dMm58IEPDQAfbbGxAaJTO1HiNhFt6u2n+Z/oXZS8zlD/mcTA2acE6Ip7AMplSrac3bkzhwULwVu1PeDhteS9cwhpwJ/B3dR5sDAA+U/4WkfDV5VyyWVn8Z7Jy5nzDyUrtZBiPCzUyKDEaKc0mlLjWJjGiboq3ACnn62CE+VWQz9J0vnjxrwwqLcZbAtPzbFn3myYNbrf2qHt7n9eL3l9c9mkzYCfC6hz1oqsa35cimL6NXzjbRPu8ruyZdDcOdPBlVcdQ/g8ZhTslo5SCi4yukGQ0NbdPF0bJ4JXmBT49KUGbPOrCqdzoe+or04uJ6KkbLsO+bSGtVcidMuUeeylhfUBMtZs8XBKB2o3WWpZKjCUlKDl/vfDmlP7G+1Bd6lR11F1Z5jmtPLBzKlURrAMPVSk4EwFLNeQ="
jdk:
- oraclejdk8
- oraclejdk7
- openjdk7
# - oraclejdk7
# - openjdk7
git:
depth: 9999999
before_script:
Expand Down
25 changes: 22 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
# Git Changelog changelog
# Changelog

Changelog of Git Changelog.
Changelog for tomasbjerre git-changelog-lib.

## Next release
### GitHub [#49](https://github.com/tomasbjerre/git-changelog-lib/issues/49) Ability to grab the url from the git config. *enhancement*

**Gathering repo provider information**

* Getting ownerName and repoName from clone URL.
* Setting GitLab server and GitHub API from clone URL.

[a78e4cdcbe3ae07](https://github.com/tomasbjerre/git-changelog-lib/commit/a78e4cdcbe3ae07) Tomas Bjerre *2017-07-23 18:42:04*


### Other changes

**Cleaning**


[3c0bf2f7885bff3](https://github.com/tomasbjerre/git-changelog-lib/commit/3c0bf2f7885bff3) Tomas Bjerre *2017-07-16 05:55:55*


## 1.69
### GitHub [#47](https://github.com/tomasbjerre/git-changelog-lib/pull/47) Add ignoreCommitsOlderThan date limiting *enhancement*

**Adjustments after merge of**
Expand All @@ -11,7 +30,7 @@ Changelog of Git Changelog.
* Making ignoreCommits more effective.
* Also cleaning up unrelated parts of the API. Use File instead of String to supply changelog file. Enabling write changelog to Writer.

[94ab68796f7d271](https://github.com/tomasbjerre/git-changelog-lib/commit/94ab68796f7d271) Tomas Bjerre *2017-07-07 20:03:46*
[121e65493ec764b](https://github.com/tomasbjerre/git-changelog-lib/commit/121e65493ec764b) Tomas Bjerre *2017-07-07 20:04:00*


### Other changes
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ This software can be used:

Here is an example template.
```
# Git Changelog changelog
# Changelog
Changelog of Git Changelog.
Changelog for {{ownerName}} {{repoName}}.
{{#tags}}
## {{name}}{{#hasTagTime}} ({{tagTime}}){{/hasTagTime}}
## {{name}}
{{#issues}}
{{#hasIssue}}
{{#hasLink}}
Expand All @@ -51,7 +51,7 @@ Changelog of Git Changelog.
* {{.}}
{{/messageBodyItems}}
[{{hash}}](https://github.com/tomasbjerre/git-changelog-lib/commit/{{hash}}) {{authorName}} *{{commitTime}}*
[{{hash}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}} *{{commitTime}}*
{{/commits}}
Expand All @@ -65,8 +65,10 @@ There are also different variations [here](https://github.com/tomasbjerre/git-ch

## Supplied information

The template is supplied with a datastructure like:
The template is supplied with this context:
```
- ownerName (Derived from the clone URL, for this repo it would be "tomasbjerre")
- repoName (Derived from the clone URL, for this repo it would be "git-changelog-lib")
* commits
- authorName
- authorEmailAddress
Expand Down
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/bash
cp src/main/resources/git-changelog-template.mustache changelog.mustache
./gradlew clean build -i
6 changes: 3 additions & 3 deletions changelog.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Git Changelog changelog
# Changelog

Changelog of Git Changelog.
Changelog for {{ownerName}} {{repoName}}.

{{#tags}}
## {{name}}
Expand All @@ -24,7 +24,7 @@ Changelog of Git Changelog.
* {{.}}
{{/messageBodyItems}}

[{{hash}}](https://github.com/tomasbjerre/git-changelog-lib/commit/{{hash}}) {{authorName}} *{{commitTime}}*
[{{hash}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}} *{{commitTime}}*

{{/commits}}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,15 @@ private Changelog getChangelog(GitRepo gitRepo) throws GitChangelogRepositoryExc
toId,
this.settings.getUntaggedName(),
this.settings.getIgnoreTagsIfNameMatches());

if (!settings.getGitHubApi().isPresent()) {
settings.setGitHubApi(gitRepoData.findGitHubApi().orNull());
}
if (!settings.getGitLabServer().isPresent()) {
settings.setGitLabServer(gitRepoData.findGitLabServer().orNull());
settings.setGitLabProjectName(gitRepoData.findOwnerName().orNull());
}

List<GitCommit> diff = gitRepoData.getGitCommits();
List<ParsedIssue> issues = new IssueParser(this.settings, diff).parseForIssues();
if (this.settings.ignoreCommitsWithoutIssue()) {
Expand All @@ -438,7 +447,9 @@ private Changelog getChangelog(GitRepo gitRepo) throws GitChangelogRepositoryExc
transformer.toTags(tags, issues), //
transformer.toAuthors(diff), //
transformer.toIssues(issues), //
transformer.toIssueTypes(issues));
transformer.toIssueTypes(issues), //
gitRepoData.findOwnerName().orNull(), //
gitRepoData.findRepoName().orNull());
}

private Optional<ObjectId> getId(GitRepo gitRepo, Optional<String> ref, Optional<String> commit)
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/se/bjurr/gitchangelog/api/model/Changelog.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ public class Changelog implements ICommits, IAuthors, IIssues, Serializable {
private final List<Author> authors;
private final List<Issue> issues;
private final List<IssueType> issueTypes;
private final String ownerName;
private final String repoName;

public Changelog(
List<Commit> commits,
List<Tag> tags,
List<Author> authors,
List<Issue> issues,
List<IssueType> issueTypes) {
List<IssueType> issueTypes,
String ownerName,
String repoName) {
this.commits = checkNotNull(commits, "commits");
this.tags = checkNotNull(tags, "tags");
this.authors = checkNotNull(authors, "authors");
this.issues = checkNotNull(issues, "issues");
this.issueTypes = checkNotNull(issueTypes, "issueTypes");
this.ownerName = ownerName;
this.repoName = repoName;
}

@Override
Expand All @@ -44,6 +50,14 @@ public List<Commit> getCommits() {
return commits;
}

public String getOwnerName() {
return ownerName;
}

public String getRepoName() {
return repoName;
}

public List<Tag> getTags() {
return tags;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public GitRepoData getGitRepoData(
ObjectId from, ObjectId to, String untaggedName, Optional<String> ignoreTagsIfNameMatches)
throws GitChangelogRepositoryException {
try {
return new GitRepoData(gitTags(from, to, untaggedName, ignoreTagsIfNameMatches));
String originUrl = this.git.getRepository().getConfig().getString("remote", "origin", "url");
List<GitTag> gitTags = gitTags(from, to, untaggedName, ignoreTagsIfNameMatches);
return new GitRepoData(originUrl, gitTags);
} catch (Exception e) {
throw new GitChangelogRepositoryException(toString(), e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package se.bjurr.gitchangelog.internal.git;

import static com.google.common.base.Optional.absent;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newTreeSet;

import com.google.common.base.Optional;
import java.util.List;
import java.util.Set;
import se.bjurr.gitchangelog.internal.git.model.GitCommit;
Expand All @@ -11,21 +13,67 @@
public class GitRepoData {
private final List<GitCommit> gitCommits;
private final List<GitTag> gitTags;
private final String originCloneUrl;

public GitRepoData(List<GitTag> gitTags) {
public GitRepoData(String originCloneUrl, List<GitTag> gitTags) {
Set<GitCommit> gitCommitsSorted = newTreeSet();
for (GitTag gitTag : gitTags) {
gitCommitsSorted.addAll(gitTag.getGitCommits());
}
this.originCloneUrl = originCloneUrl;
this.gitCommits = newArrayList(gitCommitsSorted);
this.gitTags = gitTags;
}

public String getOriginCloneUrl() {
return originCloneUrl;
}

public List<GitCommit> getGitCommits() {
return gitCommits;
}

public List<GitTag> getGitTags() {
return gitTags;
}

public Optional<String> findGitHubApi() {
if (originCloneUrl == null) {
return absent();
}
if (!originCloneUrl.contains("github.com")) {
return absent();
}
return Optional.of(
"https://api.github.com/repos/" + findOwnerName().orNull() + "/" + findRepoName().orNull());
}

public Optional<String> findGitLabServer() {
if (originCloneUrl == null) {
return absent();
}
if (!originCloneUrl.contains("gitlab.com")) {
return absent();
}
return Optional.of("https://gitlab.com/");
}

public Optional<String> findOwnerName() {
return repoUrlPartFromEnd(2);
}

public Optional<String> findRepoName() {
return repoUrlPartFromEnd(1);
}

private Optional<String> repoUrlPartFromEnd(int i) {
if (originCloneUrl == null) {
return absent();
}
String[] parts = this.originCloneUrl.split("[/:\\.]");
if (parts.length > i) {
return Optional.of(parts[parts.length - i - 1]);
}
return absent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static GitRepoData removeCommitsWithoutIssue(
}
}

return new GitRepoData(reducedGitTags);
String originCloneUrl = gitRepoData.getOriginCloneUrl();
return new GitRepoData(originCloneUrl, reducedGitTags);
}

private GitRepoDataHelper() {}
Expand Down
35 changes: 24 additions & 11 deletions src/main/resources/git-changelog-template.mustache
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
# Git Changelog changelog
# Changelog

Changelog of Git Changelog.
Changelog for {{ownerName}} {{repoName}}.

{{#tags}}
## {{name}}
{{#issues}}
### {{name}} {{issue}}
{{#authors}}
* {{authorName}}
{{#commits}}
{{commitTime}}
{{{message}}}
{{#hasIssue}}
{{#hasLink}}
### {{name}} [{{issue}}]({{link}}) {{title}} {{#hasIssueType}} *{{issueType}}* {{/hasIssueType}} {{#hasLabels}} {{#labels}} *{{.}}* {{/labels}} {{/hasLabels}}
{{/hasLink}}
{{^hasLink}}
### {{name}} {{issue}} {{title}} {{#hasIssueType}} *{{issueType}}* {{/hasIssueType}} {{#hasLabels}} {{#labels}} *{{.}}* {{/labels}} {{/hasLabels}}
{{/hasLink}}
{{/hasIssue}}
{{^hasIssue}}
### {{name}}
{{/hasIssue}}

{{/commits}}
{{#commits}}
**{{{messageTitle}}}**

{{#messageBodyItems}}
* {{.}}
{{/messageBodyItems}}

[{{hash}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}} *{{commitTime}}*

{{/commits}}

{{/authors}}
{{/issues}}
{{/tags}}
{{/tags}}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public void testThatCommitsWithoutIssueCanBeIgnoredTagsIssuesCommits() throws Ex
gitChangelogApiBuilder() //
.withFromCommit(ZERO_COMMIT) //
.withToRef("test") //
.withGitHubIssuePattern("nooo") //
.withGitLabIssuePattern("nooo") //
.withCustomIssue("JIRA", "JIR-[0-9]*", "http://${PATTERN_GROUP}", "${PATTERN_GROUP}") //
.withIgnoreCommitsWithoutIssue(true) //
.withTemplatePath(templatePath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package se.bjurr.gitchangelog.internal.git;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import se.bjurr.gitchangelog.internal.git.model.GitTag;

public class GitRepoDataTest {

public GitRepoData newGitRepoData(String originUrl) {
List<GitTag> gitTags = new ArrayList<>();
return new GitRepoData(originUrl, gitTags);
}

@Test
public void testGitHubRepo() {
GitRepoData sut = newGitRepoData("git@github.com:tomasbjerre/git-changelog-lib.git");

assertThat(sut.findOwnerName().orNull()) //
.isEqualTo("tomasbjerre");
assertThat(sut.findRepoName().orNull()) //
.isEqualTo("git-changelog-lib");

assertThat(sut.findGitHubApi().orNull()) //
.isEqualTo("https://api.github.com/repos/tomasbjerre/git-changelog-lib");
assertThat(sut.findGitLabServer().orNull()) //
.isEqualTo(null);
}

@Test
public void testGitBitbucketRepo() {
GitRepoData sut =
newGitRepoData("https://tomasbjerre@bitbucket.org/ljohansson/grunt-connect-apimock.git");

assertThat(sut.findOwnerName().orNull()) //
.isEqualTo("ljohansson");
assertThat(sut.findRepoName().orNull()) //
.isEqualTo("grunt-connect-apimock");

assertThat(sut.findGitHubApi().orNull()) //
.isEqualTo(null);
assertThat(sut.findGitLabServer().orNull()) //
.isEqualTo(null);
}

@Test
public void testGitLabRepo() {
GitRepoData sut = newGitRepoData("http://root@gitlab.com/root/violations-test.git");

assertThat(sut.findOwnerName().orNull()) //
.isEqualTo("root");
assertThat(sut.findRepoName().orNull()) //
.isEqualTo("violations-test");

assertThat(sut.findGitHubApi().orNull()) //
.isEqualTo(null);
assertThat(sut.findGitLabServer().orNull()) //
.isEqualTo("https://gitlab.com/");
}
}

0 comments on commit dd84970

Please # to comment.