diff --git a/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/git/GitPlugin.java b/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/git/GitPlugin.java index 37bcd691..308b630e 100644 --- a/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/git/GitPlugin.java +++ b/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/git/GitPlugin.java @@ -21,25 +21,25 @@ /** * Adds Git-specific tasks needed for the release process. - * + *

* Applies plugins: *

- * + *

* Adds tasks: *

*/ public class GitPlugin implements Plugin { @@ -61,7 +61,9 @@ 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()); + if (t.getCommitMessagePostfix() == null) { + t.setCommitMessagePostfix(conf.getGit().getCommitMessagePostfix()); + } } }); @@ -89,12 +91,12 @@ public void execute(final GitPushTask t) { t.setSecretValue(info.getWriteToken()); project.getPlugins().apply(GitBranchPlugin.class) - .provideBranchTo(t, new Action() { - @Override - public void execute(String branch) { - t.getTargets().add(branch); - } - }); + .provideBranchTo(t, new Action() { + @Override + public void execute(String branch) { + t.getTargets().add(branch); + } + }); } }); diff --git a/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/release/TravisPlugin.java b/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/release/TravisPlugin.java index 5c06b309..be21a077 100644 --- a/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/release/TravisPlugin.java +++ b/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/release/TravisPlugin.java @@ -5,15 +5,22 @@ import org.gradle.api.Project; import org.gradle.api.logging.Logger; import org.gradle.api.logging.Logging; +import org.shipkit.gradle.configuration.ShipkitConfiguration; +import org.shipkit.gradle.git.GitCommitTask; import org.shipkit.gradle.git.IdentifyGitBranchTask; import org.shipkit.gradle.release.ReleaseNeededTask; import org.shipkit.internal.gradle.configuration.BasicValidator; import org.shipkit.internal.gradle.configuration.LazyConfiguration; +import org.shipkit.internal.gradle.configuration.ShipkitConfigurationPlugin; import org.shipkit.internal.gradle.git.GitBranchPlugin; +import org.shipkit.internal.gradle.git.GitPlugin; import org.shipkit.internal.gradle.git.GitSetupPlugin; import org.shipkit.internal.gradle.git.tasks.GitCheckOutTask; import org.shipkit.internal.gradle.util.StringUtil; +import static java.lang.System.getenv; +import static org.shipkit.internal.gradle.util.TravisCommitMessageUtils.decorateCommitMessagePostfix; + /** * Configures the release automation to be used with Travis CI. * Intended for root project. @@ -28,6 +35,8 @@ * so that the branch information is taken from 'TRAVIS_BRANCH' env variable. *
  • Configures {@link GitSetupPlugin}/{@link GitCheckOutTask} * so that it checks out the branch specified in env variable.
  • + *
  • Configures {@link GitSetupPlugin}/{@link org.shipkit.gradle.git.GitCommitTask} + * so that it enrich commit postfix with url to Travis build
  • *
  • Configures {@link ReleaseNeededPlugin}/{@link ReleaseNeededTask} * so that it uses information from 'TRAVIS_PULL_REQUEST' and 'TRAVIS_COMMIT_MESSAGE' env variables.
  • * @@ -39,6 +48,7 @@ public class TravisPlugin implements Plugin { @Override public void apply(final Project project) { project.getPlugins().apply(CiReleasePlugin.class); + final ShipkitConfiguration conf = project.getPlugins().apply(ShipkitConfigurationPlugin.class).getConfiguration(); final String branch = System.getenv("TRAVIS_BRANCH"); LOG.info("Branch from 'TRAVIS_BRANCH' env variable: {}", branch); @@ -52,27 +62,28 @@ public void apply(final Project project) { //set the branch to be checked out on ci build final GitCheckOutTask checkout = (GitCheckOutTask) project.getTasks().getByName(GitSetupPlugin.CHECKOUT_TASK); checkout.setRev(branch); - LazyConfiguration.lazyConfiguration(checkout, new Runnable() { - public void run() { - BasicValidator.notNull(checkout.getRev(), - "Task " + checkout.getPath() + " does not know the target revision to check out.\n" + - "In Travis CI builds, it is automatically configured from 'TRAVIS_BRANCH' environment variable.\n" + - "If you are trying to run this task outside Travis, you can export the environment variable.\n" + - "Alternatively, you can set the task's 'rev' property explicitly."); - } - }); + LazyConfiguration.lazyConfiguration(checkout, () -> BasicValidator.notNull(checkout.getRev(), + "Task " + checkout.getPath() + " does not know the target revision to check out.\n" + + "In Travis CI builds, it is automatically configured from 'TRAVIS_BRANCH' environment variable.\n" + + "If you are trying to run this task outside Travis, you can export the environment variable.\n" + + "Alternatively, you can set the task's 'rev' property explicitly.")); //update release needed task based on Travis' env variables String pr = System.getenv("TRAVIS_PULL_REQUEST"); + String buildNumber = getenv("TRAVIS_BUILD_NUMBER"); LOG.info("Pull request from 'TRAVIS_PULL_REQUEST' env variable: {}", pr); final boolean isPullRequest = pr != null && !pr.trim().isEmpty() && !pr.equals("false"); LOG.info("Pull request build: {}", isPullRequest); + String commitMessage = decorateCommitMessagePostfix(conf, buildNumber); + + GitCommitTask gitCommitTask = (GitCommitTask) project.getTasks().getByName(GitPlugin.GIT_COMMIT_TASK); + if (!StringUtil.isEmpty(commitMessage)) { + gitCommitTask.setCommitMessagePostfix(commitMessage); + } - project.getTasks().withType(ReleaseNeededTask.class, new Action() { - public void execute(ReleaseNeededTask t) { - t.setCommitMessage(System.getenv("TRAVIS_COMMIT_MESSAGE")); - t.setPullRequest(isPullRequest); - } + project.getTasks().withType(ReleaseNeededTask.class, t -> { + t.setCommitMessage(System.getenv("TRAVIS_COMMIT_MESSAGE")); + t.setPullRequest(isPullRequest); }); } } diff --git a/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/util/TravisCommitMessageUtils.java b/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/util/TravisCommitMessageUtils.java new file mode 100644 index 00000000..64ffcf53 --- /dev/null +++ b/subprojects/shipkit/src/main/groovy/org/shipkit/internal/gradle/util/TravisCommitMessageUtils.java @@ -0,0 +1,24 @@ +package org.shipkit.internal.gradle.util; + +import org.shipkit.gradle.configuration.ShipkitConfiguration; + +public class TravisCommitMessageUtils { + private static final String URL_PATTERN = "https://travis-ci.org/%s/builds/%s"; + + public static String decorateCommitMessagePostfix(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); + } +} diff --git a/subprojects/shipkit/src/test/groovy/org/shipkit/internal/gradle/git/GitPluginTest.groovy b/subprojects/shipkit/src/test/groovy/org/shipkit/internal/gradle/git/GitPluginTest.groovy index 527fc7be..286ebb15 100644 --- a/subprojects/shipkit/src/test/groovy/org/shipkit/internal/gradle/git/GitPluginTest.groovy +++ b/subprojects/shipkit/src/test/groovy/org/shipkit/internal/gradle/git/GitPluginTest.groovy @@ -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 @@ -22,4 +23,16 @@ class GitPluginTest extends PluginSpecification { gitPush.url == "https://dummy:foo@github.com/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]" + } } diff --git a/subprojects/shipkit/src/test/groovy/org/shipkit/internal/gradle/util/TravisCommitMessageUtilsTest.groovy b/subprojects/shipkit/src/test/groovy/org/shipkit/internal/gradle/util/TravisCommitMessageUtilsTest.groovy new file mode 100644 index 00000000..73a185f3 --- /dev/null +++ b/subprojects/shipkit/src/test/groovy/org/shipkit/internal/gradle/util/TravisCommitMessageUtilsTest.groovy @@ -0,0 +1,40 @@ +package org.shipkit.internal.gradle.util + +import spock.lang.Specification + +import org.shipkit.gradle.configuration.ShipkitConfiguration + +class TravisCommitMessageUtilsTest extends Specification { + + 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 = TravisCommitMessageUtils.decorateCommitMessagePostfix(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 = TravisCommitMessageUtils.decorateCommitMessagePostfix(shipkitConfiguration, "") + then: + url == "[ci skip]" + } +}