Skip to content

Commit

Permalink
Merge pull request #41 from uber/taskify
Browse files Browse the repository at this point in the history
Create NonDifferentialBuildTask to handle non-differential build invocations.
  • Loading branch information
jjx committed Jun 30, 2015
2 parents 434874b + 415f6f5 commit 77abc19
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 25 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
<artifactId>jna</artifactId>
<version>3.2.2</version>
</dependency>

<!-- test dependencies -->
<dependency>
<scope>test</scope>
Expand Down
39 changes: 16 additions & 23 deletions src/main/java/com/uber/jenkins/phabricator/PhabricatorNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.uber.jenkins.phabricator.conduit.Differential;

import com.uber.jenkins.phabricator.conduit.DifferentialClient;
import com.uber.jenkins.phabricator.tasks.NonDifferentialBuildTask;
import com.uber.jenkins.phabricator.uberalls.UberallsClient;
import com.uber.jenkins.phabricator.utils.CommonUtils;
import com.uber.jenkins.phabricator.utils.Logger;
Expand Down Expand Up @@ -85,36 +86,28 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
coverage.setOwner(build);
}

UberallsClient uberalls = new UberallsClient(getDescriptor().getUberallsURL(), logger,
environment.get("GIT_URL"), environment.get("GIT_BRANCH"));
final String branch = environment.get("GIT_BRANCH");
final UberallsClient uberalls = new UberallsClient(getDescriptor().getUberallsURL(), logger,
environment.get("GIT_URL"), branch);
final boolean needsDecoration = environment.get(PhabricatorPlugin.WRAP_KEY, null) == null;
final String conduitToken = environment.get(PhabricatorPlugin.CONDUIT_TOKEN, null);
final String arcPath = environment.get(PhabricatorPlugin.ARCANIST_PATH, "arc");
final boolean uberallsConfigured = !CommonUtils.isBlank(uberalls.getBaseURL());
final String diffID = environment.get(PhabricatorPlugin.DIFFERENTIAL_ID_FIELD);

boolean uberallsConfigured = !CommonUtils.isBlank(uberalls.getBaseURL());

String diffID = environment.get(PhabricatorPlugin.DIFFERENTIAL_ID_FIELD);
// Handle non-differential build invocations.
if (CommonUtils.isBlank(diffID)) {
if (needsDecoration) {
build.addAction(PhabricatorPostbuildAction.createShortText("master", null));
}
if (uberallsEnabled && coverage != null) {
if (!uberallsConfigured) {
logger.info("uberalls", "enabled but no server configured. skipping.");
} else {
String currentSHA = environment.get("GIT_COMMIT");
CodeCoverageMetrics codeCoverageMetrics = new CodeCoverageMetrics(coverage);

if (!CommonUtils.isBlank(currentSHA) && codeCoverageMetrics.isValid()) {
logger.info("uberalls", "sending coverage report for " + currentSHA + " as " +
codeCoverageMetrics.toString());
uberalls.recordCoverage(currentSHA, codeCoverageMetrics);
} else {
logger.info("uberalls", "no line coverage available for " + currentSHA);
}
}
build.addAction(PhabricatorPostbuildAction.createShortText(branch, null));
}
return this.ignoreBuild(logger.getStream(), "No differential ID found.");

NonDifferentialBuildTask nonDifferentialBuildTask = new NonDifferentialBuildTask(logger, uberalls,
new CodeCoverageMetrics(coverage), uberallsEnabled,
environment.get("GIT_COMMIT"));

// Ignore the result.
nonDifferentialBuildTask.run();
return true;
}

LauncherFactory starter = new LauncherFactory(launcher, environment, listener.getLogger(), build.getWorkspace());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.uber.jenkins.phabricator.tasks;

import com.uber.jenkins.phabricator.CodeCoverageMetrics;
import com.uber.jenkins.phabricator.uberalls.UberallsClient;
import com.uber.jenkins.phabricator.utils.CommonUtils;
import com.uber.jenkins.phabricator.utils.Logger;
import hudson.plugins.cobertura.targets.CoverageResult;

