398 lines
9.4 KiB
Go
398 lines
9.4 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
func (db *Database) GetPipelines() ([]Pipeline, error) {
|
|
query := `
|
|
SELECT id, name, url, poll_interval, credential
|
|
FROM pipelines;`
|
|
|
|
pipelines := make([]Pipeline, 0)
|
|
|
|
rows, err := db.Conn.Query(context.Background(), query)
|
|
if err != nil {
|
|
return pipelines, fmt.Errorf("Could not query database for pipelines: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var pipeline Pipeline
|
|
var idStr string
|
|
if err := rows.Scan(&idStr, &pipeline.Name, &pipeline.Url, &pipeline.PollInterval, &pipeline.Credential); err != nil {
|
|
return pipelines, err
|
|
}
|
|
|
|
pipeline.Id, err = uuid.Parse(idStr)
|
|
if err != nil {
|
|
return pipelines, err
|
|
}
|
|
pipelines = append(pipelines, pipeline)
|
|
}
|
|
|
|
return pipelines, nil
|
|
}
|
|
|
|
func (db *Database) GetPipelineById(id uuid.UUID) (Pipeline, error) {
|
|
query := `
|
|
SELECT name, url, poll_interval
|
|
FROM pipelines
|
|
WHERE id=$1;`
|
|
|
|
pipeline := Pipeline{
|
|
Id: id,
|
|
}
|
|
|
|
err := db.Conn.QueryRow(context.Background(), query, id).Scan(&pipeline.Name, &pipeline.Url, &pipeline.PollInterval)
|
|
if err != nil {
|
|
return pipeline, fmt.Errorf("Could not query database for pipeline with id %v: %w", id.String(), err)
|
|
}
|
|
|
|
return pipeline, nil
|
|
}
|
|
|
|
func (db *Database) CreatePipeline(name string, url string, pollInterval int, credential *uuid.UUID) (Pipeline, error) {
|
|
query := `
|
|
INSERT INTO pipelines (id, name, url, poll_interval, credential)
|
|
VALUES (uuid_generate_v4(), $1, $2, $3, $4)
|
|
RETURNING id, name, url, poll_interval;`
|
|
|
|
pipeline := Pipeline{}
|
|
var idStr string
|
|
err := db.Conn.QueryRow(context.Background(), query, name, url, pollInterval, credential).Scan(&idStr, &pipeline.Name, &pipeline.Url, &pipeline.PollInterval)
|
|
if err != nil {
|
|
return pipeline, fmt.Errorf("Could not create pipeline: %w", err)
|
|
}
|
|
|
|
pipeline.Id, err = uuid.Parse(idStr)
|
|
if err != nil {
|
|
return pipeline, fmt.Errorf("Could not parse UUID generated by DB: %w", err)
|
|
}
|
|
|
|
return pipeline, nil
|
|
}
|
|
|
|
func (db *Database) SetPipelineCredential(pipelineId uuid.UUID, credentialId *uuid.UUID) (Pipeline, error) {
|
|
query := `
|
|
UPDATE pipelines
|
|
SET credential=$1
|
|
WHERE id=$2
|
|
RETURNING name, url, poll_interval, credential;`
|
|
|
|
pipeline := Pipeline{
|
|
Id: pipelineId,
|
|
}
|
|
|
|
err := db.Conn.QueryRow(context.Background(),
|
|
query, credentialId, pipelineId).Scan(
|
|
&pipeline.Name, &pipeline.Url, &pipeline.PollInterval, &pipeline.Credential,
|
|
)
|
|
if err != nil {
|
|
return pipeline, fmt.Errorf("Could not add credential to pipeline: %w", err)
|
|
}
|
|
|
|
return pipeline, err
|
|
}
|
|
|
|
func (db *Database) RemovePipelineCredential(pipelineId uuid.UUID) (Pipeline, error) {
|
|
query := `
|
|
UPDATE pipelines
|
|
SET credential=null
|
|
WHERE id=$1
|
|
RETURNING name, url, poll_interval, credential;`
|
|
|
|
pipeline := Pipeline{
|
|
Id: pipelineId,
|
|
}
|
|
|
|
err := db.Conn.QueryRow(context.Background(),
|
|
query, pipelineId).Scan(
|
|
&pipeline.Name, &pipeline.Url, &pipeline.PollInterval, &pipeline.Credential,
|
|
)
|
|
if err != nil {
|
|
return pipeline, fmt.Errorf("Could not add credential to pipeline: %w", err)
|
|
}
|
|
|
|
return pipeline, err
|
|
}
|
|
|
|
func (db *Database) GetWebhooksForPipeline(id uuid.UUID) ([]Webhook, error) {
|
|
query := `
|
|
SELECT id, server_type, secret
|
|
FROM webhooks
|
|
WHERE pipeline=$1;`
|
|
|
|
webhooks := make([]Webhook, 0)
|
|
|
|
rows, err := db.Conn.Query(context.Background(), query, id)
|
|
if err != nil {
|
|
return webhooks, fmt.Errorf("Could not get webhooks for pipeline with id \"%v\": %w", id, err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var webhook Webhook
|
|
var idStr string
|
|
if err := rows.Scan(&idStr, &webhook.ServerType, &webhook.Secret); err != nil {
|
|
return webhooks, err
|
|
}
|
|
|
|
webhook.Id, err = uuid.Parse(idStr)
|
|
if err != nil {
|
|
return webhooks, err
|
|
}
|
|
webhooks = append(webhooks, webhook)
|
|
}
|
|
|
|
return webhooks, nil
|
|
}
|
|
|
|
func (db *Database) GetWebhookById(id uuid.UUID) (Webhook, error) {
|
|
query := `
|
|
SELECT server_type, secret, pipeline
|
|
FROM webhooks
|
|
WHERE id=$1;`
|
|
|
|
webhook := Webhook{
|
|
Id: id,
|
|
}
|
|
|
|
err := db.Conn.QueryRow(context.Background(), query, id).Scan(&webhook.ServerType, &webhook.Secret, &webhook.Pipeline)
|
|
if err != nil {
|
|
return webhook, fmt.Errorf("Could not query database for webhook with id %v: %w", id.String(), err)
|
|
}
|
|
|
|
return webhook, nil
|
|
}
|
|
|
|
func (db *Database) CreateWebhook(serverType WebhookSender, pipelineId uuid.UUID) (Webhook, error) {
|
|
|
|
query := `
|
|
INSERT INTO webhooks (id, server_type, secret, pipeline)
|
|
VALUES (uuid_generate_v4(), $1, (select substr(md5(random()::text), 0, 50)), $2)
|
|
RETURNING id, server_type, secret, pipeline;`
|
|
|
|
webhook := Webhook{}
|
|
var idStr string
|
|
err := db.Conn.QueryRow(context.Background(), query, string(serverType), pipelineId).Scan(&idStr, &webhook.ServerType, &webhook.Secret, &webhook.Pipeline)
|
|
if err != nil {
|
|
return webhook, err
|
|
}
|
|
|
|
id, err := uuid.Parse(idStr)
|
|
if err != nil {
|
|
return webhook, err
|
|
}
|
|
|
|
webhook.Id = id
|
|
|
|
return webhook, nil
|
|
}
|
|
|
|
func (db *Database) CreateCredential(name string, credentialtype CredentialType, username string, secret string) (Credential, error) {
|
|
query := `
|
|
INSERT INTO credentials (id, name, type, username, secret)
|
|
VALUES(uuid_generate_v4(), $1, $2, $3, $4)
|
|
RETURNING id, name, type, username, secret;`
|
|
|
|
credential := Credential{}
|
|
var idStr string
|
|
err := db.Conn.QueryRow(
|
|
context.Background(),
|
|
query,
|
|
name,
|
|
string(credentialtype),
|
|
username,
|
|
secret,
|
|
).Scan(&idStr, &credential.Name, &credential.Type, &credential.Username, &credential.Secret)
|
|
if err != nil {
|
|
return credential, err
|
|
}
|
|
|
|
id, err := uuid.Parse(idStr)
|
|
if err != nil {
|
|
return credential, err
|
|
}
|
|
|
|
credential.Id = id
|
|
|
|
return credential, nil
|
|
}
|
|
|
|
func (db *Database) GetCredentialById(id uuid.UUID) (Credential, error) {
|
|
query := `
|
|
SELECT name, type, username, secret
|
|
FROM credentials
|
|
WHERE id=$1;`
|
|
|
|
log.Debugf("requested credential with id %v", id)
|
|
|
|
credential := Credential{
|
|
Id: id,
|
|
}
|
|
|
|
err := db.Conn.QueryRow(context.Background(), query, id).Scan(&credential.Name, &credential.Type, &credential.Username, &credential.Secret)
|
|
if err != nil {
|
|
return credential, fmt.Errorf("Could not query database for credential with id %v: %w", id.String(), err)
|
|
}
|
|
|
|
return credential, nil
|
|
}
|
|
|
|
func (db *Database) GetCredentials() ([]Credential, error) {
|
|
query := `
|
|
SELECT id, name, type, username, secret
|
|
FROM credentials;`
|
|
|
|
credentials := make([]Credential, 0)
|
|
|
|
rows, err := db.Conn.Query(context.Background(), query)
|
|
if err != nil {
|
|
return credentials, fmt.Errorf("Could not query database for credentials: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var credential Credential
|
|
var idStr string
|
|
if err := rows.Scan(&idStr, &credential.Name, &credential.Type, &credential.Username, &credential.Secret); err != nil {
|
|
return credentials, err
|
|
}
|
|
|
|
credential.Id, err = uuid.Parse(idStr)
|
|
if err != nil {
|
|
return credentials, err
|
|
}
|
|
credentials = append(credentials, credential)
|
|
}
|
|
|
|
return credentials, nil
|
|
}
|
|
|
|
func (db *Database) CreateRun(pipelineId uuid.UUID) (Run, error) {
|
|
query := `
|
|
INSERT INTO runs (id, pipeline, in_progress)
|
|
VALUES(uuid_generate_v4(), $1, true)
|
|
RETURNING id, pipeline, in_progress;`
|
|
|
|
run := Run{}
|
|
var idStr string
|
|
err := db.Conn.QueryRow(context.Background(), query, pipelineId).Scan(&idStr, &run.Pipeline, &run.InProgress)
|
|
if err != nil {
|
|
return run, err
|
|
}
|
|
|
|
run.Id, err = uuid.Parse(idStr)
|
|
if err != nil {
|
|
return run, err
|
|
}
|
|
|
|
return run, nil
|
|
}
|
|
|
|
func (db *Database) UpdateRunResult(r Run) error {
|
|
// TODO: the id fiend is the query is broken
|
|
query := `
|
|
UPDATE runs
|
|
SET in_progress=$1, result=$2, stdout=$3, stderr=$4
|
|
WHERE id=$3;`
|
|
|
|
// TODO: does r.Result need a pointer derefrence?
|
|
_, err := db.Conn.Exec(context.Background(),
|
|
query, r.InProgress, r.Result, r.Stdout, r.Stderr)
|
|
|
|
return err
|
|
}
|
|
|
|
func (db *Database) GetRunsForPipeline(pipelineId uuid.UUID) ([]Run, error) {
|
|
query := `
|
|
SELECT id, in_progress, result, stdout, stderr
|
|
FROM runs
|
|
WHERE pipeline=$1;`
|
|
|
|
runs := make([]Run, 0)
|
|
|
|
rows, err := db.Conn.Query(context.Background(), query, pipelineId)
|
|
if err != nil {
|
|
return runs, fmt.Errorf("Could not get runs for pipeline with id \"%v\": %w", pipelineId, err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var run Run
|
|
var idStr string
|
|
if err := rows.Scan(
|
|
&idStr,
|
|
&run.InProgress,
|
|
&run.Result,
|
|
&run.Stdout,
|
|
&run.Stderr,
|
|
); err != nil {
|
|
return runs, err
|
|
}
|
|
|
|
run.Id, err = uuid.Parse(idStr)
|
|
if err != nil {
|
|
return runs, err
|
|
}
|
|
runs = append(runs, run)
|
|
}
|
|
|
|
return runs, nil
|
|
}
|
|
|
|
func (db *Database) GetPipelineRefs(pipelineId uuid.UUID) (map[string]string, error) {
|
|
query := `
|
|
SELECT name, hash
|
|
FROM pipeline_refs
|
|
WHERE pipeline_id=$1;`
|
|
|
|
refsMap := make(map[string]string)
|
|
|
|
refs, err := db.Conn.Query(context.Background(), query, pipelineId)
|
|
if err != nil {
|
|
return refsMap, fmt.Errorf("Could not get pipeline refs for pipeline with id \"%v\": %w", pipelineId, err)
|
|
}
|
|
defer refs.Close()
|
|
|
|
for refs.Next() {
|
|
var name string
|
|
var hash string
|
|
if err := refs.Scan(
|
|
&name,
|
|
&hash,
|
|
); err != nil {
|
|
return refsMap, err
|
|
}
|
|
|
|
refsMap[name] = hash
|
|
}
|
|
|
|
return refsMap, nil
|
|
}
|
|
|
|
func (db *Database) UpdatePipelineRefs(pipelineId uuid.UUID, refsMap map[string]string) error {
|
|
|
|
refsSlice := make([][]interface{}, 0)
|
|
for name, ref := range refsMap {
|
|
refsSlice = append(refsSlice, []interface{}{name, pipelineId, ref})
|
|
}
|
|
|
|
copyCount, err := db.Conn.CopyFrom(
|
|
context.Background(),
|
|
pgx.Identifier{"pipeline_refs"},
|
|
[]string{"name", "pipeline_id", "hash"},
|
|
pgx.CopyFromRows(refsSlice),
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("could not insert updated pipeline refs: %w", err)
|
|
}
|
|
log.Debugf("copyCount: %v", copyCount)
|
|
return nil
|
|
}
|