Browse Source

Add web log (#27)

pull/31/head
Jonas Franz 9 months ago
parent
commit
f2c99b3ae2

+ 0
- 1
cmd/web.go View File

@@ -43,6 +43,5 @@ func runWeb(ctx *cli.Context) error {
43 43
 		port = 4000
44 44
 	}
45 45
 	logrus.Infof("Server is running at http://%s:%d", hostname, port)
46
-	logrus.SetLevel(logrus.PanicLevel)
47 46
 	return http.ListenAndServe(fmt.Sprintf("%s:%d", hostname, port), r)
48 47
 }

+ 2
- 2
main.go View File

@@ -1,10 +1,10 @@
1
-//go:generate swagger generate spec -i ./swagger.yml -o ./swagger.json
2 1
 package main
3 2
 
4 3
 import (
5 4
 	"fmt"
6 5
 	"os"
7 6
 
7
+	"github.com/sirupsen/logrus"
8 8
 	"github.com/urfave/cli"
9 9
 )
10 10
 
@@ -21,6 +21,6 @@ func main() {
21 21
 	app.Description = `Migrate your GitHub repositories including issues to Gitea`
22 22
 	app.Commands = cmds
23 23
 	if err := app.Run(os.Args); err != nil {
24
-		panic(err)
24
+		logrus.Panic(err)
25 25
 	}
26 26
 }

+ 22
- 18
migrations/github.go View File

@@ -1,6 +1,7 @@
1 1
 package migrations
2 2
 
