private static int mapFromUpdated(
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem,
            EnterpriseChangeRequest newEcr, String fieldName)
        {
            int noOfMappedChanged = 0;

            // TODO: For the "calculated" fields we have defined ourselves we know the
            // dependencies, but should be expressed more elegant than this. Also the
            // Ericsson.Defect.User.DisplayName is dependent on System.AssignedTo but
            // will not be updated - we "know" that ...
            if (fieldName.Equals(TFSMapper.TFS_OWNER))
            {
                fieldName = TFSMapper.ERICSSON_DEFECT_USER_SIGNUM;
            }

            // Can be multiple ECR properties updated by one TFS field
            List <Property> props = props = AttributesMapper.getInstance().getNotifyProperties(fieldName);

            if (props != null && props.Count > 0)
            {
                foreach (Property prop in props)
                {
                    if (prop.getNotifyChange() && TFSMapper.getInstance().setEcrValues(newEcr, prop.getKey(), workItem))
                    {
                        noOfMappedChanged++;
                    }
                }
            }

            return(noOfMappedChanged);
        }
Beispiel #2
0
 // For test. Return the Resource as content that would be transmitted over wire.
 public ObjectContent getResourceAsMessage(EnterpriseChangeRequest ecr, String user)
 {
     return(getOslcClient().GetResourceAsMessage(
                ecr,
                OslcMediaType.APPLICATION_RDF_XML,
                OslcMediaType.APPLICATION_RDF_XML,
                HandlerSettings.getRESTHeaders(user)));
 }
Beispiel #3
0
        // UC 6: Create a TR based on a Bug.
        // Return if the bug is updated or not.
        private void createTR(
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem,
            WorkItemChangedEvent notification,
            String user,
            ref Status status)
        {
            // Create the ECR
            callCreateTR(ECRMapper.mapFromWorkitem(workItem, null, ECRMapper.ACTION_CREATE), user, ref status);
            if (!status.OK)
            {
                return;
            }

            // UC 3 (one case): Update all changed attributes before handling any state change

            // Note: If adding an entry in History on create this should propagate to Progress Info, but
            // this caused Bug to appear dirty after Save, see issue 79. Given almost all fields are used on
            // create, it does not make sense to make extra update as below. TBD if we should re-add in a
            // changed form.

            //EnterpriseChangeRequest updatedEcr = ECRMapper.mapFromUpdatedWorkitem(workItem, status.TRAbout, notification);
            //if (updatedEcr != null)
            //{
            //    // Note: Failure to update will be Warning not Error, so status still OK.
            //    callUpdateTR(updatedEcr, user, null, ref status);
            //}

            // Set the ECR in state Registered
            EnterpriseChangeRequest ecr = ECRMapper.mapFromWorkitem(workItem, status.TRAbout, ECRMapper.ACTION_REGISTER_ROUTE);

            callUpdateTR(ecr, user, ECRMapper.TR_STATE_REGISTERED_S, ref status);
            if (!status.OK)
            {
                return;
            }

            // Set the ECR in state Assigned if we have a defined owner
            ecr = ECRMapper.mapFromWorkitem(workItem, status.TRAbout, ECRMapper.ACTION_ASSIGN);
            if (ecr.GetOwner() != null && ecr.GetOwner().Length > 0)
            {
                callUpdateTR(ecr, user, ECRMapper.TR_STATE_ASSIGNED_S, ref status);
                if (!status.OK)
                {
                    return;
                }
            }

            if (status.OK)
            {
                HandlerSettings.LogMessage(
                    String.Format("Created TR based on workitem named: {0}", workItem.Title),
                    HandlerSettings.LoggingLevel.INFO);
            }
        }
Beispiel #4
0
        // Update the changed fields that are mapped. Return if the bug is updated or not.
        private void updateTRFields(
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem,
            WorkItemChangedEvent notification,
            String user,
            Uri about,
            ref Status status)
        {
            // UC 3 (one case): Update all changed attributes before handling any state change.
            EnterpriseChangeRequest updatedEcr = ECRMapper.mapFromUpdatedWorkitem(workItem, about, notification);

            if (updatedEcr != null)
            {
                // Note: Failure to update will be Warning not Error, so status still OK.
                callUpdateTR(updatedEcr, user, null, ref status);
            }
        }
