Exemplo n.º 1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(
                 "get",
                 Route = Routes.KontentChangeTypeGetTypes
                 )] string body,
            string itemCodename
            )
        {
            try
            {
                var itemReference = new CodenameReference(itemCodename);

                var allTypes = await kontentRepository.ListContentTypes();

                foreach (var type in allTypes)
                {
                    if (type.Elements == null)
                    {
                        throw new NotImplementedException("Item type does not have elements.");
                    }

                    var newTypeElements = new List <ElementType>(type.Elements);

                    foreach (var typeElement in type.Elements)
                    {
                        if (typeElement.Snippet != null)
                        {
                            var itemTypeSnippet = await kontentRepository.RetrieveContentTypeSnippet(typeElement.Snippet);

                            if (itemTypeSnippet.Elements == null)
                            {
                                throw new NotImplementedException("Item type snippet does not have elements.");
                            }

                            foreach (var typeSnippetElement in itemTypeSnippet.Elements)
                            {
                                newTypeElements.Add(typeSnippetElement);
                            }
                        }
                    }

                    var supportingElements = new[] { "snippet", "guidelines" };

                    type.Elements = newTypeElements.Where(element => !supportingElements.Contains(element.Type)).ToList();
                }

                var item = await kontentRepository.RetrieveContentItem(itemReference);

                return(LogOkObject(new
                {
                    CurrentType = allTypes.First(type => type.Id == item.TypeReference?.Value),
                    OtherTypes = allTypes.Where(type => type.Id != item.TypeReference?.Value)
                }));
            }
            catch (Exception ex)
            {
                return(LogException(ex));
            }
        }
Exemplo n.º 2
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));
            }
        }