navid/veinsforge / 7629b4e4dd80194602d6bbebfa817ecf5de9b8883dc56230afa4dca3d788f5a5 / internal/data/ci.go

ci.go

← internal/data · 6447 bytes · raw · download

package data

import (
	"context"
	"database/sql"
	"time"
)

type CIConfig struct {
	ID          int64
	RepoID      int64
	Provider    string
	Endpoint    string
	Token       string
	Project     string
	Enabled     bool
	Required    bool
	ReportToken string
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

type CIRun struct {
	ID          int64
	RepoID      int64
	ConfigID    int64
	Provider    string
	Commit      string
	Bookmark    string
	Context     string
	State       string
	Description string
	URL         string
	ExternalID  string
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

const ciConfigColumns = `id, repo_id, provider, endpoint, token, project, enabled, required,
	report_token, created_at, updated_at`

const ciRunColumns = `id, repo_id, config_id, provider, commit_hash, bookmark, context, state,
	description, url, external_id, created_at, updated_at`

func scanCIConfig(sc interface{ Scan(...any) error }) (*CIConfig, error) {
	var c CIConfig
	var enabled, required int
	var created, updated int64
	err := sc.Scan(&c.ID, &c.RepoID, &c.Provider, &c.Endpoint, &c.Token, &c.Project,
		&enabled, &required, &c.ReportToken, &created, &updated)
	if err != nil {
		return nil, err
	}
	c.Enabled = intBool(enabled)
	c.Required = intBool(required)
	c.CreatedAt = fromUnix(created)
	c.UpdatedAt = fromUnix(updated)
	return &c, nil
}

func scanCIRun(sc interface{ Scan(...any) error }) (*CIRun, error) {
	var r CIRun
	var created, updated int64
	err := sc.Scan(&r.ID, &r.RepoID, &r.ConfigID, &r.Provider, &r.Commit, &r.Bookmark,
		&r.Context, &r.State, &r.Description, &r.URL, &r.ExternalID, &created, &updated)
	if err != nil {
		return nil, err
	}
	r.CreatedAt = fromUnix(created)
	r.UpdatedAt = fromUnix(updated)
	return &r, nil
}

func (s *Store) GetCIConfig(ctx context.Context, repoID int64) (*CIConfig, error) {
	c, err := scanCIConfig(s.db.QueryRowContext(ctx,
		`SELECT `+ciConfigColumns+` FROM ci_configs WHERE repo_id = ?`, repoID))
	if err == sql.ErrNoRows {
		return nil, ErrNotFound
	}
	return c, err
}

func (s *Store) UpsertCIConfig(ctx context.Context, c *CIConfig) (*CIConfig, error) {
	now := unix(time.Now())
	if c.CreatedAt.IsZero() {
		c.CreatedAt = fromUnix(now)
	}
	_, err := s.db.ExecContext(ctx, `
		INSERT INTO ci_configs(repo_id, provider, endpoint, token, project, enabled, required,
			report_token, created_at, updated_at)
		VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
		ON CONFLICT(repo_id) DO UPDATE SET
			provider = excluded.provider,
			endpoint = excluded.endpoint,
			token = excluded.token,
			project = excluded.project,
			enabled = excluded.enabled,
			required = excluded.required,
			report_token = CASE WHEN excluded.report_token = '' THEN ci_configs.report_token
				ELSE excluded.report_token END,
			updated_at = excluded.updated_at`,
		c.RepoID, c.Provider, c.Endpoint, c.Token, c.Project, boolInt(c.Enabled),
		boolInt(c.Required), c.ReportToken, now, now)
	if err != nil {
		return nil, err
	}
	return s.GetCIConfig(ctx, c.RepoID)
}

func (s *Store) DeleteCIConfig(ctx context.Context, repoID int64) error {
	_, err := s.db.ExecContext(ctx, `DELETE FROM ci_configs WHERE repo_id = ?`, repoID)
	return err
}

func (s *Store) ListPushWatchRepos(ctx context.Context) ([]*Repo, error) {
	rows, err := s.db.QueryContext(ctx, `SELECT `+repoColumns+` FROM repos
		WHERE id IN (SELECT repo_id FROM ci_configs WHERE enabled = 1)
		OR id IN (SELECT repo_id FROM webhooks WHERE active = 1
			AND (TRIM(events) = '' OR TRIM(events) = '*' OR events LIKE '%push%' OR events LIKE '%release%'))
		ORDER BY id`)
	if err != nil {
		return nil, err
	}
	return s.scanRepos(ctx, rows, nil)
}

func (s *Store) ListEnabledCIConfigs(ctx context.Context) ([]*CIConfig, error) {
	rows, err := s.db.QueryContext(ctx,
		`SELECT `+ciConfigColumns+` FROM ci_configs WHERE enabled = 1 ORDER BY repo_id`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var out []*CIConfig
	for rows.Next() {
		c, err := scanCIConfig(rows)
		if err != nil {
			return nil, err
		}
		out = append(out, c)
	}
	return out, rows.Err()
}

func (s *Store) CIConfigByToken(ctx context.Context, token string) (*CIConfig, error) {
	c, err := scanCIConfig(s.db.QueryRowContext(ctx,
		`SELECT `+ciConfigColumns+` FROM ci_configs WHERE report_token = ? AND enabled = 1`, token))
	if err == sql.ErrNoRows {
		return nil, ErrNotFound
	}
	return c, err
}

func (s *Store) RecordCIRun(ctx context.Context, r *CIRun) (*CIRun, error) {
	now := unix(time.Now())
	_, err := s.db.ExecContext(ctx, `
		INSERT INTO ci_runs(repo_id, config_id, provider, commit_hash, bookmark, context,
			state, description, url, external_id, created_at, updated_at)
		VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
		ON CONFLICT(repo_id, commit_hash, context) DO UPDATE SET
			config_id = excluded.config_id,
			provider = excluded.provider,
			bookmark = excluded.bookmark,
			state = excluded.state,
			description = excluded.description,
			url = excluded.url,
			external_id = excluded.external_id,
			updated_at = excluded.updated_at`,
		r.RepoID, r.ConfigID, r.Provider, r.Commit, r.Bookmark, r.Context, r.State,
		r.Description, r.URL, r.ExternalID, now, now)
	if err != nil {
		return nil, err
	}
	return s.getCIRun(ctx, r.RepoID, r.Commit, r.Context)
}

func (s *Store) getCIRun(ctx context.Context, repoID int64, commit, contextName string) (*CIRun, error) {
	r, err := scanCIRun(s.db.QueryRowContext(ctx,
		`SELECT `+ciRunColumns+` FROM ci_runs
		 WHERE repo_id = ? AND commit_hash = ? AND context = ?`,
		repoID, commit, contextName))
	if err == sql.ErrNoRows {
		return nil, ErrNotFound
	}
	return r, err
}

func (s *Store) LatestCIRun(ctx context.Context, repoID int64, commit string) (*CIRun, error) {
	r, err := scanCIRun(s.db.QueryRowContext(ctx,
		`SELECT `+ciRunColumns+` FROM ci_runs
		 WHERE repo_id = ? AND commit_hash = ?
		 ORDER BY updated_at DESC, id DESC LIMIT 1`, repoID, commit))
	if err == sql.ErrNoRows {
		return nil, ErrNotFound
	}
	return r, err
}

func (s *Store) ListCIRuns(ctx context.Context, repoID int64, commit string) ([]*CIRun, error) {
	rows, err := s.db.QueryContext(ctx,
		`SELECT `+ciRunColumns+` FROM ci_runs
		 WHERE repo_id = ? AND commit_hash = ?
		 ORDER BY updated_at DESC, id DESC`, repoID, commit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var out []*CIRun
	for rows.Next() {
		r, err := scanCIRun(rows)
		if err != nil {
			return nil, err
		}
		out = append(out, r)
	}
	return out, rows.Err()
}