models

package
v0.0.0-...-d6e8d26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2024 License: Apache-2.0 Imports: 16 Imported by: 4

Documentation

Index

Constants

View Source
const (
	AccessPolicyType = "access"
	AdminPolicyType  = "admin"
	CliJobAccessType = "cli_access"
)
View Source
const (
	POLICY_TYPE_ACCESS = "access"
	POLICY_TYPE_PLAN   = "plan"
	POLICY_TYPE_DRIFT  = "drift"
)

Variables

View Source
var DEFAULT_ORG_NAME = "digger"

Functions

func ConnectDatabase

func ConnectDatabase()

Types

type Database

type Database struct {
	GormDB *gorm.DB
}
var DB *Database

var DB *gorm.DB

func (*Database) CreateDiggerBatch

func (db *Database) CreateDiggerBatch(vcsType DiggerVCSType, githubInstallationId int64, repoOwner string, repoName string, repoFullname string, PRNumber int, diggerConfig string, branchName string, batchType scheduler.DiggerCommand, commentId *int64, gitlabProjectId int, aiSummaryCommentId string, reportTerraformOutputs bool) (*DiggerBatch, error)

func (*Database) CreateDiggerJob

func (db *Database) CreateDiggerJob(batchId uuid.UUID, serializedJob []byte, workflowFile string) (*DiggerJob, error)
func (db *Database) CreateDiggerJobLink(diggerJobId string, repoFullName string) (*GithubDiggerJobLink, error)
func (db *Database) CreateDiggerJobParentLink(parentJobId string, jobId string) error

func (*Database) CreateDiggerJobToken

func (db *Database) CreateDiggerJobToken(organisationId uint) (*JobToken, error)

func (*Database) CreateDiggerLock

func (db *Database) CreateDiggerLock(resource string, lockId int, orgId uint) (*DiggerLock, error)

func (*Database) CreateDiggerRun

func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status DiggerRunStatus, CommitId string, DiggerConfig string, GithubInstallationId int64, RepoId uint, ProjectName string, RunType RunType, planStageId *uint, applyStageId *uint) (*DiggerRun, error)

func (*Database) CreateDiggerRunQueueItem

func (db *Database) CreateDiggerRunQueueItem(diggeRrunId uint, projectId uint) (*DiggerRunQueueItem, error)

func (*Database) CreateDiggerRunStage

func (db *Database) CreateDiggerRunStage(batchId string) (*DiggerRunStage, error)

func (*Database) CreateGithubAppConnection

func (db *Database) CreateGithubAppConnection(name string, githubId int64, ClientID string, ClientSecretEncrypted string, WebhookSecretEncrypted string, PrivateKeyEncrypted string, PrivateKeyBase64Encrypted string, Org string, url string, orgId uint) (*GithubAppConnection, error)

func (*Database) CreateGithubAppInstallation

func (db *Database) CreateGithubAppInstallation(installationId int64, githubAppId int64, login string, accountId int, repoFullName string) (*GithubAppInstallation, error)
func (db *Database) CreateGithubInstallationLink(org *Organisation, installationId int64) (*GithubAppInstallationLink, error)

func (*Database) CreateOrganisation

func (db *Database) CreateOrganisation(name string, externalSource string, tenantId string) (*Organisation, error)

func (*Database) CreateProject

func (db *Database) CreateProject(name string, org *Organisation, repo *Repo, isGenerated bool, isInMainBranch bool) (*Project, error)

func (*Database) CreateRepo

func (db *Database) CreateRepo(name string, repoFullName string, repoOrganisation string, repoName string, repoUrl string, org *Organisation, diggerConfig string) (*Repo, error)

func (*Database) DeleteJobTokenArtefacts

func (db *Database) DeleteJobTokenArtefacts(jobTokenId uint) error

func (*Database) DequeueRunItem

func (db *Database) DequeueRunItem(queueItem *DiggerRunQueueItem) error

func (*Database) GetDefaultRepo

