Esempio n. 1
0
        /// <summary>
        /// Scans all assemblies for <see cref="WorkItemSummary"/> derived classes. This should be called once at app startup.
        /// </summary>
        public static void Scan()
        {
            _hasScanned = true;             // avoid a StackOverflow: instance.WorkItemType (below) calls Scan() via the UserContext
            _types      = new Dictionary <string, Type>();

            Type summaryType = typeof(WorkItemSummary);

            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (Assembly assembly in assemblies)
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsSubclassOf(summaryType))
                    {
                        WorkItemSummary instance = (WorkItemSummary)type.Assembly.CreateInstance(type.FullName);

                        if (instance.WorkItemType == null)
                        {
                            throw new NullReferenceException(string.Format("The {0} provided has a null WorkItemType. This is typically because its WIQLTypeName does not match the name assigned in TFS.", instance));
                        }

                        _types.Add(instance.WorkItemType.Name, type);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Saves the specified <see cref="WorkItemSummary"/> to TFS, converting it to a <see cref="WorkItem"/> first.
        /// </summary>
        /// <exception cref="SaveException">Thrown if a TFS server-based error occurs with the save.</exception>
        public void Save(WorkItemSummary summary)
        {
            WorkItem item = summary.ToWorkItem();

            try
            {
                item.Save();
                summary.Id = item.Id;
            }
            catch (ValidationException e)
            {
                StringBuilder builder = new StringBuilder();
                foreach (Field field in item.Fields)
                {
                    if (field.Status != FieldStatus.Valid)
                    {
                        builder.AppendLine(string.Format("The '{0}' field has the status {1}", field.Name, field.Status));

                        if (field.Status == FieldStatus.InvalidNotOldValue)
                        {
                            builder.Append(" (this usually means the field is now read only)");
                        }
                    }
                }

                throw new SaveException(string.Format("Save failed for '{0}' ({1}).\nFields: {2}", item.Title, e.Message, builder), e);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Returns the previous value of the field for the revision number provided.
        /// </summary>
        public static string GetFieldValueForRevision(this HtmlHelper helper, WorkItemSummary model, string fieldName, int revisionNumber)
        {
            if (revisionNumber > 0 && model.Revisions[revisionNumber - 1].Fields[fieldName].Value != null)
            {
                return(model.Revisions[revisionNumber - 1].Fields[fieldName].Value.ToString());
            }

            return("");
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a CSV string (including escaping for commas) from the provided <see cref="WorkItemSummary"/>
        /// </summary>
        public static string ToCsv(this WorkItemSummary summary)
        {
            StringBuilder builder = new StringBuilder();

            // Title
            string title = EscapeQuotes(summary.Title);

            if (title.IndexOf(",") > -1)
            {
                builder.Append("\"" + title + "\"");
            }
            else
            {
                builder.Append(title);
            }

            builder.Append(",");

            // ID
            builder.Append(summary.Id);
            builder.Append(",");

            // Assigned to
            string assignedTo = EscapeQuotes(summary.AssignedTo);

            if (assignedTo.IndexOf(",") > -1)
            {
                builder.Append("\"" + assignedTo + "\"");
            }
            else
            {
                builder.Append(assignedTo);
            }

            builder.Append(",");

            // Created on
            builder.Append(summary.CreatedDate.ToString("ddd dd MMM yyyy"));
            builder.Append(",");

            // State
            string state = EscapeQuotes(summary.State);

            if (state.IndexOf(",") > -1)
            {
                builder.Append("\"" + state + "\"");
            }
            else
            {
                builder.Append(state);
            }

            builder.Append(",");

            return(builder.AppendLine().ToString());
        }
Esempio n. 5
0
        /// <summary>
        /// Retrieves a <see cref="WorkItemSummary"/> for the given ID.
        /// </summary>
        public WorkItemSummary ItemById(int id)
        {
            WorkItem item = UserContext.Current.WorkItemStore.GetWorkItem(id);

            WorkItemSummary summary = WorkItemSummaryFactory.GetForType(item.Type);

            summary.FromWorkItem(item);
            summary.PopulateAllowedValues(item);
            summary.IsNew = false;

            return(summary);
        }
Esempio n. 6
0
        /// <summary>
        /// Converts a <see cref="WorkItemCollection"/> to an <see cref="IEnuermable`WorkItemSummary"/>
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static IEnumerable <WorkItemSummary> ToSummaries(this WorkItemCollection collection)
        {
            List <WorkItemSummary> list = new List <WorkItemSummary>();

            foreach (WorkItem item in collection)
            {
                WorkItemSummary summary = WorkItemSummaryFactory.GetForType(item.Type);
                summary.FromWorkItem(item);
                list.Add(summary);
            }

            return(list);
        }
Esempio n. 7
0
        /// <summary>
        /// Populates the core fields of a <see cref="WorkItem"/> from the values of the provided <see cref="WorkItemSummary"/>
        /// </summary>
        public static void FillCoreFields(this WorkItem item, WorkItemSummary summary)
        {
            item.Title       = summary.Title;
            item.Description = summary.Description;             // TODO: change to appropriate Field
            item.Fields["Assigned To"].Value = summary.AssignedTo;
            item.Fields["State"].Value       = summary.State;
            item.IterationPath = summary.IterationPath;
            item.AreaPath      = summary.AreaPath;

            if (item.Fields.Contains("Reason"))
            {
                item.Fields["Reason"].Value = summary.Reason;
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Populates the core fields of a <see cref="WorkItem"/> from the values of the provided <see cref="WorkItemSummary"/>
        /// </summary>
        public static void FillCoreFields(this WorkItem item, WorkItemSummary summary)
        {
            item.Title = summary.Title;
            item.Description = summary.Description; // TODO: change to appropriate Field
            item.Fields["Assigned To"].Value = summary.AssignedTo;
            item.Fields["State"].Value = summary.State;
            item.IterationPath = summary.IterationPath;
            item.AreaPath = summary.AreaPath;

            if (item.Fields.Contains("Reason"))
            {
                item.Fields["Reason"].Value = summary.Reason;
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Changes the state of a work item to 'resolved'.
        /// </summary>
        /// <param name="id">The id of the work item to change the state of.</param>
        public virtual void Resolve(int id)
        {
            QueryManager queryManager = new QueryManager();

            try
            {
                WorkItemSummary summary = queryManager.ItemById(id);
                summary.State = "Resolved";
                Save(summary);
            }
            catch (Exception ex)
            {
                Log.Warn(ex, "An exception occured resolving the work item {0}", id);
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Creates a new <see cref="WorkItem"/> from a <see cref="WorkItemSummary"/> instance, populating its core fields with
        /// the data from the <see cref="WorkItemSummary"/> object.
        /// </summary>
        protected virtual WorkItem CreateWorkItem(WorkItemType type, WorkItemSummary summary)
        {
            WorkItem item = new WorkItem(type);

            summary.CreatedBy  = UserContext.Current.Name;
            summary.AssignedTo = UserContext.Current.Name;
            summary.Fields     = item.Fields;
            summary.IsNew      = true;

            // Default area + iteration
            summary.AreaPath      = UserContext.Current.Settings.AreaPath;
            summary.IterationPath = UserContext.Current.Settings.IterationPath;

            summary.PopulateAllowedValues(item);
            summary.State  = summary.ValidStates[0];
            summary.Reason = summary.ValidReasons[0];

            return(item);
        }
Esempio n. 11
0
        /// <summary>
        /// Creates a new <see cref="WorkItem"/> from a <see cref="WorkItemSummary"/> instance, populating its core fields with 
        /// the data from the <see cref="WorkItemSummary"/> object.
        /// </summary>
        protected virtual WorkItem CreateWorkItem(WorkItemType type, WorkItemSummary summary)
        {
            WorkItem item = new WorkItem(type);

            summary.CreatedBy = UserContext.Current.Name;
            summary.AssignedTo = UserContext.Current.Name;
            summary.Fields = item.Fields;
            summary.IsNew = true;

            // Default area + iteration
            summary.AreaPath = UserContext.Current.Settings.AreaPath;
            summary.IterationPath = UserContext.Current.Settings.IterationPath;

            summary.PopulateAllowedValues(item);
            summary.State = summary.ValidStates[0];
            summary.Reason = summary.ValidReasons[0];

            return item;
        }
Esempio n. 12
0
        /// <summary>
        /// Saves the specified <see cref="WorkItemSummary"/> to TFS, converting it to a <see cref="WorkItem"/> first.
        /// </summary>
        /// <exception cref="SaveException">Thrown if a TFS server-based error occurs with the save.</exception>
        public void Save(WorkItemSummary summary)
        {
            WorkItem item = summary.ToWorkItem();

            try
            {
                item.Save();
                summary.Id = item.Id;
            }
            catch (ValidationException e)
            {
                StringBuilder builder = new StringBuilder();
                foreach (Field field in item.Fields)
                {
                    if (field.Status != FieldStatus.Valid)
                    {
                        builder.AppendLine(string.Format("The '{0}' field has the status {1}", field.Name, field.Status));

                        if (field.Status == FieldStatus.InvalidNotOldValue)
                            builder.Append(" (this usually means the field is now read only)");
                    }
                }

                throw new SaveException(string.Format("Save failed for '{0}' ({1}).\nFields: {2}", item.Title,e.Message,builder), e);
            }
        }