Skip to content

Commit

Permalink
automerge pr on batch completion
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed Dec 22, 2023
1 parent 0ae6ce9 commit 572e2ab
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 80 deletions.
57 changes: 9 additions & 48 deletions backend/controllers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func handlePushEvent(gh utils.GithubClientProvider, payload *github.PushEvent) e
return fmt.Errorf("Repo not found: Org: %v | repo: %v", orgId, diggerRepoName)
}

_, token, err := getGithubService(gh, installationId, repoFullName, repoOwner, repoName)
_, token, err := utils.GetGithubService(gh, installationId, repoFullName, repoOwner, repoName)
if err != nil {
log.Printf("Error getting github service: %v", err)
return fmt.Errorf("error getting github service")
Expand Down Expand Up @@ -456,21 +456,15 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
impactedJobsMap[j.ProjectName] = j
}

batchId, _, err := utils.ConvertJobsToDiggerJobs(impactedJobsMap, impactedProjectsMap, projectsGraph, *branch, prNumber, repoFullName)
if err != nil {
log.Printf("ConvertJobsToDiggerJobs error: %v", err)
return fmt.Errorf("error converting jobs")
}

repo, err := GetRepoByInstllationId(installationId, repoOwner, repoName)
if err != nil {
log.Printf("GetRepoByInstallationId error: %v", err)
return fmt.Errorf("error converting jobs, GetRepoByInstallationId error: %v", err)
}
_, err = models.DB.CreateDiggerBatchRepoLink(*batchId, repo.ID)
batchId, _, err := utils.ConvertJobsToDiggerJobs(impactedJobsMap, impactedProjectsMap, projectsGraph, installationId, *branch, prNumber, repoOwner, repoName, repoFullName, repo.DiggerConfig)
if err != nil {
log.Printf("CreateDiggerBatchRepoLink error: %v", err)
return fmt.Errorf("error converting jobs, GetRepoByInstallationId error: %v", err)
log.Printf("ConvertJobsToDiggerJobs error: %v", err)
return fmt.Errorf("error converting jobs")
}

err = TriggerDiggerJobs(ghService.Client, repoOwner, repoName, batchId, prNumber, ghService)
Expand All @@ -482,35 +476,8 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
return nil
}

func getGithubService(gh utils.GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string) (*dg_github.GithubService, *string, error) {
installation, err := models.DB.GetGithubAppInstallationByIdAndRepo(installationId, repoFullName)
if err != nil {
log.Printf("Error getting installation: %v", err)
return nil, nil, fmt.Errorf("Error getting installation: %v", err)
}

_, err = models.DB.GetGithubApp(installation.GithubAppId)
if err != nil {
log.Printf("Error getting app: %v", err)
return nil, nil, fmt.Errorf("Error getting app: %v", err)
}

ghClient, token, err := gh.Get(installation.GithubAppId, installation.GithubInstallationId)
if err != nil {
log.Printf("Error creating github app client: %v", err)
return nil, nil, fmt.Errorf("Error creating github app client: %v", err)
}

ghService := dg_github.GithubService{
Client: ghClient,
RepoName: repoName,
Owner: repoOwner,
}

return &ghService, token, nil
}
func getDiggerConfig(gh utils.GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string, cloneUrl string, prNumber int) (*dg_github.GithubService, *dg_configuration.DiggerConfig, graph.Graph[string, dg_configuration.Project], *string, error) {
ghService, token, err := getGithubService(gh, installationId, repoFullName, repoOwner, repoName)
ghService, token, err := utils.GetGithubService(gh, installationId, repoFullName, repoOwner, repoName)
if err != nil {
log.Printf("Error getting github service: %v", err)
return nil, nil, nil, nil, fmt.Errorf("error getting github service")
Expand Down Expand Up @@ -617,7 +584,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
log.Printf("GitHub IssueComment event processed successfully\n")

if err != nil {
log.Printf("getGithubService error: %v", err)
log.Printf("GetGithubService error: %v", err)
return fmt.Errorf("error getting github prservice")
}

Expand All @@ -644,21 +611,15 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
impactedProjectsJobMap[j.ProjectName] = j
}

batchId, _, err := utils.ConvertJobsToDiggerJobs(impactedProjectsJobMap, impactedProjectsMap, projectsGraph, *branch, issueNumber, repoFullName)
if err != nil {
log.Printf("ConvertJobsToDiggerJobs error: %v", err)
return fmt.Errorf("error convertingjobs")
}

repo, err := GetRepoByInstllationId(installationId, repoOwner, repoName)
if err != nil {
log.Printf("GetRepoByInstallationId error: %v", err)
return fmt.Errorf("error converting jobs, GetRepoByInstallationId error: %v", err)
}
_, err = models.DB.CreateDiggerBatchRepoLink(*batchId, repo.ID)
batchId, _, err := utils.ConvertJobsToDiggerJobs(impactedProjectsJobMap, impactedProjectsMap, projectsGraph, installationId, *branch, issueNumber, repoOwner, repoName, repoFullName, repo.DiggerConfig)
if err != nil {
log.Printf("CreateDiggerBatchRepoLink error: %v", err)
return fmt.Errorf("error converting jobs, CreateDiggerBatchRepoLink error: %v", err)
log.Printf("ConvertJobsToDiggerJobs error: %v", err)
return fmt.Errorf("error convertingjobs")
}

err = TriggerDiggerJobs(ghService.Client, repoOwner, repoName, batchId, issueNumber, ghService)
Expand Down
22 changes: 21 additions & 1 deletion backend/controllers/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/services"
"github.com/diggerhq/digger/backend/utils"
"github.com/diggerhq/digger/libs/digger_config"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"log"
Expand Down Expand Up @@ -357,7 +358,26 @@ func SetJobStatusForProject(c *gin.Context) {
}

if batch.Status == models.BatchJobSucceeded {

diggerYmlString := batch.DiggerConfig
diggerConfig, _, _, err := digger_config.LoadDiggerConfigFromString(diggerYmlString, "./")
if err != nil {
log.Printf("Error loading digger config from batch: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error loading digger config from batch"})
}
if diggerConfig.AutoMerge == true {
ghService, _, err := utils.GetGithubService(
&utils.DiggerGithubRealClientProvider{},
batch.GithubInstallationId,
batch.RepoFullName,
batch.RepoOwner,
batch.RepoName,
)
if err != nil {
log.Printf("Error getting github service: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error getting github service"})
}
ghService.MergePullRequest(batch.PrNumber)
}
}
}

