Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
go-version-file: 'go.mod'
- name: lint
run: |
go install honnef.co/go/tools/cmd/staticcheck@latest
go install honnef.co/go/tools/cmd/staticcheck@v0.8.0-rc.1
staticcheck ./...
- name: vet
run: |
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.25 AS builder
FROM golang:1.27 AS builder

WORKDIR /go/src/github.com/whywaita/myshoes

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/whywaita/myshoes

go 1.25
go 1.27

require (
github.com/bradleyfalzon/ghinstallation/v2 v2.17.0
Expand All @@ -16,7 +16,6 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.23.2
github.com/r3labs/diff/v2 v2.15.1
github.com/satori/go.uuid v1.2.0
goji.io v2.0.2+incompatible
golang.org/x/oauth2 v0.32.0
golang.org/x/sync v0.18.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ github.com/r3labs/diff/v2 v2.15.1 h1:EOrVqPUzi+njlumoqJwiS/TgGgmZo83619FNDB9xQUg
github.com/r3labs/diff/v2 v2.15.1/go.mod h1:I8noH9Fc2fjSaMxqF3G2lhDdC0b+JXCfyx85tWFM9kc=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
34 changes: 16 additions & 18 deletions pkg/datastore/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"strings"
"time"

uuid "github.com/satori/go.uuid"

"github.com/whywaita/myshoes/pkg/gh"
"github.com/whywaita/myshoes/pkg/logger"
)
Expand All @@ -29,27 +27,27 @@ var (
// Datastore is persistent storage
type Datastore interface {
CreateTarget(ctx context.Context, target Target) error
GetTarget(ctx context.Context, id uuid.UUID) (*Target, error)
GetTarget(ctx context.Context, id UUID) (*Target, error)
GetTargetByScope(ctx context.Context, scope string) (*Target, error)
ListTargets(ctx context.Context) ([]Target, error)
DeleteTarget(ctx context.Context, id uuid.UUID) error
DeleteTarget(ctx context.Context, id UUID) error

// Deprecated: Use datastore.UpdateTargetStatus.
UpdateTargetStatus(ctx context.Context, targetID uuid.UUID, newStatus TargetStatus, description string) error
UpdateToken(ctx context.Context, targetID uuid.UUID, newToken string, newExpiredAt time.Time) error
UpdateTargetStatus(ctx context.Context, targetID UUID, newStatus TargetStatus, description string) error
UpdateToken(ctx context.Context, targetID UUID, newToken string, newExpiredAt time.Time) error

UpdateTargetParam(ctx context.Context, targetID uuid.UUID, newResourceType ResourceType, newProviderURL sql.NullString) error
UpdateTargetParam(ctx context.Context, targetID UUID, newResourceType ResourceType, newProviderURL sql.NullString) error

EnqueueJob(ctx context.Context, job Job) error
ListJobs(ctx context.Context) ([]Job, error)
DeleteJob(ctx context.Context, id uuid.UUID) error
DeleteJob(ctx context.Context, id UUID) error

CreateRunner(ctx context.Context, runner Runner) error
ListRunners(ctx context.Context) ([]Runner, error)
ListRunnersByTargetID(ctx context.Context, targetID uuid.UUID) ([]Runner, error)
ListRunnersByTargetID(ctx context.Context, targetID UUID) ([]Runner, error)
ListRunnersLogBySince(ctx context.Context, since time.Time) ([]Runner, error)
GetRunner(ctx context.Context, id uuid.UUID) (*Runner, error)
DeleteRunner(ctx context.Context, id uuid.UUID, deletedAt time.Time, reason RunnerStatus) error
GetRunner(ctx context.Context, id UUID) (*Runner, error)
DeleteRunner(ctx context.Context, id UUID, deletedAt time.Time, reason RunnerStatus) error

// Lock
GetLock(ctx context.Context) error
Expand All @@ -58,8 +56,8 @@ type Datastore interface {

// Target is a target repository that will add auto-scaling runner.
type Target struct {
UUID uuid.UUID `db:"uuid" json:"id"`
Scope string `db:"scope" json:"scope"` // repo (:owner/:repo) or org (:organization)
UUID UUID `db:"uuid" json:"id"`
Scope string `db:"scope" json:"scope"` // repo (:owner/:repo) or org (:organization)
// deprecated
GitHubToken string `db:"github_token" json:"github_token"`
TokenExpiredAt time.Time `db:"token_expired_at" json:"token_expired_at"`
Expand Down Expand Up @@ -107,7 +105,7 @@ func ListTargets(ctx context.Context, ds Datastore) ([]Target, error) {
}

// UpdateTargetStatus update datastore
func UpdateTargetStatus(ctx context.Context, ds Datastore, targetID uuid.UUID, newStatus TargetStatus, description string) error {
func UpdateTargetStatus(ctx context.Context, ds Datastore, targetID UUID, newStatus TargetStatus, description string) error {
target, err := ds.GetTarget(ctx, targetID)
if err != nil {
return fmt.Errorf("failed to get target: %w", err)
Expand Down Expand Up @@ -170,11 +168,11 @@ const (

// Job is a runner job
type Job struct {
UUID uuid.UUID `db:"uuid"`
UUID UUID `db:"uuid"`
GHEDomain sql.NullString `db:"ghe_domain"`
Repository string `db:"repository"` // repo (:owner/:repo)
CheckEventJSON string `db:"check_event"`
TargetID uuid.UUID `db:"target_id"`
TargetID UUID `db:"target_id"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
}
Expand All @@ -198,10 +196,10 @@ func (j *Job) RepoURL() string {

// Runner is a runner
type Runner struct {
UUID uuid.UUID `db:"runner_id"`
UUID UUID `db:"runner_id"`
ShoesType string `db:"shoes_type"`
IPAddress string `db:"ip_address"`
TargetID uuid.UUID `db:"target_id"`
TargetID UUID `db:"target_id"`
CloudID string `db:"cloud_id"`
Deleted bool `db:"deleted"`
Status RunnerStatus `db:"status"`
Expand Down
34 changes: 16 additions & 18 deletions pkg/datastore/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,23 @@ import (
"sync"
"time"

uuid "github.com/satori/go.uuid"

"github.com/whywaita/myshoes/pkg/datastore"
)

// Memory is implement datastore on-memory
type Memory struct {
mu *sync.RWMutex
targets map[uuid.UUID]datastore.Target
jobs map[uuid.UUID]datastore.Job
runners map[uuid.UUID]datastore.Runner
targets map[datastore.UUID]datastore.Target
jobs map[datastore.UUID]datastore.Job
runners map[datastore.UUID]datastore.Runner
}

// New create map
func New() (*Memory, error) {
m := &sync.RWMutex{}
t := map[uuid.UUID]datastore.Target{}
j := map[uuid.UUID]datastore.Job{}
r := map[uuid.UUID]datastore.Runner{}
t := map[datastore.UUID]datastore.Target{}
j := map[datastore.UUID]datastore.Job{}
r := map[datastore.UUID]datastore.Runner{}

return &Memory{
mu: m,
Expand All @@ -45,7 +43,7 @@ func (m *Memory) CreateTarget(ctx context.Context, target datastore.Target) erro
}

// GetTarget get a target
func (m *Memory) GetTarget(ctx context.Context, id uuid.UUID) (*datastore.Target, error) {
func (m *Memory) GetTarget(ctx context.Context, id datastore.UUID) (*datastore.Target, error) {
m.mu.RLock()
defer m.mu.RUnlock()

Expand Down Expand Up @@ -87,7 +85,7 @@ func (m *Memory) ListTargets(ctx context.Context) ([]datastore.Target, error) {
}

// DeleteTarget delete a target
func (m *Memory) DeleteTarget(ctx context.Context, id uuid.UUID) error {
func (m *Memory) DeleteTarget(ctx context.Context, id datastore.UUID) error {
m.mu.Lock()
defer m.mu.Unlock()

Expand All @@ -96,7 +94,7 @@ func (m *Memory) DeleteTarget(ctx context.Context, id uuid.UUID) error {
}

// UpdateTargetStatus update status in target
func (m *Memory) UpdateTargetStatus(ctx context.Context, targetID uuid.UUID, newStatus datastore.TargetStatus, description string) error {
func (m *Memory) UpdateTargetStatus(ctx context.Context, targetID datastore.UUID, newStatus datastore.TargetStatus, description string) error {
m.mu.Lock()
defer m.mu.Unlock()

Expand All @@ -119,7 +117,7 @@ func (m *Memory) UpdateTargetStatus(ctx context.Context, targetID uuid.UUID, new
}

// UpdateToken update token in target
func (m *Memory) UpdateToken(ctx context.Context, targetID uuid.UUID, newToken string, newExpiredAt time.Time) error {
func (m *Memory) UpdateToken(ctx context.Context, targetID datastore.UUID, newToken string, newExpiredAt time.Time) error {
m.mu.Lock()
defer m.mu.Unlock()

Expand All @@ -135,7 +133,7 @@ func (m *Memory) UpdateToken(ctx context.Context, targetID uuid.UUID, newToken s
}

// UpdateTargetParam update parameter of target
func (m *Memory) UpdateTargetParam(ctx context.Context, targetID uuid.UUID, newResourceType datastore.ResourceType, newProviderURL string) error {
func (m *Memory) UpdateTargetParam(ctx context.Context, targetID datastore.UUID, newResourceType datastore.ResourceType, newProviderURL string) error {
m.mu.Lock()
defer m.mu.Unlock()

Expand Down Expand Up @@ -176,7 +174,7 @@ func (m *Memory) ListJobs(ctx context.Context) ([]datastore.Job, error) {
}

// DeleteJob delete a job
func (m *Memory) DeleteJob(ctx context.Context, id uuid.UUID) error {
func (m *Memory) DeleteJob(ctx context.Context, id datastore.UUID) error {
m.mu.Lock()
defer m.mu.Unlock()

Expand Down Expand Up @@ -208,13 +206,13 @@ func (m *Memory) ListRunners(ctx context.Context) ([]datastore.Runner, error) {
}

// ListRunnersByTargetID get a not deleted runners that has target_id
func (m *Memory) ListRunnersByTargetID(ctx context.Context, targetID uuid.UUID) ([]datastore.Runner, error) {
func (m *Memory) ListRunnersByTargetID(ctx context.Context, targetID datastore.UUID) ([]datastore.Runner, error) {
m.mu.Lock()
defer m.mu.Unlock()

var runners []datastore.Runner
for _, r := range m.runners {
if uuid.Equal(r.TargetID, targetID) {
if r.TargetID == targetID {
runners = append(runners, r)
}
}
Expand All @@ -238,7 +236,7 @@ func (m *Memory) ListRunnersLogBySince(ctx context.Context, since time.Time) ([]
}

// GetRunner get a runner
func (m *Memory) GetRunner(ctx context.Context, id uuid.UUID) (*datastore.Runner, error) {
func (m *Memory) GetRunner(ctx context.Context, id datastore.UUID) (*datastore.Runner, error) {
m.mu.Lock()
defer m.mu.Unlock()

Expand All @@ -251,7 +249,7 @@ func (m *Memory) GetRunner(ctx context.Context, id uuid.UUID) (*datastore.Runner
}

// DeleteRunner delete a runner
func (m *Memory) DeleteRunner(ctx context.Context, id uuid.UUID, deletedAt time.Time, reason datastore.RunnerStatus) error {
func (m *Memory) DeleteRunner(ctx context.Context, id datastore.UUID, deletedAt time.Time, reason datastore.RunnerStatus) error {
m.mu.Lock()
defer m.mu.Unlock()

Expand Down
7 changes: 3 additions & 4 deletions pkg/datastore/mysql/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"errors"
"fmt"

uuid "github.com/satori/go.uuid"
"github.com/whywaita/myshoes/pkg/datastore"
)

// EnqueueJob add a job
func (m *MySQL) EnqueueJob(ctx context.Context, job datastore.Job) error {
query := `INSERT INTO jobs(uuid, ghe_domain, repository, check_event, target_id) VALUES (?, ?, ?, ?, ?)`
if _, err := m.Conn.ExecContext(ctx, query, job.UUID, job.GHEDomain, job.Repository, job.CheckEventJSON, job.TargetID.String()); err != nil {
if _, err := m.Conn.ExecContext(ctx, query, job.UUID, job.GHEDomain, job.Repository, job.CheckEventJSON, job.TargetID); err != nil {
return fmt.Errorf("failed to execute INSERT query: %w", err)
}

Expand Down Expand Up @@ -43,9 +42,9 @@ func (m *MySQL) ListJobs(ctx context.Context) ([]datastore.Job, error) {
}

// DeleteJob delete a job
func (m *MySQL) DeleteJob(ctx context.Context, id uuid.UUID) error {
func (m *MySQL) DeleteJob(ctx context.Context, id datastore.UUID) error {
query := `DELETE FROM jobs WHERE uuid = ?`
if _, err := m.Conn.ExecContext(ctx, query, id.String()); err != nil {
if _, err := m.Conn.ExecContext(ctx, query, id); err != nil {
return fmt.Errorf("failed to execute DELETE query: %w", err)
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/datastore/mysql/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/jmoiron/sqlx"
uuid "github.com/satori/go.uuid"
"uuid"

"github.com/whywaita/myshoes/internal/testutils"
"github.com/whywaita/myshoes/pkg/datastore"
)

var testJobID = uuid.FromStringOrNil("1b4e5b7a-e3c1-4829-9cfd-eac4183f2c95")
var testJobID = datastore.UUID{UUID: uuid.MustParse("1b4e5b7a-e3c1-4829-9cfd-eac4183f2c95")}

func TestMySQL_EnqueueJob(t *testing.T) {
testDatastore, teardown := testutils.GetTestDatastore()
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestMySQL_DeleteJob(t *testing.T) {
}

tests := []struct {
input uuid.UUID
input datastore.UUID
want *datastore.Job
err bool
}{
Expand Down Expand Up @@ -206,14 +206,14 @@ func TestMySQL_DeleteJob(t *testing.T) {
}
}

func getJobFromSQL(testDB *sqlx.DB, id uuid.UUID) (*datastore.Job, error) {
func getJobFromSQL(testDB *sqlx.DB, id datastore.UUID) (*datastore.Job, error) {
var j datastore.Job
query := `SELECT uuid, ghe_domain, repository, check_event, target_id FROM jobs WHERE uuid = ?`
stmt, err := testDB.Preparex(query)
if err != nil {
return nil, fmt.Errorf("failed to prepare: %w", err)
}
err = stmt.Get(&j, id)
err = stmt.Get(&j, id.String())
if err != nil {
return nil, fmt.Errorf("failed to get job: %w", err)
}
Expand Down
19 changes: 9 additions & 10 deletions pkg/datastore/mysql/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"time"

uuid "github.com/satori/go.uuid"
"github.com/whywaita/myshoes/pkg/datastore"
)

Expand All @@ -16,19 +15,19 @@ func (m *MySQL) CreateRunner(ctx context.Context, runner datastore.Runner) error
tx := m.Conn.MustBegin()

queryRunner := `INSERT INTO runners(uuid) VALUES (?)`
if _, err := tx.ExecContext(ctx, queryRunner, runner.UUID.String()); err != nil {
if _, err := tx.ExecContext(ctx, queryRunner, runner.UUID); err != nil {
tx.Rollback()
return fmt.Errorf("failed to execute INSERT query runners: %w", err)
}

queryDetail := `INSERT INTO runner_detail(runner_id, shoes_type, ip_address, target_id, cloud_id, resource_type, runner_user, repository_url, request_webhook, provider_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
if _, err := tx.ExecContext(ctx, queryDetail, runner.UUID.String(), runner.ShoesType, runner.IPAddress, runner.TargetID.String(), runner.CloudID, runner.ResourceType, runner.RunnerUser, runner.RepositoryURL, runner.RequestWebhook, runner.ProviderURL); err != nil {
if _, err := tx.ExecContext(ctx, queryDetail, runner.UUID, runner.ShoesType, runner.IPAddress, runner.TargetID, runner.CloudID, runner.ResourceType, runner.RunnerUser, runner.RepositoryURL, runner.RequestWebhook, runner.ProviderURL); err != nil {
tx.Rollback()
return fmt.Errorf("failed to execute INSERT query runner_detail: %w", err)
}

queryRunning := `INSERT INTO runners_running(runner_id) VALUES (?)`
if _, err := tx.ExecContext(ctx, queryRunning, runner.UUID.String()); err != nil {
if _, err := tx.ExecContext(ctx, queryRunning, runner.UUID); err != nil {
tx.Rollback()
return fmt.Errorf("failed to execute INSERT query runners_running: %w", err)
}
Expand Down Expand Up @@ -58,7 +57,7 @@ func (m *MySQL) ListRunners(ctx context.Context) ([]datastore.Runner, error) {
}

// ListRunnersByTargetID get a not deleted runners that has target_id
func (m *MySQL) ListRunnersByTargetID(ctx context.Context, targetID uuid.UUID) ([]datastore.Runner, error) {
func (m *MySQL) ListRunnersByTargetID(ctx context.Context, targetID datastore.UUID) ([]datastore.Runner, error) {
var runners []datastore.Runner
query := `SELECT runner.runner_id, detail.shoes_type, detail.ip_address, detail.target_id, detail.cloud_id, detail.created_at, detail.updated_at, detail.resource_type, detail.repository_url, detail.request_webhook, detail.runner_user, detail.provider_url
FROM runners_running AS runner JOIN runner_detail AS detail ON runner.runner_id = detail.runner_id WHERE detail.target_id = ?`
Expand Down Expand Up @@ -92,11 +91,11 @@ func (m *MySQL) ListRunnersLogBySince(ctx context.Context, since time.Time) ([]d
}

// GetRunner get a runner
func (m *MySQL) GetRunner(ctx context.Context, id uuid.UUID) (*datastore.Runner, error) {
func (m *MySQL) GetRunner(ctx context.Context, id datastore.UUID) (*datastore.Runner, error) {
var r datastore.Runner

query := `SELECT runner_id, shoes_type, ip_address, target_id, cloud_id, created_at, updated_at, resource_type, repository_url, request_webhook, runner_user, provider_url FROM runner_detail WHERE runner_id = ?`
if err := m.Conn.GetContext(ctx, &r, query, id.String()); err != nil {
if err := m.Conn.GetContext(ctx, &r, query, id); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, datastore.ErrNotFound
}
Expand All @@ -108,17 +107,17 @@ func (m *MySQL) GetRunner(ctx context.Context, id uuid.UUID) (*datastore.Runner,
}

// DeleteRunner delete a runner
func (m *MySQL) DeleteRunner(ctx context.Context, id uuid.UUID, deletedAt time.Time, reason datastore.RunnerStatus) error {
func (m *MySQL) DeleteRunner(ctx context.Context, id datastore.UUID, deletedAt time.Time, reason datastore.RunnerStatus) error {
tx := m.Conn.MustBegin()

queryDelete := `DELETE FROM runners_running WHERE runner_id = ?`
if _, err := tx.ExecContext(ctx, queryDelete, id.String()); err != nil {
if _, err := tx.ExecContext(ctx, queryDelete, id); err != nil {
tx.Rollback()
return fmt.Errorf("failed to execute DELETE query: %w", err)
}

queryInsert := `INSERT INTO runners_deleted(runner_id, reason) VALUES (?, ?)`
if _, err := tx.ExecContext(ctx, queryInsert, id.String(), reason); err != nil {
if _, err := tx.ExecContext(ctx, queryInsert, id, reason); err != nil {
tx.Rollback()
return fmt.Errorf("failed to execute INSERT query: %w", err)
}
Expand Down
Loading
Loading