/**
* Generic build task.
*/
public class NonDifferentialBuildTask extends Task {

protected UberallsClient uberallsClient;
protected CodeCoverageMetrics codeCoverageMetrics;
protected boolean uberallsEnabled;
protected String commitSha;

/**
* GenericBuildTask constructor.
* @param logger The logger.
* @param uberallsClient The uberalls client.
* @param codeCoverageMetrics The coverage metrics.
* @param uberallsEnabled Whether uberalls is enabled.
* @param commitSha The commit sha.
*/
public NonDifferentialBuildTask(Logger logger, UberallsClient uberallsClient,
CodeCoverageMetrics codeCoverageMetrics, boolean uberallsEnabled,
String commitSha) {
super(logger);
this.uberallsClient = uberallsClient;
this.codeCoverageMetrics = codeCoverageMetrics;
this.uberallsEnabled = uberallsEnabled;
this.commitSha = commitSha;
}

/**
* {@inheritDoc}
*/
@Override
protected String getTag() {
return "non-differential";
}

/**
* {@inheritDoc}
*/
@Override
protected void setup() {
// Handle bad input.
if (codeCoverageMetrics == null || !codeCoverageMetrics.isValid()) {
info("Coverage result not found. Ignoring build.");
result = Result.IGNORED;
} else if (!uberallsEnabled || CommonUtils.isBlank(uberallsClient.getBaseURL())) {
info("Uberalls not configured. Skipping build.");
result = Result.SKIPPED;
}
}

/**
* {@inheritDoc}
*/
@Override
protected void execute() {
if (result == Result.UNKNWON) {
if (!CommonUtils.isBlank(commitSha)) {
info(String.format("Sending coverage result for %s as %s", commitSha,
codeCoverageMetrics.toString()));
result = uberallsClient.recordCoverage(commitSha, codeCoverageMetrics) ?
Result.SUCCESS : Result.FAILURE;
} else {
info("No line coverage found. Ignoring build.");
result = Result.IGNORED;
}
}
}

/**
* {@inheritDoc}
*/
@Override
protected void tearDown() {
// Do nothing.
}
}
91 changes: 91 additions & 0 deletions src/main/java/com/uber/jenkins/phabricator/tasks/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.uber.jenkins.phabricator.tasks;

import com.uber.jenkins.phabricator.utils.Logger;

/**
* Base task for all operations in the phabricator-jenkins plugin.
*/
public abstract class Task {

/**
* Task results.
*/
public enum Result {
SUCCESS,
FAILURE,
IGNORED, // For incorrect input.
SKIPPED, // For incorrect configuration.
UNKNWON
};

protected Result result = Result.UNKNWON;
private Logger logger;

/**
* Task constructor.
* @param logger The logger where logs go to.
*/
public Task(Logger logger) {
this.logger = logger;
}

/**
* Runs the task workflow.
*/
public Result run() {
setup();
execute();
tearDown();

return result;
}

/**
* Logs the message.
* @param message The message to log.
*/
protected void info(String message) {
logger.info(getTag(), message);
}

/**
* Gets the task's tag.
* @return A string representation of this task's tag.
*/
protected abstract String getTag();

/**
* Sets up the environment before task execution.
*/
protected abstract void setup();

/**
* Executes the task workflow.
*/
protected abstract void execute();

/**
* Tears down after task execution.
*/
protected abstract void tearDown();
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public CodeCoverageMetrics getParentCoverage(Differential differential) {
}

public boolean recordCoverage(String sha, CodeCoverageMetrics codeCoverageMetrics) {
if (codeCoverageMetrics.isValid()) {
if (codeCoverageMetrics != null && codeCoverageMetrics.isValid()) {
JSONObject params = new JSONObject();
params.put("sha", sha);
params.put("branch", branch);
Expand Down
Loading

0 comments on commit 77abc19

Please sign in to comment.