-
Notifications
You must be signed in to change notification settings - Fork 563
/
Copy pathscheduler.go
142 lines (129 loc) · 4.2 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package models
import (
"encoding/json"
"fmt"
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
"github.com/google/uuid"
"gorm.io/gorm"
"log"
"time"
)
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 DiggerVCSType string
const DiggerVCSGithub DiggerVCSType = "github"
const DiggerVCSGitlab DiggerVCSType = "gitlab"
type DiggerBatch struct {
ID uuid.UUID `gorm:"primary_key"`
VCS DiggerVCSType
PrNumber int
CommentId *int64
AiSummaryCommentId string
PlaceholderCommentIdForReport *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
}
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
}
type DiggerJobSummary struct {
gorm.Model
ResourcesCreated uint
ResourcesDeleted uint
ResourcesUpdated uint
}
// These tokens will be pre
type JobToken struct {
gorm.Model
Value string `gorm:"uniqueJobTokenIndex:idx_token"`
Expiry time.Time
OrganisationID uint
Organisation Organisation
Type string // AccessTokenType starts with j:
}
type DiggerJobLinkStatus int8
const (
DiggerJobLinkCreated DiggerJobLinkStatus = 1
DiggerJobLinkSucceeded DiggerJobLinkStatus = 2
)
// GithubDiggerJobLink links GitHub Workflow Job id to Digger's Job Id
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
}
func (j *DiggerJob) MapToJsonStruct() (orchestrator_scheduler.SerializedJob, error) {
var job orchestrator_scheduler.JobJson
err := json.Unmarshal(j.SerializedJobSpec, &job)
if err != nil {
log.Printf("Failed to convert unmarshall Serialized job, %v", err)
}
return orchestrator_scheduler.SerializedJob{
DiggerJobId: j.DiggerJobID,
Status: j.Status,
JobString: j.SerializedJobSpec,
PlanFootprint: j.PlanFootprint,
ProjectName: job.ProjectName,
WorkflowRunUrl: j.WorkflowRunUrl,
PRCommentUrl: j.PRCommentUrl,
ResourcesCreated: j.DiggerJobSummary.ResourcesCreated,
ResourcesUpdated: j.DiggerJobSummary.ResourcesUpdated,
ResourcesDeleted: j.DiggerJobSummary.ResourcesDeleted,
}, nil
}
func (b *DiggerBatch) MapToJsonStruct() (orchestrator_scheduler.SerializedBatch, error) {
res := orchestrator_scheduler.SerializedBatch{
ID: b.ID.String(),
PrNumber: b.PrNumber,
Status: b.Status,
BranchName: b.BranchName,
RepoFullName: b.RepoFullName,
RepoOwner: b.RepoOwner,
RepoName: b.RepoName,
BatchType: b.BatchType,
}
serializedJobs := make([]orchestrator_scheduler.SerializedJob, 0)
jobs, err := DB.GetDiggerJobsForBatch(b.ID)
if err != nil {
return res, fmt.Errorf("could not unmarshall digger batch: %v", err)
}
for _, job := range jobs {
jobJson, err := job.MapToJsonStruct()
if err != nil {
return res, fmt.Errorf("error mapping job to struct (ID: %v); %v", job.ID, err)
}
serializedJobs = append(serializedJobs, jobJson)
}
res.Jobs = serializedJobs
return res, nil
}