Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

[SHIPKIT-513] Automated commit message should have CI build ID #709

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import java.io.File;
import java.util.List;

import static java.lang.System.getProperty;
import static org.shipkit.internal.gradle.exec.ExecCommandFactory.execCommand;
import static org.shipkit.internal.gradle.util.GitUtil.getTag;
import static org.shipkit.internal.gradle.util.CommitMessageUtils.generateCommitMessagePostfix;

/**
* Adds Git-specific tasks needed for the release process.
Expand Down Expand Up @@ -55,13 +57,15 @@ public class GitPlugin implements Plugin<Project> {

public void apply(final Project project) {
final ShipkitConfiguration conf = project.getPlugins().apply(ShipkitConfigurationPlugin.class).getConfiguration();
String commitMessage = generateCommitMessagePostfix(conf, getProperty("TRAVIS_BUILD_NUMBER"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Travis exports env variables rather than system properties.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitPlugin should be decoupled from Travis, so that we can use it for Circle CI, etc. Take a look at #514 - we're adding new code to TravisPlugin.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I exposed it to CommitMessageUtil but using Travis Plugin for that seems to be really better way


TaskMaker.task(project, GIT_COMMIT_TASK, GitCommitTask.class, new Action<GitCommitTask>() {
public void execute(final GitCommitTask t) {
t.setDescription("Commits all changed files using generic --author and aggregated commit message");
t.setGitUserName(conf.getGit().getUser());
t.setGitUserEmail(conf.getGit().getEmail());
t.setCommitMessagePostfix(conf.getGit().getCommitMessagePostfix());
t.setCommitMessagePostfix(commitMessage);

}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.shipkit.internal.gradle.util;

import org.shipkit.gradle.configuration.ShipkitConfiguration;

public class CommitMessageUtils {
private static final String URL_PATTERN = "https://travis-ci.org/%s/builds/%s";

public static String generateCommitMessagePostfix(ShipkitConfiguration conf, String travisBuildNumber) {
if (!StringUtil.isEmpty(travisBuildNumber)) {
return getTravisCommitMessagePostfix(conf, travisBuildNumber);
}

return conf.getGit().getCommitMessagePostfix();
}

private static String getTravisCommitMessagePostfix(ShipkitConfiguration conf, String travisBuildNumber) {
String travisJobUrl = generateTravisBuildUrl(conf, travisBuildNumber);
return String.format("CI job: %s %s", travisJobUrl, conf.getGit().getCommitMessagePostfix());
}

private static String generateTravisBuildUrl(ShipkitConfiguration conf, String travisBuildNumber) {
return String.format(URL_PATTERN, conf.getGitHub().getRepository(), travisBuildNumber);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.shipkit.internal.gradle.git

import org.shipkit.gradle.git.GitCommitTask
import org.shipkit.gradle.git.GitPushTask
import testutil.PluginSpecification

Expand All @@ -22,4 +23,28 @@ class GitPluginTest extends PluginSpecification {
gitPush.url == "https://dummy:[email protected]/my-repo.git"
gitPush.secretValue == "foo"
}

def "configures git commit"() {
conf.gitHub.repository = 'my-repo'
conf.gitHub.writeAuthToken = 'foo'

when:
project.plugins.apply(GitPlugin)

then:
GitCommitTask gitCommit = project.tasks[GitPlugin.GIT_COMMIT_TASK]
gitCommit.commitMessagePostfix == "[ci skip]"
}

def "configures git commit in Travis environment"() {
conf.gitHub.repository = 'my-repo'
conf.gitHub.writeAuthToken = 'foo'
System.setProperty("TRAVIS_BUILD_NUMBER", "test")
when:
project.plugins.apply(GitPlugin)

then:
GitCommitTask gitCommit = project.tasks[GitPlugin.GIT_COMMIT_TASK]
gitCommit.commitMessagePostfix == "CI job: https://travis-ci.org/my-repo/builds/test [ci skip]"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.shipkit.internal.gradle.util

import spock.lang.Specification

import org.shipkit.gradle.configuration.ShipkitConfiguration

class CommitMessageUtilsTest extends Specification {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test coverage! Thanks!


def "should build travis url with [ci skip]"() {
given:
ShipkitConfiguration shipkitConfiguration = Mock(ShipkitConfiguration)
ShipkitConfiguration.GitHub gitHub = Mock(ShipkitConfiguration.GitHub)
ShipkitConfiguration.Git git = Mock(ShipkitConfiguration.Git)
shipkitConfiguration.getGitHub() >> gitHub
shipkitConfiguration.getGit() >> git
1 * git.commitMessagePostfix >> "[ci skip]"
1 * gitHub.getRepository() >> "mockito/shipkit"
0 * _
when:
def url = CommitMessageUtils.generateCommitMessagePostfix(shipkitConfiguration, "123")
then:
url == "CI job: https://travis-ci.org/mockito/shipkit/builds/123 [ci skip]"
}

def "should build postfix without travis url if blank build number"() {
given:
ShipkitConfiguration shipkitConfiguration = Mock(ShipkitConfiguration)
ShipkitConfiguration.GitHub gitHub = Mock(ShipkitConfiguration.GitHub)
ShipkitConfiguration.Git git = Mock(ShipkitConfiguration.Git)
shipkitConfiguration.getGitHub() >> gitHub
shipkitConfiguration.getGit() >> git

1 * git.commitMessagePostfix >> "[ci skip]"
0 * _
when:
def url = CommitMessageUtils.generateCommitMessagePostfix(shipkitConfiguration, "")
then:
url == "[ci skip]"
}
}