private WorkItem GetDummyWorkItem(WorkItemType type = null) { if (_targetDummyWorkItem == null) { if (_targetProject.WorkItemTypes.Count == 0) { return(null); } if (type == null) { type = _targetProject.WorkItemTypes["Task"]; } if (type == null) { type = _targetProject.WorkItemTypes[0]; } _targetDummyWorkItem = type.NewWorkItem(); _targetDummyWorkItem.Title = TargetDummyWorkItemTitle; _targetDummyWorkItem.Save(); Log.LogDebug("EmbededImagesRepairEnricher: Dummy workitem {id} created on the target collection.", _targetDummyWorkItem.Id); //_targetProject.Store.DestroyWorkItems(new List<int> { _targetDummyWorkItem.Id }); } return(_targetDummyWorkItem); }
public WorkItemState(WorkItemType wit) { this.wit = wit; selectedInProgressState = selectedApprovedState = initialState = wit.NewWorkItem().State; closedStates = new List <string> { "Done", "Inactive", "Closed", "Completed", "Rejected", "Removed" }; witd = wit.Export(true); gatherNextStates(); findDonePath(); }
/// <summary> /// Returns all allowed values for the provided work item type's field. /// </summary> /// <param name="type">The work item type create. Use null for an existing work item.</param> /// <param name="id">An id of an existing work item. Use 0 for a new work item.</param> /// <param name="fieldName">The field name to retrieve the allowed values for.</param> public virtual IEnumerable <string> GetAllowedValuesForState(string fieldName, WorkItemType type, int id = 0, string newState = "") { // This doesn't work as you can't create a new work item with a resolved state WorkItem item; if (id == 0) { item = type.NewWorkItem(); item.Title = "temp"; } else { item = ItemById(id).ToWorkItem(); item.State = newState; } ArrayList results = item.Validate(); if (results.Count == 0) { return(item.Fields[fieldName].AllowedValues.ToList()); } else { List <string> resultsList = new List <string>(); foreach (object result in results) { Field field = result as Field; if (field != null) { resultsList.Add(field.Name + " is invalid"); } else { resultsList.Add(result.ToString()); } } return(resultsList); } }
private void InitializeWorkItemFields() { TfsNameToFieldMapping = new Dictionary <string, IWorkItemField>(); if (string.IsNullOrEmpty(SelectedWorkItemTypeName)) { return; } WorkItemType workItemType = m_teamProject.WitProject.WorkItemTypes[SelectedWorkItemTypeName]; WorkItem wi = workItemType.NewWorkItem(); // iterating through each field present in the work item type foreach (Field field in wi.Fields) { // Steps Field needs special handling if (String.CompareOrdinal(field.FieldDefinition.ReferenceName, StepsReferenceName) == 0) { if (AddTestStepsField) { WorkItemField testStepTitleField = new TestStepTitleField(); WorkItemField testStepExpectedResultField = new TestStepExpectedResultField(); TfsNameToFieldMapping.Add(testStepTitleField.TfsName, testStepTitleField); TfsNameToFieldMapping.Add(testStepExpectedResultField.TfsName, testStepExpectedResultField); } else { IWorkItemField wiField = new WorkItemField(field, m_teamProject.WitProject); TfsNameToFieldMapping.Add(wiField.TfsName, wiField); } } // else if it is not a TCM Field else if (String.CompareOrdinal(WorkItemTypeToCategoryMapping[SelectedWorkItemTypeName], TestCaseCategory) != 0 || !field.FieldDefinition.ReferenceName.Contains(TCMField)) { IWorkItemField wiField = new WorkItemField(field, m_teamProject.WitProject); TfsNameToFieldMapping.Add(wiField.TfsName, wiField); } } }
/// <summary> /// Creates the work items. /// </summary> /// <param name="baseWorkItemIds">The base work item ids.</param> /// <param name="selectedPlanningTemplate">The selected planning template.</param> /// <returns></returns> /// <exception cref="System.ArgumentException"> /// Invalid WorkItemType /// or /// or /// Invalid WorkItemLinkType /// </exception> public WorkItem[] CreateRelatedWorkItems(int[] baseWorkItemIds, PlanningTemplate selectedPlanningTemplate) { TfsTeamProjectCollection projectCollection = this.GetTeamProjectCollection(); WorkItemStore store = projectCollection.GetService <WorkItemStore>(); Dictionary <WorkItemField, String> workItemFieldMatches = new Dictionary <WorkItemField, string>(ConfigurationManager.CurrentConfiguration.WorkItemFieldMatches.Count); // Load default fields from configuration foreach (WorkItemFieldMatch workItemFieldMatch in ConfigurationManager.CurrentConfiguration.WorkItemFieldMatches) { workItemFieldMatches.Add(workItemFieldMatch.Field, workItemFieldMatch.Value); } List <WorkItem> workItemsToSave = new List <WorkItem>(); foreach (int workItemId in baseWorkItemIds) { WorkItem baseWorkItem = store.GetWorkItem(workItemId); Project project = baseWorkItem.Project; foreach (TaskTemplate taskTemplate in selectedPlanningTemplate.TasksToCreateCollection) { // If Task Quantity is zero, ignore it if (taskTemplate.Quantity <= 0) { continue; } #region Get Work Item Type // Check if the Work Item Type exists if (!project.WorkItemTypes.Contains(taskTemplate.WorkItemType)) { throw new ArgumentException("Invalid WorkItemType"); } WorkItemType wiType = project.WorkItemTypes[taskTemplate.WorkItemType]; #endregion // Predefined fields match foreach (KeyValuePair <WorkItemField, String> fields in workItemFieldMatches) { if (!wiType.FieldDefinitions.Contains(workItemFieldMatches[WorkItemField.AssignedTo])) { throw new ArgumentException(String.Format("WorkItem {0} doesn't contain field with name {1}", taskTemplate.WorkItemType, workItemFieldMatches[WorkItemField.AssignedTo])); } } // Get the relation link type if (!store.WorkItemLinkTypes.Contains(taskTemplate.WorkItemLinkType)) { throw new ArgumentException("Invalid WorkItemLinkType"); } WorkItemLinkTypeEnd relationLinkType = store.WorkItemLinkTypes[taskTemplate.WorkItemLinkType].ReverseEnd; foreach (TaskTemplateInstance taskTemplateInstance in taskTemplate.InstancesCollection) { WorkItem newWorkItem = wiType.NewWorkItem(); newWorkItem.AreaId = baseWorkItem.AreaId; newWorkItem.AreaPath = baseWorkItem.AreaPath; newWorkItem.IterationPath = baseWorkItem.IterationPath; // Copy Tags if (!String.IsNullOrWhiteSpace(baseWorkItem.Tags)) { newWorkItem["Tags"] = baseWorkItem.Tags; } newWorkItem.Title = String.Format("{0}{1}{2}", taskTemplate.Prefix, baseWorkItem.Title, taskTemplate.Sufix); // AssignedTo newWorkItem.UpdateField(workItemFieldMatches[WorkItemField.AssignedTo], taskTemplateInstance.AssignedTo); // Original Estimate newWorkItem.UpdateField(workItemFieldMatches[WorkItemField.OriginalEstimate], taskTemplateInstance.EstimatedTime); // Remaining Work newWorkItem.UpdateField(workItemFieldMatches[WorkItemField.RemainingWork], taskTemplateInstance.EstimatedTime); // Custom Properties if (taskTemplate.CustomProperties != null && taskTemplate.CustomProperties.Count > 0) { foreach (TaskTemplateCustomProperty customProperty in taskTemplate.CustomProperties) { newWorkItem.UpdateField(customProperty.Name, customProperty.Value); } } if (taskTemplate.IsCopyDescriptionEnabled) { newWorkItem.Description = baseWorkItem.Description; if (String.IsNullOrWhiteSpace(newWorkItem.Description)) { // Maybe this is a bug if (baseWorkItem.Fields.Contains("Repro Steps")) { newWorkItem.Description = baseWorkItem.Fields["Repro Steps"].Value.ToString(); } } } newWorkItem.Links.Add(new RelatedLink(relationLinkType, baseWorkItem.Id)); workItemsToSave.Add(newWorkItem); } } } var witems = workItemsToSave.ToArray(); store.BatchSave(witems); return(witems); }
protected override void ProcessRecordInEH() { Project project = EnsureProject(); WorkItemType workItemType = EnsureWorkItemType(project); WorkItem workItem = workItemType.NewWorkItem(); workItem.Title = Title; Field assignedToField = workItem.Fields[WIQLSystemFieldNames.AssignedTo]; string assignedToUserPart = TfsWorkCmdletArgumentParser.ParseUserDisplayPartForWorkItemIdentityField("Assigned To", project.GetCollection(), assignedToField, AssignedTo); if (assignedToUserPart != null) { assignedToField.Value = assignedToUserPart; } if (Priority >= 0) { var priorityField = workItem.Fields[WIQLSystemFieldNames.Priority]; if (priorityField == null) { throw new PSArgumentException("This project doesn't support the default priority field."); } priorityField.Value = Priority; } if (!string.IsNullOrEmpty(Tags)) { workItem.Tags = Tags; } if (!string.IsNullOrEmpty(AreaPath)) { workItem.AreaPath = AreaPath; } if (!string.IsNullOrEmpty(IterationPath)) { workItem.IterationPath = IterationPath; } if (Parent > 0) { WorkItemLinkTypeEndCollection linkTypeCollection = project.Store.WorkItemLinkTypes.LinkTypeEnds; if (!linkTypeCollection.Contains(TfsWorkItemSystemLinkTypeNames.SystemLinkTypesHiearachyReverse)) { throw new InvalidOperationException("Parent work item link type doesn't exist, cannot create links to connect to parent work item."); } WorkItemLinkTypeEnd linkTypeEnd = project.Store.WorkItemLinkTypes.LinkTypeEnds[TfsWorkItemSystemLinkTypeNames.SystemLinkTypesHiearachyReverse]; workItem.Links.Add(new RelatedLink(linkTypeEnd, Parent)); } if (Properties != null) { foreach (DictionaryEntry property in Properties) { string propertyKey = property.Key as string; Field propertyValue = workItem.Fields[propertyKey]; if (propertyValue == null) { throw new PSArgumentException(string.Format("Unexpected property: {0}.", propertyKey)); } propertyValue.Value = property.Value; } } ArrayList validateResults = workItem.Validate(); if (validateResults.Count > 0) { StringBuilder validateResultMessage = new StringBuilder(); validateResultMessage.AppendLine("Work item validation failed!"); validateResultMessage.AppendLine("Invaild fields are listed below:"); foreach (Field field in validateResults) { validateResultMessage.AppendFormat("- Field \"{0}\": {1}.", field.Name, field.Status.ToString()); } throw new InvalidOperationException(validateResultMessage.ToString()); } workItem.Save(); WriteObject(workItem); }