Beispiel #5
0
        // Return if the workitem is updated (saved) by the handling code.
        public void handleEvent(
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem,
            String user,
            WorkItemChangedEvent notification)
        {
            Status status = new Status();

            // If the user is the OSLC Provider functional user we will only update status
            // message if so needed. This as changes from the OSLC Provider originates from
            // external source = currently: MHWeb. Need revisit to allow multiple clients.
            String aboutStr = AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_ABOUT, workItem);

            if (!user.Equals(HandlerSettings.TFSProviderUser, StringComparison.OrdinalIgnoreCase))
            {
                if (aboutStr.Length == 0)
                {
                    // We do not have a linked TR, we need to create
                    createTR(workItem, notification, user, ref status);
                }
                else
                {
                    Uri    about        = new Uri(aboutStr);
                    Status statusAssign = null;

                    EnterpriseChangeRequest ecr        = ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_ASSIGN);
                    TeamFoundationIdentity  assignedTo = HandlerSettings.GetSignumForAssignedTo(workItem);
                    if (user != null && assignedTo != null && !user.Equals(HandlerSettings.GetUserFromSignum(assignedTo.UniqueName), StringComparison.OrdinalIgnoreCase))
                    {
                        statusAssign = new Status();
                        ecr.SetOwner(user);
                        callUpdateTR(ecr, user, ECRMapper.TR_STATE_ASSIGNED_S, ref statusAssign);
                    }

                    // We have a TR linked which might need to be updated
                    updateTR(workItem, notification, user, about, ref status);

                    if (statusAssign != null && statusAssign.OK)
                    {
                        ecr.SetOwner(HandlerSettings.GetUserFromSignum(assignedTo.UniqueName));
                        callUpdateTR(ecr, user, ECRMapper.TR_STATE_ASSIGNED_S, ref statusAssign);
                    }
                }
            }

            // Handle update of Bug
            ECRMapper.updateBug(status, user, workItem);
        }
