提交 4cb7414e 编写于 作者: J Jesse Glick

fromExternalizableId should just return null, not throw IAE, if the job has...

fromExternalizableId should just return null, not throw IAE, if the job has since been renamed/move/deleted.
(It could already return null if the run has since been deleted; #1264 failed to mark this properly.)
Reserving IAE for inputs that getExternalizableId could not actually have produced,
and making sure to also throw this in case of non-numeric text after the hash.
Adding Javadoc for both methods.
上级 07fb00e0
......@@ -2281,21 +2281,40 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
return env;
}
/**
* Produces an identifier for this run unique in the system.
* @return the {@link Job#getFullName}, then {@code #}, then {@link #getNumber}
* @see #fromExternalizableId
*/
public @Nonnull String getExternalizableId() {
return project.getFullName() + "#" + getNumber();
}
public @Nonnull static Run<?,?> fromExternalizableId(String id) {
/**
* Tries to find a run from an persisted identifier.
* @param id as produced by {@link #getExternalizableId}
* @return the same run, or null if the job or run was not found
* @throws IllegalArgumentException if the ID is malformed
*/
public @CheckForNull static Run<?,?> fromExternalizableId(String id) throws IllegalArgumentException {
int hash = id.lastIndexOf('#');
if (hash <= 0) {
throw new IllegalArgumentException("Invalid id");
}
String jobName = id.substring(0, hash);
int number = Integer.parseInt(id.substring(hash + 1));
Job<?,?> job = Jenkins.getInstance().getItemByFullName(jobName, Job.class);
int number;
try {
number = Integer.parseInt(id.substring(hash + 1));
} catch (NumberFormatException x) {
throw new IllegalArgumentException(x);
}
Jenkins j = Jenkins.getInstance();
if (j == null) {
return null;
}
Job<?,?> job = j.getItemByFullName(jobName, Job.class);
if (job == null) {
throw new IllegalArgumentException("no such job " + jobName);
return null;
}
return job.getBuildByNumber(number);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册