Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f4971de
check for CI_MERGE_REQUEST_IID parameter
veziak May 18, 2023
13a7789
update gitlab events
veziak May 19, 2023
cb62004
add more debugging
veziak May 19, 2023
583f089
add default workflow
veziak May 19, 2023
bee5bd1
add more logs
veziak May 22, 2023
83a34fd
add more logs
veziak May 22, 2023
71dfaed
add more logs
veziak May 22, 2023
00b8609
fix switch/case
veziak May 22, 2023
08149e0
add logs
veziak May 22, 2023
4e9f6e1
create gitlab discussion if it doesn't exist
veziak May 22, 2023
b38332d
remove some logs used for debugging
veziak May 22, 2023
52eea86
resolve merge request threads automatically
veziak May 24, 2023
ba6e450
resolve merge request threads automatically
veziak May 24, 2023
8c7d325
add more logs
veziak May 24, 2023
5ec93ee
add more logs
veziak May 24, 2023
b89a662
add more logs
veziak May 25, 2023
e275483
add more logs
veziak May 25, 2023
2140793
add more logs
veziak May 25, 2023
a5613f6
read isMeargeable from env vars
veziak May 25, 2023
13bebeb
merge MR automatically
veziak May 25, 2023
af0bf73
merge MR automatically
veziak May 25, 2023
e352c7d
merge MR automatically
veziak May 26, 2023
d2cea31
merge MR automatically
veziak May 26, 2023
26c18dc
Merge pull request #300 from diggerhq/develop
veziak May 26, 2023
446b0a9
add logs for debugging
veziak May 26, 2023
5a07473
add logs for debugging
veziak May 26, 2023
489e493
add logs for debugging
veziak May 26, 2023
60026c4
check for CI_OPEN_MERGE_REQUESTS parameter
veziak May 30, 2023
7907a11
fix log messages
veziak May 30, 2023
1ca42f3
check for merge_request_merge event
veziak May 30, 2023
fce1cf2
fix PublishComment in GitLab
veziak May 31, 2023
70c3274
Merge branch 'gitlab-fixes' into fix-merge-conflicts
veziak May 31, 2023
69c5a75
Merge pull request #305 from diggerhq/fix-merge-conflicts
veziak Jun 1, 2023
08cdc28
remove commented out code and logs used for debugging
veziak Jun 1, 2023
b978b27
Merge remote-tracking branch 'origin/gitlab-fixes' into gitlab-fixes
veziak Jun 1, 2023
9ad4fb3
remove commented out code and logs used for debugging
veziak Jun 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions cmd/digger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func gitHubCI(lock utils.Lock) {
eventName := parsedGhContext.EventName
splitRepositoryName := strings.Split(parsedGhContext.Repository, "/")
repoOwner, repositoryName := splitRepositoryName[0], splitRepositoryName[1]
githubPrService := dg_github.NewGithubPullRequestService(ghToken, repositoryName, repoOwner)
githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner)

