示例#1
0
        private static async Task <IWorkflowMessage> GetWebhookMessage(int userId, int revisionId, long transactionId, WebhookAction webhookAction,
                                                                       IWebhooksRepository webhooksRepository, WebhookArtifactInfo webhookArtifactInfo)
        {
            List <int> webhookId = new List <int> {
                webhookAction.WebhookId
            };
            var webhookInfos = await webhooksRepository.GetWebhooks(webhookId);

            var webhookInfo = webhookInfos.FirstOrDefault();

            var securityInfo = SerializationHelper.FromXml <XmlWebhookSecurityInfo>(webhookInfo.SecurityInfo);

            var webhookMessage = new WebhookMessage
            {
                TransactionId = transactionId,
                UserId        = userId,
                RevisionId    = revisionId,
                // Authentication Information
                Url = webhookInfo.Url,
                IgnoreInvalidSSLCertificate = securityInfo.IgnoreInvalidSSLCertificate,
                HttpHeaders          = securityInfo.HttpHeaders,
                BasicAuthUsername    = securityInfo.BasicAuth?.Username,
                BasicAuthPassword    = securityInfo.BasicAuth?.Password,
                SignatureSecretToken = securityInfo.Signature?.SecretToken,
                SignatureAlgorithm   = securityInfo.Signature?.Algorithm,
                // Payload Information
                WebhookJsonPayload = webhookArtifactInfo.ToJSON()
            };

            return(webhookMessage);
        }
 public WebhooksController
 (
     IWebhooksRepository webhooksRepository,
     IEventTypesRepository eventsRepository,
     ITenant tenant
 )
 {
     this.webhooksRepository = webhooksRepository;
     this.eventsRepository   = eventsRepository;
     this.tenant             = tenant;
 }
 public StateChangeExecutorRepositories(IArtifactVersionsRepository artifactVersionsRepository,
                                        IWorkflowRepository workflowRepository,
                                        IVersionControlService versionControlService,
                                        IReuseRepository reuseRepository,
                                        ISaveArtifactRepository saveArtifactRepository,
                                        IApplicationSettingsRepository applicationSettingsRepository,
                                        IServiceLogRepository serviceLogRepository,
                                        IUsersRepository usersRepository,
                                        IWebhooksRepository webhooksRepository,
                                        IProjectMetaRepository projectMetaRepository)
 {
     ArtifactVersionsRepository    = artifactVersionsRepository;
     WorkflowRepository            = workflowRepository;
     VersionControlService         = versionControlService;
     ReuseRepository               = reuseRepository;
     SaveArtifactRepository        = saveArtifactRepository;
     ApplicationSettingsRepository = applicationSettingsRepository;
     ServiceLogRepository          = serviceLogRepository;
     UsersRepository               = usersRepository;
     WebhooksRepository            = webhooksRepository;
     ProjectMetaRepository         = projectMetaRepository;
 }
示例#4
0
        public static 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,
                                                                              string artifactUrl,
                                                                              string baseUrl,
                                                                              IEnumerable <int> ancestorArtifactTypeIds,
                                                                              IUsersRepository usersRepository,
                                                                              IServiceLogRepository serviceLogRepository,
                                                                              IWebhooksRepository webhooksRepository,
                                                                              IProjectMetaRepository projectMetaRepository)
        {
            var resultMessages = new List <IWorkflowMessage>();
            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);

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

                        Log.Debug($" Skipping Email notification action for artifact {artifactInfo.Id}. Message: Notification.");
                        continue;
                    }
                    resultMessages.Add(notificationMessage);
                    break;

                case MessageActionType.GenerateChildren:
                    var generateChildrenAction = workflowEventTrigger.Action as GenerateChildrenAction;
                    if (generateChildrenAction == null)
                    {
                        continue;
                    }
                    var ancestors = new List <int>(ancestorArtifactTypeIds ?? new int[0]);
                    ancestors.Add(artifactInfo.ItemTypeId);
                    var generateChildrenMessage = new GenerateDescendantsMessage
                    {
                        TransactionId           = transactionId,
                        ChildCount              = generateChildrenAction.ChildCount.GetValueOrDefault(10),
                        DesiredArtifactTypeId   = generateChildrenAction.ArtifactTypeId,
                        ArtifactId              = artifactInfo.Id,
                        AncestorArtifactTypeIds = ancestors,
                        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)
                    {
                        await serviceLogRepository.LogInformation(LogSource, $"Skipping GenerateTestCasesAction for artifact {artifactInfo.Id} as it is not a process");

                        Log.Debug($"Skipping GenerateTestCasesAction for artifact {artifactInfo.Id} as it is not a process. Message: Notification.");
                        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)
                    {
                        await serviceLogRepository.LogInformation(LogSource, $"Skipping GenerateUserStories for artifact {artifactInfo.Id} as it is not a process");

                        Log.Debug($"Skipping GenerateUserStories for artifact {artifactInfo.Id} as it is not a process. Message: Notification.");
                        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 webhooksRepository.GetArtifactsWithPropertyValuesAsync(
                        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             = ((WorkflowMessageArtifactInfo)artifactInfo).ParentId,
                            ArtifactTypeId       = artifactInfo.ItemTypeId,
                            ArtifactTypeName     = artifactType?.Name,
                            BaseArtifactType     = artifactType?.PredefinedType?.ToString(),
                            ArtifactPropertyInfo =
                                await ConvertToWebhookPropertyInfo(artifactPropertyInfos, customTypes.PropertyTypes, usersRepository),
                            State = new WebhookStateInfo
                            {
                                Id         = currentState.Id,
                                Name       = currentState.Name,
                                WorkflowId = currentState.WorkflowId
                            },
                            Revision     = revisionId,
                            Version      = WebhookArtifactVersion,
                            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);

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

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