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
4 changes: 2 additions & 2 deletions api/build/compile_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,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 @@ -174,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 Down
2 changes: 1 addition & 1 deletion api/build/graph.go
Original file line number Diff line number Diff line change
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
23 changes: 18 additions & 5 deletions api/types/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,37 @@ func ToString(v interface{}) string {

// helper function to unmarshal a parameter in map format.
func unmarshalMap(v interface{}) string {
yml, _ := yaml.Marshal(v)
out, _ := json.YAMLToJSON(yml)
yml, err := yaml.Marshal(v)
if err != nil {
return err.Error()
}

out, err := json.YAMLToJSON(yml)
if err != nil {
return err.Error()
}

return string(out)
}

// helper function to unmarshal a parameter in slice format.
func unmarshalSlice(v interface{}) string {
out, _ := yaml.Marshal(v)
out, err := yaml.Marshal(v)
if err != nil {
return err.Error()
}

in := []string{}

err := yaml.Unmarshal(out, &in)
err = yaml.Unmarshal(out, &in)
if err == nil {
return strings.Join(in, ",")
}

out, _ = json.YAMLToJSON(out)
out, err = json.YAMLToJSON(out)
if err != nil {
return err.Error()
}

return string(out)
}
2 changes: 1 addition & 1 deletion api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@

defer func() {
// send API call to update the webhook
_, err = database.FromContext(c).UpdateHook(ctx, h)

Check failure on line 182 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L182

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:182:32: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		_, err = database.FromContext(c).UpdateHook(ctx, h)
		                             ^
if err != nil {
logrus.Errorf("unable to update webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err)
}
Expand Down Expand Up @@ -308,7 +308,7 @@
queue.FromContext(c),
)

// capture the build, repo, and user from the items
// capture the build and repo from the items
b, repo = item.Build, item.Repo

// set hook build_id to the generated build id
Expand Down Expand Up @@ -364,7 +364,7 @@
build := append(d.GetBuilds(), b)
d.SetBuilds(build)
_, err := database.FromContext(c).UpdateDeployment(ctx, d)
if err != nil {

Check failure on line 367 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L367

only one cuddle assignment allowed before if statement (wsl)
Raw output
api/webhook/post.go:367:4: only one cuddle assignment allowed before if statement (wsl)
			if err != nil {
			^
retErr := fmt.Errorf("%s: failed to update deployment %s/%d: %w", baseErr, repo.GetFullName(), d.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)

Expand Down Expand Up @@ -499,7 +499,7 @@
case "archived", "unarchived", constants.ActionEdited:
logrus.Debugf("repository action %s for %s", h.GetEventAction(), r.GetFullName())
// send call to get repository from database
dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())

Check failure on line 502 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L502

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:502:38: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())
		                                   ^
if err != nil {
retErr := fmt.Errorf("%s: failed to get repo %s: %w", baseErr, r.GetFullName(), err)

Expand All @@ -510,7 +510,7 @@
}

// send API call to capture the last hook for the repo
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, dbRepo)

Check failure on line 513 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L513

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:513:40: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		lastHook, err := database.FromContext(c).LastHookForRepo(ctx, dbRepo)
		                                     ^
if err != nil {
retErr := fmt.Errorf("unable to get last hook for repo %s: %w", r.GetFullName(), err)

Expand Down
3 changes: 1 addition & 2 deletions compiler/native/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,7 @@ func TestNative_ParseString_Metadata(t *testing.T) {

type FailReader struct{}

//nolint:revive // parameter is unused by design
func (FailReader) Read(p []byte) (n int, err error) {
func (FailReader) Read(_ []byte) (n int, err error) {
return 0, errors.New("this is a reader that fails when you try to read")
}

Expand Down
13 changes: 6 additions & 7 deletions scm/github/changeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ import (
"github.com/sirupsen/logrus"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/types/library"
"github.com/google/go-github/v61/github"
)

// Changeset captures the list of files changed for a commit.
func (c *client) Changeset(ctx context.Context, u *library.User, r *api.Repo, sha string) ([]string, error) {
func (c *client) Changeset(ctx context.Context, r *api.Repo, sha string) ([]string, error) {
c.Logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
"user": u.GetName(),
"user": r.GetOwner().GetName(),
}).Tracef("capturing commit changeset for %s/commit/%s", r.GetFullName(), sha)

// create GitHub OAuth client with user's token
client := c.newClientToken(u.GetToken())
client := c.newClientToken(r.GetOwner().GetToken())
s := []string{}

// set the max per page for the options to capture the commit
Expand All @@ -43,15 +42,15 @@ func (c *client) Changeset(ctx context.Context, u *library.User, r *api.Repo, sh
}

// ChangesetPR captures the list of files changed for a pull request.
func (c *client) ChangesetPR(ctx context.Context, u *library.User, r *api.Repo, number int) ([]string, error) {
func (c *client) ChangesetPR(ctx context.Context, r *api.Repo, number int) ([]string, error) {
c.Logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
"user": u.GetName(),
"user": r.GetOwner().GetName(),
}).Tracef("capturing pull request changeset for %s/pull/%d", r.GetFullName(), number)

// create GitHub OAuth client with user's token
client := c.newClientToken(u.GetToken())
client := c.newClientToken(r.GetOwner().GetToken())
s := []string{}
f := []*github.CommitFile{}

Expand Down
6 changes: 4 additions & 2 deletions scm/github/changeset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ func TestGithub_Changeset(t *testing.T) {
r := new(api.Repo)
r.SetOrg("repos")
r.SetName("octocat")
r.SetOwner(u)

client, _ := NewTest(s.URL)

// run test
got, err := client.Changeset(context.TODO(), u, r, "6dcb09b5b57875f334f61aebed695e2e4193db5e")
got, err := client.Changeset(context.TODO(), r, "6dcb09b5b57875f334f61aebed695e2e4193db5e")

if resp.Code != http.StatusOK {
t.Errorf("Changeset returned %v, want %v", resp.Code, http.StatusOK)
Expand Down Expand Up @@ -88,11 +89,12 @@ func TestGithub_ChangesetPR(t *testing.T) {
r := new(api.Repo)
r.SetOrg("repos")
r.SetName("octocat")
r.SetOwner(u)

client, _ := NewTest(s.URL)

// run test
got, err := client.ChangesetPR(context.TODO(), u, r, 1)
got, err := client.ChangesetPR(context.TODO(), r, 1)

if resp.Code != http.StatusOK {
t.Errorf("ChangesetPR returned %v, want %v", resp.Code, http.StatusOK)
Expand Down
4 changes: 2 additions & 2 deletions scm/service.go
ecrupper marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ type Service interface {
// of files changed for a commit.
//
// https://en.wikipedia.org/wiki/Changeset.
Changeset(context.Context, *library.User, *api.Repo, string) ([]string, error)
Changeset(context.Context, *api.Repo, string) ([]string, error)
// ChangesetPR defines a function that captures the list
// of files changed for a pull request.
//
// https://en.wikipedia.org/wiki/Changeset.
ChangesetPR(context.Context, *library.User, *api.Repo, int) ([]string, error)
ChangesetPR(context.Context, *api.Repo, int) ([]string, error)

// Deployment SCM Interface Functions

Expand Down
Loading