Add basic graphql api

This commit is contained in:
2023-01-14 06:02:17 -07:00
parent 30fed27126
commit 4a4b6fd185
8 changed files with 435 additions and 25 deletions
+18 -16
View File
@@ -18,7 +18,7 @@ type Database struct {
Conn *pgx.Conn
}
func LaunchDB(conf config.DBConfig) error {
func LaunchDB(conf config.DBConfig) (Database, error) {
dbURL := fmt.Sprintf(
"postgres://%v:%v@%v:%v/%v",
@@ -50,7 +50,7 @@ func LaunchDB(conf config.DBConfig) error {
}
if err != nil {
return fmt.Errorf("Could not open database: %w", err)
return db, fmt.Errorf("Could not open database: %w", err)
}
log.Infof("Database connected sucessfully!")
@@ -64,7 +64,7 @@ SELECT EXISTS (
var versionTableExists bool
err = db.Conn.QueryRow(context.Background(), versionTableExistsQuery).Scan(&versionTableExists)
if err != nil {
return fmt.Errorf("Could not check if database was initalized: %w", err)
return db, fmt.Errorf("Could not check if database was initalized: %w", err)
}
if versionTableExists {
@@ -73,31 +73,33 @@ SELECT EXISTS (
log.Info("New database found, initializing....")
err = initDB(db.Conn)
if err != nil {
return fmt.Errorf("Could not initalize database: %w", err)
return db, fmt.Errorf("Could not initalize database: %w", err)
}
}
return nil
return db, nil
}
func initDB(conn *pgx.Conn) error {
createTablesQuery := `
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE version (
version INT NOT NULL
);
CREATE TABLE pipelines (
id SERIAL PRIMARY KEY,
id UUID PRIMARY KEY,
name TEXT NOT NULL,
url TEXT NOT NULL,
poll_interval INTEGER
);
CREATE TABLE webhooks (
id SERIAL PRIMARY KEY,
type TEXT,
secret TEXT,
pipeline INTEGER,
id UUID PRIMARY KEY,
server_type TEXT,
secret TEXT,
pipeline UUID,
CONSTRAINT fk_pipeline
FOREIGN KEY(pipeline)
@@ -105,14 +107,14 @@ CREATE TABLE webhooks (
);
CREATE TABLE runners (
id SERIAL PRIMARY KEY,
id UUID PRIMARY KEY,
name TEXT,
secret TEXT
);
CREATE TABLE runs (
id SERIAL PRIMARY KEY,
pipeline SERIAL,
id UUID PRIMARY KEY,
pipeline UUID,
result BOOLEAN NOT NULL,
CONSTRAINT fk_pipeline
@@ -121,10 +123,10 @@ CREATE TABLE runs (
);
CREATE TABLE command_executions (
id SERIAL PRIMARY KEY,
run_id SERIAL,
id UUID PRIMARY KEY,
run_id UUID,
command TEXT,
return_code TEXT,
return_code INT,
stdout TEXT,
stderr TEXT,
start_time TIMESTAMP,
+124
View File
@@ -0,0 +1,124 @@
package database
import (
"context"
"fmt"
"github.com/google/uuid"
)
func (d *Database) GetPipelineById(id uuid.UUID) (Pipeline, error) {
query := `
SELECT name, url, poll_interval
FROM pipelines
WHERE id=$1;`
pipeline := Pipeline{
Id: id,
}
err := d.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 (d *Database) CreatePipeline(name string, url string, pollInterval int) (Pipeline, error) {
query := `
INSERT INTO pipelines (id, name, url, poll_interval)
VALUES (uuid_generate_v4(), $1, $2, $3)
RETURNING id, name, url, poll_interval;`
pipeline := Pipeline{}
var idStr string
err := d.Conn.QueryRow(context.Background(), query, name, url, pollInterval).Scan(&idStr, &pipeline.Name, &pipeline.Url, &pipeline.PollInterval)
if err != nil {
return pipeline, err
}
id, err := uuid.Parse(idStr)
if err != nil {
return pipeline, err
}
pipeline.Id = id
return pipeline, nil
}
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 {
log.Fatal(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 (d *Database) GetWebhookById(id uuid.UUID) (Webhook, error) {
query := `
SELECT server_type, secret, pipeline
FROM webhooks
WHERE id=$1;`
webhook := Webhook{
Id: id,
}
err := d.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 (d *Database) CreateWebhook(serverType WebhookSender, pipelineId uuid.UUID) (Webhook, error) {
//WITH secret_val as (select substr(md5(random()::text), 0, 50)),
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 := d.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
}
+50
View File
@@ -0,0 +1,50 @@
package database
import (
"time"
"github.com/google/uuid"
)
type Pipeline struct {
Id uuid.UUID
Name string
Url string
PollInterval int
}
type WebhookSender string
const (
Gitea WebhookSender = "gitea"
)
type Webhook struct {
Id uuid.UUID
ServerType WebhookSender
Secret string
Pipeline uuid.UUID
}
type Runner struct {
Id uuid.UUID
Name string
Secret string
}
type Run struct {
Id uuid.UUID
Pipeline uuid.UUID
Result bool
}
type CommandExecution struct {
Id uuid.UUID
RunId uuid.UUID
Command string
ReturnCode int
Stdout string
Stderr string
StartTime time.Time
EndTime time.Time
}