Skip to content

Commit

Permalink
Plumb pr creation time through output updater. (#294)
Browse files Browse the repository at this point in the history
* Plumb pr creation time through output updater.

* Add tests.
  • Loading branch information
nishkrishnan authored Aug 10, 2022
1 parent 8953454 commit 7d5ba80
Show file tree
Hide file tree
Showing 2 changed files with 446 additions and 28 deletions.
70 changes: 42 additions & 28 deletions server/events/output_updater.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package events

import (
"context"
"fmt"
"strings"

Expand All @@ -17,6 +18,15 @@ type OutputUpdater interface {
UpdateOutput(ctx *command.Context, cmd PullCommand, res command.Result)
}

type renderer interface {
Render(res command.Result, cmdName command.Name, baseRepo models.Repo) string
RenderProject(prjRes command.ProjectResult, cmdName command.Name, baseRepo models.Repo) string
}

type checksClient interface {
UpdateStatus(ctx context.Context, request types.UpdateStatusRequest) error
}

// [WENGINES-4643] TODO: Remove PullOutputUpdater and default to checks once github checks is stable
// defaults to pull comments if checks is turned off
type FeatureAwareChecksOutputUpdater struct {
Expand Down Expand Up @@ -46,8 +56,8 @@ func (c *FeatureAwareChecksOutputUpdater) UpdateOutput(ctx *command.Context, cmd

// Used to support checks type output (Github checks for example)
type ChecksOutputUpdater struct {
VCSClient vcs.Client
MarkdownRenderer *markdown.Renderer
VCSClient checksClient
MarkdownRenderer renderer
TitleBuilder vcs.StatusTitleBuilder
}

Expand All @@ -56,13 +66,14 @@ func (c *ChecksOutputUpdater) UpdateOutput(ctx *command.Context, cmd PullCommand
if res.Error != nil || res.Failure != "" {
output := c.MarkdownRenderer.Render(res, cmd.CommandName(), ctx.Pull.BaseRepo)
updateStatusReq := types.UpdateStatusRequest{
Repo: ctx.HeadRepo,
Ref: ctx.Pull.HeadCommit,
StatusName: c.TitleBuilder.Build(cmd.CommandName().String()),
PullNum: ctx.Pull.Num,
Description: fmt.Sprintf("%s failed", strings.Title(cmd.CommandName().String())),
Output: output,
State: models.FailedCommitStatus,
Repo: ctx.HeadRepo,
Ref: ctx.Pull.HeadCommit,
StatusName: c.TitleBuilder.Build(cmd.CommandName().String()),
PullNum: ctx.Pull.Num,
Description: fmt.Sprintf("%s failed", strings.Title(cmd.CommandName().String())),
Output: output,
State: models.FailedCommitStatus,
PullCreationTime: ctx.Pull.CreatedAt,
}

if err := c.VCSClient.UpdateStatus(ctx.RequestCtx, updateStatusReq); err != nil {
Expand Down Expand Up @@ -97,13 +108,14 @@ func (c *ChecksOutputUpdater) UpdateOutput(ctx *command.Context, cmd PullCommand

output := c.MarkdownRenderer.RenderProject(projectResult, cmd.CommandName(), ctx.Pull.BaseRepo)
updateStatusReq := types.UpdateStatusRequest{
Repo: ctx.HeadRepo,
Ref: ctx.Pull.HeadCommit,
StatusName: statusName,
PullNum: ctx.Pull.Num,
Description: description,
Output: output,
State: state,
Repo: ctx.HeadRepo,
Ref: ctx.Pull.HeadCommit,
StatusName: statusName,
PullNum: ctx.Pull.Num,
Description: description,
Output: output,
State: state,
PullCreationTime: ctx.Pull.CreatedAt,
}

if err := c.VCSClient.UpdateStatus(ctx.RequestCtx, updateStatusReq); err != nil {
Expand All @@ -118,13 +130,14 @@ func (c *ChecksOutputUpdater) UpdateOutput(ctx *command.Context, cmd PullCommand
func (c *ChecksOutputUpdater) handleApprovePolicies(ctx *command.Context, cmd PullCommand, res command.Result) {
output := c.MarkdownRenderer.Render(res, cmd.CommandName(), ctx.Pull.BaseRepo)
updateStatusReq := types.UpdateStatusRequest{
Repo: ctx.HeadRepo,
Ref: ctx.Pull.HeadCommit,
StatusName: c.TitleBuilder.Build(cmd.CommandName().String()),
PullNum: ctx.Pull.Num,
Description: fmt.Sprintf("%s succeded", strings.Title(cmd.CommandName().String())),
Output: output,
State: models.SuccessCommitStatus,
Repo: ctx.HeadRepo,
Ref: ctx.Pull.HeadCommit,
StatusName: c.TitleBuilder.Build(cmd.CommandName().String()),
PullNum: ctx.Pull.Num,
Description: fmt.Sprintf("%s succeeded", strings.Title(cmd.CommandName().String())),
Output: output,
State: models.SuccessCommitStatus,
PullCreationTime: ctx.Pull.CreatedAt,
}

if err := c.VCSClient.UpdateStatus(ctx.RequestCtx, updateStatusReq); err != nil {
Expand All @@ -147,11 +160,12 @@ func (c *ChecksOutputUpdater) handleApprovePolicies(ctx *command.Context, cmd Pu
}

updateStatusReq := types.UpdateStatusRequest{
Repo: ctx.HeadRepo,
Ref: ctx.Pull.HeadCommit,
StatusName: statusName,
PullNum: ctx.Pull.Num,
State: state,
Repo: ctx.HeadRepo,
Ref: ctx.Pull.HeadCommit,
StatusName: statusName,
PullNum: ctx.Pull.Num,
State: state,
PullCreationTime: ctx.Pull.CreatedAt,
}

if err := c.VCSClient.UpdateStatus(ctx.RequestCtx, updateStatusReq); err != nil {
Expand Down
Loading

0 comments on commit 7d5ba80

Please sign in to comment.