Skip to content

getBuildByNumber() returns null when build exists #489

Open
@zhantt1

Description

Jenkins version: Jenkins 2.277.1
Dependency Version: 0.3.8

I tried to get an old job number using getBuildByNumber():

Build build = jenkins.getJob(deployServicesJob).getBuildByNumber(buildNumber);
BuildWithDetails buildWithDetails = build.details();

Calling build.details(); throws:
java.lang.NullPointerException: Cannot invoke "com.offbytwo.jenkins.model.Build.details()" because "build" is null

The job has 200+ builds, which I suspect may be the problem if Jenkins only returns at most 100 jobs.

The build is accessible using getAllBuilds() so my workaround is to use that method instead:

List<Build> allBuilds = jenkins.getJob(deployServicesJob).getAllBuilds();
Optional<Build> optional = allBuilds.stream().filter(b -> b.getNumber() == buildNumber).findFirst();
if (optional.isEmpty()) {
   throw new IllegalArgumentException("Build not found");
}
Build build = optional.get();

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions