public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");


            req.Headers.TryGetValue("X-GitHub-Event", out StringValues eventName);
            req.Headers.TryGetValue("X-Hub-Signature", out StringValues signature);
            req.Headers.TryGetValue("X-GitHub-Delivery", out StringValues delivery);
            log.LogInformation($"Event:{eventName}, Signature:{signature}, Delivery:{delivery}");


            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            log.LogInformation($"Received request payload:{requestBody}");

            if (IsGithubPushAllowed(requestBody, eventName, signature))
            {
                log.LogInformation($"GitHubEventType: {eventName}");
                var reqJSON = JObject.Parse(requestBody);


                if (eventName == "issues")
                {
                    var url   = (string)reqJSON["issue"]["url"];
                    var title = (string)reqJSON["issue"]["title"];
                    log.LogInformation($"[JSON] Recieved an issue from:{url} with title: {title}");
                    var issueJSON = reqJSON["issue"].ToString();
                    // Lets convert the JSON into a GitHub Issue
                    GitHubIssue issue = JsonConvert.DeserializeObject <GitHubIssue>(issueJSON);
                    log.LogInformation($"Recieved an issue from:{issue.Url} with title: {issue.Title}");

                    // Loading the users.
                    AzureStorage storage = new AzureStorage(log);
                    log.LogInformation($"Initialized Storage.");


                    State state = new State(log, storage);
                    log.LogInformation($"Initialized state.");
                    state.UpdateGitHubIssue(issue);


                    // Create the AzDevOps controller, and add the work item.

                    return((ActionResult) new OkObjectResult($"works with configured GitHub secret"));
                }
                else
                {
                    return((ActionResult) new BadRequestObjectResult($"Unsupported Github event type:{eventName}"));
                }
            }

            return(new StatusCodeResult((int)System.Net.HttpStatusCode.Unauthorized));
        }
Пример #2
0
        public void UpdateGitHubIssue(GitHubIssue issue)
        {
            // Check with storage if GitHub issue exists.
            var azDevOpsLabel = issue.GetAzDevOpsLabel();

            if (azDevOpsLabel.HasValue)
            {
                var ticket = _storage.GetTicket(issue);

                // If ticket does not exist, create a WI in AzDevOps and create the corresponding MAP.
                if (!ticket.HasValue)
                {
                    try
                    {
                        ticket = _storage.CreateTicket(issue, Option.None);
                        var workItem = _azDevOps.CreateWorkItem(ticket.Value);
                        // NOTE: We always store VSO work items as a ticket to have more structure around the data.
                        _storage.UpdateTicket(ticket.Value, workItem);
                    }
                    catch (System.NullReferenceException ex)
                    {
                        _log.LogCritical($"Unable to create the Azure Dev Ops workitem corresponding to issue:{issue.Url}:{ex.Message}");
                        throw;
                    }
                }
                else
                {
                    try
                    {
                        // Update the issue on the ticket since the GitHub issue details might have changed.
                        ticket = _storage.UpdateTicket(ticket.Value, issue);

                        _log.LogInformation($"Updating work item:{ticket.Value.AzDevOpsWorkItemID} for issue:{ticket.Value.GitHubIssueId}");
                        var result = _azDevOps.UpdateWorkItem(ticket.Value);
                    }
                    catch (System.NullReferenceException ex)
                    {
                        _log.LogCritical($"Unable to update the Azure Dev Ops workitem corresponding to issue:{issue.Url}:{ex.Message}");
                        throw;
                    }
                    catch (System.ArgumentException ex)
                    {
                        _log.LogCritical($"Unable to update the Azure Dev Ops workitem corresponding to issue due to non-existent work item:{issue.Url}:{ex.Message}");
                        throw;
                    }
                }
            }
        }
Пример #3
0
        public Option <Ticket> GetTicket(GitHubIssue issue)
        {
            Option <Ticket> ticket     = Option.None;
            Option <string> issueLabel = issue.GetAzDevOpsLabel();

            if (issueLabel.HasValue)
            {
                TableOperation retrieveOperation = TableOperation.Retrieve <Ticket>(issueLabel.Value, issue.Number.ToString());


                try
                {
                    TableResult result = _issuesTable.ExecuteAsync(retrieveOperation).GetAwaiter().GetResult();
                    ticket = result.Result as Ticket;
                }
                catch (StorageException ex)
                {
                    _log.LogInformation($"Unable to retreieve work item map from storage table:{ex.Message}, type:{ex.Data.ToString()}");
                }
            }

            return(ticket);
        }
Пример #4
0
 public Option <Ticket> UpdateTicket(Ticket ticket, GitHubIssue issue)
 {
     ticket.UpdateGitHubIssue(issue, _users, _productBackLog);
     return(updateTicket(ticket));
 }
Пример #5
0
 public Option <Ticket> CreateTicket(GitHubIssue issue, Option <WorkItem> workItem)
 {
     return(UpdateTicket(new Ticket(issue, _users, _productBackLog, workItem), issue));
 }