///<summary>
 /// Create a new Build Run in the given Build Project with a name and date
 ///</summary>
 ///<param name="buildProject"></param>
 ///<param name="name"></param>
 ///<param name="date"></param>
 ///<param name="attributes">Required attributes.</param>
 ///<returns>A newly minted Build Run that exists in the VersionOne system.</returns>
 public BuildRun BuildRun(BuildProject buildProject, string name, DateTime date,
     IDictionary<string, object> attributes) {
     var buildRun = new BuildRun(instance) {
         Name = name, 
         BuildProject = buildProject, 
         Date = date
     };
     AddAttributes(buildRun, attributes);
     buildRun.Save();
     return buildRun;
 }
        /// <summary>
        /// Associate this BuildRun with one or more ChangeSets. If there is no ChangeSet, then create one.
        /// </summary>
        /// <param name="run">This BuildRun.</param>
        /// <param name="changes">The changes as reported by CCNet.</param>
        private void SetChangeSets(BuildRun run, IEnumerable<ChangeInfo> changes) {
            foreach(ChangeInfo change in changes) {
                // See if we have this ChangeSet in the system.
                ChangeSetFilter filter = new ChangeSetFilter();
                filter.Reference.Add(change.Number.ToString());
                ICollection<ChangeSet> changeSets = Instance.Get.ChangeSets(filter);
                if(changeSets.Count == 0) {
                    // We don't have one yet. Create one.
                    string name = string.Format("{0} on {1}", change.User, change.Stamp);
                    ChangeSet changeSet = Instance.Create.ChangeSet(name, change.Number.ToString());
                    changeSet.Description = change.Comment;
                    changeSets = new List<ChangeSet>();
                    changeSets.Add(changeSet);
                    Trace("Created new ChangeSet: {0}", name);
                }

                IEnumerable<PrimaryWorkitem> workitems = DetermineWorkitems(change);

                // Associate with our new BuildRun.
                foreach(ChangeSet changeSet in changeSets) {
                    run.ChangeSets.Add(changeSet);
                    foreach(PrimaryWorkitem workitem in workitems) {
                        changeSet.PrimaryWorkitems.Add(workitem);
                        //workitem.CompletedIn.Clear();

                        List<BuildRun> toRemove = new List<BuildRun>();
                        foreach (BuildRun otherRun in workitem.CompletedIn) {
                            if (otherRun.BuildProject == run.BuildProject) {
                                toRemove.Add(otherRun);
                            }
                        }

                        foreach (BuildRun buildRun in toRemove) {
                            workitem.CompletedIn.Remove(buildRun);
                        }

                        workitem.CompletedIn.Add(run);
                        Trace("Associated ChangeSet with PrimaryWorkitem: {0}", workitem.ID);
                    }
                }
            }
        }