Add scaffolding for cron trigger support
This commit is contained in:
@@ -88,6 +88,33 @@ func createSchema(db database.Database, pollChan chan uuid.UUID) (graphql.Schema
|
||||
},
|
||||
})
|
||||
|
||||
cronType := graphql.NewObject(graphql.ObjectConfig{
|
||||
Name: "Cron",
|
||||
Description: "A cron available for trigger pipeline runs.",
|
||||
Fields: graphql.Fields{
|
||||
"id": &graphql.Field{
|
||||
Type: graphql.NewNonNull(graphql.String),
|
||||
Description: "The id of the cron.",
|
||||
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||
if cron, ok := p.Source.(database.Cron); ok {
|
||||
return cron.Id, nil
|
||||
}
|
||||
return nil, nil
|
||||
},
|
||||
},
|
||||
"cron": &graphql.Field{
|
||||
Type: graphql.NewNonNull(graphql.String),
|
||||
Description: "The cron.",
|
||||
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||
if cron, ok := p.Source.(database.Cron); ok {
|
||||
return cron.Cron, nil
|
||||
}
|
||||
return nil, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
cloneCredentialType := graphql.NewObject(graphql.ObjectConfig{
|
||||
Name: "CloneCredential",
|
||||
Description: "A credential for authenticating with the pipeline source host.",
|
||||
@@ -316,6 +343,16 @@ func createSchema(db database.Database, pollChan chan uuid.UUID) (graphql.Schema
|
||||
return []database.Secret{}, nil
|
||||
},
|
||||
},
|
||||
"crons": &graphql.Field{
|
||||
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(cronType))),
|
||||
Description: "The list of crons for the pipeline.",
|
||||
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||
if pipeline, ok := p.Source.(database.Pipeline); ok {
|
||||
return db.GetCronsForPipeline(pipeline.Id)
|
||||
}
|
||||
return []database.Cron{}, nil
|
||||
},
|
||||
},
|
||||
"webhooks": &graphql.Field{
|
||||
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(webhookType))),
|
||||
Description: "The list of webhooks for the pipeline.",
|
||||
@@ -728,6 +765,73 @@ func createSchema(db database.Database, pollChan chan uuid.UUID) (graphql.Schema
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pipeline, err := db.GetPipelineById(pipelineId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pipeline, nil
|
||||
},
|
||||
},
|
||||
"addCronToPipeline": &graphql.Field{
|
||||
Type: pipelineType,
|
||||
Description: "Add a cron string to trigger the pipeline",
|
||||
Args: graphql.FieldConfigArgument{
|
||||
"cron": &graphql.ArgumentConfig{
|
||||
Type: graphql.NewNonNull(graphql.String),
|
||||
},
|
||||
"pipelineId": &graphql.ArgumentConfig{
|
||||
Type: graphql.NewNonNull(graphql.String),
|
||||
},
|
||||
},
|
||||
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
|
||||
|
||||
cron := params.Args["cron"].(string)
|
||||
|
||||
pipelineId, err := uuid.Parse(params.Args["pipelineId"].(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = db.AddCronForPipeline(pipelineId, cron)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pipeline, err := db.GetPipelineById(pipelineId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pipeline, nil
|
||||
},
|
||||
},
|
||||
"removeCronFromPipeline": &graphql.Field{
|
||||
Type: pipelineType,
|
||||
Description: "Remove a cron trigger from a pipeline.",
|
||||
Args: graphql.FieldConfigArgument{
|
||||
"cronId": &graphql.ArgumentConfig{
|
||||
Type: graphql.NewNonNull(graphql.String),
|
||||
},
|
||||
"pipelineId": &graphql.ArgumentConfig{
|
||||
Type: graphql.NewNonNull(graphql.String),
|
||||
},
|
||||
},
|
||||
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
|
||||
|
||||
cronId, err := uuid.Parse(params.Args["cronId"].(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pipelineId, err := uuid.Parse(params.Args["pipelineId"].(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = db.RemoveCronForPipeline(cronId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pipeline, err := db.GetPipelineById(pipelineId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -189,6 +189,16 @@ CREATE TABLE pipeline_refs (
|
||||
FOREIGN KEY(pipeline_id)
|
||||
REFERENCES pipelines(id)
|
||||
);
|
||||
|
||||
CREATE TABLE crons (
|
||||
id UUID PRIMARY KEY,
|
||||
pipeline_id UUID NOT NULL,
|
||||
cron TEXT NOT NULL,
|
||||
|
||||
CONSTRAINT fk_pipeline_id
|
||||
FOREIGN KEY(pipeline_id)
|
||||
REFERENCES pipelines(id)
|
||||
);
|
||||
`
|
||||
|
||||
_, err := conn.Exec(context.Background(), createTablesQuery)
|
||||
|
||||
@@ -670,3 +670,55 @@ RETURNING id, name, token;`
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (db *Database) GetCronsForPipeline(pipelineId uuid.UUID) ([]Cron, error) {
|
||||
query := `
|
||||
SELECT id, cron
|
||||
FROM crons
|
||||
WHERE pipeline_id=$1;`
|
||||
|
||||
var crons []Cron
|
||||
|
||||
cronEntrys, err := db.Conn.Query(context.Background(), query, pipelineId)
|
||||
if err != nil {
|
||||
return crons, fmt.Errorf("Could not get crons for pipeline with id \"%v\": %w", pipelineId, err)
|
||||
}
|
||||
defer cronEntrys.Close()
|
||||
|
||||
for cronEntrys.Next() {
|
||||
var cron Cron
|
||||
var idStr string
|
||||
if err := cronEntrys.Scan(
|
||||
&idStr, &cron.Cron,
|
||||
); err != nil {
|
||||
return crons, err
|
||||
}
|
||||
|
||||
cron.Id, err = uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
return crons, err
|
||||
}
|
||||
|
||||
crons = append(crons, cron)
|
||||
}
|
||||
|
||||
return crons, nil
|
||||
}
|
||||
|
||||
func (db *Database) AddCronForPipeline(pipelineId uuid.UUID, cron string) error {
|
||||
query := `
|
||||
INSERT INTO crons (id, pipeline_id, cron)
|
||||
VALUES (uuid_generate_v4(), $1, $2);`
|
||||
|
||||
_, err := db.Conn.Exec(context.Background(), query, pipelineId, cron)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Database) RemoveCronForPipeline(cronId uuid.UUID) error {
|
||||
query := `
|
||||
DELETE FROM crons
|
||||
WHERE id=$1;`
|
||||
|
||||
_, err := db.Conn.Exec(context.Background(), query, cronId)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -79,3 +79,8 @@ type Runner struct {
|
||||
Name string
|
||||
Token string
|
||||
}
|
||||
|
||||
type Cron struct {
|
||||
Id uuid.UUID
|
||||
Cron string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user