protected override async Task <EventHandlerResult> executeCore(WorkflowRunEventPayload webhookPayload) { if (webhookPayload.Action.Equals("completed", StringComparison.OrdinalIgnoreCase)) { if (string.IsNullOrEmpty(webhookPayload.Sender?.Login)) { return(EventHandlerResult.PayloadError("missing actor user")); } if (await isUserOrganizationMember(webhookPayload, webhookPayload.Sender.Login)) { return(EventHandlerResult.NoActionNeeded("workflow_run ok, not triggered by student")); } var workflowRuns = await GitHubClient.CountWorkflowRunsInRepository(webhookPayload.Repository.Owner.Login, webhookPayload.Repository.Name, webhookPayload.Sender.Login); if (workflowRuns <= WorkflowRunThreshold) { return(EventHandlerResult.NoActionNeeded("workflow_run ok, has less then threshold")); } var prNum = await getMostRecentPullRequest(webhookPayload); if (prNum.HasValue) { await GitHubClient.Issue.Comment.Create(webhookPayload.Repository.Id, prNum.Value, WarningText); } return(EventHandlerResult.ActionPerformed("workflow_run warning, threshold exceeded")); } return(EventHandlerResult.EventNotOfInterest(webhookPayload.Action)); }
protected async Task <EventHandlerResult> processComment(ICommentPayload <T> webhookPayload) { var gradeCommand = new GradeCommentParser(webhookPayload.CommentBody); if (gradeCommand.IsMatch) { if (!await isAllowed(webhookPayload)) { return(await handleUserNotAllowed(webhookPayload)); } var pr = await getPullRequest(webhookPayload); if (pr == null) { return(await handleNotPr(webhookPayload)); } await handleApprove(webhookPayload, pr); await handleStoreGrade(webhookPayload, gradeCommand, pr); await handleReaction(webhookPayload, ReactionType.Plus1); return(EventHandlerResult.ActionPerformed($"comment operation to grade done; grades: {string.Join(" ", gradeCommand.Grades)}")); } else { return(EventHandlerResult.NoActionNeeded("not recognized as command")); } }
protected override async Task <EventHandlerResult> execute(PullRequestEventPayload webhookPayload, RepositorySettings repoSettings) { if (webhookPayload.PullRequest == null) { return(EventHandlerResult.PayloadError("no pull request information in webhook payload")); } if (repoSettings.ReviewerToAssignee == null || !repoSettings.ReviewerToAssignee.Enabled) { return(EventHandlerResult.Disabled()); } if (webhookPayload.Action.Equals("review_requested", StringComparison.OrdinalIgnoreCase)) { if (webhookPayload.PullRequest.RequestedReviewers == null || webhookPayload.PullRequest.RequestedReviewers.Count == 0) { return(EventHandlerResult.PayloadError("no requested reviewer in webhook payload")); } else if (!isPrAssignedToReviewer(webhookPayload)) { await GitHubClient.Issue.Assignee.AddAssignees(webhookPayload.Repository.Owner.Login, webhookPayload.Repository.Name, webhookPayload.PullRequest.Number, getUsersToAssign(webhookPayload)); return(EventHandlerResult.ActionPerformed("pull request review_requested handled, assignee set")); } else { return(EventHandlerResult.NoActionNeeded("pull request review_requested is ok, assignee is present")); } } return(EventHandlerResult.EventNotOfInterest(webhookPayload.Action)); }
protected override async Task <EventHandlerResult> execute(IssueCommentPayload webhookPayload, RepositorySettings repoSettings) { if (webhookPayload.Issue == null) { return(EventHandlerResult.PayloadError("no issue information in webhook payload")); } if (repoSettings.CommentProtection == null || !repoSettings.CommentProtection.Enabled) { return(EventHandlerResult.Disabled()); } if (webhookPayload.Action.Equals("edited", StringComparison.OrdinalIgnoreCase) || webhookPayload.Action.Equals("deleted", StringComparison.OrdinalIgnoreCase)) { if (webhookPayload.Sender != null && webhookPayload.Comment?.User != null && webhookPayload.Sender.Login == webhookPayload.Comment.User.Login) { return(EventHandlerResult.NoActionNeeded($"comment action {webhookPayload.Action} by {webhookPayload.Sender.Login} allowed, referencing own comment")); } else { await GitHubClient.Issue.Comment.Create(webhookPayload.Repository.Id, webhookPayload.Issue.Number, getWarningText(repoSettings.CommentProtection)); return(EventHandlerResult.ActionPerformed("comment action resulting in warning")); } } return(EventHandlerResult.EventNotOfInterest(webhookPayload.Action)); }
protected override async Task <EventHandlerResult> executeCore(PullRequestEventPayload webhookPayload) { if (webhookPayload.PullRequest == null) { return(EventHandlerResult.PayloadError("no pull request information in webhook payload")); } if (webhookPayload.Action.Equals("opened", StringComparison.OrdinalIgnoreCase)) { var repositoryPrs = await getAllRepoPullRequests(webhookPayload); if (repositoryPrs.Count <= 1) { return(EventHandlerResult.NoActionNeeded("pull request open is ok, there are no other PRs")); } else { var(handledOpen, resultOpen) = await handleAnyOpenPrs(webhookPayload, repositoryPrs); var(handledClosed, resultClosed) = await handleAnyClosedPrs(webhookPayload, repositoryPrs); if (!handledOpen && !handledClosed) { return(EventHandlerResult.NoActionNeeded($"{resultOpen}; {resultClosed}")); } else { return(EventHandlerResult.ActionPerformed($"{resultOpen}; {resultClosed}")); } } } return(EventHandlerResult.EventNotOfInterest(webhookPayload.Action)); }
private async Task <EventHandlerResult> handleUserNotAllowed(ICommentPayload <T> webhookPayload) { await handleReaction(webhookPayload, ReactionType.Confused); var comment = WarningText.Replace("{}", webhookPayload.CommentingUser, StringComparison.OrdinalIgnoreCase); await GitHubClient.Issue.Comment.Create(webhookPayload.Repository.Id, webhookPayload.PullRequestNumber, comment); return(EventHandlerResult.ActionPerformed("comment operation to grade not allowed for user")); }
protected override async Task <EventHandlerResult> executeCore(CreateEventPayload webhookPayload) { if (!webhookPayload.RefType.StringValue.Equals("branch", StringComparison.OrdinalIgnoreCase)) { return(EventHandlerResult.NoActionNeeded($"create event for ref {webhookPayload.RefType} is not of interest")); } await GitHubClient.Repository.Branch.UpdateBranchProtection( webhookPayload.Repository.Id, webhookPayload.Ref, getBranchProtectionSettingsUpdate(webhookPayload.Ref, webhookPayload.Repository.DefaultBranch)); return(EventHandlerResult.ActionPerformed("branch protection rule applied")); }
private async Task <EventHandlerResult> OkCommandExecute(string commentContents, IssueCommentPayload payload) { var merge = new MergePullRequest(); merge.CommitTitle = $"merged PR via +ok: #{payload.Issue.Number} {payload.Issue.Title}"; var result = await GitHubClient.PullRequest.Merge(payload.Repository.Id, payload.Issue.Number, merge); return(result.Merged ? EventHandlerResult.ActionPerformed( $"merged pull request #{payload.Issue.Number} {payload.Issue.Title}") : EventHandlerResult.PayloadError( $"failed to merge pull request ${payload.Issue.Number} {payload.Issue.Title}")); }
private async Task <EventHandlerResult> handleNotPr(ICommentPayload <T> webhookPayload) { await handleReaction(webhookPayload, ReactionType.Confused); return(EventHandlerResult.ActionPerformed("comment operation to grade not called for PR")); }