Browse Source

Refactor some parts of github.go

Add additional qa tasks to Makefile
Add vendor to gitignore
pull/31/head
Jonas Franz 4 months ago
parent
commit
f758e29097
4 changed files with 125 additions and 104 deletions
  1. 2
    0
      .gitignore
  2. 8
    1
      Makefile
  3. 113
    101
      migrations/github.go
  4. 2
    2
      migrations/github_test.go

+ 2
- 0
.gitignore View File

@@ -16,3 +16,5 @@
16 16
 .glide/
17 17
 
18 18
 data/
19
+
20
+vendor/

+ 8
- 1
Makefile View File

@@ -74,12 +74,19 @@ lint:
74 74
 	fi
75 75
 	golint -set_exit_status $(go list ./...)
76 76
 
77
+.PHONY: ineffassign
78
+ineffassign:
79
+	@hash ineffassign > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
80
+		$(GO) get -u github.com/gordonklaus/ineffassign; \
81
+	fi
82
+	ineffassign .
83
+
77 84
 .PHONY: vet
78 85
 vet:
79 86
 	go vet ./...
80 87
 
81 88
 .PHONY: test
82
-test: lint vet
89
+test: lint vet ineffassign
83 90
 	go test -tags web -cover ./...
84 91
 
85 92
 .PHONY: coverage

+ 113
- 101
migrations/github.go View File

@@ -53,124 +53,136 @@ func (fm *FetchMigratory) MigrateFromGitHub() error {
53 53
 		"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
54 54
 	}).Info("git repository migrated")