func (db *Database) GetDefaultRepo(c *gin.Context, orgIdKey string) (*Repo, bool)

func (*Database) GetDiggerBatch

func (db *Database) GetDiggerBatch(batchId *uuid.UUID) (*DiggerBatch, error)

func (*Database) GetDiggerJob

func (db *Database) GetDiggerJob(jobId string) (*DiggerJob, error)

func (*Database) GetDiggerJobFromRunStage

func (db *Database) GetDiggerJobFromRunStage(stage DiggerRunStage) (*DiggerJob, error)
func (db *Database) GetDiggerJobLink(diggerJobId string) (*GithubDiggerJobLink, error)

func (*Database) GetDiggerJobParentLinksByParentId

func (db *Database) GetDiggerJobParentLinksByParentId(parentId *string) ([]DiggerJobParentLink, error)

func (*Database) GetDiggerJobParentLinksChildId

func (db *Database) GetDiggerJobParentLinksChildId(childId *string) ([]DiggerJobParentLink, error)

func (*Database) GetDiggerJobsForBatch

func (db *Database) GetDiggerJobsForBatch(batchId uuid.UUID) ([]DiggerJob, error)

func (*Database) GetDiggerJobsForBatchWithStatus

func (db *Database) GetDiggerJobsForBatchWithStatus(batchId uuid.UUID, status []scheduler.DiggerJobStatus) ([]DiggerJob, error)

func (*Database) GetDiggerJobsWithStatus

func (db *Database) GetDiggerJobsWithStatus(status scheduler.DiggerJobStatus) ([]DiggerJob, error)

func (*Database) GetDiggerLock

func (db *Database) GetDiggerLock(resource string) (*DiggerLock, error)

func (*Database) GetDiggerRun

func (db *Database) GetDiggerRun(id uint) (*DiggerRun, error)

func (*Database) GetDiggerRunQueueItem

func (db *Database) GetDiggerRunQueueItem(id uint) (*DiggerRunQueueItem, error)

func (*Database) GetFirstRunQueueForEveryProject

func (db *Database) GetFirstRunQueueForEveryProject() ([]DiggerRunQueueItem, error)

func (*Database) GetGithubAppConnection

func (db *Database) GetGithubAppConnection(gitHubAppId any) (*GithubAppConnection, error)

GetGithubApp return GithubApp by Id

func (*Database) GetGithubAppConnectionById

func (db *Database) GetGithubAppConnectionById(id string) (*GithubAppConnection, error)

func (*Database) GetGithubAppInstallationByIdAndRepo

func (db *Database) GetGithubAppInstallationByIdAndRepo(installationId int64, repoFullName string) (*GithubAppInstallation, error)

GetGithubAppInstallationByIdAndRepo repoFullName should be in the following format: org/repo_name, for example "diggerhq/github-job-scheduler"

func (*Database) GetGithubAppInstallationByOrgAndRepo

func (db *Database) GetGithubAppInstallationByOrgAndRepo(orgId any, repo string, status GithubAppInstallStatus) (*GithubAppInstallation, error)
func (db *Database) GetGithubAppInstallationLink(installationId int64) (*GithubAppInstallationLink, error)

GetGithubAppInstallationLink repoFullName should be in the following format: org/repo_name, for example "diggerhq/github-job-scheduler"

func (*Database) GetGithubAppInstallations

func (db *Database) GetGithubAppInstallations(installationId int64) ([]GithubAppInstallation, error)

func (*Database) GetGithubInstallationLinkForInstallationId

func (db *Database) GetGithubInstallationLinkForInstallationId(installationId any) (*GithubAppInstallationLink, error)

func (*Database) GetGithubInstallationLinkForOrg

func (db *Database) GetGithubInstallationLinkForOrg(orgId any) (*GithubAppInstallationLink, error)

func (*Database) GetJobArtefact

func (db *Database) GetJobArtefact(jobTokenId uint) (*JobArtefact, error)

func (*Database) GetJobToken

func (db *Database) GetJobToken(tenantId any) (*JobToken, error)

