Skip to content

Commit

Permalink
fix: rerun checks if a mr-pipeline finishes (#43)
Browse files Browse the repository at this point in the history
* fix: rerun checks if a mr-pipeline finishes

* feat: ✨ check merge-request titles for conventional commit syntax

* Revert "feat: ✨ check merge-request titles for conventional commit syntax"

This reverts commit 380b75b.

* perf: improve pipeline loading

* refactor: improve message
  • Loading branch information
anbraten authored Aug 23, 2021
1 parent db147c8 commit 2912ca2
Showing 1 changed file with 99 additions and 33 deletions.
132 changes: 99 additions & 33 deletions plugins/auto_merge/auto_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,23 @@ 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)
return
}
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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 2912ca2

Please sign in to comment.