Beispiel #6
0
        // =========================================================
        // Handle the WorkItemChangedEvent event

        // Disconnect TR if conditions are matching. Return if workitem has been saved.
        public void handleDisconnectEvent(
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem,
            String user,
            WorkItemChangedEvent notification)
        {
            Uri about = ECRMapper.getDisconnectedTRLink(workItem, notification);

            if (about == null)
            {
                return;
            }
            EnterpriseChangeRequest ecr = ECRMapper.mapFromWorkitem(
                workItem, about, ECRMapper.ACTION_DISCONNECT);
            Status status = new Status();

            callUpdateTR(ecr, user, null, ref status);
            if (!status.OK)
            {
                HandlerSettings.LogMessage(
                    "Failed to disconnect TR from Bug.",
                    HandlerSettings.LoggingLevel.WARN);
            }
        }
        // Return an EnterpriseChangeRequest with mapped values needed for the state transiton
        // If there is a fail with mapping some values, this will be captured in event log.
        // TODO: This could likely be described in a xml mapping file for configuration
        public static EnterpriseChangeRequest mapFromWorkitem(
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem,
            Uri about,
            String action)
        {
            EnterpriseChangeRequest newEcr = new EnterpriseChangeRequest();

            newEcr.SetAbout(about);

            // Create a mapped ECR based on suggested action

            switch (action)
            {
            case ACTION_CREATE:
                newEcr.SetTitle(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_TITLE, workItem));
                newEcr.SetDescription(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_DESCRIPTION, workItem));
                newEcr.SetCurrentMho(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_CURRENT_MHO, workItem));
                newEcr.SetCustomer(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_CUSTOMER, workItem));
                newEcr.SetProduct(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_PRODUCT, workItem));
                newEcr.SetProductRevision(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_PRODUCT_REVISION, workItem));
                newEcr.SetNodeProduct(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_NODE_PRODUCT, workItem));
                newEcr.SetNodeProductRevision(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_NODE_PRODUCT_REVISION, workItem));

                // Note: The field firstTechnicalContact also is needed for TR creation. This we retrieve from
                // the user in the create notification, and put in the REST call header as required by MHWeb.
                // Formally we have mapping specified in ERICSSON_DEFECT_CREATOR_SIGNUM, but not used.

                // Add the connected Bug as related link
                Uri    relatedBug             = new Uri(HandlerSettings.GetUriForBug(workItem.Id.ToString()));
                String label                  = workItem.Id.ToString() + ": " + workItem.Title;
                OSLC4Net.Core.Model.Link link = new OSLC4Net.Core.Model.Link(relatedBug, label);
                newEcr.AddRelatedChangeRequest(link);

                break;

            case ACTION_REGISTER_ROUTE:
                newEcr.SetAction(ACTION_REGISTER_ROUTE);
                newEcr.SetImpactOnISP(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_IMPACT_ON_ISP, workItem));
                newEcr.SetPriority(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_PRIORITY, workItem));
                newEcr.SetDiddet(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_DIDDET, workItem));
                newEcr.SetActivity(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_ACTIVITY, workItem));
                newEcr.SetFirstTechContactInfo(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_FIRST_TECHNICAL_CONTACT_INFO, workItem));
                newEcr.SetCountry(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_COUNTRY, workItem));
                newEcr.SetSite(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_SITE, workItem));

                break;

            case ACTION_ASSIGN:
                newEcr.SetAction(ACTION_ASSIGN);
                newEcr.SetOwner(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_OWNER, workItem));
                break;

            case ACTION_PROPOSE:
                newEcr.SetAction(ACTION_PROPOSE);
                newEcr.SetDiddet(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_DIDDET, workItem));
                newEcr.SetActivity(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_ACTIVITY, workItem));
                newEcr.SetFirstTechContactInfo(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_FIRST_TECHNICAL_CONTACT_INFO, workItem));
                newEcr.SetExpectedImpactOnISP(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_EXPECTED_IMPACT_ON_ISP, workItem));
                newEcr.SetAnswer(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_ANSWER, workItem));
                newEcr.SetFaultCode(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_FAULTCODE, workItem));
                newEcr.SetAnswerCode(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_ANSWER_CODE, workItem));

                // Corrected Product info is mandatory for some  answerCodes and optional for others. Here
                // we pass in all cases and let the bug.xml handle mandatoryness and mhweb complain if not
                // present.

                newEcr.SetCorrectedProduct(
                    AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_CORRECTED_PRODUCT, workItem));
                newEcr.SetCorrectedProductRevision(
                    AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_CORRECTED_PRODUCT_REVISION, workItem));
                newEcr.SetCorrectedNodeProduct(
                    AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_CORRECTED_NODE_PRODUCT, workItem));
                newEcr.SetCorrectedNodeProductRevision(
                    AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_CORRECTED_NODE_PRODUCT_REVISION, workItem));
                break;

            case ACTION_APPROVE:
                newEcr.SetAction(ACTION_APPROVE);
                break;

            case ACTION_VERIFY:
                newEcr.SetAction(ACTION_VERIFY);
                newEcr.SetCorrectedNodeProduct(
                    AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_CORRECTED_NODE_PRODUCT, workItem));
                newEcr.SetCorrectedNodeProductRevision(
                    AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_CORRECTED_NODE_PRODUCT_REVISION, workItem));
                break;

            case ACTION_ANSWER:
                newEcr.SetAction(ACTION_ANSWER);
                newEcr.SetCorrectedNodeProduct(
                    AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_CORRECTED_NODE_PRODUCT, workItem));
                newEcr.SetCorrectedNodeProductRevision(
                    AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_CORRECTED_NODE_PRODUCT_REVISION, workItem));
                break;

            case ACTION_ACCEPT:
                newEcr.SetAction(ACTION_ACCEPT);
                break;

            case ACTION_REJECT:
                newEcr.SetAction(ACTION_REJECT);
                newEcr.SetNotebook(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_NOTEBOOK, workItem));
                break;

            case ACTION_DUPLICATE:
                newEcr.SetAction(ACTION_DUPLICATE);
                newEcr.SetPrimaryTR(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_PRIMARYTR, workItem));
                break;

            case ACTION_UNDUPLICATE:
                newEcr.SetAction(ACTION_UNDUPLICATE);
                break;

            case ACTION_CHANGE_PRODUCT:
                newEcr.SetAction(ACTION_CHANGE_PRODUCT);
                newEcr.SetProduct(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_PRODUCT, workItem));
                newEcr.SetProductRevision(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_PRODUCT_REVISION, workItem));
                newEcr.SetNodeProduct(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_NODE_PRODUCT, workItem));
                newEcr.SetNodeProductRevision(AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_NODE_PRODUCT_REVISION, workItem));
                break;

            case ACTION_REACTIVATE:
                newEcr.SetAction(ACTION_REACTIVATE);
                break;

            case ACTION_FINISH:
                newEcr.SetAction(ACTION_FINISH);
                break;

            case ACTION_DISCONNECT:
                newEcr.SetAction(ACTION_DISCONNECT);
                break;
            }

            return(newEcr);
        }
        // Return an EnterpriseChangeRequest with mapped values for the update
        public static EnterpriseChangeRequest mapFromUpdatedWorkitem(
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem,
            Uri about,
            WorkItemChangedEvent notification)
        {
            EnterpriseChangeRequest newEcr = new EnterpriseChangeRequest();

            newEcr.SetAbout(about);

            // The notification contain all changed fields. To understand what to
            // propagate to the client, we need to check which ecm fields that are
            // affected by the changes and are configured for notifyChange

            int noOfMappedChanged = 0;

            if (notification.ChangedFields != null)
            {
                // Most fields are String fields
                StringField[] changedStrFields = notification.ChangedFields.StringFields;
                if (changedStrFields != null)
                {
                    for (int i = 0; i < changedStrFields.Length; i++)
                    {
                        String fieldName = changedStrFields[i].ReferenceName;
                        noOfMappedChanged += mapFromUpdated(workItem, newEcr, fieldName);
                    }
                }

                // For example Priority is an Integer field
                IntegerField[] changedIntFields = notification.ChangedFields.IntegerFields;
                if (changedIntFields != null)
                {
                    for (int i = 0; i < changedIntFields.Length; i++)
                    {
                        String fieldName = changedIntFields[i].ReferenceName;
                        noOfMappedChanged += mapFromUpdated(workItem, newEcr, fieldName);
                    }
                }
            }

            // For example the Description is a Text field
            TextField[] changedTextFields = notification.TextFields;
            if (changedTextFields != null)
            {
                for (int i = 0; i < changedTextFields.Length; i++)
                {
                    String fieldName = changedTextFields[i].ReferenceName;
                    noOfMappedChanged += mapFromUpdated(workItem, newEcr, fieldName);
                }
            }

            // To find a change in the Comment/History one need to look at revision - 1.
            noOfMappedChanged += mapFromUpdated(workItem, newEcr, TFSMapper.TFS_HISTORY);

            // Need to send list of attachments in all cases when we have another update. So if we already have
            // an update (noOfMappedChanged > 0), send - otherwise, check if changed then send.
            if ((noOfMappedChanged > 0 || TFSMapper.getInstance().hasLinksChanged(workItem)))
            {
                noOfMappedChanged += mapFromUpdated(workItem, newEcr, TFSMapper.ERICSSON_DEFECT_HYPERLINK);
            }

            if (noOfMappedChanged > 0)
            {
                // More than 1 field that was mapped changed
                return(newEcr);
            }
            else
            {
                // No field of interest was changed
                HandlerSettings.LogMessage(
                    String.Format("No mapped fields was updated for: {0}", workItem.Title),
                    HandlerSettings.LoggingLevel.INFO);
                return(null);
            }
        }
