Example #1
0
        public async void TestGithubPayload()
        {
            string text = await File.ReadAllTextAsync("../../../Jsons/hook_issue_github_payload.json");

            JsonDocument doc = JsonDocument.Parse(text);

            HookIssue hookIssue = service.HandleGithubPayload(doc.RootElement);

            Console.WriteLine(hookIssue);

            Assert.True(hookIssue.Issue.Title == "Spelling error in the README file", "Title is wrong");
            Assert.True(hookIssue.Existing, "The issue already does not exist");
        }
Example #2
0
        public async Task <IActionResult> OnHook(string hook, [FromBody] JsonElement json)
        {
            if (db.Projects.Where(p => p.Hook == hook).Any())
            {
                if (hookService.VerifyGithubHeaders(Request.Headers))
                {
                    HookIssue hookIssue = hookService.HandleGithubPayload(json);

                    // Mapping values
                    hookIssue.Issue.IssueFrom  = hookIssue.IssueFrom.ToString();
                    hookIssue.Issue.IssueState = hookIssue.IssueState.ToString();

                    if (hookIssue.Existing) // Prexisting issue to be updated
                    {
                        Issue toBeUpdatedIssue = await db.Issues.FirstOrDefaultAsync(p => p.IssueUrl == hookIssue.Issue.IssueUrl);

                        if (toBeUpdatedIssue != null)
                        {
                            toBeUpdatedIssue.IssueFrom     = hookIssue.Issue.IssueFrom;
                            toBeUpdatedIssue.IssueState    = hookIssue.Issue.IssueState;
                            toBeUpdatedIssue.Title         = hookIssue.Issue.Title;
                            toBeUpdatedIssue.Description   = hookIssue.Issue.Description;
                            toBeUpdatedIssue.RepositoryUrl = hookIssue.Issue.RepositoryUrl;
                            toBeUpdatedIssue.IssueUrl      = hookIssue.Issue.IssueUrl;

                            await db.SaveChangesAsync();
                        }
                        else
                        {
                            return(BadRequest());
                        }
                    }
                    else   // A new issue
                    {
                        db.Issues.Add(hookIssue.Issue);
                    }

                    return(Ok());
                }

                return(BadRequest());
            }

            return(NotFound());
        }