func (*Database) GetLastDiggerRunForProject

func (db *Database) GetLastDiggerRunForProject(projectName string) (*DiggerRun, error)

func (*Database) GetOrganisation

func (db *Database) GetOrganisation(tenantId any) (*Organisation, error)

func (*Database) GetOrganisationById

func (db *Database) GetOrganisationById(orgId any) (*Organisation, error)

func (*Database) GetPendingParentDiggerJobs

func (db *Database) GetPendingParentDiggerJobs(batchId *uuid.UUID) ([]DiggerJob, error)

func (*Database) GetPoliciesFromContext

func (db *Database) GetPoliciesFromContext(c *gin.Context, orgIdKey string) ([]Policy, bool)

func (*Database) GetPolicyByPolicyId

func (db *Database) GetPolicyByPolicyId(c *gin.Context, policyId uint, orgIdKey string) (*Policy, bool)

func (*Database) GetProject

func (db *Database) GetProject(projectId uint) (*Project, error)

func (*Database) GetProjectByName

func (db *Database) GetProjectByName(orgId any, repo *Repo, name string) (*Project, error)

GetProjectByName return project for specified org and repo if record doesn't exist return nil

func (*Database) GetProjectByProjectId

func (db *Database) GetProjectByProjectId(c *gin.Context, projectId uint, orgIdKey string) (*Project, bool)

func (*Database) GetProjectByRepo

func (db *Database) GetProjectByRepo(orgId any, repo *Repo) ([]Project, error)

GetProjectByRepo return projects for specified org and repo

func (*Database) GetProjectByRunId

func (db *Database) GetProjectByRunId(c *gin.Context, runId uint, orgIdKey string) (*ProjectRun, bool)

func (*Database) GetProjectRunsForOrg

func (db *Database) GetProjectRunsForOrg(orgId int) ([]ProjectRun, error)

func (*Database) GetProjectRunsFromContext

func (db *Database) GetProjectRunsFromContext(c *gin.Context, orgIdKey string) ([]ProjectRun, bool)

func (*Database) GetProjectsFromContext

func (db *Database) GetProjectsFromContext(c *gin.Context, orgIdKey string) ([]Project, bool)

func (*Database) GetRepo

func (db *Database) GetRepo(orgIdKey any, repoName string) (*Repo, error)

GetRepo returns digger repo by organisationId and repo name (diggerhq-digger) it will return an empty object if record doesn't exist in database

func (*Database) GetRepoById

func (db *Database) GetRepoById(orgIdKey any, repoId any) (*Repo, error)

GetRepoById returns digger repo by organisationId and repo name (diggerhq-digger)

func (*Database) GetRepoCache

func (db *Database) GetRepoCache(orgId uint, repoFullName string) (*RepoCache, error)

func (*Database) GetReposFromContext

func (db *Database) GetReposFromContext(c *gin.Context, orgIdKey string) ([]Repo, bool)

func (*Database) GetToken

func (db *Database) GetToken(tenantId any) (*Token, error)

func (*Database) GithubRepoAdded

func (db *Database) GithubRepoAdded(installationId int64, appId int64, login string, accountId int64, repoFullName string) (*GithubAppInstallation, error)

GithubRepoAdded handles github drift that github repo has been added to the app installation

func (*Database) GithubRepoRemoved

func (db *Database) GithubRepoRemoved(installationId int64, appId int64, repoFullName string) (*GithubAppInstallation, error)

func (*Database) ListDiggerRunsForProject

func (db *Database) ListDiggerRunsForProject(projectName string, repoId uint) ([]DiggerRun, error)

func (*Database) MakeGithubAppInstallationLinkInactive

func (db *Database) MakeGithubAppInstallationLinkInactive(link *GithubAppInstallationLink) (*GithubAppInstallationLink, error)

func (*Database) UpdateBatchStatus

func (db *Database) UpdateBatchStatus(batch *DiggerBatch) error

func (*Database) UpdateDiggerBatch

