Example #1
0
 public void TryOpen()
 {
     try
     {
         workItem.Open();
     }
     catch (Exception e)
     {
         MiscHelpers.LogMessage(String.Format("Unable to open work item '{0}'\nException: {1}", workItem.Id.ToString(), e.Message), true);
     }
 }
        /// <param name="values">The list of fields and their desired values to apply to the work item</param>
        /// <returns>Work item ID of the newly created work item</returns>
        public int CreateWorkItem(Dictionary<string, string> values)
        {
            if (values == null)
            {
                throw new ArgumentNullException("values", "Must supply field values when creating new work item");
            }

            //create a work item
            var workItemType = _tfsProject.WorkItemTypes[_config.TfsServerConfig.WorkItemTemplate];
            var workItem = new WorkItem(workItemType);

            workItem.Open();
            foreach (var key in values.Keys)
            {
                TryApplyFieldValue(workItem, key, values[key]);
            }

            // Workaround for TFS issue - if you change the "Assigned To" field, and then you change the "Activated by" field, the "Assigned To" field reverts
            // to its original setting. To prevent that, we reapply the "Assigned To" field in case it's in the list of values to change.
            if (values.ContainsKey(AssignedToFieldKey))
            {
                TryApplyFieldValue(workItem, AssignedToFieldKey, values[AssignedToFieldKey]);
            }

            ValidateAndSaveWorkItem(workItem);

            CacheWorkItem(workItem);
            return workItem.Id;
        }
        //save final state transition and set final reason.
        private bool ChangeWorkItemStatus(WorkItem workItem, string orginalSourceState, string destState, string reason)
        {
            //Try to save the new state.  If that fails then we also go back to the orginal state.
            try
            {
                workItem.Open();
                workItem.Fields["State"].Value = destState;
                workItem.Fields["Reason"].Value = reason;

                ArrayList list = workItem.Validate();
                workItem.Save();

                return true;
            }
            catch (Exception)
            {
                logger.WarnFormat("Failed to save state for workItem: {0}  type:'{1}' state from '{2}' to '{3}' =>rolling workItem status to original state '{4}'",
                    workItem.Id, workItem.Type.Name, orginalSourceState, destState, orginalSourceState);
                //Revert back to the original value.
                workItem.Fields["State"].Value = orginalSourceState;
                return false;
            }
        }
        public void updateToLatestStatus(WorkItem oldWorkItem, WorkItem newWorkItem)
        {
            Queue<string> result = new Queue<string>();
            string previousState = null;
            string originalState = (string)newWorkItem.Fields["State"].Value;
            string sourceState = (string)oldWorkItem.Fields["State"].Value;
            string sourceFinalReason = (string)oldWorkItem.Fields["Reason"].Value;

            //try to change the status directly
            newWorkItem.Open();
            newWorkItem.Fields["State"].Value = oldWorkItem.Fields["State"].Value;
            //System.Diagnostics.Debug.WriteLine(newWorkItem.Type.Name + "      " + newWorkItem.Fields["State"].Value);

            //if status can't be changed directly...
            if (newWorkItem.Fields["State"].Status != FieldStatus.Valid)
            {
                //get the state transition history of the source work item.
                foreach (Revision revision in oldWorkItem.Revisions)
                {
                    // Get Status
                    if (!revision.Fields["State"].Value.Equals(previousState))
                    {
                        previousState = revision.Fields["State"].Value.ToString();
                        result.Enqueue(previousState);
                    }
                }

                int i = 1;
                previousState = originalState;
                //traverse new work item through old work items's transition states
                foreach (String currentStatus in result)
                {
                    bool success = false;
                    if (i != result.Count)
                    {
                        success = ChangeWorkItemStatus(newWorkItem, previousState, currentStatus);
                        previousState = currentStatus;
                    }
                    else
                    {
                        success = ChangeWorkItemStatus(newWorkItem, previousState, currentStatus, sourceFinalReason);
                    }
                    i++;
                    // If we could not do the incremental state change then we are done.  We will have to go back to the orginal...
                    if (!success)
                        break;
                }
            }
            else
            {
                // Just save it off if we can.
                bool success = ChangeWorkItemStatus(newWorkItem, originalState, sourceState);
            }
        }
Example #5
0
 /// <summary>
 /// Opens this work item for modification.
 /// </summary>
 public void Open()
 {
     _item.Open();
 }
Example #6
0
 public WorkItem OpenWorkItem(WorkItem _wi)
 {
     _wi.Open();
     return _wi;
 }