예제 #1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(
                 "post",
                 Route = Routes.KontentWebhook
                 )] string body,
            IDictionary <string, string> headers,
            string language
            )
        {
            try
            {
                var(valid, getWebhook) = webhookValidator.ValidateWebhook(body, headers);

                if (!valid)
                {
                    return(LogUnauthorized());
                }

                var(data, message) = getWebhook();

                if (data.Items == null)
                {
                    throw new ArgumentNullException(nameof(data.Items));
                }

                switch (message.Type)
                {
                case "content_item_variant":
                    switch (message.Operation)
                    {
                    case "change_workflow_step":
                        var translationLanguage = new CultureInfo(language).TwoLetterISOLanguageName;

                        foreach (var item in data.Items)
                        {
                            await TranslateItem(item, language, translationLanguage);
                        }
                        break;
                    }
                    break;
                }

                return(LogOk());
            }
            catch (ArgumentNullException ex)
            {
                return(LogOkException(ex));
            }
            catch (ApiException ex)
            {
                return(LogOkException(ex));
            }
            catch (Exception ex)
            {
                return(LogException(ex));
            }
        }
예제 #2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(
                 "post",
                 Route = Routes.KontentGatsbyThrottle
                 )] string body,
            IDictionary <string, string> headers,
            string gatsbyWebhook
            )
        {
            try
            {
                var(valid, getWebhook) = webhookValidator.ValidateWebhook(body, headers);

                //if (!valid) return LogUnauthorized();

                var webhook = getWebhook();
                var(data, message) = webhook;

                switch (message.Type)
                {
                case "content_item_variant":
                    switch (message.Operation)
                    {
                    default:
                        var parsedGatsbyWebhook = HttpUtility.UrlDecode(HttpUtility.UrlDecode(gatsbyWebhook));

                        queueClient.SendMessage(JsonConvert.SerializeObject(new QueueItem(webhook, parsedGatsbyWebhook)));
                        debounced(new DebouncedItem(webhook, parsedGatsbyWebhook));

                        // timer checks queue, sends latest
                        break;
                    }
                    break;
                }

                return(LogOk());
            }
            catch (ArgumentNullException ex)
            {
                return(LogOkException(ex));
            }
            catch (ApiException ex)
            {
                return(LogOkException(ex));
            }
            catch (Exception ex)
            {
                return(LogException(ex));
            }
        }
예제 #3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(
                 "post",
                 Route = Routes.GitHubCreatePullRequest
                 )] string body,
            IDictionary <string, string> headers,
            string typeCodename,
            string baseName,
            string repositoryName,
            string headName
            )
        {
            try
            {
                if (string.IsNullOrWhiteSpace(baseName))
                {
                    throw new ApiException("Base name is null or empty.");
                }
                if (string.IsNullOrWhiteSpace(repositoryName))
                {
                    throw new ApiException("Repository name is null or empty.");
                }
                if (string.IsNullOrWhiteSpace(headName))
                {
                    throw new ApiException("Head name is null or empty.");
                }

                var(valid, getWebhook) = webhookValidator.ValidateWebhook(body, headers);

                //if (!valid) return LogUnauthorized();

                var(data, message) = getWebhook();

                switch (message.Type)
                {
                case "content_item_variant":
                    switch (message.Operation)
                    {
                    case "change_workflow_step":
                        if (data.Items == null)
                        {
                            throw new ApiException("Webhook does not have items.");
                        }

                        foreach (var item in data.Items)
                        {
                            if (item.ItemReference == null)
                            {
                                throw new ApiException("Item not available.");
                            }

                            var contentItem = await kontentRepository.RetrieveContentItem(item.ItemReference);

                            if (contentItem.TypeReference == null)
                            {
                                throw new ApiException("Item type not available.");
                            }

                            var contentType = await kontentRepository.RetrieveContentType(contentItem.TypeReference);

                            if (contentType.Codename != typeCodename)
                            {
                                continue;
                            }

                            var languageVariant = await kontentRepository.RetrieveLanguageVariant(new RetrieveLanguageVariantParameters
                            {
                                ItemReference     = item.ItemReference,
                                LanguageReference = item.LanguageReference,
                                TypeReference     = new CodenameReference(typeCodename)
                            });

                            if (languageVariant == null)
                            {
                                throw new ApiException("Variant could not be retrieved.");
                            }
                            if (languageVariant.Elements == null)
                            {
                                throw new ApiException("Variant does not have elements.");
                            }
                            if (contentType.Elements == null)
                            {
                                throw new ApiException("Type does not have elements.");
                            }

                            var(title, description, motivation, readmeUrl, categories, thumbnail, thumbnailType) = await ParseElements(languageVariant.Elements, contentType.Elements);

                            var branch = ToKebabCase(title);

                            await RunPullRequest(
                                $"{baseName}/{repositoryName}",
                                $"{headName}:{branch}",
                                $"{headName}/{repositoryName}",
                                branch,
                                title,
                                description,
                                motivation,
                                readmeUrl,
                                categories,
                                thumbnail,
                                thumbnailType
                                );
                        }
                        break;
                    }
                    break;
                }

                return(LogOk());
            }
            catch (ApiException ex) when(ex.Skip)
            {
                return(LogOkException(ex));
            }
            catch (Exception ex)
            {
                return(LogException(ex));
            }
        }
예제 #4
0
        public async Task <IActionResult> Run(
            [HttpTrigger(
                 "post",
                 Route = Routes.KontentWebhook
                 )] string body,
            IDictionary <string, string> headers,
            string region
            )
        {
            try
            {
                coreContext.SetRegion(region);

                var(valid, getWebhook) = webhookValidator.ValidateWebhook(body, headers, region);

                if (!valid)
                {
                    return(LogUnauthorized());
                }

                var(data, message) = getWebhook();

                var(items, _) = data;

                switch (message.Type)
                {
                case "content_item_variant":
                    switch (message.Operation)
                    {
                    case "publish":
                        foreach (var item in items)
                        {
                            if (item.Type == TransferItem.Codename)
                            {
                                var container = await storageRepository.GetContainer(new GetContainerParameters
                                {
                                    ContainerName = storageRepository.GetSafeContainerName(item.Codename)
                                });

                                if (string.IsNullOrEmpty(container.TransferToken))
                                {
                                    container.TransferToken = storageRepository.EncryptTransferToken(new TransferToken
                                    {
                                        Codename     = item.Codename,
                                        Localization = item.Language,
                                        Region       = coreContext.Region.Name
                                    });
                                }

                                container.DeleteWhen = DateTime.MaxValue;

                                await container.Update();
                            }
                        }
                        break;

                    case "unpublish":
                        foreach (var item in items)
                        {
                            if (item.Type == TransferItem.Codename)
                            {
                                var container = await storageRepository.GetContainer(new GetContainerParameters
                                {
                                    ContainerName = storageRepository.GetSafeContainerName(item.Codename)
                                });

                                container.DeleteWhen = DateTime.UtcNow.AddMonths(1);

                                await container.Update();
                            }
                        }
                        break;

                    case "archive":
                        foreach (var item in items)
                        {
                            if (item.Type == TransferItem.Codename)
                            {
                                var container = await storageRepository.GetContainer(new GetContainerParameters
                                {
                                    ContainerName = storageRepository.GetSafeContainerName(item.Codename)
                                });

                                await container.Delete();
                            }
                        }
                        break;
                    }
                    break;
                }

                return(LogOk());
            }
            catch (Exception ex)
            {
                return(LogException(ex));
            }
        }