Skip to content

Commit 31c12fd

Browse files
authored
feat/drift readiness (#2023)
* initial tmp refactor of models * refactor drift service to reuse same gh app * fix tests and address review items * fix type * fix tests * fix column name * fix type * fix bugs * add workflow for drift backend deploy * fix issues
1 parent 1bfb31d commit 31c12fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+407
-5390
lines changed

.github/workflows/drift-deploy.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# See https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/
2+
3+
name: Deploy drift backend
4+
on:
5+
push:
6+
branches:
7+
- develop
8+
jobs:
9+
deploy:
10+
name: Deploy drift backend
11+
runs-on: ubuntu-latest
12+
concurrency: drift-deploy # optional: ensure only one action runs at a time
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: superfly/flyctl-actions/setup-flyctl@master
16+
- run: flyctl deploy --remote-only --config fly-drift.toml
17+
18+
env:
19+
FLY_API_TOKEN: ${{ secrets.FLYIO_DRIFT_TOKEN }}

backend/controllers/github.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func (d DiggerController) GithubSetupExchangeCode(c *gin.Context) {
289289
})
290290
}
291291

292-
func createOrGetDiggerRepoForGithubRepo(ghRepoFullName string, ghRepoOrganisation string, ghRepoName string, ghRepoUrl string, installationId int64) (*models.Repo, *models.Organisation, error) {
292+
func createOrGetDiggerRepoForGithubRepo(ghRepoFullName string, ghRepoOrganisation string, ghRepoName string, ghRepoUrl string, installationId int64, appId int64, defaultBranch string, cloneUrl string) (*models.Repo, *models.Organisation, error) {
293293
slog.Info("Creating or getting Digger repo for GitHub repo",
294294
slog.Group("githubRepo",
295295
slog.String("fullName", ghRepoFullName),
@@ -331,6 +331,10 @@ func createOrGetDiggerRepoForGithubRepo(ghRepoFullName string, ghRepoOrganisatio
331331
if r.RowsAffected > 0 {
332332
slog.Info("Digger repo already exists, restoring if deleted", "diggerRepoName", diggerRepoName, "repoId", existingRepo.ID)
333333
existingRepo.DeletedAt = gorm.DeletedAt{}
334+
existingRepo.GithubAppId = appId
335+
existingRepo.GithubAppInstallationId = installationId
336+
existingRepo.CloneUrl = cloneUrl
337+
existingRepo.DefaultBranch = defaultBranch
334338
models.DB.GormDB.Save(&existingRepo)
335339
return &existingRepo, org, nil
336340
}
@@ -339,7 +343,7 @@ func createOrGetDiggerRepoForGithubRepo(ghRepoFullName string, ghRepoOrganisatio
339343
repo, err := models.DB.CreateRepo(diggerRepoName, ghRepoFullName, ghRepoOrganisation, ghRepoName, ghRepoUrl, org, `
340344
generate_projects:
341345
include: "."
342-
`)
346+
`, installationId, appId, defaultBranch, cloneUrl)
343347
if err != nil {
344348
slog.Error("Error creating Digger repo", "diggerRepoName", diggerRepoName, "error", err)
345349
return nil, nil, err
@@ -2548,7 +2552,10 @@ func (d DiggerController) GithubAppCallbackPage(c *gin.Context) {
25482552
return
25492553
}
25502554

2551-
_, _, err = createOrGetDiggerRepoForGithubRepo(repoFullName, repoOwner, repoName, repoUrl, installationId64)
2555+
cloneUrl := *repo.CloneURL
2556+
defaultBranch := *repo.DefaultBranch
2557+
2558+
_, _, err = createOrGetDiggerRepoForGithubRepo(repoFullName, repoOwner, repoName, repoUrl, installationId64, *installation.AppID, defaultBranch, cloneUrl)
25522559
if err != nil {
25532560
slog.Error("Error creating or getting Digger repo",
25542561
"repoFullName", repoFullName,

backend/controllers/github_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) {
607607

608608
// create digger repo
609609
repoName := "test repo"
610-
repo, err := database.CreateRepo(repoName, "", "", "", "", org, "")
610+
repo, err := database.CreateRepo(repoName, "", "", "", "", org, "", 0, 0, "", "")
611611
if err != nil {
612612
panic(err)
613613
}
@@ -652,7 +652,7 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) {
652652
`
653653

654654
diggerRepoName := strings.Replace(repoFullName, "/", "-", 1)
655-
_, err = database.CreateRepo(diggerRepoName, "", "", "", "", org, diggerConfig)
655+
_, err = database.CreateRepo(diggerRepoName, "", "", "", "", org, diggerConfig, 0, 0, "", "")
656656
if err != nil {
657657
panic(err)
658658
}

backend/migrations/20250716043004.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Modify "digger_jobs" table
2+
ALTER TABLE "public"."digger_jobs" ADD COLUMN "run_name" text NULL, ADD COLUMN "project_name" text NULL, ADD COLUMN "serialized_reporter_spec" bytea NULL, ADD COLUMN "serialized_comment_updater_spec" bytea NULL, ADD COLUMN "serialized_lock_spec" bytea NULL, ADD COLUMN "serialized_backend_spec" bytea NULL, ADD COLUMN "serialized_vcs_spec" bytea NULL, ADD COLUMN "serialized_policy_spec" bytea NULL, ADD COLUMN "serialized_variables_spec" bytea NULL;
3+
-- Modify "organisations" table
4+
ALTER TABLE "public"."organisations" ADD COLUMN "slack_notification_url" text NULL;
5+
-- Modify "projects" table
6+
ALTER TABLE "public"."projects" ADD COLUMN "drift_to_create" bigint NULL, ADD COLUMN "drift_to_update" bigint NULL, ADD COLUMN "drift_to_delete" bigint NULL;
7+
-- Modify "repos" table
8+
ALTER TABLE "public"."repos" ADD COLUMN "github_installation_id" text NULL, ADD COLUMN "github_app_id" bigint NULL, ADD COLUMN "default_branch" text NULL, ADD COLUMN "clone_url" text NULL;

backend/migrations/20250716222603.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Modify "repos" table
2+
ALTER TABLE "public"."repos" DROP COLUMN "github_installation_id", ADD COLUMN "github_app_installation_id" bigint NULL;

backend/migrations/20250717032021.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Modify "organisations" table
2+
ALTER TABLE "public"."organisations" DROP COLUMN "slack_notification_url";

backend/migrations/atlas.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:lTHG1XO1wASaCOl6gEp635MxFxAzxBuzxTe8pOPqKQc=
1+
h1:pk1704W4CHtf2K+qFKIkjTh8pN2R8Lor32ecJPtQgoY=
22
20231227132525.sql h1:43xn7XC0GoJsCnXIMczGXWis9d504FAWi4F1gViTIcw=
33
20240115170600.sql h1:IW8fF/8vc40+eWqP/xDK+R4K9jHJ9QBSGO6rN9LtfSA=
44
20240116123649.sql h1:R1JlUIgxxF6Cyob9HdtMqiKmx/BfnsctTl5rvOqssQw=
@@ -57,3 +57,6 @@ h1:lTHG1XO1wASaCOl6gEp635MxFxAzxBuzxTe8pOPqKQc=
5757
20250711030248.sql h1:oKzrMdfE75UyMSx++OMrXOAi1AOLN6kuv8iBLq8+srs=
5858
20250711030323.sql h1:vN9g0H99CItCw9aG6tRo73py5qQ4iD6qew2Y66WcoBk=
5959
20250712014920.sql h1:tu6MmFyXwCEvXjhjMYze79Vs3+OPqv6te5IiJsjnd5k=
60+
20250716043004.sql h1:WFz35jhsJ1pswpt5mmewW7L8LP5taRcNMf+pqUi3Yxs=
61+
20250716222603.sql h1:a66teR/6BQwNwhJ/zgNA/NjUVTz5DcoQl7cDZvzVS8c=
62+
20250717032021.sql h1:HaIhNsz3C+c87CDmFjgFlc9zGqoE5BU4m0dofDpeDYk=

backend/models/orgs.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@ type Organisation struct {
3030

3131
type Repo struct {
3232
gorm.Model
33-
Name string `gorm:"uniqueIndex:idx_org_repo"`
34-
RepoFullName string
35-
RepoOrganisation string
36-
RepoName string
37-
RepoUrl string
38-
VCS DiggerVCSType `gorm:"default:'github'"`
39-
OrganisationID uint `gorm:"uniqueIndex:idx_org_repo"`
40-
Organisation *Organisation
41-
DiggerConfig string
33+
Name string `gorm:"uniqueIndex:idx_org_repo"`
34+
RepoFullName string
35+
RepoOrganisation string
36+
RepoName string
37+
RepoUrl string
38+
VCS DiggerVCSType `gorm:"default:'github'"`
39+
OrganisationID uint `gorm:"uniqueIndex:idx_org_repo"`
40+
Organisation *Organisation
41+
DiggerConfig string
42+
GithubAppInstallationId int64
43+
GithubAppId int64
44+
DefaultBranch string
45+
CloneUrl string
4246
}
4347

4448
type ProjectRun struct {
@@ -103,6 +107,9 @@ type Project struct {
103107
DriftStatus DriftStatus `gorm:"default:'no drift'"`
104108
LatestDriftCheck time.Time
105109
DriftTerraformPlan string
110+
DriftToCreate uint
111+
DriftToUpdate uint
112+
DriftToDelete uint
106113
Status ProjectStatus
107114
IsGenerated bool
108115
IsInMainBranch bool

backend/models/scheduler.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,25 @@ type DiggerBatch struct {
4949

5050
type DiggerJob struct {
5151
gorm.Model
52-
DiggerJobID string `gorm:"size:50,index:idx_digger_job_id"`
53-
Status orchestrator_scheduler.DiggerJobStatus
54-
Batch *DiggerBatch
55-
BatchID *string `gorm:"index:idx_digger_job_id"`
56-
PRCommentUrl string
57-
PRCommentId *int64
58-
DiggerJobSummary DiggerJobSummary
59-
DiggerJobSummaryID uint
60-
SerializedJobSpec []byte
61-
TerraformOutput string
52+
DiggerJobID string `gorm:"size:50,index:idx_digger_job_id"`
53+
Status orchestrator_scheduler.DiggerJobStatus
54+
RunName string
55+
ProjectName string
56+
Batch *DiggerBatch
57+
BatchID *string `gorm:"index:idx_digger_job_id"`
58+
PRCommentUrl string
59+
PRCommentId *int64
60+
DiggerJobSummary DiggerJobSummary
61+
DiggerJobSummaryID uint
62+
SerializedJobSpec []byte
63+
SerializedReporterSpec []byte
64+
SerializedCommentUpdaterSpec []byte
65+
SerializedLockSpec []byte
66+
SerializedBackendSpec []byte
67+
SerializedVcsSpec []byte
68+
SerializedPolicySpec []byte
69+
SerializedVariablesSpec []byte
70+
TerraformOutput string
6271
// represents a footprint of terraform plan json for similarity checks
6372
PlanFootprint []byte
6473
WorkflowFile string

backend/models/scheduler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func setupSuiteScheduler(tb testing.TB) (func(tb testing.TB), *Database) {
4848
}
4949

5050
repoName := "test repo"
51-
repo, err := database.CreateRepo(repoName, "", "", "", "", org, "")
51+
repo, err := database.CreateRepo(repoName, "", "", "", "", org, "", 0, 0, "", "")
5252
if err != nil {
5353
panic(err)
5454
}

0 commit comments

Comments
 (0)