Beispiel #9
0
        public void callUpdateTR(EnterpriseChangeRequest ecr, String user, String expectedState, ref Status status)
        {
            String message = null;
            HttpResponseMessage updateResponse = null;

            try
            {
                String about = ecr.GetAbout().ToString();
                updateResponse = getOslcClient().UpdateResource(
                    about, ecr,
                    OslcMediaType.APPLICATION_RDF_XML,
                    OslcMediaType.APPLICATION_RDF_XML,
                    HandlerSettings.getRESTHeaders(user));
                message = (updateResponse != null) ?
                          updateResponse.Content.ReadAsStringAsync().Result :
                          "No result when calling " + about;
            }
            catch (Exception ex)
            {
                HandlerSettings.LogMessage(
                    String.Format("Failed to update ECR: '{0}'", ex.Message),
                    HandlerSettings.LoggingLevel.ERROR);
                status.ErrorMessage = "Failed to update TR from Bug. Error: '" + ex.Message + "'";
                return;
            }

            if (updateResponse == null || updateResponse.StatusCode != HttpStatusCode.OK)
            {
                HandlerSettings.LogMessage(
                    String.Format("Failed to update ECR. Error from MHWeb: '{0}'", message),
                    HandlerSettings.LoggingLevel.ERROR);

                // If trying a state change, report as error - otherwise as warning
                if (expectedState != null)
                {
                    status.ErrorMessage = "Failed to update TR from Bug. Error from MHWeb: '" + message + "'";
                }
                else
                {
                    status.WarningMessage = "Warnings when update TR from Bug. Message from MHWeb: '" + message + "'";
                }
                return;
            }
            else
            {
                // Log the incoming rdf
                HandlerSettings.LogMessage(
                    String.Format("Response from update:\n{0}", message),
                    HandlerSettings.LoggingLevel.INFO);
            }

            // NOTE: MHWeb return an updated item. This is convenient, but not by spec.
            // Might need an explicit GET here to make more robust if other clients.
            EnterpriseChangeRequest newEcr = updateResponse.Content.
                                             ReadAsAsync <EnterpriseChangeRequest>(getOslcClient().GetFormatters()).Result;
            String shortState = newEcr.GetStatus();

            status.TRState = ECRMapper.getLongTRState(shortState);

            if (expectedState != null)
            {
                // Verify result if we did a state change

                if (shortState == null || shortState != expectedState)
                {
                    status.ExpectedStateMessage =
                        String.Format("The ECR is in wrong state: {0} expected: {1}", shortState, expectedState);
                    HandlerSettings.LogMessage(status.ExpectedStateMessage, HandlerSettings.LoggingLevel.WARN);
                }
                HandlerSettings.LogMessage(
                    String.Format("The ECR named {0} is updated to state: {1}", newEcr.GetTitle(), status.TRState),
                    HandlerSettings.LoggingLevel.INFO);
            }
            else
            {
                HandlerSettings.LogMessage(
                    String.Format("The ECR named: {0} is updated", newEcr.GetTitle()),
                    HandlerSettings.LoggingLevel.INFO);
            }

            return;
        }