func (db *Database) UpdateDiggerBatch(batch *DiggerBatch) error

func (*Database) UpdateDiggerJob

func (db *Database) UpdateDiggerJob(job *DiggerJob) error
func (db *Database) UpdateDiggerJobLink(diggerJobId string, repoFullName string, githubJobId int64) (*GithubDiggerJobLink, error)

func (*Database) UpdateDiggerJobSummary

func (db *Database) UpdateDiggerJobSummary(diggerJobId string, resourcesCreated uint, resourcesUpdated uint, resourcesDeleted uint) (*DiggerJob, error)

func (*Database) UpdateDiggerRun

func (db *Database) UpdateDiggerRun(diggerRun *DiggerRun) error

func (*Database) UpdateProject

func (db *Database) UpdateProject(project *Project) error

func (*Database) UpdateRepoDiggerConfig

func (db *Database) UpdateRepoDiggerConfig(orgId any, config configuration.DiggerConfigYaml, repo *Repo, isMainBranch bool) error

func (*Database) UpsertRepoCache

func (db *Database) UpsertRepoCache(orgId uint, repoFullName string, diggerYmlStr string, diggerConfig configuration.DiggerConfig) (*RepoCache, error)

type DiggerBatch

type DiggerBatch struct {
	ID                     uuid.UUID `gorm:"primary_key"`
	VCS                    DiggerVCSType
	PrNumber               int
	CommentId              *int64
	AiSummaryCommentId     string
	Status                 orchestrator_scheduler.DiggerBatchStatus
	BranchName             string
	DiggerConfig           string
	GithubInstallationId   int64
	GitlabProjectId        int
	RepoFullName           string
	RepoOwner              string
	RepoName               string
	BatchType              orchestrator_scheduler.DiggerCommand
	ReportTerraformOutputs bool
	// used for module source grouping comments
	SourceDetails []byte
}

func (*DiggerBatch) MapToJsonStruct

func (b *DiggerBatch) MapToJsonStruct() (orchestrator_scheduler.SerializedBatch, error)

type DiggerJob

type DiggerJob struct {
	gorm.Model
	DiggerJobID        string `gorm:"size:50,index:idx_digger_job_id"`
	Status             orchestrator_scheduler.DiggerJobStatus
	Batch              *DiggerBatch
	BatchID            *string `gorm:"index:idx_digger_job_id"`
	PRCommentUrl       string
	DiggerJobSummary   DiggerJobSummary
	DiggerJobSummaryID uint
	SerializedJobSpec  []byte
	TerraformOutput    string
	// represents a footprint of terraform plan json for similarity checks
	PlanFootprint   []byte
	WorkflowFile    string
	WorkflowRunUrl  *string
	StatusUpdatedAt time.Time
}

func (*DiggerJob) MapToJsonStruct

func (j *DiggerJob) MapToJsonStruct() (orchestrator_scheduler.SerializedJob, error)

type DiggerJobLinkStatus

type DiggerJobLinkStatus int8
const (
	DiggerJobLinkCreated   DiggerJobLinkStatus = 1
	DiggerJobLinkSucceeded DiggerJobLinkStatus = 2
)
type DiggerJobParentLink struct {
	gorm.Model
	DiggerJobId       string `gorm:"size:50,index:idx_digger_job_id"`
	ParentDiggerJobId string `gorm:"size:50,index:idx_parent_digger_job_id"`
}

type DiggerJobSummary

type DiggerJobSummary struct {
	gorm.Model
	ResourcesCreated uint
	ResourcesDeleted uint
	ResourcesUpdated uint
}

type DiggerLock

type DiggerLock struct {
	gorm.Model
	Resource       string `gorm:"index:idx_digger_locked_resource"`
	LockId         int
	Organisation   *Organisation
	OrganisationID uint
}

type DiggerRun

