private void setChangeSets(BuildRun buildRun, BuildInfo info) {
    for (VcsModification change : info.getChanges()) {
      // See if we have this ChangeSet in the system.
      ChangeSetFilter filter = new ChangeSetFilter();
      String id = change.getId();

      filter.reference.add(id);
      Collection<ChangeSet> changeSetList = config.getV1Instance().get().changeSets(filter);
      if (changeSetList.isEmpty()) {
        // We don't have one yet. Create one.
        StringBuilder name = new StringBuilder();
        name.append('\'');
        name.append(change.getUserName());
        if (change.getDate() != null) {
          name.append("\' on \'");
          name.append(new DB.DateTime(change.getDate()));
        }
        name.append('\'');

        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put("Description", change.getComment());
        ChangeSet changeSet =
            config.getV1Instance().create().changeSet(name.toString(), id, attributes);

        changeSetList = new ArrayList<ChangeSet>(1);
        changeSetList.add(changeSet);
      }

      Set<PrimaryWorkitem> workitems = determineWorkitems(change.getComment());
      associateWithBuildRun(buildRun, changeSetList, workitems);
    }
  }
  private boolean isBuildExist(BuildProject buildProject, BuildInfo info) {
    BuildRunFilter filter = new BuildRunFilter();
    filter.references.add(Long.toString(info.getBuildId()));
    filter.name.add(getBuildName(info));
    filter.buildProjects.add(buildProject);

    Collection<BuildRun> buildRuns = config.getV1Instance().get().buildRuns(filter);
    return buildRuns != null && buildRuns.size() != 0;
  }
  /**
   * Find the first BuildProject where the Reference matches the projectName.
   *
   * @param info information about build run
   * @return V1 representation of the project if match; otherwise - null.
   */
  private BuildProject getBuildProject(BuildInfo info) {
    BuildProjectFilter filter = new BuildProjectFilter();

    filter.references.add(info.getProjectName());
    Collection<BuildProject> projects = config.getV1Instance().get().buildProjects(filter);
    if (projects.isEmpty()) {
      return null;
    }
    return projects.iterator().next();
  }
 /** Adds to the VersionOne BuildRun and ChangesSet. */
 public Result submitBuildRun(final BuildInfo info) {
   // cancel notification if connection is not valid
   if (!config.isConnectionValid()) {
     return Result.FAIL_CONNECTION;
   }
   final BuildProject buildProject = getBuildProject(info);
   if (buildProject == null) {
     return Result.FAIL_NO_BUILDPROJECT;
   }
   if (isBuildExist(buildProject, info)) {
     return Result.FAIL_DUPLICATE;
   }
   final BuildRun buildRun = createBuildRun(buildProject, info);
   if (info.hasChanges()) {
     setChangeSets(buildRun, info);
   }
   return Result.SUCCESS;
 }
  /**
   * Resolve a check-in comment identifier to a PrimaryWorkitem. if the reference matches a
   * SecondaryWorkitem, we need to navigate to the parent.
   *
   * @param reference The identifier in the check-in comment.
   * @return A collection of matching PrimaryWorkitems.
   */
  private List<PrimaryWorkitem> getPrimaryWorkitemsByReference(String reference) {
    List<PrimaryWorkitem> result = new ArrayList<PrimaryWorkitem>();

    WorkitemFilter filter = new WorkitemFilter();
    filter.find.setSearchString(reference);
    filter.find.fields.add(config.referenceField);
    Collection<Workitem> workitems = config.getV1Instance().get().workitems(filter);
    for (Workitem workitem : workitems) {
      if (workitem instanceof PrimaryWorkitem) {
        result.add((PrimaryWorkitem) workitem);
      } else if (workitem instanceof SecondaryWorkitem) {
        result.add(((SecondaryWorkitem) workitem).getParent());
      } else {
        throw new RuntimeException("Found unexpected Workitem type: " + workitem.getClass());
      }
    }

    return result;
  }
  /**
   * Create short information about workitem by display id
   *
   * @param id idsplay id of workitem
   * @return short information about workitem
   */
  public WorkitemData getWorkitemData(String id) {
    Workitem workitem = config.getV1Instance().get().workitemByDisplayID(id);

    return new WorkitemData(workitem, config.url);
  }