Beispiel #10
0
        // ======================================================================================
        // REST calls to create or update

        public void callCreateTR(EnterpriseChangeRequest ecr, String user, ref Status status)
        {
            HttpResponseMessage creationResponse = null;
            String message;

            try
            {
                creationResponse = getOslcClient().CreateResource(
                    HandlerSettings.CreationFactoryUri, ecr,
                    OslcMediaType.APPLICATION_RDF_XML,
                    OslcMediaType.APPLICATION_RDF_XML,
                    HandlerSettings.getRESTHeaders(user));
                message = (creationResponse != null) ?
                          creationResponse.Content.ReadAsStringAsync().Result :
                          "No result when calling " + HandlerSettings.CreationFactoryUri;
            }
            catch (Exception ex)
            {
                HandlerSettings.LogMessage(
                    String.Format("Failed to create ECR: '{0}'", ex.Message),
                    HandlerSettings.LoggingLevel.ERROR);
                status.ErrorMessage = "Failed to create TR from Bug. Error: '" + ex.Message + "'";
                return;
            }

            if (creationResponse == null || creationResponse.StatusCode != HttpStatusCode.Created)
            {
                HandlerSettings.LogMessage(
                    String.Format("Failed to create ECR. Error from MHWeb: '{0}'", message),
                    HandlerSettings.LoggingLevel.ERROR);
                status.ErrorMessage = "Failed to create TR from Bug. Error from MHWeb: '" + message + "'";
                return;
            }
            else
            {
                // Log the incoming rdf
                HandlerSettings.LogMessage(
                    String.Format("Response from create:\n{0}", message),
                    HandlerSettings.LoggingLevel.INFO);
            }

            EnterpriseChangeRequest newEcr = creationResponse.Content.
                                             ReadAsAsync <EnterpriseChangeRequest>(getOslcClient().GetFormatters()).Result;

            status.TRAbout = newEcr.GetAbout();
            String shortState = newEcr.GetStatus();

            status.TRState = ECRMapper.getLongTRState(shortState);

            HandlerSettings.LogMessage(
                String.Format("ECR named {0} created a location: {1}", newEcr.GetTitle(), status.TRAbout),
                HandlerSettings.LoggingLevel.INFO);

            // Verify result
            if (shortState == null || shortState != ECRMapper.TR_STATE_PRIVATE_S)
            {
                status.ExpectedStateMessage =
                    String.Format("Created ECR in wrong state: {0} expected: PR", shortState);
                HandlerSettings.LogMessage(status.ExpectedStateMessage, HandlerSettings.LoggingLevel.WARN);
            }

            return;
        }
