public static async Task Send(IEnumerable<int> artifactIds, TenantInformation tenant, ActionMessage sourceMessage, IWorkflowMessagingProcessor workflowMessagingProcessor)
        {
            var artifacts = artifactIds?.ToList();
            if (artifacts == null || !artifacts.Any())
            {
                Logger.Log("No artifact IDs found; no need to send Artifact Changed Message", sourceMessage, tenant);
                return;
            }
            Logger.Log($"Found {artifacts.Count} artifact IDs", sourceMessage, tenant);

            var artifactsChangedMessage = new ArtifactsChangedMessage
            {
                TransactionId = sourceMessage.TransactionId,
                RevisionId = sourceMessage.RevisionId,
                UserId = sourceMessage.UserId,
                ChangeType = ArtifactChangedType.Indirect
            };

            while (artifacts.Any())
            {
                var batch = artifacts.Take(MaximumArtifactBatchSize).ToList();
                artifactsChangedMessage.ArtifactIds = batch;

                Logger.Log($"Sending Artifacts Changed Message for {batch.Count} artifact IDs: {string.Join(",", batch)}", sourceMessage, tenant);
                await ActionMessageSender.Send(artifactsChangedMessage, tenant, workflowMessagingProcessor);
                Logger.Log("Finished sending Artifacts Changed Message", sourceMessage, tenant);

                artifacts = artifacts.Skip(MaximumArtifactBatchSize).ToList();
            }
        }
Beispiel #2
0
        public void ArtifactsChangedMessageHandler_RethrowsException()
        {
            // arrange
            _actionHelperMock.Setup(m => m.HandleAction(It.IsAny <TenantInformation>(), It.IsAny <ActionMessage>(), It.IsAny <BaseRepository>())).Throws(new TestException());
            var handler = new ArtifactsChangedMessageHandler(_actionHelperMock.Object, _tenantInfoRetrieverMock.Object, _configHelperMock.Object, _transactionValidatorMock.Object);
            var message = new ArtifactsChangedMessage();

            // act
            TestHandlerAndMessageWithHeader(handler, message);
            // assert
            _actionHelperMock.Verify(m => m.HandleAction(It.IsAny <TenantInformation>(), It.IsAny <ActionMessage>(), It.IsAny <BaseRepository>()), Times.Once);
        }
Beispiel #3
0
        public void ArtifactsChangedMessageHandler_HandlesMessageSuccessfully()
        {
            // arrange
            _actionHelperMock.Setup(m => m.HandleAction(It.IsAny <TenantInformation>(), It.IsAny <ActionMessage>(), It.IsAny <BaseRepository>())).Returns(Task.FromResult(true));
            var handler = new ArtifactsChangedMessageHandler(_actionHelperMock.Object, _tenantInfoRetrieverMock.Object, _configHelperMock.Object, _transactionValidatorMock.Object);
            var message = new ArtifactsChangedMessage();

            // act
            TestHandlerAndMessageWithHeader(handler, message);
            // assert
            Assert.IsNotNull(handler);
            _actionHelperMock.Verify(m => m.HandleAction(It.IsAny <TenantInformation>(), It.IsAny <ActionMessage>(), It.IsAny <BaseRepository>()), Times.Once);
        }
Beispiel #4
0
        private async Task PostOperation(IEnumerable <int> artifactIds, int revisionId, int userId, long transactionId, IDbTransaction transaction = null)
        {
            if (artifactIds.IsEmpty())
            {
                return;
            }

            var message = new ArtifactsChangedMessage(artifactIds)
            {
                TransactionId = transactionId,
                RevisionId    = revisionId,
                UserId        = userId,
                ChangeType    = ArtifactChangedType.Publish
            };

            await _sendMessageExecutor.Execute(_applicationSettingsRepository, _serviceLogRepository, message, transaction);
        }