Expand Down
24 changes: 10 additions & 14 deletions backend/models/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,25 @@ type DiggerJobParentLink struct {
ParentDiggerJobId string `gorm:"size:50,index:idx_parent_digger_job_id"`
}

type DiggerBatchRepoLink struct {
gorm.Model
Batch *DiggerBatch
BatchId uuid.UUID `gorm:"index:idx_batch_id"`
Repo *Repo
RepoID uint
}

type DiggerBatch struct {
gorm.Model
ID uuid.UUID `gorm:"primary_key"`
PrNumber int
Status DiggerBatchStatus
BranchName string
// TODO: add diggerYml stored here as a tring
ID uuid.UUID `gorm:"primary_key"`
PrNumber int
Status DiggerBatchStatus
BranchName string
DiggerConfig string
GithubInstallationId int64
RepoFullName string
RepoOwner string
RepoName string
}

type DiggerJob struct {
gorm.Model
DiggerJobId string `gorm:"size:50,index:idx_digger_job_id"`
Status DiggerJobStatus
Batch *DiggerBatch
BatchId uuid.UUID `gorm:"index:idx_batch_id"`
BatchId *string `gorm:"index:idx_batch_id"`
SerializedJob []byte
StatusUpdatedAt time.Time
}
Expand Down
29 changes: 14 additions & 15 deletions backend/models/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,17 @@ func (db *Database) GetOrganisationById(orgId any) (*Organisation, error) {
return &org, nil
}