Beispiel #11
0
        // Update the connected TR State. Return if the bug is updated or not.
        private void updateTRState(
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem,
            String oldState, String newState,
            String user,
            Uri about,
            ref Status status)
        {
            status.TRState = ECRMapper.getTRState(workItem);

            if (oldState.Equals(ECRMapper.BUG_STATE_ACTIVE, StringComparison.OrdinalIgnoreCase) &&
                newState.Equals(ECRMapper.BUG_STATE_RESOLVED, StringComparison.OrdinalIgnoreCase))
            {
                // UC 4: Updated TR based on the Bug's Active -> Resolve state change

                EnterpriseChangeRequest ecr = null;
                if (status.TRState.Equals(ECRMapper.TR_STATE_ASSIGNED))
                {
                    ecr = ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_PROPOSE);
                    callUpdateTR(ecr, user, ECRMapper.TR_STATE_PROPOSED_S, ref status);
                    if (!status.OK)
                    {
                        return;
                    }
                }

                if (status.TRState.Equals(ECRMapper.TR_STATE_PROPOSED))
                {
                    String answerCode = "";
                    if (ecr != null)
                    {
                        answerCode = ecr.GetAnswerCode();
                    }
                    else
                    {
                        answerCode = AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_ANSWER_CODE, workItem);
                    }

                    if (answerCode.Contains("A") || answerCode.Contains("D") || answerCode.Contains("B11"))
                    {
                        // In case Answer Code = A*, D*, or B11 go directly to Answer
                        callUpdateTR(ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_ANSWER),
                                     user, ECRMapper.TR_STATE_TECH_ANSW_PROV_S, ref status);
                        return;
                    }
                    else
                    {
                        callUpdateTR(ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_APPROVE),
                                     user, ECRMapper.TR_STATE_PROP_APPROV_S, ref status);
                    }
                }

                String expectedState = ECRMapper.TR_STATE_PROP_APPROV;
                if (!status.TRState.Equals(expectedState))
                {
                    // Incorrect pre condition
                    HandlerSettings.LogMessage(
                        String.Format("Expected TR State: {0}, current TR state: {1}", expectedState, status.TRState),
                        HandlerSettings.LoggingLevel.WARN);
                }

                // Note: Here we assume we are in trState PA, as we accepted AS, PP and PA. If we have not failed
                // before, this is where we will fail if any assumption was wrong -> error message.
                if (status.OK)
                {
                    callUpdateTR(ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_VERIFY),
                                 user, ECRMapper.TR_STATE_TECH_ANSW_PROV_S, ref status);
                }
            }

            else if (oldState.Equals(ECRMapper.BUG_STATE_RESOLVED, StringComparison.OrdinalIgnoreCase) &&
                     newState.Equals(ECRMapper.BUG_STATE_CLOSED, StringComparison.OrdinalIgnoreCase))
            {
                // Update a TR based on Close of the Bug's Resolve -> Close state change

                String expectedState = ECRMapper.TR_STATE_TECH_ANSW_PROV;
                if (!status.TRState.Equals(expectedState))
                {
                    // Incorrect pre condition
                    HandlerSettings.LogMessage(
                        String.Format("Expected TR State: {0}, current TR state: {1}", expectedState, status.TRState),
                        HandlerSettings.LoggingLevel.WARN);
                }

                // TODO: Put the condition in configuration file etc.
                String answerCode = AttributesMapper.GetTfsValueForEcrKey(TFSMapper.ECM_ANSWER_CODE, workItem);
                if (answerCode.Equals("D4"))
                {
                    // UC 9.3 - Bug set as Postponed
                    callUpdateTR(ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_ACCEPT),
                                 user, ECRMapper.TR_STATE_POSTPONED_S, ref status);
                }
                else if (answerCode.Equals("Duplicate"))
                {
                    // UC 9.4 - Bug set as Duplicate
                    callUpdateTR(ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_DUPLICATE), user, null, ref status);
                }
                else
                {
                    // UC 9.1 - Close of Bug from TFS
                    callUpdateTR(ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_FINISH), user,
                                 ECRMapper.TR_STATE_FINISHED_S, ref status);
                }
            }

            else if (oldState.Equals(ECRMapper.BUG_STATE_RESOLVED, StringComparison.OrdinalIgnoreCase) &&
                     newState.Equals(ECRMapper.BUG_STATE_ACTIVE, StringComparison.OrdinalIgnoreCase))
            {
                // UC 9.2: Update a TR based the Bug's Resolved -> Active state change

                callUpdateTR(ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_REJECT), user,
                             ECRMapper.TR_STATE_REGISTERED_S, ref status);
                if (!status.OK)
                {
                    return;
                }

                // If Bug is assigned to a user we need to drive change back to Assigned state
                EnterpriseChangeRequest ecr = ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_ASSIGN);
                if (ecr.GetOwner() != null && ecr.GetOwner().Length > 0)
                {
                    callUpdateTR(ecr, user, ECRMapper.TR_STATE_ASSIGNED_S, ref status);
                }
            }
            else if (oldState.Equals(ECRMapper.BUG_STATE_CLOSED, StringComparison.OrdinalIgnoreCase) &&
                     newState.Equals(ECRMapper.BUG_STATE_ACTIVE, StringComparison.OrdinalIgnoreCase))
            {
                // UC 9.2: Update a TR based the Bug's Closed -> Active state change

                // In case of Closed -> Active, this is only allowed for the "no action" answer codes
                // in MHWeb, so OK for this to fail if selecting the incorrect answer code.
                // Could block in Bug.xml if we want to prevent some cases.

                callUpdateTR(ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_REACTIVATE), user,
                             ECRMapper.TR_STATE_REGISTERED_S, ref status);
                if (!status.OK)
                {
                    return;
                }

                // If Bug is assigned to a user we need to drive change back to Assigned state
                EnterpriseChangeRequest ecr = ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_ASSIGN);
                if (ecr.GetOwner() != null && ecr.GetOwner().Length > 0)
                {
                    callUpdateTR(ecr, user, ECRMapper.TR_STATE_ASSIGNED_S, ref status);
                }
            }

            else if (oldState.Equals(ECRMapper.BUG_STATE_ACTIVE, StringComparison.OrdinalIgnoreCase) &&
                     newState.Equals(ECRMapper.BUG_STATE_ACTIVE, StringComparison.OrdinalIgnoreCase))
            {
                // UC 3 (one case): Handle Assign case - state change for TR, attribute change for Bug

                // Incorrect pre condition
                String expectedState = ECRMapper.TR_STATE_REGISTERED;
                if (!status.TRState.Equals(expectedState))
                {
                    HandlerSettings.LogMessage(
                        String.Format("Expected TR State: {0}, current TR state: {1}", expectedState, status.TRState),
                        HandlerSettings.LoggingLevel.WARN);
                }

                // Handle case when we are in state PR
                if (status.TRState.Equals(ECRMapper.TR_STATE_PRIVATE))
                {
                    callUpdateTR(ECRMapper.mapFromWorkitem(
                                     workItem, about, ECRMapper.ACTION_REGISTER_ROUTE),
                                 user, ECRMapper.TR_STATE_REGISTERED_S, ref status);
                }

                callUpdateTR(ECRMapper.mapFromWorkitem(
                                 workItem, about, ECRMapper.ACTION_ASSIGN), user, ECRMapper.TR_STATE_ASSIGNED_S, ref status);
            }
        }