type DiggerRun struct {
	gorm.Model
	Triggertype          string // pr_merge, manual_invocation, push_to_trunk
	PrNumber             *int
	Status               DiggerRunStatus
	CommitId             string
	DiggerConfig         string
	GithubInstallationId int64
	RepoId               uint
	Repo                 *Repo
	ProjectName          string
	RunType              RunType
	PlanStage            DiggerRunStage
	PlanStageId          *uint
	ApplyStage           DiggerRunStage
	ApplyStageId         *uint
	IsApproved           bool
	ApprovalAuthor       string
	ApprovalDate         time.Time
}

func (*DiggerRun) MapToJsonStruct

func (r *DiggerRun) MapToJsonStruct() (interface{}, error)

type DiggerRunQueueItem

type DiggerRunQueueItem struct {
	gorm.Model
	DiggerRunId uint `gorm:"index:idx_digger_run_queue_run_id"`
	DiggerRun   DiggerRun
	ProjectId   uint
	Project     *Project
	// contains filtered or unexported fields
}

type DiggerRunStage

type DiggerRunStage struct {
	gorm.Model
	Batch   *DiggerBatch
	BatchID *string `gorm:"index:idx_digger_run_batch_id"`
}

func (DiggerRunStage) MapToJsonStruct

func (r DiggerRunStage) MapToJsonStruct() (*SerializedRunStage, error)

type DiggerRunStatus

type DiggerRunStatus string
const (
	RunQueued          DiggerRunStatus = "Queued"
	RunPendingPlan     DiggerRunStatus = "Pending Plan"
	RunPlanning        DiggerRunStatus = "Running Plan"
	RunPendingApproval DiggerRunStatus = "Pending Approval"
	RunApproved        DiggerRunStatus = "Approved"
	RunPendingApply    DiggerRunStatus = "Pending Apply"
	RunApplying        DiggerRunStatus = "Running Apply"
	RunSucceeded       DiggerRunStatus = "Succeeded"
	RunFailed          DiggerRunStatus = "Failed"
)

type DiggerVCSType

type DiggerVCSType string
const DiggerVCSGithub DiggerVCSType = "github"
const DiggerVCSGitlab DiggerVCSType = "gitlab"

type GithubAppConnection

type GithubAppConnection struct {
	gorm.Model
	GithubId                  int64 // app id
	ClientID                  string
	ClientSecretEncrypted     string
	WebhookSecretEncrypted    string
	PrivateKeyEncrypted       string
	PrivateKeyBase64Encrypted string
	Org                       string
	Name                      string
	GithubAppUrl              string
	OrganisationID            uint
	Organisation              Organisation
}

type GithubAppInstallStatus

type GithubAppInstallStatus int
const (
	GithubAppInstallActive  GithubAppInstallStatus = 1
	GithubAppInstallDeleted GithubAppInstallStatus = 2
)

type GithubAppInstallation

type GithubAppInstallation struct {
	gorm.Model
	GithubInstallationId int64
	GithubAppId          int64
	AccountId            int
	Login                string
	Repo                 string
	Status               GithubAppInstallStatus
}
type GithubAppInstallationLink struct {
	gorm.Model
	GithubInstallationId int64 `gorm:"index:idx_github_installation_org"`
	OrganisationId       uint  `gorm:"index:idx_github_installation_org"`
	Organisation         *Organisation
	Status               GithubAppInstallationLinkStatus
}

GithubAppInstallationLink links GitHub App installation Id to Digger's organisation Id

type GithubAppInstallationLinkStatus

type GithubAppInstallationLinkStatus int8
const (
	GithubAppInstallationLinkActive   GithubAppInstallationLinkStatus = 1
	GithubAppInstallationLinkInactive GithubAppInstallationLinkStatus = 2
)
type GithubDiggerJobLink struct {
	gorm.Model
	DiggerJobId         string `gorm:"size:50,index:idx_digger_job_id"`
	RepoFullName        string
	GithubJobId         int64 `gorm:"index:idx_github_job_id"`
	GithubWorkflowRunId int64
	Status              DiggerJobLinkStatus
}

GithubDiggerJobLink links GitHub Workflow Job id to Digger's Job Id

