Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
Added the instanceName configuration parameter. This name is used to …
Browse files Browse the repository at this point in the history
…populate git commit messages so that you can identify automated commits. Ready CI also uses the instanceName to avoid cyclic builds triggered by the web-hook receiving one of it's own commit notifications.

Fixed matching of the branch identifier received from GitHub's web-hook requests.
  • Loading branch information
Bradley Clayton committed Jun 14, 2018
1 parent f1569a6 commit 116ed5e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Lets take a look at some of these parameters
| Parameter | Description |
| :-------- | :---------- |
| instanceName | A name for your instance. This name is used to populate git commit messages so that you can identify automated commits. Ready CI also uses the instanceName to avoid cyclic builds triggered by the web-hook receiving one of it's own commit notifications.
| pipelines | An array of as many pipeline configurations as you want |
| - name | Each pipeline is named, and you use this name to start a command-line build |
| gitPath | The path to your code repository |
Expand Down
1 change: 1 addition & 0 deletions readyConfigExample.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
instanceName: Ready CI

pipelines:
- name: ready-ci
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public PipelineConfiguration() {

public boolean matchesRepositoryName(String repositoryName, String branch) {
return gitPath.toLowerCase().contains(repositoryName.toLowerCase()) &&
gitBranch.equalsIgnoreCase(branch) &&
branch.contains(gitBranch) &&
repositoryName.length() > 0 &&
branch.length() > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
*/
public class ReadyCIConfiguration {


public static final String ARG_SERVER = "server";
public static final String ARG_PIPELINE = "pipeline=";
private static final Logger LOGGER = LoggerFactory.getLogger(ReadyCIConfiguration.class);
private static ReadyCIConfiguration instance;

public String instanceName;
public boolean isServerMode;
public List<PipelineConfiguration> pipelines;
public PipelineConfiguration piplineToRun;
Expand All @@ -32,6 +32,7 @@ public static ReadyCIConfiguration instance() {
}

private ReadyCIConfiguration() {
this.instanceName = "Ready CI";
this.isServerMode = false;
this.pipelines = new ArrayList<PipelineConfiguration>();
this.piplineToRun = null;
Expand Down Expand Up @@ -80,6 +81,7 @@ private void loadConfiguration(String fileName) {
try {
File configurationFile = new File(fileName);
ReadyCIConfiguration newConfiguration = mapper.readValue(configurationFile, ReadyCIConfiguration.class);
this.instanceName = newConfiguration.instanceName;
this.isServerMode = newConfiguration.isServerMode;
this.pipelines = newConfiguration.pipelines;
LOGGER.info(String.format("Loaded configuration %s with %s pipelines", fileName, pipelines.size()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.squarepolka.readyci.tasks.code;

import com.squarepolka.readyci.configuration.ReadyCIConfiguration;
import com.squarepolka.readyci.taskrunner.BuildEnvironment;
import com.squarepolka.readyci.tasks.Task;
import org.springframework.stereotype.Component;
Expand All @@ -20,9 +21,11 @@ public String taskIdentifier() {

@Override
public void performTask(BuildEnvironment buildEnvironment) throws Exception {
String commitMessage = buildEnvironment.getProperty(BUILD_PROP_GIT_COMMIT_MESSAGE, "");
String configuredCommitMessage = buildEnvironment.getProperty(BUILD_PROP_GIT_COMMIT_MESSAGE, "");
List<String> filesToCommit = buildEnvironment.getProperties(BUILD_PROP_GIT_COMMIT_FILE_LIST);
String projectPath = buildEnvironment.projectPath;
String instanceName = ReadyCIConfiguration.instance().instanceName;
String commitMessage = String.format("%s: %s", instanceName, configuredCommitMessage);

stageFiles(filesToCommit, projectPath);
createCommit(commitMessage, projectPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.squarepolka.readyci.webhook;

import com.squarepolka.readyci.ReadyCI;
import com.squarepolka.readyci.configuration.PipelineConfiguration;
import com.squarepolka.readyci.configuration.ReadyCIConfiguration;
import com.squarepolka.readyci.taskrunner.TaskRunner;
Expand Down Expand Up @@ -34,16 +35,20 @@ public void handleWebHook(Map<String, Object> webHookRequest) {
String repository = Util.getMappedValueAtPath(webHookRequest, "repository.name");
String branchName = Util.getMappedValueAtPath(webHookRequest, "push.changes.new.name");
String gitAuthor = Util.getMappedValueAtPath(webHookRequest, "push.changes.new.target.author.raw");
String commitMessage = Util.getMappedValueAtPath(webHookRequest, "push.changes.new.target.summary.raw");

// Try load GitHub values if they are missing
if (!Util.valueExists(branchName)) {
branchName = Util.getMappedValueAtPath(webHookRequest, "repository.default_branch");
branchName = Util.getMappedValueAtPath(webHookRequest, "ref");
}
if (!Util.valueExists(gitAuthor)) {
gitAuthor = Util.getMappedValueAtPath(webHookRequest, "sender.login");
}
if (!Util.valueExists(commitMessage)) {
commitMessage = Util.getMappedValueAtPath(webHookRequest, "head_commit.message");
}

if (Util.valueExists(repository) && Util.valueExists(branchName) && Util.valueExists(gitAuthor)) {
if (validateGitCommit(repository, branchName, gitAuthor, commitMessage)) {
LOGGER.info(String.format("Webhook received for repository %s and branch %s by user %s", repository, branchName, gitAuthor));
handleBuildRequest(repository, branchName);
} else {
Expand All @@ -70,4 +75,19 @@ private void runPipelines(List<PipelineConfiguration> pipelineConfigurations) {
}
}

private boolean validateGitCommit(String repository, String branchName, String gitAuthor, String commitMessage) {
if (Util.valueExists(repository) && Util.valueExists(branchName) && Util.valueExists(gitAuthor) && Util.valueExists(commitMessage)) {
String instanceName = ReadyCIConfiguration.instance().instanceName;
if (commitMessage.toLowerCase().contains(instanceName.toLowerCase())) {
LOGGER.warn("Hmmm, I recognise this GIT commit on %s for branch %s by %s, " +
"because my name is in the commit message. I'm going to ignore this " +
"commit to avoid cyclic builds triggered through the web-hook. " +
"The commit message is %s.", repository, branchName, gitAuthor, commitMessage);
} else {
return true;
}
}
return false;
}

}

0 comments on commit 116ed5e

Please sign in to comment.