Beispiel #12
0
        // Update the connected TR.
        private void updateTR(
            Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem,
            WorkItemChangedEvent notification,
            String user,
            Uri about,
            ref Status status)
        {
            String oldState, newState, action;

            ECRMapper.getStatusChange(notification, workItem, out oldState, out newState, out action);

            if (action != null)
            {
                if (action.Equals(ECRMapper.ACTION_IGNORE))
                {
                    // Ignore all changes
                    return;
                }

                if (action.Equals(ECRMapper.ACTION_DUPLICATE))
                {
                    // UC 9.4: Duplicate - Find Bug with duplicate id, get attached TR and send action duplicate

                    // Update needs to be done before setting the Bug as Duplicate as the TR can't change after.
                    updateTRFields(workItem, notification, user, about, ref status);

                    EnterpriseChangeRequest ecr = ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_DUPLICATE);

                    if (ecr.GetPrimaryTR() == null || ecr.GetPrimaryTR().Length == 0)
                    {
                        // Failed to find a Bug with a connected TR. Log error and return.
                        String msg = String.Format(
                            "Failed to find duplicate to Bug: {0}, or duplicate Bug had not TR Link set",
                            workItem.Title);
                        HandlerSettings.LogMessage(msg, HandlerSettings.LoggingLevel.ERROR);
                        status.ErrorMessage = msg;
                        return;
                    }

                    callUpdateTR(ecr, user, null, ref status);
                    if (!status.OK)
                    {
                        // Log issue, but continue if e.g. a state change
                        HandlerSettings.LogMessage(
                            String.Format("Failed to set TR as duplicate based on Bug: {0}", workItem.Title),
                            HandlerSettings.LoggingLevel.WARN);
                    }

                    // After setting Bug/TR as Duplicated, no futher changes should be propagated.
                    return;
                }

                if (action.Equals(ECRMapper.ACTION_UNDUPLICATE))
                {
                    // UC 9.4: Unduplicate - Move TR back to Registered by sending action unduplicate
                    EnterpriseChangeRequest ecr = ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_UNDUPLICATE);
                    callUpdateTR(ecr, user, null, ref status);
                    if (!status.OK)
                    {
                        // Log issue, but continue if e.g. a state change
                        HandlerSettings.LogMessage(
                            String.Format("Failed to unduplicate TR based on Bug ", workItem.Title),
                            HandlerSettings.LoggingLevel.WARN);
                    }

                    // Update needs to be done after "Unduplicating" the Bug as the TR can't change before.
                    updateTRFields(workItem, notification, user, about, ref status);

                    // Return before handling a possible state change as states can be out of sync
                    // (normal case) when Bug has been Duplicate. Bug then Resolved / Closed, and
                    // TR in an Active state. User needs to set TR state to Active.
                    return;
                }

                else if (action.Equals(ECRMapper.ACTION_CHANGE_PRODUCT))
                {
                    // UC 10.2: Update of the release - propagate info to TR
                    EnterpriseChangeRequest ecr = ECRMapper.mapFromWorkitem(workItem, about, ECRMapper.ACTION_CHANGE_PRODUCT);

                    bool noProduct = ecr.GetProduct().Length == 0;
                    if (noProduct)
                    {
                        // No product found, so value is for internal release - add message in notebook
                        ecr.SetNotebook("The referenced design item was moved to a product internal to Design");
                    }

                    callUpdateTR(ecr, user, null, ref status);
                    if (!status.OK)
                    {
                        // Log issue, but continue if e.g. a state change
                        HandlerSettings.LogMessage(
                            String.Format("Failed to change product on TR based on Bug ", workItem.Title),
                            HandlerSettings.LoggingLevel.WARN);
                    }
                }
            }

            updateTRFields(workItem, notification, user, about, ref status);

            if (newState != null)
            {
                // Handle state change
                updateTRState(workItem, oldState, newState, user, about, ref status);;
            }
        }