type JobArtefact

type JobArtefact struct {
	gorm.Model
	JobTokenID  uint
	JobToken    JobToken
	Filename    string
	Contents    []byte `gorm:"type:bytea"`
	Size        int64
	ContentType string
}

type JobToken

type JobToken struct {
	gorm.Model
	Value          string `gorm:"uniqueJobTokenIndex:idx_token"`
	Expiry         time.Time
	OrganisationID uint
	Organisation   Organisation
	Type           string // AccessTokenType starts with j:
}

These tokens will be pre

type Organisation

type Organisation struct {
	gorm.Model
	Name           string `gorm:"uniqueIndex:idx_organisation"`
	ExternalSource string `gorm:"uniqueIndex:idx_external_source"`
	ExternalId     string `gorm:"uniqueIndex:idx_external_source"`
}

type Policy

type Policy struct {
	gorm.Model
	Project        *Project
	ProjectID      *uint
	Policy         string
	Type           string
	CreatedBy      *User
	CreatedByID    *uint
	Organisation   *Organisation
	OrganisationID uint
	Repo           *Repo
	RepoID         *uint
}

type Project

type Project struct {
	gorm.Model
	Name              string `gorm:"uniqueIndex:idx_project"`
	OrganisationID    uint   `gorm:"uniqueIndex:idx_project"`
	Organisation      *Organisation
	RepoID            uint `gorm:"uniqueIndex:idx_project"`
	Repo              *Repo
	ConfigurationYaml string // TODO: probably needs to be deleted
	Status            ProjectStatus
	IsGenerated       bool
	IsInMainBranch    bool
}

func (*Project) MapToJsonStruct

func (p *Project) MapToJsonStruct() interface{}

type ProjectRun

type ProjectRun struct {
	gorm.Model
	ProjectID     uint
	Project       *Project
	StartedAt     int64
	EndedAt       int64
	Status        string
	Command       string
	Output        string
	ActorUsername string
}

func (*ProjectRun) MapToJsonStruct

func (p *ProjectRun) MapToJsonStruct() interface{}

type ProjectStatus

type ProjectStatus int
const (
	ProjectActive   ProjectStatus = 1
	ProjectInactive ProjectStatus = 2
)

type Repo

type Repo struct {
	gorm.Model
	Name             string `gorm:"uniqueIndex:idx_org_repo"`
	RepoFullName     string
	RepoOrganisation string
	RepoName         string
	RepoUrl          string
	OrganisationID   uint `gorm:"uniqueIndex:idx_org_repo"`
	Organisation     *Organisation
	DiggerConfig     string
}

type RepoCache

type RepoCache struct {
	gorm.Model
	OrgId        uint
	RepoFullName string
	DiggerYmlStr string
	DiggerConfig []byte `gorm:"type:bytea"`
}

storing repo cache such as digger.yml configuration

type RunType

type RunType string
const (
	PlanAndApply RunType = "Plan and Apply"
	PlanOnly     RunType = "Plan Only"
)

type SerializedRunStage

type SerializedRunStage struct {
	//DiggerRunId           uint                                   `json:"digger_run_id"`
	DiggerJobId           string                                 `json:"digger_job_id"`
	Status                orchestrator_scheduler.DiggerJobStatus `json:"status"`
	ProjectName           string                                 `json:"project_name"`
	WorkflowRunUrl        *string                                `json:"workflow_run_url"`
	ResourcesCreated      uint                                   `json:"resources_created"`
	ResourcesDeleted      uint                                   `json:"resources_deleted"`
	ResourcesUpdated      uint                                   `json:"resources_updated"`
	LastActivityTimeStamp string                                 `json:"last_activity_timestamp"`
}

type Token

type Token struct {
	gorm.Model
	Value          string `gorm:"uniqueIndex:idx_token"`
	OrganisationID uint
	Organisation   *Organisation
	Type           string
}

type User

type User struct {
	gorm.Model
	Username string `gorm:"uniqueIndex:idx_user"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL