Skip to content

Commit

Permalink
refactor: updating data model (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ViBiOh authored Apr 8, 2020
1 parent 8d7c608 commit c4a9485
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 35 deletions.
5 changes: 3 additions & 2 deletions ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ DROP SEQUENCE IF EXISTS target_id_seq;
CREATE SEQUENCE target_seq;
CREATE TABLE target (
id BIGINT NOT NULL DEFAULT nextval('target_seq'),
owner TEXT NOT NULL,
repository TEXT NOT NULL,
version TEXT NOT NULL,
current_version TEXT NOT NULL,
latest_version TEXT NOT NULL,
creation_date TIMESTAMP WITH TIME ZONE DEFAULT now()
);
ALTER SEQUENCE target_seq OWNED BY target.id;

CREATE UNIQUE INDEX target_id ON target (id);
CREATE UNIQUE INDEX target_repository ON target (repository);
12 changes: 6 additions & 6 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Release struct {

// App of package
type App interface {
LastRelease(owner, repository string) (Release, error)
LastRelease(repository string) (Release, error)
}

// Config of package
Expand Down Expand Up @@ -53,22 +53,22 @@ func (a app) newClient() *request.Request {
return request.New().Header("Authorization", fmt.Sprintf("token %s", a.token))
}

func (a app) LastRelease(owner, repository string) (Release, error) {
func (a app) LastRelease(repository string) (Release, error) {
var release Release

req := a.newClient()
resp, err := req.Get(fmt.Sprintf("%s/repos/%s/%s/releases/latest", apiURL, owner, repository)).Send(context.Background(), nil)
resp, err := req.Get(fmt.Sprintf("%s/repos/%s/releases/latest", apiURL, repository)).Send(context.Background(), nil)
if err != nil {
return release, fmt.Errorf("unable to get latest release for %s/%s: %s", owner, repository, err)
return release, fmt.Errorf("unable to get latest release for %s: %s", repository, err)
}

payload, err := request.ReadBodyResponse(resp)
if err != nil {
return release, fmt.Errorf("unable to read release body for %s/%s: %s", owner, repository, err)
return release, fmt.Errorf("unable to read release body for %s: %s", repository, err)
}

if err := json.Unmarshal(payload, &release); err != nil {
return release, fmt.Errorf("unable to parse release body for %s/%s: %s", owner, repository, err)
return release, fmt.Errorf("unable to parse release body for %s: %s", repository, err)
}

return release, err
Expand Down
15 changes: 10 additions & 5 deletions pkg/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@ func (a app) checkUpdates(_ time.Time) error {
for _, o := range targets {
target := o.(target.Target)

release, err := a.githubApp.LastRelease(target.Owner, target.Repository)
release, err := a.githubApp.LastRelease(target.Repository)
if err != nil {
return err
}

if release.TagName != target.Version {
logger.Info("New version available for %s/%s", target.Owner, target.Repository)
} else {
logger.Info("%s/%s is up-to-date!", target.Owner, target.Repository)
if release.TagName != target.LatestVersion {
logger.Info("New version available for %s", target.Repository)
target.LatestVersion = release.TagName

if _, err = a.targetApp.Update(context.Background(), target); err != nil {
logger.Error("unable to update target %s: %s", target.Repository, err)
}
} else if release.TagName == target.CurrentVersion {
logger.Info("%s is up-to-date!", target.Repository)
}
}

Expand Down
23 changes: 12 additions & 11 deletions pkg/target/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func scanTarget(row model.RowScanner) (Target, error) {
var target Target

err := row.Scan(&target.ID, &target.Owner, &target.Repository, &target.Version)
err := row.Scan(&target.ID, &target.Repository, &target.CurrentVersion, &target.LatestVersion)
if err != nil {
if err == sql.ErrNoRows {
return target, err
Expand All @@ -31,7 +31,7 @@ func scanTargets(rows *sql.Rows) ([]Target, uint, error) {
for rows.Next() {
var target Target

if err := rows.Scan(&target.ID, &target.Owner, &target.Repository, &target.Version, &totalCount); err != nil {
if err := rows.Scan(&target.ID, &target.Repository, &target.CurrentVersion, &target.LatestVersion, &totalCount); err != nil {
if err == sql.ErrNoRows {
return nil, 0, err
}
Expand All @@ -48,9 +48,9 @@ func scanTargets(rows *sql.Rows) ([]Target, uint, error) {
const listQuery = `
SELECT
id,
owner,
repository,
version,
current_version,
latest_version,
count(id) OVER() AS full_count
FROM
target
Expand Down Expand Up @@ -87,9 +87,9 @@ func (a app) listTargets(page, pageSize uint, sortKey string, sortAsc bool) ([]T
const getByIDQuery = `
SELECT
id,
owner,
repository,
version
current_version,
latest_version
FROM
target
WHERE
Expand All @@ -104,9 +104,9 @@ const insertQuery = `
INSERT INTO
target
(
owner,
repository,
version
current_version,
latest_version,
) VALUES (
$1,
$2,
Expand All @@ -119,7 +119,8 @@ const updateQuery = `
UPDATE
target
SET
version = $2
current_version = $2,
latest_version = $3
WHERE
id = $1
`
Expand All @@ -143,8 +144,8 @@ func (a app) saveTarget(o *Target, tx *sql.Tx) (err error) {
var newID uint64

if o.ID != 0 {
_, err = usedTx.Exec(updateQuery, o.ID, o.Version)
} else if insertErr := usedTx.QueryRow(insertQuery, o.Owner, o.Repository, o.Version).Scan(&newID); insertErr == nil {
_, err = usedTx.Exec(updateQuery, o.ID, o.CurrentVersion, o.LatestVersion)
} else if insertErr := usedTx.QueryRow(insertQuery, o.Repository, o.CurrentVersion, o.LatestVersion).Scan(&newID); insertErr == nil {
o.ID = newID
} else {
err = insertErr
Expand Down
8 changes: 4 additions & 4 deletions pkg/target/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package target

// Target is a watched repository for checking update
type Target struct {
ID uint64 `json:"id"`
Owner string `json:"owner"`
Repository string `json:"repository"`
Version string `json:"version"`
ID uint64 `json:"id"`
Repository string `json:"repository"`
CurrentVersion string `json:"current_version"`
LatestVersion string `json:"latest_version"`
}
10 changes: 3 additions & 7 deletions pkg/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,12 @@ func (a app) Check(ctx context.Context, old, new interface{}) []crud.Error {

errors := make([]crud.Error, 0)

if strings.TrimSpace(item.Owner) == "" {
errors = append(errors, crud.NewError("owner", "owner is required"))
}

if strings.TrimSpace(item.Repository) == "" {
errors = append(errors, crud.NewError("repository", "repository is required"))
errors = append(errors, crud.NewError("repository", "repository is required, in the form user/repository"))
}

if strings.TrimSpace(item.Version) == "" {
errors = append(errors, crud.NewError("version", "version is required"))
if strings.TrimSpace(item.CurrentVersion) == "" {
errors = append(errors, crud.NewError("current_version", "current version is required"))
}

return errors
Expand Down

0 comments on commit c4a9485

Please sign in to comment.