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 #682

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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,7 +16,9 @@
import java.io.File;
import java.util.List;

import static java.lang.System.getenv;
import static org.shipkit.internal.gradle.exec.ExecCommandFactory.execCommand;
import static org.shipkit.internal.gradle.travis.TravisUtils.generateCommitMessagePostfix;
import static org.shipkit.internal.gradle.util.GitUtil.getTag;

/**
Expand Down Expand Up @@ -56,12 +58,14 @@ 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, getenv("TRAVIS_BUILD_NUMBER"));

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
@@ -1,19 +1,24 @@
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.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.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;
import static org.shipkit.internal.gradle.travis.TravisUtils.generateCommitMessagePostfix;

/**
* Configures the release automation to be used with Travis CI.
* Intended for root project.
Expand Down Expand Up @@ -41,8 +46,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 +61,25 @@ 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);
});

project.getTasks().withType(GitCommitTask.class, t -> {
t.setCommitMessagePostfix(generateCommitMessagePostfix(conf, travisCommitMessage, travisBuildNumber));
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 generateCommitMessagePostfix(ShipkitConfiguration conf, String travisBuildNumber) {
if (travisBuildNumber == null) {
return conf.getGit().getCommitMessagePostfix();
}
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
@@ -0,0 +1,38 @@
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.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 = TravisUtils.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 = TravisUtils.generateCommitMessagePostfix(shipkitConfiguration, "")
then:
url == "[ci skip]"
}
}