diff --git a/web/auth/gitea.go b/web/auth/gitea.go index 25184b5..ea1f388 100644 --- a/web/auth/gitea.go +++ b/web/auth/gitea.go @@ -5,6 +5,7 @@ import ( "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/web/context" ) +// GiteaLoginForm represents the data required for logging in into gitea type GiteaLoginForm struct { Username string `form:"username"` Password string `form:"password"` @@ -13,6 +14,7 @@ type GiteaLoginForm struct { Type string `form:"use" binding:"Required;In(token,password)"` } +// LoginToGitea handles the POST request for signing in with a Gitea account func LoginToGitea(ctx *context.Context, form GiteaLoginForm) { var token string if form.Type == "password" { diff --git a/web/context/context.go b/web/context/context.go index ade7780..9385b34 100644 --- a/web/context/context.go +++ b/web/context/context.go @@ -4,9 +4,11 @@ import ( "fmt" "strings" + bgctx "context" "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/config" "github.com/go-macaron/session" "github.com/google/go-github/github" + "golang.org/x/oauth2" "gopkg.in/macaron.v1" ) @@ -22,12 +24,14 @@ type Context struct { Link string // current request URL } +// User is an abstraction of a Gitea or GitHub user, saving the required information type User struct { Username string AvatarURL string Token string } +// Handle displays the corresponding error message func (ctx *Context) Handle(status int, title string, err error) { if err != nil { if macaron.Env != macaron.PROD { @@ -48,6 +52,7 @@ func (ctx *Context) Handle(status int, title string, err error) { ctx.Context.HTML(status, fmt.Sprintf("status/%d", status)) } +// Contexter injects context.Context into macaron func Contexter() macaron.Handler { return func(c *macaron.Context, sess session.Store, f *session.Flash) { ctx := &Context{ @@ -69,11 +74,17 @@ func Contexter() macaron.Handler { ctx.User = usr.(*User) ctx.Data["User"] = ctx.User } - gitea_usr := sess.Get("gitea_user") - if gitea_usr != nil { - ctx.GiteaUser = gitea_usr.(*User) + giteaUsr := sess.Get("gitea_user") + if giteaUsr != nil { + ctx.GiteaUser = giteaUsr.(*User) ctx.Data["GiteaUser"] = ctx.GiteaUser } + if ctx.User != nil && ctx.User.Token != "" { + tc := oauth2.NewClient(bgctx.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: ctx.User.Token})) + ctx.Client = github.NewClient(tc) + } else { + ctx.Client = github.NewClient(nil) + } c.Map(ctx) } } diff --git a/web/fs.go b/web/fs.go index a4cd6f3..89a2934 100644 --- a/web/fs.go +++ b/web/fs.go @@ -23,6 +23,7 @@ func (fs *BundledFS) Exists(prefix string, filepath string) bool { return false } +// ListFiles returns all files in FS func (fs *BundledFS) ListFiles() (files []macaron.TemplateFile) { for _, filename := range fs.List() { files = append(files, &BundledFile{fs: fs, FileName: filename}) @@ -30,23 +31,28 @@ func (fs *BundledFS) ListFiles() (files []macaron.TemplateFile) { return files } +// Get returns the content of filename func (fs *BundledFS) Get(filename string) (io.Reader, error) { return bytes.NewReader(fs.Bytes(filename)), nil } +// BundledFile represents a file in a BundledFS type BundledFile struct { fs *BundledFS FileName string } +// Name represents the name of the file func (b *BundledFile) Name() string { return strings.TrimSuffix(b.FileName, path.Ext(b.FileName)) } +// Data returns the content of file func (b *BundledFile) Data() []byte { return b.fs.Bytes(b.FileName) } +// Ext returns the file extension func (b *BundledFile) Ext() string { return path.Ext(b.FileName) } diff --git a/web/migration/repos.go b/web/migration/repos.go new file mode 100644 index 0000000..94db403 --- /dev/null +++ b/web/migration/repos.go @@ -0,0 +1,18 @@ +package migration + +import ( + "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/web/context" + "github.com/google/go-github/github" + + bgctx "context" +) + +// ListRepos shows all available repos of the signed in user +func ListRepos(ctx *context.Context) { + repos, _, err := ctx.Client.Repositories.List(bgctx.Background(), ctx.User.Username, &github.RepositoryListOptions{}) + if err != nil { + ctx.Handle(500, "list repositories", err) + } + ctx.Data["Repos"] = repos + ctx.HTML(200, "repos") +} diff --git a/web/router.go b/web/router.go index b18f73f..928aea1 100644 --- a/web/router.go +++ b/web/router.go @@ -5,6 +5,7 @@ import ( "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/web/auth" "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/web/context" + "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/web/migration" "github.com/go-macaron/binding" "github.com/go-macaron/session" "github.com/gobuffalo/packr" @@ -31,6 +32,8 @@ func InitRoutes() *macaron.Macaron { FileSystem: publicBox, }, "")) m.Use(context.Contexter()) + + // BEGIN: Router m.Get("/", func(ctx *context.Context) { if ctx.User != nil { if ctx.GiteaUser == nil { @@ -53,5 +56,12 @@ func InitRoutes() *macaron.Macaron { m.Group("/gitea", func() { m.Post("/", binding.BindIgnErr(auth.GiteaLoginForm{}), auth.LoginToGitea) }) + m.Get("/repos", reqSignIn, migration.ListRepos) return m } + +func reqSignIn(ctx *context.Context) { + if ctx.User == nil || ctx.GiteaUser == nil { + ctx.Redirect("/") + } +} diff --git a/web/templates/base/head.tmpl b/web/templates/base/head.tmpl index 675b101..1593b63 100644 --- a/web/templates/base/head.tmpl +++ b/web/templates/base/head.tmpl @@ -19,51 +19,10 @@ body > .grid { height: 100%; } - .image { - margin-top: -100px; - } .column { max-width: 450px; } - {{if .Flash.ErrorMsg}} diff --git a/web/templates/dashboard.tmpl b/web/templates/dashboard.tmpl index 89cb76e..c7a0516 100644 --- a/web/templates/dashboard.tmpl +++ b/web/templates/dashboard.tmpl @@ -13,19 +13,19 @@

GitHub connected

- You're logged in as {{.User.Username}}. Not you? + You're logged in as {{template "modules/username" .User}}. Not you?

Gitea connected

- You're logged in as {{.GiteaUser.Username}}. Not you? + You're logged in as {{template "modules/username" .GiteaUser}}. Not you?
- + Migrate Repositories...
diff --git a/web/templates/login_gitea.tmpl b/web/templates/login_gitea.tmpl index b058617..3d0b174 100644 --- a/web/templates/login_gitea.tmpl +++ b/web/templates/login_gitea.tmpl @@ -13,7 +13,7 @@

GitHub connected

- You're logged in as {{.User.Username}}. Not you? + You're logged in as {{template "modules/username" .User}}. Not you?
diff --git a/web/templates/modules/username.tmpl b/web/templates/modules/username.tmpl new file mode 100644 index 0000000..bed0b5e --- /dev/null +++ b/web/templates/modules/username.tmpl @@ -0,0 +1 @@ +{{.Username}} \ No newline at end of file diff --git a/web/templates/repos.tmpl b/web/templates/repos.tmpl new file mode 100644 index 0000000..957d714 --- /dev/null +++ b/web/templates/repos.tmpl @@ -0,0 +1,76 @@ +{{template "base/head" .}} +
+
+

+
+ Migrate Repositories +
+

+
+ Select the repositories you'd like to migrate. +
+
+
+ +
+ {{range .Repos}} +
+ +
+ +
+
+ {{end}} +
+ +
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+ +{{template "base/footer" .}} \ No newline at end of file