private void associateWithBuildRun(
      BuildRun buildRun, Collection<ChangeSet> changeSets, Set<PrimaryWorkitem> workitems) {
    for (ChangeSet changeSet : changeSets) {
      buildRun.getChangeSets().add(changeSet);
      for (PrimaryWorkitem workitem : workitems) {
        if (workitem.isClosed()) {
          logger.println(MessagesRes.workitemClosedCannotAttachData(workitem.getDisplayID()));
          continue;
        }

        final Collection<BuildRun> completedIn = workitem.getCompletedIn();
        final List<BuildRun> toRemove = new ArrayList<BuildRun>(completedIn.size());

        changeSet.getPrimaryWorkitems().add(workitem);

        for (BuildRun otherRun : completedIn) {
          if (otherRun.getBuildProject().equals(buildRun.getBuildProject())) {
            toRemove.add(otherRun);
          }
        }

        for (BuildRun buildRunDel : toRemove) {
          completedIn.remove(buildRunDel);
        }

        completedIn.add(buildRun);
      }
    }
  }
  private static BuildRun createBuildRun(BuildProject buildProject, BuildInfo info) {
    // Generate the BuildRun instance to be saved to the recipient
    BuildRun run =
        buildProject.createBuildRun(getBuildName(info), new DB.DateTime(info.getStartTime()));

    run.setElapsed((double) info.getElapsedTime());
    run.setReference(Long.toString(info.getBuildId()));
    run.getSource().setCurrentValue(getSourceName(info.isForced()));
    run.getStatus().setCurrentValue(getStatusName(info.isSuccessful()));

    if (info.hasChanges()) {
      run.setDescription(getModificationDescription(info.getChanges()));
    }
    run.save();

    run.createLink("Build Report", info.getUrl(), true);
    return run;
  }