Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions internal/interactor/deploymentstatistics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package interactor

import (
"context"

"github.com/gitploy-io/gitploy/ent"
)

func (i *Interactor) ProduceDeploymentStatisticsOfRepo(ctx context.Context, r *ent.Repo, d *ent.Deployment) (*ent.DeploymentStatistics, error) {
s, err := i.Store.FindDeploymentStatisticsOfRepoByEnv(ctx, r, d.Env)

if ent.IsNotFound(err) {
return i.Store.CreateDeploymentStatistics(ctx, &ent.DeploymentStatistics{
Namespace: r.Namespace,
Name: r.Name,
Env: d.Env,
Count: 1,
})
}

s.Count = s.Count + 1

return i.Store.UpdateDeploymentStatistics(ctx, s)
}
50 changes: 50 additions & 0 deletions internal/interactor/deploymentstatistics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package interactor

import (
"context"
"testing"

"github.com/gitploy-io/gitploy/ent"
"github.com/gitploy-io/gitploy/internal/interactor/mock"
"github.com/golang/mock/gomock"
)

func TestInteractor_ProduceDeploymentStatisticsOfRepo(t *testing.T) {
t.Run("Create the statistics when the record is not found.", func(t *testing.T) {
input := struct {
repo *ent.Repo
d *ent.Deployment
}{
repo: &ent.Repo{
Namespace: "octocat",
Name: "HelloWorld",
},
d: &ent.Deployment{
Env: "production",
},
}

ctrl := gomock.NewController(t)
store := mock.NewMockStore(ctrl)
scm := mock.NewMockSCM(ctrl)

t.Log("MOCK - Find the deployment_statistics by the environment.")
store.
EXPECT().
FindDeploymentStatisticsOfRepoByEnv(gomock.Any(), gomock.Eq(input.repo), gomock.Eq(input.d.Env)).
Return(nil, &ent.NotFoundError{})

t.Log("MOCK - Create a new statistics.")
store.
EXPECT().
CreateDeploymentStatistics(gomock.Any(), gomock.AssignableToTypeOf(&ent.DeploymentStatistics{})).
Return(&ent.DeploymentStatistics{ID: 1}, nil)

i := newMockInteractor(store, scm)

_, err := i.ProduceDeploymentStatisticsOfRepo(context.Background(), input.repo, input.d)
if err != nil {
t.Fatalf("ProduceDeploymentStatisticsOfRepo returns an error: %s", err)
}
})
}
8 changes: 6 additions & 2 deletions internal/interactor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ type (

CreateDeploymentStatus(ctx context.Context, s *ent.DeploymentStatus) (*ent.DeploymentStatus, error)

ListAllDeploymentStatisticss(ctx context.Context) ([]*ent.DeploymentStatistics, error)
ListDeploymentStatisticssGreaterThanTime(ctx context.Context, updated time.Time) ([]*ent.DeploymentStatistics, error)
FindDeploymentStatisticsOfRepoByEnv(ctx context.Context, r *ent.Repo, env string) (*ent.DeploymentStatistics, error)
CreateDeploymentStatistics(ctx context.Context, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error)
UpdateDeploymentStatistics(ctx context.Context, s *ent.DeploymentStatistics) (*ent.DeploymentStatistics, error)

ListAllDeploymentStatistics(ctx context.Context) ([]*ent.DeploymentStatistics, error)
ListDeploymentStatisticsGreaterThanTime(ctx context.Context, updated time.Time) ([]*ent.DeploymentStatistics, error)

CreateCallback(ctx context.Context, cb *ent.Callback) (*ent.Callback, error)
FindCallbackByHash(ctx context.Context, hash string) (*ent.Callback, error)
Expand Down
69 changes: 57 additions & 12 deletions internal/interactor/mock/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 0 additions & 44 deletions internal/pkg/store/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"entgo.io/ent/dialect/sql"
"github.com/gitploy-io/gitploy/ent"
"github.com/gitploy-io/gitploy/ent/deployment"
"github.com/gitploy-io/gitploy/ent/deploymentstatistics"
"github.com/gitploy-io/gitploy/ent/perm"
"github.com/gitploy-io/gitploy/ent/predicate"
"go.uber.org/zap"
)