Beispiel #5
0
        public void TestInitialize()
        {
            var artifactIds = new[]
            {
                1
            };

            _message = new ArtifactsChangedMessage(artifactIds)
            {
                ChangeType = ArtifactChangedType.Save,
                RevisionId = 12,
                UserId     = 34
            };
            _repositoryMock    = new Mock <IArtifactsChangedRepository>(MockBehavior.Strict);
            _helper            = new ArtifactsChangedActionHelper();
            _tenantInformation = new TenantInformation();
        }
        public async Task <IList <IWorkflowMessage> > GenerateMessages(int userId,
                                                                       int revisionId,
                                                                       string userName,
                                                                       long transactionId,
                                                                       WorkflowEventTriggers postOpTriggers,
                                                                       IBaseArtifactVersionControlInfo artifactInfo,
                                                                       string projectName,
                                                                       IDictionary <int, IList <Property> > modifiedProperties,
                                                                       WorkflowState currentState,
                                                                       WorkflowState newState,
                                                                       bool sendArtifactPublishedMessage,
                                                                       string artifactUrl,
                                                                       string baseUrl,
                                                                       IUsersRepository usersRepository,
                                                                       IServiceLogRepository serviceLogRepository,
                                                                       IWebhooksRepository webhooksRepository,
                                                                       IProjectMetaRepository projectMetaRepository,
                                                                       IArtifactVersionsRepository artifactVersionsRepository,
                                                                       IDbTransaction transaction = null)
        {
            var resultMessages = new List <IWorkflowMessage>();
            // var project = artifactResultSet?.Projects?.FirstOrDefault(d => d.Id == artifactInfo.ProjectId);
            var baseHostUri = baseUrl ?? ServerUriHelper.GetBaseHostUri()?.ToString();

            foreach (var workflowEventTrigger in postOpTriggers)
            {
                if (workflowEventTrigger?.Action == null)
                {
                    continue;
                }
                switch (workflowEventTrigger.ActionType)
                {
                case MessageActionType.Notification:
                    var notificationAction = workflowEventTrigger.Action as EmailNotificationAction;
                    if (notificationAction == null)
                    {
                        continue;
                    }
                    var notificationMessage = await GetNotificationMessage(userId,
                                                                           revisionId,
                                                                           transactionId,
                                                                           artifactInfo,
                                                                           projectName,
                                                                           notificationAction,
                                                                           artifactUrl,
                                                                           baseHostUri,
                                                                           usersRepository,
                                                                           transaction);

                    if (notificationMessage == null)
                    {
                        await serviceLogRepository.LogInformation(LogSource,
                                                                  $"Skipping Email notification action for artifact {artifactInfo.Id}");

                        continue;
                    }
                    resultMessages.Add(notificationMessage);
                    break;

                case MessageActionType.GenerateChildren:
                    var generateChildrenAction = workflowEventTrigger.Action as GenerateChildrenAction;
                    if (generateChildrenAction == null)
                    {
                        continue;
                    }
                    var generateChildrenMessage = new GenerateDescendantsMessage
                    {
                        TransactionId         = transactionId,
                        ChildCount            = generateChildrenAction.ChildCount.GetValueOrDefault(10),
                        DesiredArtifactTypeId = generateChildrenAction.ArtifactTypeId,
                        ArtifactId            = artifactInfo.Id,
                        RevisionId            = revisionId,
                        UserId         = userId,
                        ProjectId      = artifactInfo.ProjectId,
                        UserName       = userName,
                        BaseHostUri    = baseHostUri,
                        ProjectName    = projectName,
                        TypePredefined = (int)artifactInfo.PredefinedType
                    };
                    resultMessages.Add(generateChildrenMessage);
                    break;

                case MessageActionType.GenerateTests:
                    var generateTestsAction = workflowEventTrigger.Action as GenerateTestCasesAction;
                    if (generateTestsAction == null || artifactInfo.PredefinedType != ItemTypePredefined.Process)
                    {
                        continue;
                    }
                    var generateTestsMessage = new GenerateTestsMessage
                    {
                        TransactionId = transactionId,
                        ArtifactId    = artifactInfo.Id,
                        RevisionId    = revisionId,
                        UserId        = userId,
                        ProjectId     = artifactInfo.ProjectId,
                        UserName      = userName,
                        BaseHostUri   = baseHostUri,
                        ProjectName   = projectName
                    };
                    resultMessages.Add(generateTestsMessage);
                    break;

                case MessageActionType.GenerateUserStories:
                    var generateUserStories = workflowEventTrigger.Action as GenerateUserStoriesAction;
                    if (generateUserStories == null || artifactInfo.PredefinedType != ItemTypePredefined.Process)
                    {
                        continue;
                    }
                    var generateUserStoriesMessage = new GenerateUserStoriesMessage
                    {
                        TransactionId = transactionId,
                        ArtifactId    = artifactInfo.Id,
                        RevisionId    = revisionId,
                        UserId        = userId,
                        ProjectId     = artifactInfo.ProjectId,
                        UserName      = userName,
                        BaseHostUri   = baseHostUri,
                        ProjectName   = projectName
                    };
                    resultMessages.Add(generateUserStoriesMessage);
                    break;

                case MessageActionType.Webhooks:
                    var webhookAction = workflowEventTrigger.Action as WebhookAction;
                    if (webhookAction == null)
                    {
                        continue;
                    }

                    var customTypes =
                        await projectMetaRepository.GetCustomProjectTypesAsync(artifactInfo.ProjectId, userId);

                    var artifactType =
                        customTypes.ArtifactTypes.FirstOrDefault(at => at.Id == artifactInfo.ItemTypeId);

                    var artifactPropertyInfos = await artifactVersionsRepository.GetArtifactPropertyInfoAsync(
                        userId,
                        new List <int> {
                        artifactInfo.Id
                    },
                        new List <int>
                    {
                        (int)PropertyTypePredefined.Name,
                        (int)PropertyTypePredefined.Description,
                        (int)PropertyTypePredefined.ID,
                        (int)PropertyTypePredefined.CreatedBy,
                        (int)PropertyTypePredefined.LastEditedOn,
                        (int)PropertyTypePredefined.LastEditedBy,
                        (int)PropertyTypePredefined.CreatedOn
                    },
                        artifactType.CustomPropertyTypeIds);

                    var webhookArtifactInfo = new WebhookArtifactInfo
                    {
                        Id          = Guid.NewGuid().ToString(),
                        EventType   = WebhookEventType,
                        PublisherId = WebhookPublisherId,
                        Scope       = new WebhookArtifactInfoScope
                        {
                            Type       = WebhookType,
                            WorkflowId = currentState.WorkflowId
                        },
                        Resource = new WebhookResource
                        {
                            Name                 = artifactInfo.Name,
                            ProjectId            = artifactInfo.ProjectId,
                            ParentId             = ((VersionControlArtifactInfo)artifactInfo).ParentId,
                            ArtifactTypeId       = artifactInfo.ItemTypeId,
                            ArtifactTypeName     = artifactType?.Name,
                            BaseArtifactType     = artifactType?.PredefinedType?.ToString(),
                            ArtifactPropertyInfo =
                                await ConvertToWebhookPropertyInfo(artifactPropertyInfos, customTypes.PropertyTypes, usersRepository),
                            ChangedState = new WebhookStateChangeInfo
                            {
                                NewValue = new WebhookStateInfo
                                {
                                    Id         = newState.Id,
                                    Name       = newState.Name,
                                    WorkflowId = newState.WorkflowId
                                },
                                OldValue = new WebhookStateInfo
                                {
                                    Id         = currentState.Id,
                                    Name       = currentState.Name,
                                    WorkflowId = currentState.WorkflowId
                                }
                            },
                            Revision     = revisionId,
                            Version      = ((VersionControlArtifactInfo)artifactInfo).Version,
                            Id           = artifactInfo.Id,
                            BlueprintUrl = string.Format($"{baseHostUri}?ArtifactId={artifactInfo.Id}"),
                            Link         = string.Format(
                                $"{baseHostUri}api/v1/projects/{artifactInfo.ProjectId}/artifacts/{artifactInfo.Id}")
                        }
                    };

                    var webhookMessage = await GetWebhookMessage(userId, revisionId, transactionId, webhookAction,
                                                                 webhooksRepository, webhookArtifactInfo, transaction);

                    if (webhookMessage == null)
                    {
                        await serviceLogRepository.LogInformation(LogSource,
                                                                  $"Skipping Webhook action for artifact {artifactInfo.Id}: {artifactInfo.Name}.");

                        continue;
                    }
                    resultMessages.Add(webhookMessage);
                    break;
                }
            }

            // Add published artifact message
            if (sendArtifactPublishedMessage)
            {
                var publishedMessage =
                    GetPublishedMessage(userId, revisionId, transactionId, artifactInfo, modifiedProperties) as
                    ArtifactsPublishedMessage;
                if (publishedMessage != null && publishedMessage.Artifacts.Any())
                {
                    resultMessages.Add(publishedMessage);
                }
            }

            var artifactIds = new List <int>
            {
                artifactInfo.Id
            };
            var artifactsChangedMessage = new ArtifactsChangedMessage(artifactIds)
            {
                TransactionId = transactionId,
                UserId        = userId,
                RevisionId    = revisionId,
                ChangeType    = ArtifactChangedType.Publish
            };

            resultMessages.Add(artifactsChangedMessage);

            return(resultMessages);
        }