Add list repos page

Fix some lint / fmt errors

Signed-off-by: Jonas Franz <info@jonasfranz.software>
web-ui
Jonas Franz 6 years ago
parent 1047ac3bfe
commit 1ecc1f727d
Signed by untrusted user: JonasFranzDEV
GPG Key ID: 506AEEBE80BEDECD
  1. 2
      web/auth/gitea.go
  2. 17
      web/context/context.go
  3. 6
      web/fs.go
  4. 18
      web/migration/repos.go
  5. 10
      web/router.go
  6. 41
      web/templates/base/head.tmpl
  7. 6
      web/templates/dashboard.tmpl
  8. 2
      web/templates/login_gitea.tmpl
  9. 1
      web/templates/modules/username.tmpl
  10. 76
      web/templates/repos.tmpl

@ -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" {

@ -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)
}
}

@ -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)
}

@ -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")
}

@ -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("/")
}
}

@ -19,51 +19,10 @@
body > .grid {
height: 100%;
}
.image {
margin-top: -100px;
}
.column {
max-width: 450px;
}
</style>
<script>
$(document)
.ready(function() {
$('.ui.form')
.form({
fields: {
email: {
identifier : 'email',
rules: [
{
type : 'empty',
prompt : 'Please enter your e-mail'
},
{
type : 'email',
prompt : 'Please enter a valid e-mail'
}
]
},
password: {
identifier : 'password',
rules: [
{
type : 'empty',
prompt : 'Please enter your password'
},
{
type : 'length[6]',
prompt : 'Your password must be at least 6 characters'
}
]
}
}
})
;
})
;
</script>
</head>
<body>
{{if .Flash.ErrorMsg}}

@ -13,19 +13,19 @@
<i class="icon github"></i>
<div class="content">
<h3 class="header">GitHub connected</h3>
You're logged in as <code>{{.User.Username}}</code>. <a href="/logout">Not you?</a>
You're logged in as {{template "modules/username" .User}}. <a href="/logout">Not you?</a>
</div>
</div>
<div class="ui icon attached message">
<i class="icon lock"></i>
<div class="content">
<h3 class="header">Gitea connected</h3>
You're logged in as <code>{{.GiteaUser.Username}}</code>. <a href="/logout">Not you?</a>
You're logged in as {{template "modules/username" .GiteaUser}}. <a href="/logout">Not you?</a>
</div>
</div>
<div class="ui stacked segment">
<button class="ui fluid large green labeld icon button"><i class="icon list"></i> Migrate Repositories...</button>
<a href="/repos" class="ui fluid large green labeld icon button"><i class="icon list"></i> Migrate Repositories...</a>
</div>
</div>
</div>

@ -13,7 +13,7 @@
<i class="icon github"></i>
<div class="content">
<h3 class="header">GitHub connected</h3>
You're logged in as <code>{{.User.Username}}</code>. <a href="/logout">Not you?</a>
You're logged in as {{template "modules/username" .User}}. <a href="/logout">Not you?</a>
</div>
</div>
<div class="ui icon attached message">

@ -0,0 +1 @@
<code>{{.Username}}</code>

@ -0,0 +1,76 @@
{{template "base/head" .}}
<div class="ui middle aligned center aligned grid">
<div class="column">
<h1 class="ui image header">
<div class="content">
Migrate Repositories
</div>
</h1>
<div class="ui message">
Select the repositories you'd like to migrate.
</div>
<div class="ui stacked segment">
<form action="/migrate" method="POST">
<div class="ui horizontal link list">
<a class="item" onclick="$('.repo-toggle').prop('checked', true);">
Select all
</a>
<a class="item" onclick="$('.repo-toggle').prop('checked', false);">
Deselect all
</a>
</div>
<div class="ui relaxed divided list">
{{range .Repos}}
<div class="item">
<i class="large github middle aligned icon"></i>
<div class="content">
<div class="ui left toggle checkbox">
<input checked id="{{.GetFullName}}" class="repo-toggle" name="{{.GetFullName}}" type="checkbox">
<label for="{{.GetFullName}}"><a class="header">{{.GetFullName}}</a></label>
</div>
</div>
</div>
{{end}}
</div>
<button type="button" class="ui fluid large labeled icon button"><i class="icon add"></i> Add other repositories...</button>
<div class="ui divider"></div>
<button type="submit" class="ui fluid large green labeled icon button"><i class="icon exchange"></i> Migrate selected repositories...</button>
</form>
</div>
</div>
</div>
<div id="repo-item" style="display: none !important;">
<div class="item">
<i class="large github middle aligned icon"></i>
<div class="content">
<div class="ui left toggle checkbox">
<input id="{{.GetFullName}}" name="{{.GetFullName}}" type="checkbox">
<label for="{{.GetFullName}}"><a class="header">{{.GetFullName}}</a></label>
</div>
</div>
</div>
</div>
<div class="ui modal" id="add-repos">
<div class="header">Add other repositories...</div>
<div class="content">
<div class="ui small icon message"><i class="icon code"></i>
<div class="content">
Please add all repositories you'd like to add in the box below. Write
each repository in a separate line and split the repository owner and name with a "/".
</div>
</div>
<div class="ui form">
<div class="field">
<label>Line seperated list of repositories</label>
<textarea id="repo-textform" placeholder="go-gitea/gitea&#10;go-gitea/git"></textarea>
</div>
</div>
</div>
<div class="actions">
<div class="ui approve green button">Add</div>
<div class="ui cancel button">Cancel</div>
</div>
</div>
{{template "base/footer" .}}
Loading…
Cancel
Save