forked from uber-archive/phabricator-jenkins-plugin
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send harbormaster failure on arc patch failure
Fixes uber-archive#63
- Loading branch information
1 parent
aa30e77
commit 2a089b2
Showing
4 changed files
with
155 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/com/uber/jenkins/phabricator/tasks/SendHarbormasterResultTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.uber.jenkins.phabricator.tasks; | ||
|
||
import com.uber.jenkins.phabricator.conduit.ConduitAPIException; | ||
import com.uber.jenkins.phabricator.conduit.DifferentialClient; | ||
import com.uber.jenkins.phabricator.utils.Logger; | ||
import net.sf.json.JSONNull; | ||
import net.sf.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
|
||
public class SendHarbormasterResultTask extends Task { | ||
|
||
private final DifferentialClient diffClient; | ||
private final String phid; | ||
private final boolean harbormasterSuccess; | ||
|
||
public SendHarbormasterResultTask(Logger logger, DifferentialClient diffClient, String phid, boolean harbormasterSuccess) { | ||
super(logger); | ||
this.diffClient = diffClient; | ||
this.phid = phid; | ||
this.harbormasterSuccess = harbormasterSuccess; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
protected String getTag() { | ||
return "send-harbormaster-result"; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
protected void setup() { | ||
// Do nothing | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
protected void execute() { | ||
try { | ||
JSONObject result = diffClient.sendHarbormasterMessage(phid, harbormasterSuccess); | ||
|
||
if (result.containsKey("error_info") && !(result.get("error_info") instanceof JSONNull)) { | ||
info(String.format("Error from Harbormaster: %s", result.getString("error_info"))); | ||
this.result = Result.FAILURE; | ||
} else { | ||
this.result = Result.SUCCESS; | ||
} | ||
} catch (ConduitAPIException e) { | ||
e.printStackTrace(); | ||
this.result = Result.FAILURE; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
this.result = Result.FAILURE; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
protected void tearDown() { | ||
// Do nothing | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/test/java/com/uber/jenkins/phabricator/tasks/SendHarbormasterResultTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.uber.jenkins.phabricator.tasks; | ||
|
||
import com.uber.jenkins.phabricator.conduit.ConduitAPIException; | ||
import com.uber.jenkins.phabricator.conduit.DifferentialClient; | ||
import com.uber.jenkins.phabricator.utils.TestUtils; | ||
import net.sf.json.JSONObject; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class SendHarbormasterResultTaskTest { | ||
private final JSONObject validResponse = new JSONObject(); | ||
private DifferentialClient diffClient; | ||
|
||
@Before | ||
public void setUp() { | ||
diffClient = mock(DifferentialClient.class); | ||
} | ||
|
||
@Test | ||
public void testSuccessfulHarbormaster() throws IOException, ConduitAPIException { | ||
when(diffClient.sendHarbormasterMessage(TestUtils.TEST_PHID, false)).thenReturn(validResponse); | ||
|
||
assertEquals(Task.Result.SUCCESS, getResult()); | ||
} | ||
|
||
@Test | ||
public void testErrorInfoResponse() throws IOException, ConduitAPIException { | ||
JSONObject errorResponse = new JSONObject(); | ||
errorResponse.put("error_info", "i'm having a bad day"); | ||
when(diffClient.sendHarbormasterMessage(TestUtils.TEST_PHID, false)).thenReturn(errorResponse); | ||
|
||
assertEquals(Task.Result.FAILURE, getResult()); | ||
} | ||
|
||
@Test | ||
public void testConduitAPIFailure() throws IOException, ConduitAPIException { | ||
when(diffClient.sendHarbormasterMessage(TestUtils.TEST_PHID, false)).thenThrow(ConduitAPIException.class); | ||
|
||
assertEquals(Task.Result.FAILURE, getResult()); | ||
} | ||
|
||
@Test | ||
public void testIOExceptionFailure() throws IOException, ConduitAPIException { | ||
when(diffClient.sendHarbormasterMessage(TestUtils.TEST_PHID, false)).thenThrow(IOException.class); | ||
|
||
assertEquals(Task.Result.FAILURE, getResult()); | ||
} | ||
|
||
private Task.Result getResult() { | ||
return new SendHarbormasterResultTask( | ||
TestUtils.getDefaultLogger(), | ||
diffClient, | ||
TestUtils.TEST_PHID, | ||
false | ||
).run(); | ||
} | ||
} |