3 3
 import (
4
+	"bytes"
4 5
 	"context"
5 6
 	"fmt"
6 7
 	"regexp"
@@ -19,6 +20,8 @@ type FetchMigratory struct {
19 20
 	GHClient  *github.Client
20 21
 	RepoOwner string
21 22
 	RepoName  string
23
+	Logger    *logrus.Logger
24
+	LogOutput *bytes.Buffer
22 25
 }
23 26
 
24 27
 func (fm *FetchMigratory) ctx() context.Context {
@@ -30,7 +33,8 @@ func (fm *FetchMigratory) MigrateFromGitHub() error {
30 33
 	fm.Status = &MigratoryStatus{
31 34
 		Stage: Importing,
32 35
 	}
33
-	logrus.WithFields(logrus.Fields{
36
+
37
+	fm.Logger.WithFields(logrus.Fields{
34 38
 		"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
35 39
 	}).Info("migrating git repository")
36 40
 	ghRepo, _, err := fm.GHClient.Repositories.Get(fm.ctx(), fm.RepoOwner, fm.RepoName)
@@ -45,7 +49,7 @@ func (fm *FetchMigratory) MigrateFromGitHub() error {
45 49
 		fm.Status.FatalError = err
46 50
 		return fmt.Errorf("Repository migration: %v", err)
47 51
 	}
48
-	logrus.WithFields(logrus.Fields{
52
+	fm.Logger.WithFields(logrus.Fields{
49 53
 		"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
50 54
 	}).Info("git repository migrated")
51 55
 	if fm.Options.Issues || fm.Options.PullRequests {
@@ -57,9 +61,9 @@ func (fm *FetchMigratory) MigrateFromGitHub() error {
57 61
 		if err != nil {
58 62
 			fm.Status.Stage = Failed
59 63
 			fm.Status.FatalError = err
60
-			logrus.WithFields(logrus.Fields{
64
+			fm.Logger.WithFields(logrus.Fields{
61 65
 				"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
62
-			}).Fatalf("migration failed: %v", fm.Status.FatalError)
66
+			}).Errorf("migration failed: %v", fm.Status.FatalError)
63 67
 			return err
64 68
 		}
65 69
 		fm.Status.Stage = Migrating
@@ -71,14 +75,14 @@ func (fm *FetchMigratory) MigrateFromGitHub() error {
71 75
 				migratedIssues[issue.GetNumber()], err = fm.Issue(issue)
72 76
 				if err != nil {
73 77
 					fm.Status.IssuesError++
74
-					logrus.WithFields(logrus.Fields{
78
+					fm.Logger.WithFields(logrus.Fields{
75 79
 						"repo":  fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
76 80
 						"issue": issue.GetNumber(),
77 81
 					}).Warnf("error while migrating: %v", err)
78 82
 					continue
79 83
 				}
80 84
 				fm.Status.IssuesMigrated++
81
-				logrus.WithFields(logrus.Fields{
85
+				fm.Logger.WithFields(logrus.Fields{
82 86
 					"repo":  fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
83 87
 					"issue": issue.GetNumber(),
84 88
 				}).Info("issue migrated")
@@ -91,9 +95,9 @@ func (fm *FetchMigratory) MigrateFromGitHub() error {
91 95
 			if cmts := <-commentsChan; cmts == nil {
92 96
 				fm.Status.Stage = Failed
93 97
 				err := fmt.Errorf("error while fetching issue comments")
94
-				logrus.WithFields(logrus.Fields{
98
+				fm.Logger.WithFields(logrus.Fields{
95 99
 					"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
96
-				}).Fatalf("migration failed: %v", fm.Status.FatalError)
100
+				}).Errorf("migration failed: %v", fm.Status.FatalError)
97 101
 				return err
98 102
 			} else {
99 103
 				comments = *cmts
@@ -102,9 +106,9 @@ func (fm *FetchMigratory) MigrateFromGitHub() error {
102 106
 			if err != nil {
103 107
 				fm.Status.Stage = Failed
104 108
 				fm.Status.FatalError = err
105
-				logrus.WithFields(logrus.Fields{
109
+				fm.Logger.WithFields(logrus.Fields{
106 110
 					"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
107
-				}).Fatalf("migration failed: %v", fm.Status.FatalError)
111
+				}).Errorf("migration failed: %v", fm.Status.FatalError)
108 112
 				return err
109 113
 			}
110 114
 			fm.Status.Comments = int64(len(comments))
@@ -113,7 +117,7 @@ func (fm *FetchMigratory) MigrateFromGitHub() error {
113 117
 				issueIndex, err := getIssueIndexFromHTMLURL(comment.GetHTMLURL())
114 118
 				if err != nil {
115 119
 					fm.Status.CommentsError++
116
-					logrus.WithFields(logrus.Fields{
120
+					fm.Logger.WithFields(logrus.Fields{
117 121
 						"repo":    fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
118 122
 						"issue":   issueIndex,
119 123
 						"comment": comment.GetID(),
@@ -138,14 +142,14 @@ func (fm *FetchMigratory) MigrateFromGitHub() error {
138 142
 					for _, comm := range cs {
139 143
 						if _, err := fm.IssueComment(i, comm); err != nil {
140 144
 							fm.Status.CommentsError++
141
-							logrus.WithFields(logrus.Fields{
145
+							fm.Logger.WithFields(logrus.Fields{
142 146
 								"repo":    fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
143 147
 								"comment": comm.GetID(),
144 148
 							}).Warnf("error while migrating comment: %v", err)
145 149
 							continue
146 150
 						}
147 151
 						fm.Status.CommentsMigrated++
148
-						logrus.WithFields(logrus.Fields{
152
+						fm.Logger.WithFields(logrus.Fields{
149 153
 							"repo":    fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
150 154
 							"comment": comm.GetID(),
151 155
 						}).Info("comment migrated")
@@ -158,13 +162,13 @@ func (fm *FetchMigratory) MigrateFromGitHub() error {
158 162
 	}
159 163
 	if fm.Status.FatalError != nil {
160 164
 		fm.Status.Stage = Failed
161
-		logrus.WithFields(logrus.Fields{
165
+		fm.Logger.WithFields(logrus.Fields{
162 166
 			"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
163
-		}).Fatalf("migration failed: %v", fm.Status.FatalError)
167
+		}).Errorf("migration failed: %v", fm.Status.FatalError)
164 168
 		return nil
165 169
 	}
166 170
 	fm.Status.Stage = Finished
167
-	logrus.WithFields(logrus.Fields{
171
+	fm.Logger.WithFields(logrus.Fields{
168 172
 		"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
169 173
 	}).Info("migration successful")
170 174
 	return nil
@@ -252,9 +256,9 @@ func (fm *FetchMigratory) fetchCommentsAsync() chan *[]*github.IssueComment {
252 256
 		if err != nil {
253 257
 			f.Status.FatalError = err
254 258
 			ret <- nil
255
-			logrus.WithFields(logrus.Fields{
259
+			fm.Logger.WithFields(logrus.Fields{
256 260
 				"repo": fmt.Sprintf("%s/%s", fm.RepoOwner, fm.RepoName),
257
-			}).Fatalf("fetching comments failed: %v", fm.Status.FatalError)
261
+			}).Errorf("fetching comments failed: %v", fm.Status.FatalError)
258 262
 			return
259 263
 		}
260 264
 		f.Status.Comments = int64(len(comments))

+ 22
- 5
migrations/job.go View File

@@ -1,11 +1,13 @@
1 1
 package migrations
2 2
 
3 3
 import (
4
+	"bytes"
4 5
 	"fmt"
5 6
 	"strings"
6 7
 
7 8
 	"code.gitea.io/sdk/gitea"
8 9
 	"github.com/google/go-github/github"
10
+	"github.com/sirupsen/logrus"
9 11
 )
10 12
 
11 13
 // Job manages all migrations of a "migartion job"
@@ -14,8 +16,9 @@ type Job struct {
14 16
 	Options      *Options
15 17
 	Client       *gitea.Client
16 18
 	GHClient     *github.Client
19
+	UseStdErr    bool
17 20
 
18
-	migratories map[string]*Migratory
21
+	migratories map[string]*FetchMigratory
19 22
 }
20 23
 
21 24
 // JobReport represents the current status of a Job
@@ -41,6 +44,7 @@ func (job *Job) StatusReport() *JobReport {
41 44
 	}
42 45
 	for _, repo := range job.Repositories {
43 46
 		if migratory, ok := job.migratories[repo]; ok {
47
+			migratory.Status.Log = migratory.LogOutput.String()
44 48
 			switch migratory.Status.Stage {
45 49
 			case Finished:
46 50
 				report.Finished[repo] = migratory.Status
@@ -70,10 +74,10 @@ func (job *Job) StartMigration() chan error {
70 74
 			close(errs)
71 75
 		}
72 76
 	}
73
-	job.migratories = make(map[string]*Migratory, pendingRepos)
77
+	job.migratories = make(map[string]*FetchMigratory, pendingRepos)
74 78
 	for _, repo := range job.Repositories {
75 79
 		mig, err := job.initFetchMigratory(repo)
76
-		job.migratories[repo] = &mig.Migratory
80
+		job.migratories[repo] = mig
77 81
 		if err != nil {
78 82
 			mig.Status = &MigratoryStatus{
79 83
 				Stage:      Failed,
@@ -97,7 +101,7 @@ func (job *Job) initFetchMigratory(repo string) (*FetchMigratory, error) {
97 101
 	if len(res) != 2 {
98 102
 		return nil, fmt.Errorf("invalid repo name: %s", repo)
99 103
 	}
100
-	return &FetchMigratory{
104
+	fm := &FetchMigratory{
101 105
 		Migratory: Migratory{
102 106
 			Client:  job.Client,
103 107
 			Options: *job.Options,
@@ -105,7 +109,20 @@ func (job *Job) initFetchMigratory(repo string) (*FetchMigratory, error) {
105 109
 		RepoName:  res[1],
106 110
 		RepoOwner: res[0],
107 111
 		GHClient:  job.GHClient,
108
-	}, nil
112
+		Logger:    logrus.New(),
113
+		LogOutput: new(bytes.Buffer),
114
+	}
115
+	if !job.UseStdErr {
116
+		fm.Logger.Formatter = &logrus.TextFormatter{
117
+			DisableColors:          true,
118
+			DisableLevelTruncation: true,
119
+			DisableTimestamp:       true,
120
+		}
121
+		fm.Logger.SetOutput(fm.LogOutput)
122
+	} else {
123
+		fm.LogOutput = nil
124
+	}
125
+	return fm, nil
109 126
 }
110 127
 
111 128
 // Finished indicates if the job is finished or not

+ 1
- 0
migrations/migratory.go View File

@@ -44,4 +44,5 @@ type MigratoryStatus struct {
44 44
 
45 45
 	// FatalError should only be used if stage == failed; indicates which fatal error occurred
46 46
 	FatalError error
47
+	Log        string `json:"log"`
47 48
 }

+ 4
- 0
web/public/js/repos-status.js View File

@@ -51,6 +51,8 @@ function handleData(data) {
51 51
         }
52 52
         content.find(".comment-progress").progress('set progress', report.migrated_comments + report.failed_comments);
53 53
         content.find(".issue-progress").progress('set progress', report.migrated_issues + report.failed_issues);
54
+        content.find('.log-content').val(report.log);
55
+        content.find('.log-content').scrollTop(content.find('.log-content')[0].scrollHeight)
54 56
     });
55 57
     forEach(data.finished, function (repo, report) {
56 58
         var content = handleNonPending(repo, report);
@@ -64,6 +66,7 @@ function handleData(data) {
64 66
         content.find(".issue-progress").progress('set progress', report.migrated_issues + report.failed_issues);
65 67
         content.find(".issue-progress").progress('complete');
66 68
         content.find(".comment-progress").progress('complete');
69
+        content.find('.log-content').val(report.log);
67 70
     });
68 71
 }
69 72
 function forEach(object, callback) {
@@ -78,6 +81,7 @@ function handleNonPending(repo, report) {
78 81
     var content = contentFromRepo(repo);
79 82
     if(!content.hasClass("non-pending")) {
80 83
         content.html(renderNonPending().html());
84
+        content.find(".accordion").accordion();
81 85
         content.find(".issue-progress").progress({
82 86
             text: {
83 87
                 active  : 'Migrated {value} of {total} issues',

+ 1
- 1
web/templates/base/head.tmpl View File

@@ -1,5 +1,5 @@
1 1
 <!DOCTYPE html>
2
-<html>
2
+<html lang="en">
3 3
 <head>
4 4
     <!-- Standard Meta -->
5 5
     <meta charset="utf-8" />

+ 14
- 1
web/templates/migration.tmpl View File

@@ -14,7 +14,7 @@
14 14
             <div class="bar"></div>
15 15
             <div class="label">Migrating repositories</div>
16 16
         </div>
17
-        <div class="ui three stackable cards" id="migration-list">
17
+        <div class="ui two stackable cards" id="migration-list">
18 18
         {{range .StatusReport.Pending}}
19 19
             <div class="ui repo-card card" data-repo="{{.}}" data-status="pending">
20 20
                 <div class="content">
@@ -43,6 +43,19 @@
43 43
         <div class="label">Migrating comments</div>
44 44
     </div>
45 45
     <p><b class="failed-comments">0</b> migration(s) of comments failed</p>
46
+    <div class="ui accordion" onload="$(this).accordion();">
47
+        <div class="title">
48
+            <i class="dropdown icon"></i>
49
+            View Log
50
+        </div>
51
+        <div class="content">
52
+            <div class="ui form">
53
+                <div class="ui field">
54
+                    <textarea title="Log content" class="log-content" readonly></textarea>
55
+                </div>
56
+            </div>
57
+        </div>
58
+    </div>
46 59
 </div>
47 60
 <div id="content-failed" style="display: none;">
48 61
     <div class="ui negative message">

Loading…
Cancel
Save