From 7a54ef9d65e0f8b081969928902548b45226066e Mon Sep 17 00:00:00 2001 From: Enver Bisevac Date: Wed, 30 Nov 2022 22:36:01 +0100 Subject: [PATCH] fix with file content on commit, fix linter, removed unused provider --- cli/cli.go | 2 +- cli/operations/hooks/hooks.go | 5 +++++ cli/operations/hooks/update.go | 5 +++++ cli/server/harness.wire_gen.go | 1 + cli/server/standalone.wire_gen.go | 1 + internal/api/controller/pullreq/create.go | 7 ++++++- internal/api/controller/pullreq/find.go | 7 ++++++- internal/api/controller/pullreq/list.go | 7 ++++++- internal/api/controller/repo/commit.go | 4 +++- internal/api/handler/pullreq/list.go | 2 +- internal/store/database/dbtx/ctx.go | 1 + internal/store/database/dbtx/db.go | 4 ++-- internal/store/database/dbtx/interface.go | 1 + internal/store/database/dbtx/runner.go | 5 +++-- internal/store/database/dbtx/runner_test.go | 8 +++++--- internal/store/database/dbtx/tx.go | 6 +++--- internal/store/database/pullreq.go | 5 +++-- internal/store/database/pullreq_sync.go | 10 +++++++--- internal/store/database/wire.go | 5 ----- internal/store/store.go | 2 +- web/src/framework/strings/stringTypes.ts | 2 +- 21 files changed, 62 insertions(+), 28 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 57bbddaab..665a77348 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -6,13 +6,13 @@ package cli import ( "errors" - "github.com/harness/gitness/cli/operations/hooks" "io/fs" "os" "path" "path/filepath" "github.com/harness/gitness/cli/operations/account" + "github.com/harness/gitness/cli/operations/hooks" "github.com/harness/gitness/cli/operations/user" "github.com/harness/gitness/cli/operations/users" "github.com/harness/gitness/cli/server" diff --git a/cli/operations/hooks/hooks.go b/cli/operations/hooks/hooks.go index 5487f762c..5192c6f25 100644 --- a/cli/operations/hooks/hooks.go +++ b/cli/operations/hooks/hooks.go @@ -1,7 +1,12 @@ +// Copyright 2022 Harness Inc. All rights reserved. +// Use of this source code is governed by the Polyform Free Trial License +// that can be found in the LICENSE.md file for this repository. + package hooks import ( "github.com/harness/gitness/client" + "gopkg.in/alecthomas/kingpin.v2" ) diff --git a/cli/operations/hooks/update.go b/cli/operations/hooks/update.go index ddf7e31a3..6f1b91073 100644 --- a/cli/operations/hooks/update.go +++ b/cli/operations/hooks/update.go @@ -1,7 +1,12 @@ +// Copyright 2022 Harness Inc. All rights reserved. +// Use of this source code is governed by the Polyform Free Trial License +// that can be found in the LICENSE.md file for this repository. + package hooks import ( "github.com/harness/gitness/client" + "github.com/rs/zerolog/log" "gopkg.in/alecthomas/kingpin.v2" ) diff --git a/cli/server/harness.wire_gen.go b/cli/server/harness.wire_gen.go index b217947b3..1d3138afe 100644 --- a/cli/server/harness.wire_gen.go +++ b/cli/server/harness.wire_gen.go @@ -7,6 +7,7 @@ package server import ( "context" + "github.com/harness/gitness/gitrpc" server2 "github.com/harness/gitness/gitrpc/server" "github.com/harness/gitness/harness/auth/authn" diff --git a/cli/server/standalone.wire_gen.go b/cli/server/standalone.wire_gen.go index 0a4058291..8c11479f9 100644 --- a/cli/server/standalone.wire_gen.go +++ b/cli/server/standalone.wire_gen.go @@ -7,6 +7,7 @@ package server import ( "context" + "github.com/harness/gitness/gitrpc" server2 "github.com/harness/gitness/gitrpc/server" "github.com/harness/gitness/internal/api/controller/pullreq" diff --git a/internal/api/controller/pullreq/create.go b/internal/api/controller/pullreq/create.go index 53da806eb..2f689ca0e 100644 --- a/internal/api/controller/pullreq/create.go +++ b/internal/api/controller/pullreq/create.go @@ -26,7 +26,12 @@ type CreateInput struct { } // Create creates a new pull request. -func (c *Controller) Create(ctx context.Context, session *auth.Session, repoRef string, in *CreateInput) (*types.PullReq, error) { +func (c *Controller) Create( + ctx context.Context, + session *auth.Session, + repoRef string, + in *CreateInput, +) (*types.PullReq, error) { var pr *types.PullReq now := time.Now().UnixMilli() diff --git a/internal/api/controller/pullreq/find.go b/internal/api/controller/pullreq/find.go index 33b342217..924ad5c2d 100644 --- a/internal/api/controller/pullreq/find.go +++ b/internal/api/controller/pullreq/find.go @@ -15,7 +15,12 @@ import ( ) // Find returns a pull request from the provided repository. -func (c *Controller) Find(ctx context.Context, session *auth.Session, repoRef string, pullreqNum int64) (*types.PullReq, error) { +func (c *Controller) Find( + ctx context.Context, + session *auth.Session, + repoRef string, + pullreqNum int64, +) (*types.PullReq, error) { if repoRef == "" { return nil, usererror.BadRequest("A valid repository reference must be provided.") } diff --git a/internal/api/controller/pullreq/list.go b/internal/api/controller/pullreq/list.go index 053da0361..dfd50fc41 100644 --- a/internal/api/controller/pullreq/list.go +++ b/internal/api/controller/pullreq/list.go @@ -15,7 +15,12 @@ import ( ) // List returns a list of pull requests from the provided repository. -func (c *Controller) List(ctx context.Context, session *auth.Session, repoRef string, filter *types.PullReqFilter) ([]*types.PullReq, error) { +func (c *Controller) List( + ctx context.Context, + session *auth.Session, + repoRef string, + filter *types.PullReqFilter, +) ([]*types.PullReq, error) { if repoRef == "" { return nil, usererror.BadRequest("A valid repository reference must be provided.") } diff --git a/internal/api/controller/repo/commit.go b/internal/api/controller/repo/commit.go index 071776cf5..ae5e86a04 100644 --- a/internal/api/controller/repo/commit.go +++ b/internal/api/controller/repo/commit.go @@ -6,6 +6,7 @@ package repo import ( "context" + "strings" "github.com/harness/gitness/gitrpc" apiauth "github.com/harness/gitness/internal/api/auth" @@ -49,10 +50,11 @@ func (c *Controller) CommitFiles(ctx context.Context, session *auth.Session, actions := make([]gitrpc.CommitFileAction, len(in.Actions)) for i, action := range in.Actions { + payload := strings.ReplaceAll(action.Payload, "\r", "") actions[i] = gitrpc.CommitFileAction{ Action: action.Action, Path: action.Path, - Payload: []byte(action.Payload), + Payload: []byte(payload), Encoding: action.Encoding, SHA: action.SHA, } diff --git a/internal/api/handler/pullreq/list.go b/internal/api/handler/pullreq/list.go index 9c624e3a0..d5a43c1ed 100644 --- a/internal/api/handler/pullreq/list.go +++ b/internal/api/handler/pullreq/list.go @@ -5,12 +5,12 @@ package pullreq import ( - "github.com/harness/gitness/types/enum" "net/http" "github.com/harness/gitness/internal/api/controller/pullreq" "github.com/harness/gitness/internal/api/render" "github.com/harness/gitness/internal/api/request" + "github.com/harness/gitness/types/enum" ) // HandleList returns a http.HandlerFunc that lists pull requests for a repository. diff --git a/internal/store/database/dbtx/ctx.go b/internal/store/database/dbtx/ctx.go index 7190da4e7..93ef09f34 100644 --- a/internal/store/database/dbtx/ctx.go +++ b/internal/store/database/dbtx/ctx.go @@ -6,6 +6,7 @@ package dbtx import ( "context" + "github.com/jmoiron/sqlx" ) diff --git a/internal/store/database/dbtx/db.go b/internal/store/database/dbtx/db.go index 86e9a0edb..1d1853a79 100644 --- a/internal/store/database/dbtx/db.go +++ b/internal/store/database/dbtx/db.go @@ -7,10 +7,11 @@ package dbtx import ( "context" "database/sql" + "github.com/jmoiron/sqlx" ) -// New returns new database Runner interface +// New returns new database Runner interface. func New(db *sqlx.DB) Transactor { run := &runnerDB{sqlDB{db}} return run @@ -32,5 +33,4 @@ var _ transactor = (*sqlDB)(nil) func (db sqlDB) startTx(ctx context.Context, opts *sql.TxOptions) (Tx, error) { tx, err := db.DB.BeginTxx(ctx, opts) return tx, err - } diff --git a/internal/store/database/dbtx/interface.go b/internal/store/database/dbtx/interface.go index b977d8ed1..b903d3114 100644 --- a/internal/store/database/dbtx/interface.go +++ b/internal/store/database/dbtx/interface.go @@ -7,6 +7,7 @@ package dbtx import ( "context" "database/sql" + "github.com/jmoiron/sqlx" ) diff --git a/internal/store/database/dbtx/runner.go b/internal/store/database/dbtx/runner.go index 60f376cb4..e403e7662 100644 --- a/internal/store/database/dbtx/runner.go +++ b/internal/store/database/dbtx/runner.go @@ -7,6 +7,7 @@ package dbtx import ( "context" "database/sql" + "errors" ) type runnerDB struct { @@ -52,9 +53,9 @@ func (r runnerDB) WithTx(ctx context.Context, txFn func(context.Context) error, if !rtx.commit && !rtx.rollback { err = rtx.Commit() - if err == sql.ErrTxDone { + if errors.Is(err, sql.ErrTxDone) { // Check if the transaction failed because of the context, if yes return the ctx error. - if ctxErr := ctx.Err(); ctxErr == context.Canceled || ctxErr == context.DeadlineExceeded { + if ctxErr := ctx.Err(); errors.Is(ctxErr, context.Canceled) || errors.Is(ctxErr, context.DeadlineExceeded) { err = ctxErr } } diff --git a/internal/store/database/dbtx/runner_test.go b/internal/store/database/dbtx/runner_test.go index ce2af937a..c94050db4 100644 --- a/internal/store/database/dbtx/runner_test.go +++ b/internal/store/database/dbtx/runner_test.go @@ -8,10 +8,12 @@ import ( "context" "database/sql" "errors" - "github.com/jmoiron/sqlx" "testing" + + "github.com/jmoiron/sqlx" ) +//nolint:gocognit func TestWithTx(t *testing.T) { errTest := errors.New("dummy error") @@ -113,7 +115,7 @@ func TestWithTx(t *testing.T) { func() { defer func() { - recover() + _ = recover() }() err = run.WithTx(ctx, func(ctx context.Context) error { @@ -134,7 +136,7 @@ func TestWithTx(t *testing.T) { t.Error("transaction not finished") } - if want, got := test.expectErr, err; want != got { + if want, got := test.expectErr, err; !errors.Is(got, want) { t.Errorf("expected error %v, but got %v", want, got) } diff --git a/internal/store/database/dbtx/tx.go b/internal/store/database/dbtx/tx.go index 9de8f3740..2dbbc7af7 100644 --- a/internal/store/database/dbtx/tx.go +++ b/internal/store/database/dbtx/tx.go @@ -6,11 +6,11 @@ package dbtx import "database/sql" -// TxDefault represents default transaction options +// TxDefault represents default transaction options. var TxDefault = &sql.TxOptions{Isolation: sql.LevelDefault, ReadOnly: false} -// TxDefaultReadOnly represents default transaction options for read-only transactions +// TxDefaultReadOnly represents default transaction options for read-only transactions. var TxDefaultReadOnly = &sql.TxOptions{Isolation: sql.LevelDefault, ReadOnly: true} -// TxSerializable represents serializable transaction options +// TxSerializable represents serializable transaction options. var TxSerializable = &sql.TxOptions{Isolation: sql.LevelSerializable, ReadOnly: false} diff --git a/internal/store/database/pullreq.go b/internal/store/database/pullreq.go index 6cc702150..864beec40 100644 --- a/internal/store/database/pullreq.go +++ b/internal/store/database/pullreq.go @@ -6,6 +6,7 @@ package database import ( "context" + "github.com/harness/gitness/internal/store" "github.com/harness/gitness/internal/store/database/dbtx" "github.com/harness/gitness/types" @@ -66,7 +67,7 @@ WHERE pullreq_id = $1 } // FindByNumber finds the pull request by repo ID and pull request number. -func (s *PullReqStore) FindByNumber(ctx context.Context, repoId, number int64) (*types.PullReq, error) { +func (s *PullReqStore) FindByNumber(ctx context.Context, repoID, number int64) (*types.PullReq, error) { const sqlQuery = pullReqSelectBase + ` WHERE pullreq_target_repo_id = $1 AND pullreq_number = $2 ` @@ -74,7 +75,7 @@ WHERE pullreq_target_repo_id = $1 AND pullreq_number = $2 db := dbtx.GetAccessor(ctx, s.db) dst := new(types.PullReq) - if err := db.GetContext(ctx, dst, sqlQuery, repoId, number); err != nil { + if err := db.GetContext(ctx, dst, sqlQuery, repoID, number); err != nil { return nil, processSQLErrorf(err, "Select query failed") } return dst, nil diff --git a/internal/store/database/pullreq_sync.go b/internal/store/database/pullreq_sync.go index 0c1a6dd94..307643693 100644 --- a/internal/store/database/pullreq_sync.go +++ b/internal/store/database/pullreq_sync.go @@ -36,10 +36,10 @@ func (s *PullReqStoreSync) Find(ctx context.Context, id int64) (*types.PullReq, } // FindByNumber finds the pull request by repo ID and pull request number. -func (s *PullReqStoreSync) FindByNumber(ctx context.Context, repoId, number int64) (*types.PullReq, error) { +func (s *PullReqStoreSync) FindByNumber(ctx context.Context, repoID, number int64) (*types.PullReq, error) { mutex.RLock() defer mutex.RUnlock() - return s.base.FindByNumber(ctx, repoId, number) + return s.base.FindByNumber(ctx, repoID, number) } // Create creates a new pull request. @@ -78,7 +78,11 @@ func (s *PullReqStoreSync) Count(ctx context.Context, repoID int64, opts *types. } // List returns a list of pull requests for a repo. -func (s *PullReqStoreSync) List(ctx context.Context, repoID int64, opts *types.PullReqFilter) ([]*types.PullReq, error) { +func (s *PullReqStoreSync) List( + ctx context.Context, + repoID int64, + opts *types.PullReqFilter, +) ([]*types.PullReq, error) { mutex.RLock() defer mutex.RUnlock() return s.base.List(ctx, repoID, opts) diff --git a/internal/store/database/wire.go b/internal/store/database/wire.go index 7870a2475..6e6c89c86 100644 --- a/internal/store/database/wire.go +++ b/internal/store/database/wire.go @@ -6,7 +6,6 @@ package database import ( "context" - "github.com/harness/gitness/internal/store/database/_dbtx" "github.com/harness/gitness/internal/store" "github.com/harness/gitness/types" @@ -40,10 +39,6 @@ func ProvideDatabase(ctx context.Context, config *types.Config) (*sqlx.DB, error ) } -func ProvideTransactor(ctx context.Context, db *sqlx.DB) _dbtx.Transactor { - return _dbtx.New(db.DB) -} - // ProvideUserStore provides a user store. func ProvideUserStore(db *sqlx.DB, uidTransformation store.PrincipalUIDTransformation) store.UserStore { switch db.DriverName() { diff --git a/internal/store/store.go b/internal/store/store.go index 017ddf930..28fa82116 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -204,7 +204,7 @@ type ( Find(ctx context.Context, id int64) (*types.PullReq, error) // FindByNumber finds the pull request by repo ID and the pull request number. - FindByNumber(ctx context.Context, repoId, number int64) (*types.PullReq, error) + FindByNumber(ctx context.Context, repoID, number int64) (*types.PullReq, error) // Create a new pull request. Create(ctx context.Context, pullreq *types.PullReq) error diff --git a/web/src/framework/strings/stringTypes.ts b/web/src/framework/strings/stringTypes.ts index ef703ce7b..932a6e7ce 100644 --- a/web/src/framework/strings/stringTypes.ts +++ b/web/src/framework/strings/stringTypes.ts @@ -139,7 +139,7 @@ export interface StringsMap { viewAllBranches: string viewAllTags: string viewCommitDetails: string + webhook: string webhookEventsLabel: string yourBranches: string - webhook: string }