Skip to content
This repository was archived by the owner on Jan 18, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 4 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
@@ -1,19 +1,22 @@
package org.shipkit.internal.gradle.release;

import org.gradle.api.Action;
import org.gradle.api.Plugin;
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.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.GitSetupPlugin;
import org.shipkit.internal.gradle.git.tasks.GitCheckOutTask;
import org.shipkit.internal.gradle.util.StringUtil;

import static org.shipkit.internal.gradle.travis.TravisUtils.generateCommitMessage;

/**
* Configures the release automation to be used with Travis CI.
* Intended for root project.
Expand Down Expand Up @@ -41,8 +44,12 @@ public void apply(final Project project) {
project.getPlugins().apply(CiReleasePlugin.class);

final String branch = System.getenv("TRAVIS_BRANCH");
LOG.info("Branch from 'TRAVIS_BRANCH' env variable: {}", branch);
final String travisCommitMessage = System.getenv("TRAVIS_COMMIT_MESSAGE");
final String travisBuildNumber = System.getenv("TRAVIS_BUILD_NUMBER");
final String pr = System.getenv("TRAVIS_PULL_REQUEST");

LOG.info("Branch from 'TRAVIS_BRANCH' env variable: {}", branch);
ShipkitConfiguration conf = project.getPlugins().apply(ShipkitConfigurationPlugin.class).getConfiguration();
//configure branch based on Travis' env variable
IdentifyGitBranchTask identifyBranch = (IdentifyGitBranchTask) project.getTasks().getByName(GitBranchPlugin.IDENTIFY_GIT_BRANCH);
if (!StringUtil.isEmpty(branch)) {
Expand All @@ -52,27 +59,21 @@ 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");
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);

project.getTasks().withType(ReleaseNeededTask.class, new Action<ReleaseNeededTask>() {
public void execute(ReleaseNeededTask t) {
t.setCommitMessage(System.getenv("TRAVIS_COMMIT_MESSAGE"));
t.setPullRequest(isPullRequest);
}
project.getTasks().withType(ReleaseNeededTask.class, t -> {
Copy link
Member

Choose a reason for hiding this comment

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

We want to set the commit message on the GitCommitTask. We don't want to generate commit message for ReleaseNeededTask because this task should work on verbatim commit message supplied by Travis. ReleaseNeededTask is not making commits :)

Check out the initial design of this feature at #514

When you work on this feature, I suggest to test it this way:

./gradlew performRelease -PdryRun

Above will get you a local commit with the message. Please ensure that this commit has the stuff we need.

Thank you for working on this!!!

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I created PR to confirm my assumptions were right (which were wrong, but now I got feedback :)).

I'll add necessary changes.

t.setCommitMessage(generateCommitMessage(conf, travisCommitMessage, travisBuildNumber));
t.setPullRequest(isPullRequest);
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.shipkit.internal.gradle.travis;

import org.shipkit.gradle.configuration.ShipkitConfiguration;

public class TravisUtils {

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

public static String generateCommitMessage(ShipkitConfiguration conf, String travisCommitMessage, String travisBuildNumber) {
if (travisCommitMessage == null) {
return null;
}
String travisJobUrl = generateTravisBuildUrl(conf, travisBuildNumber);

if (travisCommitMessage.contains("[ci skip]")) {
return travisCommitMessage.replace(" [ci skip]", ". CI job: " + travisJobUrl + " [ci skip]");
} else {
return travisCommitMessage + ". CI job: " + travisJobUrl;
}
}

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
@@ -0,0 +1,33 @@
package org.shipkit.internal.gradle.travis

import org.shipkit.gradle.configuration.ShipkitConfiguration
import spock.lang.Specification

class TravisUtilsTest extends Specification {

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

def "should build travis url without [ci skip]"() {
given:
ShipkitConfiguration shipkitConfiguration = Mock(ShipkitConfiguration)
ShipkitConfiguration.GitHub gitHub = Mock(ShipkitConfiguration.GitHub)
shipkitConfiguration.getGitHub() >> gitHub
1 * gitHub.getRepository() >> "mockito/shipkit"
0 * _
when:
def url = TravisUtils.generateCommitMessage(shipkitConfiguration, "original", "123")
then:
url == "original. CI job: https://travis-ci.org/mockito/shipkit/builds/123"
}
}