Skip to content

Commit

Permalink
[GITHA] Introduce Generic store Package (harness#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesHarness authored and Harness committed Jun 20, 2023
1 parent 34db95b commit 36e8bdb
Show file tree
Hide file tree
Showing 59 changed files with 361 additions and 285 deletions.
2 changes: 1 addition & 1 deletion cli/operations/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"

"github.com/harness/gitness/cli/server"
"github.com/harness/gitness/internal/store/database"
"github.com/harness/gitness/store/database"

"github.com/jmoiron/sqlx"
"github.com/joho/godotenv"
Expand Down
9 changes: 9 additions & 0 deletions cli/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/harness/gitness/gitrpc/server"
"github.com/harness/gitness/internal/services/webhook"
"github.com/harness/gitness/lock"
"github.com/harness/gitness/store/database"
"github.com/harness/gitness/types"

"github.com/kelseyhightower/envconfig"
Expand Down Expand Up @@ -86,6 +87,14 @@ func getSanitizedMachineName() (string, error) {
return hostName, nil
}

// ProvideDatabaseConfig loads the database config from the main config.
func ProvideDatabaseConfig(config *types.Config) database.Config {
return database.Config{
Driver: config.Database.Driver,
Datasource: config.Database.Datasource,
}
}

// ProvideGitRPCServerConfig loads the gitrpc server config from the environment.
// It backfills certain config elements to work with cmdone.
func ProvideGitRPCServerConfig() (server.Config, error) {
Expand Down
1 change: 1 addition & 0 deletions cmd/gitness/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e
cliserver.NewSystem,
cliserver.ProvideRedis,
bootstrap.WireSet,
cliserver.ProvideDatabaseConfig,
database.WireSet,
cache.WireSet,
router.WireSet,
Expand Down
3 changes: 2 additions & 1 deletion cmd/gitness/wire_gen.go

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

2 changes: 1 addition & 1 deletion internal/api/controller/pullreq/comment_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/harness/gitness/gitrpc"
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/store"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"

Expand Down
2 changes: 1 addition & 1 deletion internal/api/controller/pullreq/pr_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"

"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
Expand Down
4 changes: 2 additions & 2 deletions internal/api/controller/pullreq/review_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/harness/gitness/gitrpc"
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"

Expand Down
4 changes: 2 additions & 2 deletions internal/api/controller/pullreq/reviewer_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/controller/repo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/bootstrap"
"github.com/harness/gitness/internal/paths"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/resources"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/check"
"github.com/harness/gitness/types/enum"
Expand Down
2 changes: 1 addition & 1 deletion internal/api/controller/repo/delete_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/controller/repo/list_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/controller/repo/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/paths"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/controller/space/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/paths"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/check"
"github.com/harness/gitness/types/enum"
Expand Down
2 changes: 1 addition & 1 deletion internal/api/controller/space/delete_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/controller/space/list_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
Expand Down
4 changes: 2 additions & 2 deletions internal/api/controller/space/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/paths"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/check"
"github.com/harness/gitness/types/enum"
Expand Down
4 changes: 2 additions & 2 deletions internal/api/controller/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"context"
"errors"

"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store"
"github.com/harness/gitness/store/database/dbtx"

"github.com/jmoiron/sqlx"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/api/controller/user/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/token"
"github.com/harness/gitness/store"
"github.com/harness/gitness/types"

"github.com/rs/zerolog/log"
Expand Down
2 changes: 1 addition & 1 deletion internal/api/usererror/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/harness/gitness/gitrpc"
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/services/webhook"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/store"
"github.com/harness/gitness/types/check"

"github.com/harness/go-rbac"
Expand Down
2 changes: 1 addition & 1 deletion internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/harness/gitness/internal/api/controller/service"
"github.com/harness/gitness/internal/api/controller/user"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/store"
"github.com/harness/gitness/types"

"github.com/rs/zerolog/log"
Expand Down
2 changes: 1 addition & 1 deletion internal/services/webhook/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"fmt"

"github.com/harness/gitness/events"
"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/store"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"

Expand Down
2 changes: 1 addition & 1 deletion internal/services/webhook/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"net/http"
"time"

"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/store"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
"github.com/harness/gitness/version"
Expand Down
15 changes: 8 additions & 7 deletions internal/store/database/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
"time"

"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store/database"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"

Expand Down Expand Up @@ -121,19 +122,19 @@ func (s *CheckStore) Upsert(ctx context.Context, check *types.Check) error {

query, arg, err := db.BindNamed(sqlQuery, mapInternalCheck(check))
if err != nil {
return processSQLErrorf(err, "Failed to bind status check object")
return database.ProcessSQLErrorf(err, "Failed to bind status check object")
}

if err = db.QueryRowContext(ctx, query, arg...).Scan(&check.ID, &check.CreatedBy, &check.Created); err != nil {
return processSQLErrorf(err, "Upsert query failed")
return database.ProcessSQLErrorf(err, "Upsert query failed")
}

return nil
}

// List returns a list of status check results for a specific commit in a repo.
func (s *CheckStore) List(ctx context.Context, repoID int64, commitSHA string) ([]*types.Check, error) {
stmt := builder.
stmt := database.Builder.
Select(checkColumns).
From("checks").
Where("check_repo_id = ?", repoID).
Expand All @@ -150,7 +151,7 @@ func (s *CheckStore) List(ctx context.Context, repoID int64, commitSHA string) (
db := dbtx.GetAccessor(ctx, s.db)

if err = db.SelectContext(ctx, &dst, sql, args...); err != nil {
return nil, processSQLErrorf(err, "Failed to execute list status checks query")
return nil, database.ProcessSQLErrorf(err, "Failed to execute list status checks query")
}

result, err := s.mapSliceCheck(ctx, dst)
Expand All @@ -163,7 +164,7 @@ func (s *CheckStore) List(ctx context.Context, repoID int64, commitSHA string) (

// ListRecent returns a list of recently executed status checks in a repository.
func (s *CheckStore) ListRecent(ctx context.Context, repoID int64, since time.Time) ([]string, error) {
stmt := builder.
stmt := database.Builder.
Select("distinct check_uid").
From("checks").
Where("check_repo_id = ?", repoID).
Expand All @@ -180,7 +181,7 @@ func (s *CheckStore) ListRecent(ctx context.Context, repoID int64, since time.Ti
db := dbtx.GetAccessor(ctx, s.db)

if err = db.SelectContext(ctx, &dst, sql, args...); err != nil {
return nil, processSQLErrorf(err, "Failed to execute list recent status checks query")
return nil, database.ProcessSQLErrorf(err, "Failed to execute list recent status checks query")
}

return dst, nil
Expand Down
15 changes: 8 additions & 7 deletions internal/store/database/check_req.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"fmt"

"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store/database"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"

"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -78,19 +79,19 @@ func (s *ReqCheckStore) Create(ctx context.Context, reqCheck *types.ReqCheck) er

query, arg, err := db.BindNamed(sqlQuery, mapInternalReqCheck(reqCheck))
if err != nil {
return processSQLErrorf(err, "Failed to bind required status check object")
return database.ProcessSQLErrorf(err, "Failed to bind required status check object")
}

if err = db.QueryRowContext(ctx, query, arg...).Scan(&reqCheck.ID); err != nil {
return processSQLErrorf(err, "Insert query failed")
return database.ProcessSQLErrorf(err, "Insert query failed")
}

return nil
}

// List returns a list of required status checks for a repo.
func (s *ReqCheckStore) List(ctx context.Context, repoID int64) ([]*types.ReqCheck, error) {
stmt := builder.
stmt := database.Builder.
Select(reqCheckColumns).
From("reqchecks").
Where("reqcheck_repo_id = ?", repoID).
Expand All @@ -106,7 +107,7 @@ func (s *ReqCheckStore) List(ctx context.Context, repoID int64) ([]*types.ReqChe
db := dbtx.GetAccessor(ctx, s.db)

if err = db.SelectContext(ctx, &dst, sql, args...); err != nil {
return nil, processSQLErrorf(err, "Failed to execute list required status checks query")
return nil, database.ProcessSQLErrorf(err, "Failed to execute list required status checks query")
}

result, err := s.mapSliceReqCheck(ctx, dst)
Expand All @@ -119,7 +120,7 @@ func (s *ReqCheckStore) List(ctx context.Context, repoID int64) ([]*types.ReqChe

// Delete removes a required status checks for a repo.
func (s *ReqCheckStore) Delete(ctx context.Context, repoID, reqCheckID int64) error {
stmt := builder.
stmt := database.Builder.
Delete("reqchecks").
Where("reqcheck_repo_id = ?", repoID).
Where("reqcheck_id = ?", reqCheckID)
Expand All @@ -133,7 +134,7 @@ func (s *ReqCheckStore) Delete(ctx context.Context, repoID, reqCheckID int64) er

_, err = db.ExecContext(ctx, sql, args...)
if err != nil {
return processSQLErrorf(err, "Failed to execute delete required status check query")
return database.ProcessSQLErrorf(err, "Failed to execute delete required status check query")
}

return nil
Expand Down
13 changes: 7 additions & 6 deletions internal/store/database/code_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"time"

"github.com/harness/gitness/internal/store"
"github.com/harness/gitness/internal/store/database/dbtx"
"github.com/harness/gitness/store/database"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"

Expand Down Expand Up @@ -65,7 +66,7 @@ func (s *CodeCommentView) list(ctx context.Context,
,coalesce(pullreq_activity_code_comment_line_old, 1) as "pullreq_activity_code_comment_line_old"
,coalesce(pullreq_activity_code_comment_span_old, 0) as "pullreq_activity_code_comment_span_old"`

stmt := builder.
stmt := database.Builder.
Select(codeCommentColumns).
From("pullreq_activities").
Where("pullreq_activity_pullreq_id = ?", prID).
Expand Down Expand Up @@ -95,7 +96,7 @@ func (s *CodeCommentView) list(ctx context.Context,
db := dbtx.GetAccessor(ctx, s.db)

if err = db.SelectContext(ctx, &result, sql, args...); err != nil {
return nil, processSQLErrorf(err, "Failed executing code comment list query")
return nil, database.ProcessSQLErrorf(err, "Failed executing code comment list query")
}

return result, nil
Expand Down Expand Up @@ -126,7 +127,7 @@ func (s *CodeCommentView) UpdateAll(ctx context.Context, codeComments []*types.C

stmt, err := db.PrepareNamedContext(ctx, sqlQuery)
if err != nil {
return processSQLErrorf(err, "Failed to prepare update statement for update code comments")
return database.ProcessSQLErrorf(err, "Failed to prepare update statement for update code comments")
}

updatedAt := time.Now()
Expand All @@ -137,12 +138,12 @@ func (s *CodeCommentView) UpdateAll(ctx context.Context, codeComments []*types.C

result, err := stmt.ExecContext(ctx, codeComment)
if err != nil {
return processSQLErrorf(err, "Failed to update code comment=%d", codeComment.ID)
return database.ProcessSQLErrorf(err, "Failed to update code comment=%d", codeComment.ID)
}

count, err := result.RowsAffected()
if err != nil {
return processSQLErrorf(err, "Failed to get number of updated rows for code comment=%d", codeComment.ID)
return database.ProcessSQLErrorf(err, "Failed to get number of updated rows for code comment=%d", codeComment.ID)
}

if count == 0 {
Expand Down
Loading

0 comments on commit 36e8bdb

Please sign in to comment.