diff --git a/plugins/auto_merge/auto_merge.go b/plugins/auto_merge/auto_merge.go index d2b2e6d..6598c65 100644 --- a/plugins/auto_merge/auto_merge.go +++ b/plugins/auto_merge/auto_merge.go @@ -29,16 +29,6 @@ func (plugin autoMergePlugin) Name() string { } func (plugin autoMergePlugin) Execute(project *gitlab.Project, config config.ProjectConfig) { - opt := &gitlab.ListProjectMergeRequestsOptions{ - State: gitlab.String("opened"), - UpdatedAfter: plugin.lastestChecks[project.ID], - // TargetBranch: &project.DefaultBranch, - ListOptions: gitlab.ListOptions{ - PerPage: 10, - Page: 1, - }, - } - err := json.Unmarshal(config.Plugins[plugin.Name()], &plugin.loadedConfig) if err != nil { log.Debug("Can't load config", err) @@ -46,34 +36,16 @@ func (plugin autoMergePlugin) Execute(project *gitlab.Project, config config.Pro } log.Debugf("Loaded config: %+v", plugin.loadedConfig) - totalMergeRequests := 0 now := time.Now() - for { - // Get the first page with merge-requests - mergeRequests, resp, err := plugin.Client.MergeRequests.ListProjectMergeRequests(project.ID, opt) - if err != nil { - log.Debug("Can't load merge-requests", err) - } - - log.Debugf("checking %d merge-requests ...\n", len(mergeRequests)) - - totalMergeRequests = totalMergeRequests + len(mergeRequests) - - for _, mergeRequest := range mergeRequests { - plugin.autoMerge(project, mergeRequest) - } - - // Exit the loop when we've seen all pages - if resp.CurrentPage >= resp.TotalPages { - break - } + mergeRequests := plugin.getUpdatedMergeRequests(project) + mergeRequests = append(mergeRequests, plugin.getUpdatedPipelineMergeRequests(project)...) - // Update the page number to get the next page - opt.Page = resp.NextPage + for _, mergeRequest := range mergeRequests { + plugin.autoMerge(project, mergeRequest) } - log.Debugf("checked total of %d merge-requests\n", totalMergeRequests) + log.Debugf("checked total of %d merge-requests\n", len(mergeRequests)) plugin.lastestChecks[project.ID] = &now } @@ -105,3 +77,97 @@ func (plugin autoMergePlugin) autoMerge(project *gitlab.Project, mergeRequest *g plugin.updateStatusComment(project, mergedMergeRequest, status) } + +func (plugin autoMergePlugin) getUpdatedPipelineMergeRequests(project *gitlab.Project) []*gitlab.MergeRequest { + var mergeRequests []*gitlab.MergeRequest + + lastCheck := plugin.lastestChecks[project.ID] + + // if this is the first run => we don't need to get MRs with updated pipelines as we already checked all MRs + if lastCheck == nil { + return mergeRequests + } + + opt := &gitlab.ListProjectPipelinesOptions{ + UpdatedAfter: lastCheck, + ListOptions: gitlab.ListOptions{ + PerPage: 10, + Page: 1, + }, + } + + for { + pipelines, resp, err := plugin.Client.Pipelines.ListProjectPipelines(project.ID, opt) + if err != nil { + log.Debug("Can't load pipelines", err) + } + + for _, pipeline := range pipelines { + optMR := &gitlab.ListProjectMergeRequestsOptions{ + State: gitlab.String("opened"), + SourceBranch: &pipeline.Ref, + ListOptions: gitlab.ListOptions{ + PerPage: 2, + Page: 1, + }, + } + + _mergeRequests, _, err := plugin.Client.MergeRequests.ListProjectMergeRequests(project.ID, optMR) + if err != nil { + log.Debug("Can't load merge-requests", err) + } + + if len(_mergeRequests) == 1 { + mergeRequests = append(mergeRequests, _mergeRequests...) + } else if len(_mergeRequests) > 1 { + log.Warn("Found more than one merge-request for your pipeline") + } + } + + // Exit the loop when we've seen all pages + if resp.CurrentPage >= resp.TotalPages { + break + } + + // Update the page number to get the next page + opt.Page = resp.NextPage + } + + return mergeRequests +} + +func (plugin autoMergePlugin) getUpdatedMergeRequests(project *gitlab.Project) []*gitlab.MergeRequest { + var mergeRequests []*gitlab.MergeRequest + + lastCheck := plugin.lastestChecks[project.ID] + + opt := &gitlab.ListProjectMergeRequestsOptions{ + State: gitlab.String("opened"), + // TargetBranch: &project.DefaultBranch, + UpdatedAfter: lastCheck, + ListOptions: gitlab.ListOptions{ + PerPage: 10, + Page: 1, + }, + } + + for { + // Get the first page with merge-requests + _mergeRequests, resp, err := plugin.Client.MergeRequests.ListProjectMergeRequests(project.ID, opt) + if err != nil { + log.Debug("Can't load merge-requests", err) + } + + mergeRequests = append(mergeRequests, _mergeRequests...) + + // Exit the loop when we've seen all pages + if resp.CurrentPage >= resp.TotalPages { + break + } + + // Update the page number to get the next page + opt.Page = resp.NextPage + } + + return mergeRequests +}