55 55
 	if fm.Options.Issues || fm.Options.PullRequests {
56
-		var commentsChan chan *[]*github.IssueComment
57
-		if fm.Options.Comments {
58
-			commentsChan = fm.fetchCommentsAsync()
59
-		}
60
-		issues, err := fm.FetchIssues()
61
-		if err != nil {
62
-			fm.Status.Stage = Failed
63
-			fm.Status.FatalError = err
64
-			fm.Logger.WithFields(logrus.Fields{
65
-				"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
66
-			}).Errorf("migration failed: %v", fm.Status.FatalError)
56
+		if err := fm.MigrateIssuesFromGitHub(); err != nil {
67 57
 			return err
68 58
 		}
69
-		fm.Status.Stage = Migrating
70
-		fm.Status.Issues = int64(len(issues))
71
-		migratedIssues := make(map[int]*gitea.Issue)
72
-		for _, issue := range issues {
73
-			if (!issue.IsPullRequest() || fm.Options.PullRequests) &&
74
-				(issue.IsPullRequest() || fm.Options.Issues) {
75
-				migratedIssues[issue.GetNumber()], err = fm.Issue(issue)
76
-				if err != nil {
77
-					fm.Status.IssuesError++
78
-					fm.Logger.WithFields(logrus.Fields{
79
-						"repo":  fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
80
-						"issue": issue.GetNumber(),
81
-					}).Warnf("error while migrating: %v", err)
82
-					continue
83
-				}
84
-				fm.Status.IssuesMigrated++
59
+	}
60
+	if fm.Status.FatalError != nil {
61
+		fm.Status.Stage = Failed
62
+		fm.Logger.WithFields(logrus.Fields{
63
+			"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
64
+		}).Errorf("migration failed: %v", fm.Status.FatalError)
65
+		return nil
66
+	}
67
+	fm.Status.Stage = Finished
68
+	fm.Logger.WithFields(logrus.Fields{
69
+		"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
70
+	}).Info("migration successful")
71
+	return nil
72
+}
73
+
74
+// MigrateIssuesFromGitHub migrates all issues from GitHub to Gitea
75
+func (fm *FetchMigratory) MigrateIssuesFromGitHub() error {
76
+	var commentsChan chan *[]*github.IssueComment
77
+	if fm.Options.Comments {
78
+		commentsChan = fm.fetchCommentsAsync()
79
+	}
80
+	issues, err := fm.FetchIssues()
81
+	if err != nil {
82
+		fm.Status.Stage = Failed
83
+		fm.Status.FatalError = err
84
+		fm.Logger.WithFields(logrus.Fields{
85
+			"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
86
+		}).Errorf("migration failed: %v", fm.Status.FatalError)
87
+		return err
88
+	}
89
+	fm.Status.Stage = Migrating
90
+	fm.Status.Issues = int64(len(issues))
91
+	migratedIssues := make(map[int]*gitea.Issue)
92
+	for _, issue := range issues {
93
+		if (!issue.IsPullRequest() || fm.Options.PullRequests) &&
94
+			(issue.IsPullRequest() || fm.Options.Issues) {
95
+			migratedIssues[issue.GetNumber()], err = fm.Issue(issue)
96
+			if err != nil {
97
+				fm.Status.IssuesError++
85 98
 				fm.Logger.WithFields(logrus.Fields{
86 99
 					"repo":  fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
87 100
 					"issue": issue.GetNumber(),
88
-				}).Info("issue migrated")
89
-			} else {
90
-				fm.Status.Issues--
101
+				}).Warnf("error while migrating: %v", err)
102
+				continue
91 103
 			}
104
+			fm.Status.IssuesMigrated++
105
+			fm.Logger.WithFields(logrus.Fields{
106
+				"repo":  fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
107
+				"issue": issue.GetNumber(),
108
+			}).Info("issue migrated")
109
+		} else {
110
+			fm.Status.Issues--
92 111
 		}
93
-		if fm.Options.Comments {
94
-			var comments []*github.IssueComment
95
-			if cmts := <-commentsChan; cmts == nil {
96
-				fm.Status.Stage = Failed
97
-				err := fmt.Errorf("error while fetching issue comments")
98
-				fm.Logger.WithFields(logrus.Fields{
99
-					"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
100
-				}).Errorf("migration failed: %v", fm.Status.FatalError)
101
-				return err
112
+	}
113
+	if fm.Options.Comments {
114
+		return fm.MigrateCommentsFromGitHub()
115
+	}
116
+	return nil
117
+}
118
+
119
+func (fm *FetchMigratory) MigrateCommentsFromGitHub() error {
120
+	var comments []*github.IssueComment
121
+	var cmts *[]*github.IssueComment
122
+	if cmts = <-commentsChan; cmts == nil {
123
+		fm.Status.Stage = Failed
124
+		err := fmt.Errorf("error while fetching issue comments")
125
+		fm.Logger.WithFields(logrus.Fields{
126
+			"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
127
+		}).Errorf("migration failed: %v", fm.Status.FatalError)
128
+		return err
129
+	}
130
+	comments = *cmts
131
+	if err != nil {
132
+		fm.Status.Stage = Failed
133
+		fm.Status.FatalError = err
134
+		fm.Logger.WithFields(logrus.Fields{
135
+			"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
136
+		}).Errorf("migration failed: %v", fm.Status.FatalError)
137
+		return err
138
+	}
139
+	fm.Status.Comments = int64(len(comments))
140
+	commentsByIssue := make(map[*gitea.Issue][]*github.IssueComment, len(migratedIssues))
141
+	for _, comment := range comments {
142
+		issueIndex, err := getIssueIndexFromHTMLURL(comment.GetHTMLURL())
143
+		if err != nil {
144
+			fm.Status.CommentsError++
145
+			fm.Logger.WithFields(logrus.Fields{
146
+				"repo":    fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
147
+				"issue":   issueIndex,
148
+				"comment": comment.GetID(),
149
+			}).Warnf("error while migrating comment: %v", err)
150
+			continue
151
+		}
152
+		if issue, ok := migratedIssues[issueIndex]; ok && issue != nil {
153
+			if list, ok := commentsByIssue[issue]; !ok && list != nil {
154
+				commentsByIssue[issue] = []*github.IssueComment{comment}
102 155
 			} else {
103
-				comments = *cmts
156
+				commentsByIssue[issue] = append(list, comment)
104 157
 			}
105
-			if err != nil {
106
-				fm.Status.Stage = Failed
107
-				fm.Status.FatalError = err
108
-				fm.Logger.WithFields(logrus.Fields{
109
-					"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
110
-				}).Errorf("migration failed: %v", fm.Status.FatalError)
111
-				return err
112
-			}
113
-			fm.Status.Comments = int64(len(comments))
114
-			commentsByIssue := make(map[*gitea.Issue][]*github.IssueComment, len(migratedIssues))
115
-			for _, comment := range comments {
116
-				issueIndex, err := getIssueIndexFromHTMLURL(comment.GetHTMLURL())
117
-				if err != nil {
158
+		} else {
159
+			fm.Status.CommentsError++
160
+			continue
161
+		}
162
+	}
163
+	wg := sync.WaitGroup{}
164
+	for issue, comms := range commentsByIssue {
165
+		wg.Add(1)
166
+		go func(i *gitea.Issue, cs []*github.IssueComment) {
167
+			for _, comm := range cs {
168
+				if _, err := fm.IssueComment(i, comm); err != nil {
118 169
 					fm.Status.CommentsError++
119 170
 					fm.Logger.WithFields(logrus.Fields{
120 171
 						"repo":    fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
121
-						"issue":   issueIndex,
122
-						"comment": comment.GetID(),
172
+						"comment": comm.GetID(),
123 173
 					}).Warnf("error while migrating comment: %v", err)
124 174
 					continue
125 175
 				}
126
-				if issue, ok := migratedIssues[issueIndex]; ok && issue != nil {
127
-					if list, ok := commentsByIssue[issue]; !ok && list != nil {
128
-						commentsByIssue[issue] = []*github.IssueComment{comment}
129
-					} else {
130
-						commentsByIssue[issue] = append(list, comment)
131
-					}
132
-				} else {
133
-					fm.Status.CommentsError++
134
-					continue
135
-				}
136
-			}
137
-			wg := sync.WaitGroup{}
138
-			for issue, comms := range commentsByIssue {
139
-				wg.Add(1)
140
-				go func(i *gitea.Issue, cs []*github.IssueComment) {
141
-					for _, comm := range cs {
142
-						if _, err := fm.IssueComment(i, comm); err != nil {
143
-							fm.Status.CommentsError++
144
-							fm.Logger.WithFields(logrus.Fields{
145
-								"repo":    fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
146
-								"comment": comm.GetID(),
147
-							}).Warnf("error while migrating comment: %v", err)
148
-							continue
149
-						}
150
-						fm.Status.CommentsMigrated++
151
-						fm.Logger.WithFields(logrus.Fields{
152
-							"repo":    fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
153
-							"comment": comm.GetID(),
154
-						}).Info("comment migrated")
155
-					}
156
-					wg.Done()
157
-				}(issue, comms)
176
+				fm.Status.CommentsMigrated++
177
+				fm.Logger.WithFields(logrus.Fields{
178
+					"repo":    fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
179
+					"comment": comm.GetID(),
180
+				}).Info("comment migrated")
158 181
 			}
159
-			wg.Wait()
160
-		}
182
+			wg.Done()
183
+		}(issue, comms)
161 184
 	}
162
-	if fm.Status.FatalError != nil {
163
-		fm.Status.Stage = Failed
164
-		fm.Logger.WithFields(logrus.Fields{
165
-			"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
166
-		}).Errorf("migration failed: %v", fm.Status.FatalError)
167
-		return nil
168
-	}
169
-	fm.Status.Stage = Finished
170
-	fm.Logger.WithFields(logrus.Fields{
171
-		"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
172
-	}).Info("migration successful")
173
-	return nil
185
+	wg.Wait()
174 186
 }
175 187
 
176 188
 var issueIndexRegex = regexp.MustCompile(`/(issues|pull)/([0-9]+)#`)

+ 2
- 2
migrations/github_test.go View File

@@ -17,7 +17,7 @@ func TestGetIssueIndexFromHTMLURLAlt(t *testing.T) {
17 17
 	res, err := getIssueIndexFromHTMLURLAlt("https://github.com/octocat/Hello-World/issues/1347#issuecomment-1")
18 18
 	assert.NoError(t, err)
19 19
 	assert.Equal(t, 1347, res)
20
-	res, err = getIssueIndexFromHTMLURLAlt("https://github.com/oment-1")
20
+	_, err = getIssueIndexFromHTMLURLAlt("https://github.com/oment-1")
21 21
 	assert.Error(t, err)
22 22
 }
23 23
 
@@ -31,7 +31,7 @@ func TestGetIssueIndexFromHTMLURL(t *testing.T) {
31 31
 	res, err := getIssueIndexFromHTMLURL("https://github.com/octocat/Hello-World/issues/1347#issuecomment-1")
32 32
 	assert.NoError(t, err)
33 33
 	assert.Equal(t, 1347, res)
34
-	res, err = getIssueIndexFromHTMLURL("https://github.com/oment-1")
34
+	_, err = getIssueIndexFromHTMLURL("https://github.com/oment-1")
35 35
 	assert.Error(t, err)
36 36
 }
37 37
 

Loading…
Cancel
Save