-
Notifications
You must be signed in to change notification settings - Fork 561
/
Copy pathscheduler.go
172 lines (150 loc) · 4.89 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package models
import (
"encoding/json"
"fmt"
"log/slog"
"time"
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
"github.com/google/uuid"
"gorm.io/gorm"
)
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"
const DiggerVCSBitbucket DiggerVCSType = "bitbucket"
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
VCSConnectionId *uint ``
VCSConnection VCSConnection
}
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 {
slog.Error("Failed to unmarshal serialized job spec", "jobId", j.DiggerJobID, "error", err)
return orchestrator_scheduler.SerializedJob{}, err
}
serialized := 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,
}
slog.Debug("Mapped job to JSON struct",
"jobId", j.DiggerJobID,
"status", j.Status,
"projectName", job.ProjectName)
return serialized, 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,
}
slog.Debug("Mapping batch to JSON struct",
"batchId", b.ID.String(),
"repoFullName", b.RepoFullName,
"prNumber", b.PrNumber)
serializedJobs := make([]orchestrator_scheduler.SerializedJob, 0)
jobs, err := DB.GetDiggerJobsForBatch(b.ID)
if err != nil {
slog.Error("Could not get jobs for batch", "batchId", b.ID.String(), "error", err)
return res, fmt.Errorf("could not unmarshall digger batch: %v", err)
}
for _, job := range jobs {
jobJson, err := job.MapToJsonStruct()
if err != nil {
slog.Error("Error mapping job to struct",
"jobId", job.ID,
"diggerJobId", job.DiggerJobID,
"batchId", b.ID.String(),
"error", err)
return res, fmt.Errorf("error mapping job to struct (ID: %v); %v", job.ID, err)
}
serializedJobs = append(serializedJobs, jobJson)
}
res.Jobs = serializedJobs
slog.Debug("Successfully mapped batch to JSON struct",
"batchId", b.ID.String(),
"jobCount", len(serializedJobs))
return res, nil
}