Skip to content

Commit 7ae35fa

Browse files
authored
Merge pull request #19 from manicminer/max-merge-requests-age
Support max merge requests age
2 parents b3aa4ad + e788972 commit 7ae35fa

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Written in Go, this is a cross-platform CLI utility that accepts the following r
4747
continue migrating until canceled
4848
-max-concurrency int
4949
how many projects to migrate in parallel (default 4)
50+
-merge-requests-max-age string
51+
optional maximum age in days of merge requests to migrate
5052
-migrate-pull-requests
5153
whether pull requests should be migrated
5254
-projects-csv string
@@ -59,6 +61,8 @@ Written in Go, this is a cross-platform CLI utility that accepts the following r
5961
report on primitives to be migrated instead of beginning migration
6062
-skip-invalid-merge-requests
6163
when true, will log and skip invalid merge requests instead of raising an error
64+
-version
65+
output version information
6266
```
6367

6468
## Authentication
@@ -98,6 +102,8 @@ This tool also migrates merged/closed merge requests from your GitLab projects.
98102

99103
If you have a large number of merge requests, or projects with a long history spanning many GitLab upgrades, you may wish to specify the `-skip-invalid-merge-requests` argument. This will cause the tool to emit INFO messages for merge requests that it considers invalid, such as those that are still marked as Open but have no source/head branch, or where there is no diff for a closed merge request. Without this option, an error will be logged instead.
100104

105+
Similarly, you can specify a maximum age for merge requests to migrate with the `-merge-requests-max-age` argument, which is useful for 'topping off' projects that are already migrated.
106+
101107
_Example migrated pull request (open)_
102108

103109
![example migrated open pull request](pr-example-open.jpeg)

main.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
var loop, report bool
3333
var deleteExistingRepos, enablePullRequests, renameMasterToMain, skipInvalidMergeRequests bool
3434
var githubDomain, githubRepo, githubToken, githubUser, gitlabDomain, gitlabProject, gitlabToken, projectsCsvPath, renameTrunkBranch string
35+
var mergeRequestsAge int
3536

3637
var (
3738
cache *objectCache
@@ -86,19 +87,8 @@ func main() {
8687

8788
cache = newObjectCache()
8889

89-
githubToken = os.Getenv("GITHUB_TOKEN")
90-
if githubToken == "" {
91-
logger.Error("missing environment variable", "name", "GITHUB_TOKEN")
92-
os.Exit(1)
93-
}
94-
95-
gitlabToken = os.Getenv("GITLAB_TOKEN")
96-
if gitlabToken == "" {
97-
logger.Error("missing environment variable", "name", "GITLAB_TOKEN")
98-
os.Exit(1)
99-
}
100-
10190
var showVersion bool
91+
var mergeRequestsAgeRaw string
10292
fmt.Printf(fmt.Sprintf("gitlab-migrator %s\n", version))
10393

10494
flag.BoolVar(&loop, "loop", false, "continue migrating until canceled")
@@ -116,6 +106,7 @@ func main() {
116106
flag.StringVar(&gitlabDomain, "gitlab-domain", defaultGitlabDomain, "specifies the GitLab domain to use")
117107
flag.StringVar(&gitlabProject, "gitlab-project", "", "the GitLab project to migrate")
118108
flag.StringVar(&projectsCsvPath, "projects-csv", "", "specifies the path to a CSV file describing projects to migrate (incompatible with -gitlab-project and -github-repo)")
109+
flag.StringVar(&mergeRequestsAgeRaw, "merge-requests-max-age", "", "optional maximum age in days of merge requests to migrate")
119110
flag.StringVar(&renameTrunkBranch, "rename-trunk-branch", "", "specifies the new trunk branch name (incompatible with -rename-master-to-main)")
120111

121112
flag.IntVar(&maxConcurrency, "max-concurrency", 4, "how many projects to migrate in parallel")
@@ -126,6 +117,18 @@ func main() {
126117
return
127118
}
128119

120+
githubToken = os.Getenv("GITHUB_TOKEN")
121+
if githubToken == "" {
122+
logger.Error("missing environment variable", "name", "GITHUB_TOKEN")
123+
os.Exit(1)
124+
}
125+
126+
gitlabToken = os.Getenv("GITLAB_TOKEN")
127+
if gitlabToken == "" {
128+
logger.Error("missing environment variable", "name", "GITLAB_TOKEN")
129+
os.Exit(1)
130+
}
131+
129132
if githubUser == "" {
130133
githubUser = os.Getenv("GITHUB_USER")
131134
}
@@ -150,6 +153,13 @@ func main() {
150153
os.Exit(1)
151154
}
152155

156+
if mergeRequestsAgeRaw != "" {
157+
if mergeRequestsAge, err = strconv.Atoi(mergeRequestsAgeRaw); err != nil {
158+
logger.Error("must specify an integer for -merge-requests-age")
159+
os.Exit(1)
160+
}
161+
}
162+
153163
retryClient := &retryablehttp.Client{
154164
HTTPClient: cleanhttp.DefaultPooledClient(),
155165
Logger: nil,

project.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"sort"
1212
"strconv"
1313
"strings"
14+
"time"
1415

1516
"github.com/go-git/go-billy/v5/memfs"
1617
"github.com/go-git/go-git/v5"
@@ -242,6 +243,10 @@ func (p *project) migrateMergeRequests(ctx context.Context) {
242243
Sort: pointer("asc"),
243244
}
244245

246+
if mergeRequestsAge > 0 {
247+
opts.CreatedAfter = pointer(time.Now().AddDate(0, 0, -mergeRequestsAge))
248+
}
249+
245250
logger.Debug("retrieving GitLab merge requests", "name", p.gitlabPath[1], "group", p.gitlabPath[0], "project_id", p.project.ID)
246251
for {
247252
result, resp, err := gl.MergeRequests.ListProjectMergeRequests(p.project.ID, opts)

0 commit comments

Comments
 (0)