Skip to content

Commit

Permalink
added tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jan 21, 2024
1 parent a727d24 commit 2ee0e1e
Show file tree
Hide file tree
Showing 17 changed files with 318 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
go-version: [latest]
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 0 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ inputs:
description: "Type of score"
metric:
description: "Metric of score"
value:
description: "Value of Score"
destroy:
description: "Destroy all"
default: 'false'
Expand Down
6 changes: 4 additions & 2 deletions app/db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func configureSQL(sqlDB *sql.DB) {
sqlDB.SetConnMaxLifetime(time.Hour * time.Duration(1))
}

func Migrate(command string, embedMigrations embed.FS) error {
func Migrate(command string, embedMigrations embed.FS, dir string) error {
if command == "create" {
create()
return nil
Expand All @@ -93,8 +93,10 @@ func Migrate(command string, embedMigrations embed.FS) error {
if err != nil {
panic(err)
}

goose.SetBaseFS(embedMigrations)
err = goose.RunContext(ctx, command, driver, "migrations")

err = goose.RunContext(ctx, command, driver, dir)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
db.SetupDatabase()

if f.migrate != "" {
err := db.Migrate(f.migrate, embedMigrations)
err := db.Migrate(f.migrate, embedMigrations, "pkg/migrations")
if err != nil {
pkg.Logger().Error(err)
}
Expand Down
15 changes: 11 additions & 4 deletions app/models/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (c *Coverage) GetLatestBranchScore(orgName string, repoName string, branchN
AND
t.name = @typeName
ORDER BY
c.created_at DESC
c.created_at DESC, c.id DESC
LIMIT 1;
`
err := db.Db().Raw(
Expand Down Expand Up @@ -519,7 +519,10 @@ func (c *Coverage) Create(
score float32) (*Coverage, error) {
var ret Coverage

query := `INSERT INTO
branchName = strings.TrimSpace(branchName)
commit = strings.TrimSpace(commit)

insertQ := `INSERT INTO
coverages (
org_id,
repo_id,
Expand All @@ -541,7 +544,7 @@ func (c *Coverage) Create(
@score
)`
err := db.Db().Raw(
query,
insertQ,
sql.Named("org_id", orgID),
sql.Named("repo_id", repoID),
sql.Named("user_id", userID),
Expand All @@ -552,6 +555,10 @@ func (c *Coverage) Create(
sql.Named("score", score)).
Scan(&ret).Error

if err != nil {
return &ret, err
}

return &ret, err
}
func (c *Coverage) SoftDeleteCoverages(orgID int64, repoID int64, branches string) error {
Expand All @@ -565,7 +572,7 @@ func (c *Coverage) SoftDeleteCoverages(orgID int64, repoID int64, branches strin
AND
repo_id = @repo_id
AND
branch_name IN (@branches)
branch_name NOT IN (@branches)
`
err := db.Db().Exec(
query,
Expand Down
23 changes: 17 additions & 6 deletions app/models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (Org) TableName() string {
return "orgs"
}

func (o *Org) Get(name string) (Org, error) {
func (o *Org) Get(name string) (*Org, error) {
var ret Org

query := `SELECT * FROM orgs WHERE name = @name LIMIT 1`
Expand All @@ -29,17 +29,28 @@ func (o *Org) Get(name string) (Org, error) {

ret.Name = strings.TrimSpace(ret.Name)

return ret, err
return &ret, err
}

func (o *Org) Create(name string) (Org, error) {
func (o *Org) Create(name string) (*Org, error) {
var ret Org
name = strings.TrimSpace(name)

query := `INSERT INTO orgs (name) VALUES (@name)`
insertQ := `INSERT INTO orgs (name) VALUES (@name)`
err := db.Db().Raw(
query,
insertQ,
sql.Named("name", name)).
Scan(&ret).Error

if err != nil {
return &ret, err
}

selectQ := `SELECT * FROM orgs WHERE name = @name LIMIT 1`
err = db.Db().Raw(
selectQ,
sql.Named("name", name)).
Scan(&ret).Error

return ret, err
return &ret, err
}
26 changes: 18 additions & 8 deletions app/models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type Repo struct {
func (Repo) TableName() string {
return "repos"
}
func (r *Repo) Get(orgID int64, name string) (Repo, error) {
func (r *Repo) Get(orgID int64, name string) (*Repo, error) {
var ret Repo
name = strings.TrimSpace(name)

query := `SELECT * FROM repos WHERE org_id = @org_id AND name = @name LIMIT 1`
err := db.Db().Raw(
Expand All @@ -28,20 +29,29 @@ func (r *Repo) Get(orgID int64, name string) (Repo, error) {
sql.Named("name", name)).
Scan(&ret).Error

ret.Name = strings.TrimSpace(ret.Name)

return ret, err
return &ret, err
}

func (r *Repo) Create(orgID int64, name string) (Repo, error) {
func (r *Repo) Create(orgID int64, name string) (*Repo, error) {
var ret Repo
name = strings.TrimSpace(name)

query := `INSERT INTO repos (org_id, name) VALUES (@org_id, @name)`
insertQ := `INSERT INTO repos (org_id, name) VALUES (@org_id, @name)`
err := db.Db().Raw(
query,
insertQ,
sql.Named("org_id", orgID),
sql.Named("name", name)).
Scan(&ret).Error

return ret, err
if err != nil {
return &ret, err
}
selectQ := `SELECT * FROM repos WHERE org_id = @orgID AND name = @name LIMIT 1`
err = db.Db().Raw(
selectQ,
sql.Named("orgID", orgID),
sql.Named("name", name)).
Scan(&ret).Error

return &ret, err
}
24 changes: 18 additions & 6 deletions app/models/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const (
TYPE_NUMBER_OF_CONTRIBUTORS = "number-of-contributors"
)

func (t *Type) Get(name string) (Type, error) {
func (t *Type) Get(name string) (*Type, error) {
var ret Type
name = strings.TrimSpace(name)

query := `SELECT * FROM types WHERE name = @name LIMIT 1`
err := db.Db().Raw(
Expand All @@ -34,19 +35,30 @@ func (t *Type) Get(name string) (Type, error) {
ret.Metric = strings.TrimSpace(ret.Metric)
ret.Name = strings.TrimSpace(ret.Name)

return ret, err
return &ret, err
}

func (t *Type) Create(name string, metric string) (Type, error) {
func (t *Type) Create(name string, metric string) (*Type, error) {
var ret Type
query := `INSERT INTO types (name, metric) VALUES (@name, @metric)`
name = strings.TrimSpace(name)
metric = strings.TrimSpace(metric)
insertQ := `INSERT INTO types (name, metric) VALUES (@name, @metric)`
err := db.Db().Raw(
query,
insertQ,
sql.Named("name", name),
sql.Named("metric", metric)).
Scan(&ret).Error
if err != nil {
return &ret, err
}

selectQ := `SELECT * FROM types WHERE name = @name LIMIT 1`
err = db.Db().Raw(
selectQ,
sql.Named("name", name)).
Scan(&ret).Error

return ret, err
return &ret, err
}

func (t *Type) GetTypesFor(orgName string, repoName string) ([]Type, error) {
Expand Down
25 changes: 17 additions & 8 deletions app/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (User) TableName() string {
return "users"
}

func (u *User) Get(name string) (User, error) {
func (u *User) Get(name string) (*User, error) {
var ret User

query := `SELECT * FROM users WHERE name = @name LIMIT 1`
Expand All @@ -31,19 +31,28 @@ func (u *User) Get(name string) (User, error) {
sql.Named("name", name)).
Scan(&ret).Error

ret.Name = strings.TrimSpace(ret.Name)

return ret, err
return &ret, err
}

func (u *User) Create(name string) (User, error) {
func (u *User) Create(name string) (*User, error) {
var ret User
name = strings.TrimSpace(name)

query := `INSERT INTO users (name) VALUES (@name)`
insertQ := `INSERT INTO users (name) VALUES (@name)`
err := db.Db().Raw(
query,
insertQ,
sql.Named("name", name)).
Scan(&ret).Error

if err != nil {
return &ret, err
}

selectQ := `SELECT * FROM users WHERE name = @name LIMIT 1`
err = db.Db().Raw(
selectQ,
sql.Named("name", name)).
Scan(&ret).Error

return ret, err
return &ret, err
}
6 changes: 3 additions & 3 deletions app/pkg/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewBadge() *Badge {
return &Badge{}
}

func (b *Badge) Get(req *BadgeRequest, t models.Type) ([]byte, error) {
func (b *Badge) Get(req *BadgeRequest, t *models.Type) ([]byte, error) {

if t.Name == models.TYPE_AVERAGE_PR_DAYS {
num, err := b.coverageModel.GetAveragePRDays(req.Org, req.Repo)
Expand Down Expand Up @@ -61,9 +61,9 @@ func (b *Badge) Get404(req *BadgeRequest) ([]byte, error) {
return badge.RenderBytes(req.Branch+"|"+req.Type, "404", "#fff", "white", "red")
}

func (b *Badge) GetType(name string) (models.Type, error) {
func (b *Badge) GetType(name string) (*models.Type, error) {
if (name == models.TYPE_AVERAGE_PR_DAYS) || (name == models.TYPE_NUMBER_OF_CONTRIBUTORS) {
return models.Type{
return &models.Type{
Name: name,
}, nil
}
Expand Down
2 changes: 0 additions & 2 deletions app/pkg/badge_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ type BadgeRequest struct {
Repo string `json:"repo" query:"repo" validate:"required,ascii,excludes=/" message:"repo is required"`
Branch string `json:"branch" query:"branch" validate:"required,ascii,excludes=/" message:"ascii branch is required"`
Type string `json:"type" query:"type" validate:"ascii,required,excludes=/" message:"ascii type is required"`
Style string `json:"style" query:"style" default:"badge" validate:"oneof=badge chart table" message:"style must be badge or chart"`
Color string `json:"color" query:"color" default:"blue" validate:"oneof=blue red green orange" message:"color must be blue, red, green or orange"`
}

func (h *BadgeHandler) Get(c echo.Context) error {
Expand Down
Loading

0 comments on commit 2ee0e1e

Please sign in to comment.