Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing build result from string #1

Merged
merged 1 commit into from
Jan 24, 2022
Merged
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
19 changes: 15 additions & 4 deletions src/main/java/hudson/model/ExternalRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.util.zip.GZIPInputStream;
import java.util.Scanner;

import static javax.xml.stream.XMLStreamConstants.*;

Expand Down Expand Up @@ -85,6 +86,18 @@ private void setCharset(String c) { // JENKINS-14107
charset = c;
}

private Result parseResult(String text) {
Result result;
Scanner sc = new Scanner(text);
if (sc.hasNextInt()) {
Comment on lines +91 to +92
Copy link
Member

Choose a reason for hiding this comment

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

Probably better to use Integer.parseInt + catch (NumberFormatException but OK.

result = sc.nextInt()==0 ? Result.SUCCESS : Result.FAILURE;
}
else {
result = Result.fromString(text);
}
return result;
}

/**
* Instead of performing a build, accept the log and the return code
* from a remote machine.
Expand Down Expand Up @@ -130,11 +143,9 @@ public Result run(BuildListener listener) throws Exception {
if(type== CHARACTERS || type== CDATA)
logger.print(p.getText());
}
p.nextTag(); // get to <result>



Result r = Integer.parseInt(elementText(p))==0?Result.SUCCESS:Result.FAILURE;
p.nextTag(); // get to <result>
Result r = parseResult(elementText(p));

do {
p.nextTag();
Expand Down
21 changes: 18 additions & 3 deletions src/test/java/hudson/model/ExternalRunTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,29 @@ public void test1() throws Exception {
b.acceptRemoteSubmission(new StringReader(
"<run><log content-encoding='UTF-8'>AAAAAAAA</log><result>0</result><duration>100</duration></run>"
));
assertEquals(b.getResult(),Result.SUCCESS);
assertEquals(Result.SUCCESS,b.getResult());
assertEquals(b.getDuration(),100);

b = p.newBuild();
b.acceptRemoteSubmission(new StringReader(
"<run><log content-encoding='UTF-8'>AAAAAAAA</log><result>1</result>"
"<run><log content-encoding='UTF-8'>AAAAAAAA</log><result>1</result></run>"
));
assertEquals(b.getResult(),Result.FAILURE);
assertEquals(Result.FAILURE,b.getResult());
}

public void testStringResult() throws Exception {
ExternalJob p = jenkins.createProject(ExternalJob.class, createUniqueProjectName());
ExternalRun b = p.newBuild();
b.acceptRemoteSubmission(new StringReader(
"<run><log/><result>SUCCESS</result></run>"
));
assertEquals(Result.SUCCESS,b.getResult());

b = p.newBuild();
b.acceptRemoteSubmission(new StringReader(
"<run><log/><result>ABORTED</result></run>"
));
assertEquals(Result.ABORTED,b.getResult());
}

@Bug(11592)
Expand Down