impactedProjects, requestedProject, prNumber, err := digger.ProcessGitHubEvent(ghEvent, diggerConfig, githubPrService)
if err != nil {
Expand Down Expand Up @@ -106,7 +106,9 @@ func gitHubCI(lock utils.Lock) {

func gitLabCI(lock utils.Lock) {
println("Using GitLab.")

projectNamespace := os.Getenv("CI_PROJECT_NAMESPACE")
projectName := os.Getenv("CI_PROJECT_NAME")
gitlabToken := os.Getenv("GITLAB_TOKEN")
if gitlabToken == "" {
fmt.Println("GITLAB_TOKEN is empty")
Expand All @@ -131,6 +133,12 @@ func gitLabCI(lock utils.Lock) {
os.Exit(4)
}

// it's ok to not have merge request info if it has been merged
if (gitLabContext.MergeRequestIId == nil || len(gitLabContext.OpenMergeRequests) == 0) && gitLabContext.EventType != "merge_request_merge" {
fmt.Println("No merge request found.")
os.Exit(0)
}

gitlabService, err := gitlab.NewGitLabService(gitlabToken, gitLabContext)
if err != nil {
fmt.Printf("failed to initialise GitLab service, %v", err)
Expand All @@ -153,21 +161,33 @@ func gitLabCI(lock utils.Lock) {
}
println("GitLab event converted to commands successfully")

println("Digger commands to be executed:")
for _, v := range commandsToRunPerProject {
fmt.Printf("command: %s, project: %s\n", strings.Join(v.Commands, ", "), v.ProjectName)
}

//planStorage := newPlanStorage(ghToken, repoOwner, repositoryName, prNumber)
var planStorage utils.PlanStorage
planStorage := newPlanStorage(gitlabToken, projectNamespace, projectName, *gitLabContext.MergeRequestIId)

result, err := gitlab.RunCommandsPerProject(commandsToRunPerProject, *gitLabContext, diggerConfig, gitlabService, lock, planStorage, currentDir)
allAppliesSuccess, err := gitlab.RunCommandsPerProject(commandsToRunPerProject, *gitLabContext, diggerConfig, gitlabService, lock, planStorage, currentDir)
if err != nil {
fmt.Printf("failed to execute command, %v", err)
os.Exit(8)
}
print(result)

if diggerConfig.AutoMerge && allAppliesSuccess {
digger.MergePullRequest(gitlabService, *gitLabContext.MergeRequestIId)
println("Merge request changes has been applied successfully")
}

println("Commands executed successfully")

reportErrorAndExit(projectName, "Digger finished successfully", 0)

defer func() {
if r := recover(); r != nil {
reportErrorAndExit(projectName, fmt.Sprintf("Panic occurred. %s", r), 1)
}
}()
}

/*
Expand Down Expand Up @@ -218,7 +238,8 @@ func main() {
func newPlanStorage(ghToken string, repoOwner string, repositoryName string, prNumber int) utils.PlanStorage {
var planStorage utils.PlanStorage

if os.Getenv("PLAN_UPLOAD_DESTINATION") == "github" {
uploadDestination := strings.ToLower(os.Getenv("PLAN_UPLOAD_DESTINATION"))
if uploadDestination == "github" {
zipManager := utils.Zipper{}
planStorage = &utils.GithubPlanStorage{
Client: github.NewTokenClient(context.Background(), ghToken),
Expand All @@ -227,9 +248,8 @@ func newPlanStorage(ghToken string, repoOwner string, repositoryName string, prN
PullRequestNumber: prNumber,
ZipManager: zipManager,
}
} else if os.Getenv("PLAN_UPLOAD_DESTINATION") == "gcp" {
} else if uploadDestination == "gcp" {
ctx, client := gcp.GetGoogleStorageClient()

bucketName := strings.ToLower(os.Getenv("GOOGLE_STORAGE_BUCKET"))
if bucketName == "" {
reportErrorAndExit(repoOwner, fmt.Sprintf("GOOGLE_STORAGE_BUCKET is not defined"), 9)
Expand All @@ -240,7 +260,10 @@ func newPlanStorage(ghToken string, repoOwner string, repositoryName string, prN
Bucket: bucket,
Context: ctx,
}
} else if uploadDestination == "gitlab" {
//TODO implement me
}

return planStorage
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/ci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ci
type CIService interface {
GetChangedFiles(prNumber int) ([]string, error)
PublishComment(prNumber int, comment string)

// SetStatus set status of specified pull/merge request, status could be: "pending", "failure", "success"
SetStatus(prNumber int, status string, statusContext string) error
GetCombinedPullRequestStatus(prNumber int) (string, error)
MergePullRequest(prNumber int) error
Expand Down
41 changes: 40 additions & 1 deletion pkg/configuration/digger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,50 @@ func (s *Step) extract(stepMap map[string]interface{}, action string) {
}
}

// duplicate copied from digger.go
func defaultWorkflow() *Workflow {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we remove original one ?

return &Workflow{
Configuration: &WorkflowConfiguration{
OnCommitToDefault: []string{"digger unlock"},
OnPullRequestPushed: []string{"digger plan"},
OnPullRequestClosed: []string{"digger unlock"},
},
Plan: &Stage{
Steps: []Step{
{
Action: "init", ExtraArgs: []string{},
},
{
Action: "plan", ExtraArgs: []string{},
},
},
},
Apply: &Stage{
Steps: []Step{
{
Action: "init", ExtraArgs: []string{},
},
{
Action: "apply", ExtraArgs: []string{},
},
},
},
}
}

func ConvertDiggerYamlToConfig(diggerYaml *DiggerConfigYaml, workingDir string, walker DirWalker) (*DiggerConfig, error) {
var diggerConfig DiggerConfig

diggerConfig.AutoMerge = diggerYaml.AutoMerge
diggerConfig.Workflows = diggerYaml.Workflows

if diggerYaml.Workflows != nil {
diggerConfig.Workflows = diggerYaml.Workflows
} else {
workflow := *defaultWorkflow()
diggerConfig.Workflows = make(map[string]Workflow)
diggerConfig.Workflows["default"] = workflow
}

diggerConfig.Projects = diggerYaml.Projects
diggerConfig.CollectUsageData = diggerYaml.CollectUsageData

Expand Down
2 changes: 1 addition & 1 deletion pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/google/go-github/v51/github"
)

func NewGithubPullRequestService(ghToken string, repoName string, owner string) ci.CIService {
func NewGitHubService(ghToken string, repoName string, owner string) ci.CIService {
client := github.NewTokenClient(context.Background(), ghToken)
return &GithubService{
Client: client,
Expand Down
Loading