func (db *Database) CreateDiggerBatch(PRNumber int, branchName string) (*DiggerBatch, error) {
batch := &DiggerBatch{PrNumber: PRNumber, Status: BatchJobCreated, BranchName: branchName}
func (db *Database) CreateDiggerBatch(githubInstallationId int64, repoOwner string, repoName string, repoFullname string, PRNumber int, branchName string, diggerConfig string) (*DiggerBatch, error) {
batch := &DiggerBatch{
GithubInstallationId: githubInstallationId,
RepoOwner: repoOwner,
RepoName: repoName,
RepoFullName: repoFullname,
PrNumber: PRNumber,
Status: BatchJobCreated,
BranchName: branchName,
DiggerConfig: diggerConfig,
}
result := db.GormDB.Save(batch)
if result.Error != nil {
return nil, result.Error
Expand All @@ -568,17 +577,6 @@ func (db *Database) CreateDiggerBatch(PRNumber int, branchName string) (*DiggerB
return batch, nil
}

func (db *Database) CreateDiggerBatchRepoLink(batchId uuid.UUID, repoId uint) (*DiggerBatchRepoLink, error) {
batchRepoLink := &DiggerBatchRepoLink{BatchId: batchId, RepoID: repoId}
result := db.GormDB.Save(batchRepoLink)
if result.Error != nil {
return nil, result.Error
}

log.Printf("batchRepoLink %v, (id: %v) has been created successfully\n", batchRepoLink.ID)
return batchRepoLink, nil
}

func (db *Database) UpdateBatchStatus(batch *DiggerBatch) error {
if batch.Status == BatchJobInvalidated || batch.Status == BatchJobFailed || batch.Status == BatchJobSucceeded {
return nil
Expand All @@ -605,13 +603,14 @@ func (db *Database) UpdateBatchStatus(batch *DiggerBatch) error {
return nil

}
func (db *Database) CreateDiggerJob(batch uuid.UUID, serializedJob []byte) (*DiggerJob, error) {
func (db *Database) CreateDiggerJob(batchId uuid.UUID, serializedJob []byte) (*DiggerJob, error) {
if serializedJob == nil || len(serializedJob) == 0 {
return nil, fmt.Errorf("serializedJob can't be empty")
}
jobId := uniuri.New()
batchIdStr := batchId.String()
job := &DiggerJob{DiggerJobId: jobId, Status: DiggerJobCreated,
BatchId: batch, SerializedJob: serializedJob}
BatchId: &batchIdStr, SerializedJob: serializedJob}
result := db.GormDB.Save(job)
if result.Error != nil {
return nil, result.Error
Expand Down
30 changes: 30 additions & 0 deletions backend/utils/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/diggerhq/digger/backend/models"
github2 "github.com/diggerhq/digger/libs/orchestrator/github"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
Expand Down Expand Up @@ -85,3 +87,31 @@ func (gh *DiggerGithubClientMockProvider) Get(githubAppId int64, installationId
token := "token"
return ghClient, &token, nil
}

func GetGithubService(gh GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string) (*github2.GithubService, *string, error) {
installation, err := models.DB.GetGithubAppInstallationByIdAndRepo(installationId, repoFullName)
if err != nil {
log.Printf("Error getting installation: %v", err)
return nil, nil, fmt.Errorf("Error getting installation: %v", err)
}

_, err = models.DB.GetGithubApp(installation.GithubAppId)
if err != nil {
log.Printf("Error getting app: %v", err)
return nil, nil, fmt.Errorf("Error getting app: %v", err)
}

ghClient, token, err := gh.Get(installation.GithubAppId, installation.GithubInstallationId)
if err != nil {
log.Printf("Error creating github app client: %v", err)
return nil, nil, fmt.Errorf("Error creating github app client: %v", err)
}

ghService := github2.GithubService{
Client: ghClient,
RepoName: repoName,
Owner: repoOwner,
}

return &ghService, token, nil
}
4 changes: 2 additions & 2 deletions backend/utils/graphs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// ConvertJobsToDiggerJobs jobs is map with project name as a key and a Job as a value
func ConvertJobsToDiggerJobs(jobsMap map[string]orchestrator.Job, projectMap map[string]configuration.Project, projectsGraph graph.Graph[string, configuration.Project], branch string, prNumber int, repoFullName string) (*uuid.UUID, map[string]*models.DiggerJob, error) {
func ConvertJobsToDiggerJobs(jobsMap map[string]orchestrator.Job, projectMap map[string]configuration.Project, projectsGraph graph.Graph[string, configuration.Project], githubInstallationId int64, branch string, prNumber int, repoOwner string, repoName string, repoFullName string, diggerConfig string) (*uuid.UUID, map[string]*models.DiggerJob, error) {
result := make(map[string]*models.DiggerJob)

log.Printf("Number of Jobs: %v\n", len(jobsMap))
Expand All @@ -28,7 +28,7 @@ func ConvertJobsToDiggerJobs(jobsMap map[string]orchestrator.Job, projectMap map

log.Printf("marshalledJobsMap: %v\n", marshalledJobsMap)

batch, err := models.DB.CreateDiggerBatch(prNumber, branch)
batch, err := models.DB.CreateDiggerBatch(githubInstallationId, repoOwner, repoName, repoFullName, prNumber, branch, diggerConfig)
if err != nil {
return nil, nil, fmt.Errorf("failed to create batch: %v", err)
}
Expand Down

0 comments on commit 572e2ab

Please sign in to comment.