Add support for access repos with credentials

This commit is contained in:
2023-02-07 21:34:32 -07:00
parent d870335d25
commit 7a665aa348
5 changed files with 401 additions and 36 deletions
+176 -3
View File
@@ -14,6 +14,63 @@ import (
var log = logging.MustGetLogger("cursorius-server")
func createSchema(db database.Database) (graphql.Schema, error) {
credentialType := graphql.NewObject(graphql.ObjectConfig{
Name: "Credential",
Description: "A credential for authenticating with the pipeline source host.",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "The id of the credential.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if credential, ok := p.Source.(database.Credential); ok {
return credential.Id, nil
}
return nil, nil
},
},
"name": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "The name of the credential.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if credential, ok := p.Source.(database.Credential); ok {
return credential.Name, nil
}
return nil, nil
},
},
"type": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "The credential type.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if credential, ok := p.Source.(database.Credential); ok {
return credential.Type, nil
}
return nil, nil
},
},
"username": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "The username to user with the credential.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if credential, ok := p.Source.(database.Credential); ok {
return credential.Username, nil
}
return nil, nil
},
},
"secret": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "The secret for the credential.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if credential, ok := p.Source.(database.Credential); ok {
return credential.Secret, nil
}
return nil, nil
},
},
},
})
webhookType := graphql.NewObject(graphql.ObjectConfig{
Name: "Webhook",
Description: "A webhook for triggering pipelines",
@@ -153,6 +210,16 @@ func createSchema(db database.Database) (graphql.Schema, error) {
return nil, nil
},
},
"credentialId": &graphql.Field{
Type: graphql.String,
Description: "The configured credential for cloning the pipeline source.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if pipeline, ok := p.Source.(database.Pipeline); ok {
return pipeline.Credential, nil
}
return nil, nil
},
},
"webhooks": &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(webhookType))),
Description: "The list of webhooks for the pipeline.",
@@ -202,6 +269,29 @@ func createSchema(db database.Database) (graphql.Schema, error) {
return db.GetPipelines()
},
},
"Credential": &graphql.Field{
Type: credentialType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
Description: "The id of the requested credential.",
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
id, err := uuid.Parse(p.Args["id"].(string))
if err != nil {
return nil, err
}
return db.GetCredentialById(id)
},
},
"Credentials": &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(credentialType)),
Args: graphql.FieldConfigArgument{},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return db.GetCredentials()
},
},
},
})
@@ -221,6 +311,9 @@ func createSchema(db database.Database) (graphql.Schema, error) {
"pollInterval": &graphql.ArgumentConfig{
Type: graphql.Int,
},
"credentialId": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
var interval int
@@ -229,18 +322,28 @@ func createSchema(db database.Database) (graphql.Schema, error) {
} else {
interval = 0
}
var credential *uuid.UUID
if credentialVal, ok := params.Args["credentialId"]; ok {
id, err := uuid.Parse(credentialVal.(string))
if err != nil {
return nil, err
}
credential = &id
} else {
credential = nil
}
pipeline, err := db.CreatePipeline(
params.Args["name"].(string),
params.Args["url"].(string),
interval,
credential,
)
if err != nil {
return nil, err
}
log.Infof("Created pipeline with id: %v, name: %v, url: %v, and poll interval: %v",
pipeline.Id, pipeline.Name, pipeline.Url, pipeline.PollInterval)
return pipeline, nil
},
},
@@ -272,6 +375,76 @@ func createSchema(db database.Database) (graphql.Schema, error) {
return webhook, nil
},
},
"createCredential": &graphql.Field{
Type: credentialType,
Description: "Create a new credential",
Args: graphql.FieldConfigArgument{
"name": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"type": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"username": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"secret": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
credential, err := db.CreateCredential(
params.Args["name"].(string),
database.CredentialType(params.Args["type"].(string)),
params.Args["username"].(string),
params.Args["secret"].(string),
)
if err != nil {
return nil, err
}
return credential, nil
},
},
"setPipelineCredential": &graphql.Field{
Type: pipelineType,
Description: "Add an credential to a pipeline",
Args: graphql.FieldConfigArgument{
"credentialId": &graphql.ArgumentConfig{
Type: graphql.String,
},
"pipelineId": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
pipelineId, err := uuid.Parse(params.Args["pipelineId"].(string))
if err != nil {
return nil, err
}
if credentialIdVal, ok := params.Args["credentialId"]; ok {
credentialId, err := uuid.Parse(credentialIdVal.(string))
if err != nil {
return nil, err
}
pipeline, err := db.SetPipelineCredential(pipelineId, &credentialId)
if err != nil {
return nil, err
}
return pipeline, nil
} else {
pipeline, err := db.SetPipelineCredential(pipelineId, nil)
if err != nil {
return nil, err
}
return pipeline, nil
}
},
},
},
})