Add support for access repos with credentials
This commit is contained in:
+132
-9
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
func (db *Database) GetPipelines() ([]Pipeline, error) {
|
||||
query := `
|
||||
SELECT id, name, url, poll_interval
|
||||
SELECT id, name, url, poll_interval, credential
|
||||
FROM pipelines;`
|
||||
|
||||
pipelines := make([]Pipeline, 0)
|
||||
@@ -23,7 +23,7 @@ FROM pipelines;`
|
||||
for rows.Next() {
|
||||
var pipeline Pipeline
|
||||
var idStr string
|
||||
if err := rows.Scan(&idStr, &pipeline.Name, &pipeline.Url, &pipeline.PollInterval); err != nil {
|
||||
if err := rows.Scan(&idStr, &pipeline.Name, &pipeline.Url, &pipeline.PollInterval, &pipeline.Credential); err != nil {
|
||||
return pipelines, err
|
||||
}
|
||||
|
||||
@@ -55,29 +55,71 @@ WHERE id=$1;`
|
||||
return pipeline, nil
|
||||
}
|
||||
|
||||
func (db *Database) CreatePipeline(name string, url string, pollInterval int) (Pipeline, error) {
|
||||
func (db *Database) CreatePipeline(name string, url string, pollInterval int, credential *uuid.UUID) (Pipeline, error) {
|
||||
query := `
|
||||
INSERT INTO pipelines (id, name, url, poll_interval)
|
||||
VALUES (uuid_generate_v4(), $1, $2, $3)
|
||||
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).Scan(&idStr, &pipeline.Name, &pipeline.Url, &pipeline.PollInterval)
|
||||
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)
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(idStr)
|
||||
pipeline.Id, err = uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
return pipeline, fmt.Errorf("Could not parse UUID generated by DB: %w", err)
|
||||
}
|
||||
|
||||
pipeline.Id = id
|
||||
|
||||
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
|
||||
@@ -151,6 +193,86 @@ RETURNING id, server_type, secret, pipeline;`
|
||||
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)
|
||||
@@ -173,6 +295,7 @@ RETURNING id, pipeline, in_progress;`
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user