Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(repo)!: nested repository with migration from types #1095

Merged
merged 11 commits into from
Apr 10, 2024
5 changes: 2 additions & 3 deletions api/admin/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"fmt"
"net/http"

"github.com/go-vela/server/api/types"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we move everything to this pattern, any thoughts on giving every types import the api alias?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was operating on the following pattern:

  • go-vela/server/api —> types
  • anything else —> api

But I'm open to using api always if people would prefer that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool that works for me, not blocking either way

"github.com/go-vela/server/database"
"github.com/go-vela/server/util"

"github.com/go-vela/types/library"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -55,7 +54,7 @@ func UpdateRepo(c *gin.Context) {
ctx := c.Request.Context()

// capture body from API request
input := new(library.Repo)
input := new(types.Repo)

err := c.Bind(input)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions api/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"net/url"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/util"
"github.com/go-vela/types"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -38,7 +38,7 @@ import (
// process a user logging in to Vela.
func Login(c *gin.Context) {
// load the metadata
m := c.MustGet("metadata").(*types.Metadata)
m := c.MustGet("metadata").(*internal.Metadata)

// capture query params
t := util.FormParameter(c, "type")
Expand Down
4 changes: 2 additions & 2 deletions api/auth/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"net/url"

"github.com/go-vela/server/database"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"

"github.com/go-vela/types"
"github.com/go-vela/types/constants"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -41,7 +41,7 @@ import (
// refresh token cookie.
func Logout(c *gin.Context) {
// grab the metadata to help deal with the cookie
m := c.MustGet("metadata").(*types.Metadata)
m := c.MustGet("metadata").(*internal.Metadata)
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()
Expand Down
4 changes: 2 additions & 2 deletions api/auth/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/util"
"github.com/go-vela/types"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -68,7 +68,7 @@ import (
// This will only handle non-headless flows (ie. web or cli).
func GetAuthRedirect(c *gin.Context) {
// load the metadata
m := c.MustGet("metadata").(*types.Metadata)
m := c.MustGet("metadata").(*internal.Metadata)

logrus.Info("redirecting for final auth flow destination")

Expand Down
16 changes: 3 additions & 13 deletions api/build/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/queue"
"github.com/go-vela/server/queue/models"
"github.com/go-vela/server/router/middleware/build"
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
"github.com/go-vela/types"
"github.com/go-vela/types/constants"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -105,23 +105,13 @@ func ApproveBuild(c *gin.Context) {

logger.Debugf("user %s approved build %s/%d for execution", u.GetName(), r.GetFullName(), b.GetNumber())

// send API call to capture the repo owner
owner, err := database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
retErr := fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err)

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

return
}

// set fields
b.SetStatus(constants.StatusPending)
b.SetApprovedAt(time.Now().Unix())
b.SetApprovedBy(u.GetName())

// update the build in the db
_, err = database.FromContext(c).UpdateBuild(ctx, b)
_, err := database.FromContext(c).UpdateBuild(ctx, b)
if err != nil {
logrus.Errorf("Failed to update build %d during publish to queue for %s: %v", b.GetNumber(), r.GetFullName(), err)
}
Expand All @@ -131,7 +121,7 @@ func ApproveBuild(c *gin.Context) {
ctx,
queue.FromGinContext(c),
database.FromContext(c),
types.ToItem(b, r, owner),
models.ToItem(b, r),
b.GetHost(),
)

Expand Down
7 changes: 4 additions & 3 deletions api/build/auto_cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/api/types"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal/token"
"github.com/go-vela/types/constants"
Expand All @@ -21,7 +22,7 @@ import (

// AutoCancel is a helper function that checks to see if any pending or running
// builds for the repo can be replaced by the current build.
func AutoCancel(c *gin.Context, b *library.Build, rB *library.Build, r *library.Repo, cancelOpts *pipeline.CancelOptions) (bool, error) {
func AutoCancel(c *gin.Context, b *library.Build, rB *library.Build, r *types.Repo, cancelOpts *pipeline.CancelOptions) (bool, error) {
// if build is the current build, continue
if rB.GetID() == b.GetID() {
return false, nil
Expand Down Expand Up @@ -73,8 +74,8 @@ func AutoCancel(c *gin.Context, b *library.Build, rB *library.Build, r *library.

// cancelRunning is a helper function that determines the executor currently running a build and sends an API call
// to that executor's worker to cancel the build.
func cancelRunning(c *gin.Context, b *library.Build, r *library.Repo) error {
e := new([]library.Executor)
func cancelRunning(c *gin.Context, b *library.Build, r *types.Repo) error {
e := new([]types.Executor)
// retrieve the worker
w, err := database.FromContext(c).GetWorkerForHostname(c, b.GetHost())
if err != nil {
Expand Down
38 changes: 15 additions & 23 deletions api/build/compile_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"strings"
"time"

"github.com/go-vela/server/api/types"
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/queue"
"github.com/go-vela/server/queue/models"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/util"
"github.com/go-vela/types"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
Expand All @@ -26,8 +28,8 @@ import (
// CompileAndPublishConfig is a struct that contains information for the CompileAndPublish function.
type CompileAndPublishConfig struct {
Build *library.Build
Repo *library.Repo
Metadata *types.Metadata
Repo *types.Repo
Metadata *internal.Metadata
BaseErr string
Source string
Comment string
Expand All @@ -47,27 +49,17 @@ func CompileAndPublish(
scm scm.Service,
compiler compiler.Engine,
queue queue.Service,
) (*pipeline.Build, *types.Item, error) {
) (*pipeline.Build, *models.Item, error) {
logrus.Debugf("generating queue items for build %s/%d", cfg.Repo.GetFullName(), cfg.Build.GetNumber())

// assign variables from form for readibility
r := cfg.Repo
u := cfg.Repo.GetOwner()
b := cfg.Build
baseErr := cfg.BaseErr

// send API call to capture repo owner
logrus.Debugf("capturing owner of repository %s", cfg.Repo.GetFullName())

u, err := database.GetUser(c, r.GetUserID())
if err != nil {
retErr := fmt.Errorf("%s: failed to get owner for %s: %w", baseErr, r.GetFullName(), err)
util.HandleError(c, http.StatusBadRequest, retErr)

return nil, nil, retErr
}

// confirm current repo owner has at least write access to repo (needed for status update later)
_, err = scm.RepoAccess(c, u.GetName(), u.GetToken(), r.GetOrg(), r.GetName())
_, err := scm.RepoAccess(c, u.GetName(), u.GetToken(), r.GetOrg(), r.GetName())
if err != nil {
retErr := fmt.Errorf("unable to publish build to queue: repository owner %s no longer has write access to repository %s", u.GetName(), r.GetFullName())
util.HandleError(c, http.StatusUnauthorized, retErr)
Expand Down Expand Up @@ -97,7 +89,7 @@ func CompileAndPublish(
return nil, nil, retErr
}

commit, branch, baseref, headref, err := scm.GetPullRequest(c, u, r, prNum)
commit, branch, baseref, headref, err := scm.GetPullRequest(c, r, prNum)
if err != nil {
retErr := fmt.Errorf("%s: failed to get pull request info for %s: %w", baseErr, r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand All @@ -114,7 +106,7 @@ func CompileAndPublish(
// if the source is from a schedule, fetch the commit sha from schedule branch (same as build branch at this moment)
if strings.EqualFold(cfg.Source, "schedule") {
// send API call to capture the commit sha for the branch
_, commit, err := scm.GetBranch(c, u, r, b.GetBranch())
_, commit, err := scm.GetBranch(c, r, b.GetBranch())
if err != nil {
retErr := fmt.Errorf("failed to get commit for repo %s on %s branch: %w", r.GetFullName(), r.GetBranch(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand Down Expand Up @@ -170,7 +162,7 @@ func CompileAndPublish(
!strings.EqualFold(b.GetEvent(), constants.EventPull) &&
!strings.EqualFold(b.GetEvent(), constants.EventDelete) {
// send API call to capture list of files changed for the commit
files, err = scm.Changeset(c, u, r, b.GetCommit())
files, err = scm.Changeset(c, r, b.GetCommit())
if err != nil {
retErr := fmt.Errorf("%s: failed to get changeset for %s: %w", baseErr, r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand All @@ -182,7 +174,7 @@ func CompileAndPublish(
// check if the build event is a pull_request
if strings.EqualFold(b.GetEvent(), constants.EventPull) && prNum > 0 {
// send API call to capture list of files changed for the pull request
files, err = scm.ChangesetPR(c, u, r, prNum)
files, err = scm.ChangesetPR(c, r, prNum)
if err != nil {
retErr := fmt.Errorf("%s: failed to get changeset for %s: %w", baseErr, r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand All @@ -201,7 +193,7 @@ func CompileAndPublish(
// variable to store the pipeline type for the repository
pipelineType = r.GetPipelineType()
// variable to store updated repository record
repo *library.Repo
repo *types.Repo
)

// implement a loop to process asynchronous operations with a retry limit
Expand Down Expand Up @@ -324,7 +316,7 @@ func CompileAndPublish(
}

return nil,
&types.Item{
&models.Item{
Build: b,
},
errors.New(skip)
Expand Down Expand Up @@ -448,7 +440,7 @@ func CompileAndPublish(
return nil, nil, retErr
}

return p, types.ToItem(b, repo, u), nil
return p, models.ToItem(b, repo), nil
}

// getPRNumberFromBuild is a helper function to
Expand Down
4 changes: 2 additions & 2 deletions api/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/queue"
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/util"
"github.com/go-vela/types"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -74,7 +74,7 @@ import (
// CreateBuild represents the API handler to create a build in the configured backend.
func CreateBuild(c *gin.Context) {
// capture middleware values
m := c.MustGet("metadata").(*types.Metadata)
m := c.MustGet("metadata").(*internal.Metadata)
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
Expand Down
4 changes: 2 additions & 2 deletions api/build/enqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

"github.com/go-vela/server/database"
"github.com/go-vela/server/queue"
"github.com/go-vela/types"
"github.com/go-vela/server/queue/models"
"github.com/sirupsen/logrus"
)

// Enqueue is a helper function that pushes a queue item (build, repo, user) to the queue.
func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, item *types.Item, route string) {
func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, item *models.Item, route string) {
logrus.Infof("Converting queue item to json for build %d for %s", item.Build.GetNumber(), item.Repo.GetFullName())

byteItem, err := json.Marshal(item)
Expand Down
3 changes: 2 additions & 1 deletion api/build/get_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/api/types"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/scm"
Expand Down Expand Up @@ -51,7 +52,7 @@ func GetBuildByID(c *gin.Context) {
// Variables that will hold the library types of the build and repo
var (
b *library.Build
r *library.Repo
r *types.Repo
)

// Capture user from middleware
Expand Down
6 changes: 3 additions & 3 deletions api/build/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/router/middleware/build"
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/util"
"github.com/go-vela/types"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
Expand Down Expand Up @@ -142,7 +142,7 @@ func GetBuildGraph(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
m := c.MustGet("metadata").(*types.Metadata)
m := c.MustGet("metadata").(*internal.Metadata)
ctx := c.Request.Context()

// update engine logger with API metadata
Expand Down Expand Up @@ -194,7 +194,7 @@ func GetBuildGraph(c *gin.Context) {
// check if the build event is not pull_request
if !strings.EqualFold(b.GetEvent(), constants.EventPull) {
// send API call to capture list of files changed for the commit
files, err = scm.FromContext(c).Changeset(ctx, u, r, b.GetCommit())
files, err = scm.FromContext(c).Changeset(ctx, r, b.GetCommit())
if err != nil {
retErr := fmt.Errorf("%s: failed to get changeset for %s: %w", baseErr, r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/build/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/go-vela/server/api/service"
"github.com/go-vela/server/api/step"
"github.com/go-vela/server/api/types"
"github.com/go-vela/server/database"
"github.com/go-vela/server/scm"
"github.com/go-vela/types/library"
Expand All @@ -20,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, scm scm.Service, 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 *types.Repo) error {
// update fields in build object
b.SetCreated(time.Now().UTC().Unix())

Expand Down
Loading
Loading