func (s *Store) SearchDeployments(ctx context.Context, u *ent.User, ss []deployment.Status, owned bool, from time.Time, to time.Time, page, perPage int) ([]*ent.Deployment, error) {
Expand Down Expand Up @@ -239,47 +237,5 @@ func (s *Store) UpdateDeployment(ctx context.Context, d *ent.Deployment) (*ent.D
return nil, err
}

if d.Status != deployment.StatusSuccess {
return d, nil
}

// Update the statistics of deployment by
// increasing the count.
err = s.WithTx(ctx, func(tx *ent.Tx) error {
r, err := tx.Repo.Get(ctx, d.RepoID)
if err != nil {
return err
}

dc, err := s.c.DeploymentStatistics.
Query().
Where(
deploymentstatistics.NamespaceEQ(r.Namespace),
deploymentstatistics.NameEQ(r.Name),
deploymentstatistics.EnvEQ(d.Env),
).
Only(ctx)
if ent.IsNotFound(err) {
s.c.DeploymentStatistics.
Create().
SetNamespace(r.Namespace).
SetName(r.Name).
SetEnv(d.Env).
Save(ctx)
return nil
} else if err != nil {
return err
}

s.c.DeploymentStatistics.
UpdateOne(dc).
SetCount(dc.Count + 1).
Save(ctx)
return nil
})
if err != nil {
zap.L().Error("It has failed to increase the deployment count.", zap.Error(err))
}

return d, nil
}
95 changes: 0 additions & 95 deletions internal/pkg/store/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,99 +426,4 @@ func TestStore_UpdateDeployment(t *testing.T) {
t.Fatalf("UpdateDeployment = %v, wanted %v", d.Status, expected)
}
})

t.Run("Add a new statistics when the status of deployment is updated.", func(t *testing.T) {
ctx := context.Background()

client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1",
enttest.WithMigrateOptions(migrate.WithForeignKeys(false)),
)
defer client.Close()

client.Repo.
Create().
SetID(1).
SetNamespace("octocat").
SetName("Hello").
SetDescription("").
SaveX(ctx)

d := client.Deployment.Create().
SetType(deployment.TypeBranch).
SetNumber(1).
SetType("branch").
SetRef("main").
SetEnv("prod").
SetStatus(deployment.StatusCreated).
SetUserID(1).
SetRepoID(1).
SaveX(ctx)

s := NewStore(client)

d.Status = deployment.StatusSuccess

_, err := s.UpdateDeployment(ctx, d)
if err != nil {
t.Fatalf("UpdateDeployment returns an error: %s", err)
}

expected := 1
dc := client.DeploymentStatistics.GetX(ctx, 1)

if dc.Count != expected {
t.Fatalf("The statistics was not created.")
}
})

t.Run("Update the statistics is updated when the status of deployment is updated.", func(t *testing.T) {
ctx := context.Background()

client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1",
enttest.WithMigrateOptions(migrate.WithForeignKeys(false)),
)
defer client.Close()

client.Repo.
Create().
SetID(1).
SetNamespace("octocat").
SetName("Hello").
SetDescription("").
SaveX(ctx)

d := client.Deployment.Create().
SetType(deployment.TypeBranch).
SetNumber(1).
SetType("branch").
SetRef("main").
SetEnv("prod").
SetStatus(deployment.StatusCreated).
SetUserID(1).
SetRepoID(1).
SaveX(ctx)

t.Log("Add the deployment count.")
client.DeploymentStatistics.Create().
SetNamespace("octocat").
SetName("Hello").
SetEnv("prod").
SaveX(ctx)

s := NewStore(client)

d.Status = deployment.StatusSuccess

_, err := s.UpdateDeployment(ctx, d)
if err != nil {
t.Fatalf("UpdateDeployment returns an error: %s", err)
}

expected := 2
dc := client.DeploymentStatistics.GetX(ctx, 1)

if dc.Count != expected {
t.Fatalf("The statistics was not created.")
}
})
}
24 changes: 0 additions & 24 deletions internal/pkg/store/deploymentcount.go

This file was deleted.

Loading