Skip to content

Commit

Permalink
chore: push up updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockopp committed Feb 27, 2024
1 parent b011b7c commit d4efd6b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
6 changes: 4 additions & 2 deletions api/build/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"time"

"github.com/go-vela/server/scm"

"github.com/go-vela/server/api/service"
"github.com/go-vela/server/api/step"
"github.com/go-vela/server/database"
Expand All @@ -19,7 +21,7 @@ import (
// and services, for the build in the configured backend.
// TODO:
// - return build and error.
func PlanBuild(ctx context.Context, database database.Interface, p *pipeline.Build, b *library.Build, r *library.Repo) error {
func PlanBuild(ctx context.Context, database database.Interface, scm scm.Service, p *pipeline.Build, b *library.Build, r *library.Repo) error {
// update fields in build object
b.SetCreated(time.Now().UTC().Unix())

Expand Down Expand Up @@ -49,7 +51,7 @@ func PlanBuild(ctx context.Context, database database.Interface, p *pipeline.Bui
}

// plan all steps for the build
steps, err := step.PlanSteps(ctx, database, p, b)
steps, err := step.PlanSteps(ctx, database, scm, p, b, r)
if err != nil {
// clean up the objects from the pipeline in the database
CleanBuild(ctx, database, b, services, steps, err)
Expand Down
17 changes: 14 additions & 3 deletions api/step/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"time"

"github.com/go-vela/server/scm"

"github.com/go-vela/server/database"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
Expand All @@ -16,7 +18,7 @@ import (
// PlanSteps is a helper function to plan all steps
// in the build for execution. This creates the steps
// for the build in the configured backend.
func PlanSteps(ctx context.Context, database database.Interface, p *pipeline.Build, b *library.Build) ([]*library.Step, error) {
func PlanSteps(ctx context.Context, database database.Interface, scm scm.Service, p *pipeline.Build, b *library.Build, r *library.Repo) ([]*library.Step, error) {
// variable to store planned steps
steps := []*library.Step{}

Expand All @@ -25,7 +27,7 @@ func PlanSteps(ctx context.Context, database database.Interface, p *pipeline.Bui
// iterate through all steps for each pipeline stage
for _, step := range stage.Steps {
// create the step object
s, err := planStep(ctx, database, b, step, stage.Name)
s, err := planStep(ctx, database, scm, b, r, step, stage.Name)
if err != nil {
return steps, err
}
Expand All @@ -47,7 +49,7 @@ func PlanSteps(ctx context.Context, database database.Interface, p *pipeline.Bui
return steps, nil
}

func planStep(ctx context.Context, database database.Interface, b *library.Build, c *pipeline.Container, stage string) (*library.Step, error) {
func planStep(ctx context.Context, database database.Interface, scm scm.Service, b *library.Build, r *library.Repo, c *pipeline.Container, stage string) (*library.Step, error) {
// create the step object
s := new(library.Step)
s.SetBuildID(b.GetID())
Expand All @@ -59,6 +61,15 @@ func planStep(ctx context.Context, database database.Interface, b *library.Build
s.SetStatus(constants.StatusPending)
s.SetCreated(time.Now().UTC().Unix())

id, err := scm.CreateChecks(ctx, r, b.GetCommit(), s.GetName())
if err != nil {
// TODO: make this error more meaningful
return nil, err
}

// TODO: have to store the check ID somewhere
s.SetCheckID(id)

Check failure on line 71 in api/step/plan.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/step/plan.go#L71

s.SetCheckID undefined (type *library.Step has no field or method SetCheckID) (typecheck)
Raw output
api/step/plan.go:71:4: s.SetCheckID undefined (type *library.Step has no field or method SetCheckID) (typecheck)
	s.SetCheckID(id)
	  ^

// send API call to create the step
s, err := database.CreateStep(ctx, s)

Check failure on line 74 in api/step/plan.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/step/plan.go#L74

no new variables on left side of := (typecheck)
Raw output
api/step/plan.go:74:9: no new variables on left side of := (typecheck)
	s, err := database.CreateStep(ctx, s)
	       ^
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions api/step/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/http"

"github.com/go-vela/server/scm"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/build"
Expand Down Expand Up @@ -155,5 +157,14 @@ func UpdateStep(c *gin.Context) {
return
}

err = scm.FromContext(c).UpdateChecks(ctx, r, s, b.GetCommit())
if err != nil {
retErr := fmt.Errorf("unable to set step check %s: %w", entry, err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

c.JSON(http.StatusOK, s)
}
53 changes: 43 additions & 10 deletions scm/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,14 +587,13 @@ func (c *client) GetBranch(ctx context.Context, u *library.User, r *library.Repo
}

// CreateChecks defines a function that does stuff...
func (c *client) CreateChecks(ctx context.Context, r *library.Repo, s *library.Step, branch string) (int64, error) {
func (c *client) CreateChecks(ctx context.Context, r *library.Repo, commit, step string) (int64, error) {
// create client from GitHub App
client := c.newGithubAppToken(r)

opts := github.CreateCheckRunOptions{
// TODO: add step name?
Name: fmt.Sprintf("vela-%s-%s", branch, s.GetName()),
HeadSHA: branch,
Name: fmt.Sprintf("vela-%s-%s", commit, step),
HeadSHA: commit,
}

check, _, err := client.Checks.CreateCheckRun(ctx, r.GetOrg(), r.GetName(), opts)
Expand All @@ -606,18 +605,52 @@ func (c *client) CreateChecks(ctx context.Context, r *library.Repo, s *library.S
}

// UpdateChecks defines a function that does stuff...
func (c *client) UpdateChecks(ctx context.Context, r *library.Repo, s *library.Step, id int64, branch string) error {
func (c *client) UpdateChecks(ctx context.Context, r *library.Repo, s *library.Step, commit string) error {
// create client from GitHub App
client := c.newGithubAppToken(r)

var (
conclusion string
status string
)
// set the conclusion and status for the step check depending on what the status of the step is
switch s.GetStatus() {
case constants.StatusPending:
conclusion = "neutral"
status = "queued"
case constants.StatusPendingApproval:
conclusion = "action_required"
status = "queued"
case constants.StatusRunning:
conclusion = "neutral"
status = "in_progress"
case constants.StatusSuccess:
conclusion = "success"
status = "completed"
case constants.StatusFailure:
conclusion = "failure"
status = "completed"
case constants.StatusCanceled:
conclusion = "cancelled"
status = "completed"
case constants.StatusKilled:
conclusion = "cancelled"
status = "completed"
case constants.StatusSkipped:
conclusion = "skipped"
status = "completed"
default:
conclusion = "neutral"
status = "completed"
}

opts := github.UpdateCheckRunOptions{
// TODO: add step name?
Name: fmt.Sprintf("vela-%s-%s", branch, s.GetName()),
Status: github.String("completed"),
Conclusion: github.String("success"),
Name: fmt.Sprintf("vela-%s-%s", commit, s.GetName()),
Conclusion: github.String(conclusion),
Status: github.String(status),
}

_, _, err := client.Checks.UpdateCheckRun(ctx, r.GetOrg(), r.GetName(), id, opts)
_, _, err := client.Checks.UpdateCheckRun(ctx, r.GetOrg(), r.GetName(), s.GetCheckID(), opts)

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/repo.go#L653

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)) (typecheck)
Raw output
scm/github/repo.go:653:76: s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)) (typecheck)
	"github.com/go-vela/server/scm/github"
	^

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/repo.go#L653

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)) (typecheck)
Raw output
scm/github/repo.go:653:76: s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)) (typecheck)
	"github.com/go-vela/server/scm/github"
	^

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/repo.go#L653

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)) (typecheck)
Raw output
scm/github/repo.go:653:76: s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)) (typecheck)
	"github.com/go-vela/server/scm/github"
	^

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/repo.go#L653

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID) (typecheck)
Raw output
scm/github/repo.go:653:76: s.GetCheckID undefined (type *library.Step has no field or method GetCheckID) (typecheck)
// SPDX-License-Identifier: Apache-2.0

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / build

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / build

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / validate

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / validate

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / test

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / test

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)

Check failure on line 653 in scm/github/repo.go

View workflow job for this annotation

GitHub Actions / Analyze (go)

s.GetCheckID undefined (type *library.Step has no field or method GetCheckID)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions scm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ type Service interface {
GetHTMLURL(context.Context, *library.User, string, string, string, string) (string, error)

// TODO: add comments
CreateChecks(context.Context, *library.Repo, *library.Step, string) (int64, error)
UpdateChecks(context.Context, *library.Repo, *library.Step, int64, string) error
CreateChecks(context.Context, *library.Repo, string, string) (int64, error)
UpdateChecks(context.Context, *library.Repo, *library.Step, string) error

// Webhook SCM Interface Functions

Expand Down

0 comments on commit d4efd6b

Please sign in to comment.