From 9d872dcf755c44acf8f5b12b3ab378e85986f288 Mon Sep 17 00:00:00 2001
From: Jonas Franz <info@jonasfranz.software>
Date: Thu, 26 Jul 2018 15:16:12 +0200
Subject: [PATCH] Final changes

Signed-off-by: Jonas Franz <info@jonasfranz.software>
---
 .drone.yml                              | 15 ++++--
 Dockerfile                              |  2 +-
 Dockerfile.web                          | 23 ++++++++++
 Makefile                                | 39 ++++++++++++++--
 README.md                               | 61 ++++++++++++++++++++++++-
 cmd/web.go                              | 12 ++++-
 config.example.yml                      |  9 +++-
 config/config.go                        |  1 -
 web/context/context.go                  |  4 ++
 web/templates/base/head.tmpl            |  6 +--
 web/templates/status/403.tmpl           | 14 ++++++
 web/templates/status/404.tmpl           | 14 ++++++
 web/templates/status/500.tmpl           | 14 ++++++
 web/templates/status/unknown_error.tmpl | 14 ++++++
 14 files changed, 210 insertions(+), 18 deletions(-)
 create mode 100644 Dockerfile.web
 create mode 100644 web/templates/status/403.tmpl
 create mode 100644 web/templates/status/404.tmpl
 create mode 100644 web/templates/status/500.tmpl
 create mode 100644 web/templates/status/unknown_error.tmpl

diff --git a/.drone.yml b/.drone.yml
index 1a1ddf3..75d3eb7 100644
--- a/.drone.yml
+++ b/.drone.yml
@@ -25,7 +25,7 @@ pipeline:
     environment:
       GOPATH: /go
     commands:
-      - make test
+      - make test build
   static:
     image: golang:1.10
     pull: true
@@ -70,11 +70,20 @@ pipeline:
     image: plugins/docker:17.12
     secrets: [ docker_username, docker_password ]
     pull: true
-    repo: jonasfranz/gitea-github-migrator
+    repo: ggmigrator/cli
+    default_tags: true
+    when:
+      event: [ push, tag ]
+
+  docker-web:
+    image: plugins/docker:17.12
+    secrets: [ docker_username, docker_password ]
+    pull: true
+    dockerfile: Dockerfile.web
+    repo: ggmigrator/web
     default_tags: true
     when:
       event: [ push, tag ]
-      branch: [ master ]
   s3:
     image: plugins/s3:1
     pull: true
diff --git a/Dockerfile b/Dockerfile
index 75344ba..91473ad 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -2,7 +2,7 @@
 FROM golang:1.10-alpine3.7 AS build-env
 
 #Build deps
-RUN apk --no-cache add build-base git
+RUN apk --no-cache add build-base git ca-certificates
 
 #Setup repo
 COPY . ${GOPATH}/src/git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator
diff --git a/Dockerfile.web b/Dockerfile.web
new file mode 100644
index 0000000..aa18655
--- /dev/null
+++ b/Dockerfile.web
@@ -0,0 +1,23 @@
+#Build stage
+FROM golang:1.10-alpine3.7 AS build-env
+
+#Build deps
+RUN apk --no-cache add build-base git ca-certificates
+
+#Setup repo
+COPY . ${GOPATH}/src/git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator
+WORKDIR ${GOPATH}/src/git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator
+
+RUN make clean docker-binary-web
+
+FROM alpine:3.7
+LABEL maintainer="info@jonasfranz.software"
+
+COPY --from=build-env /go/src/git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/gitea-github-migrator /usr/local/bin/gitea-github-migrator
+
+VOLUME "/data"
+VOLUME "/usr/local/bin/data"
+
+COPY --from=build-env /go/src/git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/config.example.yml /data/config.example.yml
+
+ENTRYPOINT ["/usr/local/bin/gitea-github-migrator", "web", "-c", "/data/config.yml"]
diff --git a/Makefile b/Makefile
index 18315b2..329c37d 100644
--- a/Makefile
+++ b/Makefile
@@ -17,19 +17,50 @@ LDFLAGS := -X main.version=$(VERSION) -X main.build=$(DRONE_BUILD_NUMBER)
 .PHONY: all
 all:
 
-.PHONY: docker-binary
-docker-binary:
+.PHONY: build
 	go build -ldflags "$(LDFLAGS)" -o gitea-github-migrator
 
-.PHONY: docker-binary-web
+.PHONY: build-binary-web
+build-binary-web:
 	go build -ldflags "$(LDFLAGS)" -tags web -o gitea-github-migrator
 
+.PHONY: build-web
+build-web: packr build-binary-web packr-clean
+
+.PHONY: packr
+packr:
+	@hash packr > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
+		$(GO) get -u github.com/gobuffalo/packr/...; \
+	fi
+	packr -z
+
+.PHONY: packr-clean
+packr-clean:
+	@hash packr > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
+		$(GO) get -u github.com/gobuffalo/packr/...; \
+	fi
+	packr clean
+
+.PHONY: clean
+clean: packr-clean
+	go clean ./...
+
+.PHONY: docker-binary
+docker-binary: build
+
+.PHONY: docker-binary-web
+docker-binary-web: build-web
+
+
 .PHONY: generate-release-file
 generate-release-file:
 	echo $(VERSION) > .version
 
 .PHONY: release
-release:
+release: packr release-builds packr-clean
+
+.PHONY: release-builds
+release-builds:
 	@hash gox > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
 		$(GO) get -u github.com/mitchellh/gox; \
 	fi
diff --git a/README.md b/README.md
index a0b2ad6..f375373 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,8 @@
 
 [![Build Status](https://drone.jonasfranz.software/api/badges/JonasFranzDEV/gitea-github-migrator/status.svg)](https://drone.jonasfranz.software/JonasFranzDEV/gitea-github-migrator)
 [![Latest Release](https://img.shields.io/badge/dynamic/json.svg?label=release&url=https%3A%2F%2Fgit.jonasfranz.software%2Fapi%2Fv1%2Frepos%2FJonasFranzDEV%2Fgitea-github-migrator%2Freleases&query=%24%5B0%5D.tag_name)](https://git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/releases)
-[![Docker Pulls](https://img.shields.io/docker/pulls/jonasfranz/gitea-github-migrator.svg)](https://hub.docker.com/r/jonasfranz/gitea-github-migrator/)
+[![Docker Pulls](https://img.shields.io/docker/pulls/ggmigrator/cli.svg)](https://hub.docker.com/r/ggmigrator/cli/)
+[![Docker Pulls](https://img.shields.io/docker/pulls/ggmigrator/web.svg)](https://hub.docker.com/r/ggmigrator/web/)
 
 A tool to migrate [GitHub](https://github.com) Repositories to [Gitea](https://gitea.io) including all issues, labels, milestones
 and comments.
@@ -28,8 +29,11 @@ Migrates:
 go get git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator
 cd $GOPATH/src/git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator
 dep ensure
-go install
+make build
 ```
+#### Web Support
+
+Run `make web-build` instead of `make build` to include web support.
 
 ### From Binary
 We provide binaries of master builds and all releases at our [minio storage server](https://storage.h.jonasfranz.software/minio/gitea-github-migrator/dist/).
@@ -37,8 +41,28 @@ We provide binaries of master builds and all releases at our [minio storage serv
 Additionally we provide them for every release as release attachment under [releases](https://git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/releases).
 
 You don't need any dependencies except the binary to run the migrator.
+
+These binaries include web support by default.
+
+### From Docker image
+
+We provide a [cli docker image](https://hub.docker.com/r/ggmigrator/cli/):
+
+For master builds:
+```docker
+docker run ggmigrator/cli:latest
+```
+
+For release builds:
+```docker
+docker run ggmigrator/cli:0.0.9
+```
+
+
 ## Usage
 
+### Command line
+
 Migrate one repository:
 
 ```bash
@@ -75,6 +99,39 @@ gitea-github-migrator migrate-all \
     --only-repo
 ```
 
+### Web interface
+
+Since 0.1.0 gitea-github-migrator comes with an integrated web interface.
+Follow these steps to get the web interface running:
+
+1. Download or build a web-capable binary of the gitea-github-migrator. The builds on our storage server are build with web support included.
+If you build from source, please follow [web support](#web-support).
+2. Create `config.yml` file and change the properties according to your wishes. Please keep in mind that
+you have to create a GitHub OAuth application to make the web interface work.
+3. Run `./gitea-github-migrator web`
+4. Visit `http://localhost:4000`
+
+#### Docker
+
+We're providing a docker image with web support. To start the web service please run:
+```docker
+docker run ggmigrator/web -p 4000:4000 -v data/:/data
+```
+Place your `config.yml` into `data/config.yml`.
+
+#### Config
+Example:
+```yaml
+# GitHub contains the OAuth2 application data obtainable from GitHub
+GitHub:
+  client_id: GITHUB_OAUTH_CLIENT_ID
+  client_secret: GITHUB_OAUTH_SECRET
+# Web contains the configuration for the integrated web server
+Web:
+  port: 4000
+  host: 0.0.0.0
+```
+
 ## Problems
 
 - This migration tool does not work with Gitea instances using a SQLite database.
diff --git a/cmd/web.go b/cmd/web.go
index 611804c..43fd810 100644
--- a/cmd/web.go
+++ b/cmd/web.go
@@ -18,10 +18,18 @@ var CmdWeb = cli.Command{
 	Name:   "web",
 	Usage:  "Starts the web interface",
 	Action: runWeb,
+	Flags: []cli.Flag{
+		cli.StringFlag{
+			Name:   "c,config",
+			Usage:  "config file",
+			Value:  "config.yml",
+			EnvVar: "MIGRATOR_CONFIG",
+		},
+	},
 }
 
-func runWeb(_ *cli.Context) error {
-	if err := configor.New(&configor.Config{ErrorOnUnmatchedKeys: true}).Load(&config.Config, "config.yml"); err != nil {
+func runWeb(ctx *cli.Context) error {
+	if err := configor.New(&configor.Config{ErrorOnUnmatchedKeys: true}).Load(&config.Config, ctx.String("config")); err != nil {
 		return err
 	}
 	r := web.InitRoutes()
diff --git a/config.example.yml b/config.example.yml
index 8df5265..78a3c80 100644
--- a/config.example.yml
+++ b/config.example.yml
@@ -1,3 +1,8 @@
+# GitHub contains the OAuth2 application data obtainable from GitHub
 GitHub:
-  client_id: heöll
-  client_secret: secret
\ No newline at end of file
+  client_id: GITHUB_OAUTH_CLIENT_ID
+  client_secret: GITHUB_OAUTH_SECRET
+# Web contains the configuration for the integrated web server
+Web:
+  port: 4000
+  host: 0.0.0.0
\ No newline at end of file
diff --git a/config/config.go b/config/config.go
index 2945105..9893b22 100644
--- a/config/config.go
+++ b/config/config.go
@@ -10,5 +10,4 @@ var Config = struct {
 		Host string `yaml:"host"`
 		Port int    `yaml:"port"`
 	}
-	Secret string `required:"true"`
 }{}
diff --git a/web/context/context.go b/web/context/context.go
index 0d8379d..96ee201 100644
--- a/web/context/context.go
+++ b/web/context/context.go
@@ -10,6 +10,7 @@ import (
 	"git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/migrations"
 	"github.com/go-macaron/session"
 	"github.com/google/go-github/github"
+	"github.com/sirupsen/logrus"
 	"golang.org/x/oauth2"
 	"gopkg.in/macaron.v1"
 )
@@ -56,9 +57,12 @@ func (ctx *Context) Handle(status int, title string, err error) {
 			ctx.Data["ErrorMsg"] = err
 		}
 	}
+	logrus.Warnf("Handle: %v", err)
 	ctx.Data["ErrTitle"] = title
 
 	switch status {
+	case 403:
+		ctx.Data["Title"] = "Access denied"
 	case 404:
 		ctx.Data["Title"] = "Page not found"
 	case 500:
diff --git a/web/templates/base/head.tmpl b/web/templates/base/head.tmpl
index 032cfbb..06189d8 100644
--- a/web/templates/base/head.tmpl
+++ b/web/templates/base/head.tmpl
@@ -9,9 +9,9 @@
     <!-- Site Properties -->
     <title>Gitea Migrator</title>
 
-    <script src="static/js/jquery-3.1.1.min.js"></script>
-    <script src="static/js/semantic.min.js"></script>
-    <link href="static/css/semantic.min.css" rel="stylesheet">
+    <script src="/static/js/jquery-3.1.1.min.js"></script>
+    <script src="/static/js/semantic.min.js"></script>
+    <link href="/static/css/semantic.min.css" rel="stylesheet">
     <style type="text/css">
         body {
             background-color: #DADADA;
diff --git a/web/templates/status/403.tmpl b/web/templates/status/403.tmpl
new file mode 100644
index 0000000..1613b4d
--- /dev/null
+++ b/web/templates/status/403.tmpl
@@ -0,0 +1,14 @@
+{{template "base/head" .}}
+<div class="ui middle aligned center aligned grid">
+    <div class="column">
+        <h1 class="ui image header">
+            <div class="content">
+                Access denied
+            </div>
+        </h1>
+        <div class="ui error message">
+            {{.ErrTitle}}
+        </div>
+    </div>
+</div>
+{{template "base/footer" .}}
\ No newline at end of file
diff --git a/web/templates/status/404.tmpl b/web/templates/status/404.tmpl
new file mode 100644
index 0000000..052fc40
--- /dev/null
+++ b/web/templates/status/404.tmpl
@@ -0,0 +1,14 @@
+{{template "base/head" .}}
+<div class="ui middle aligned center aligned grid">
+    <div class="column">
+        <h1 class="ui image header">
+            <div class="content">
+                Error 404 - Not found
+            </div>
+        </h1>
+        <div class="ui error message">
+            {{.ErrTitle}}
+        </div>
+    </div>
+</div>
+{{template "base/footer" .}}
\ No newline at end of file
diff --git a/web/templates/status/500.tmpl b/web/templates/status/500.tmpl
new file mode 100644
index 0000000..4c907fb
--- /dev/null
+++ b/web/templates/status/500.tmpl
@@ -0,0 +1,14 @@
+{{template "base/head" .}}
+<div class="ui middle aligned center aligned grid">
+    <div class="column">
+        <h1 class="ui image header">
+            <div class="content">
+                Error 500 - Internal Server Error
+            </div>
+        </h1>
+        <div class="ui error message">
+        {{.ErrTitle}}
+        </div>
+    </div>
+</div>
+{{template "base/footer" .}}
\ No newline at end of file
diff --git a/web/templates/status/unknown_error.tmpl b/web/templates/status/unknown_error.tmpl
new file mode 100644
index 0000000..01a2764
--- /dev/null
+++ b/web/templates/status/unknown_error.tmpl
@@ -0,0 +1,14 @@
+{{template "base/head" .}}
+<div class="ui middle aligned center aligned grid">
+    <div class="column">
+        <h1 class="ui image header">
+            <div class="content">
+                Unknown Error
+            </div>
+        </h1>
+        <div class="ui error message">
+        {{.ErrTitle}}
+        </div>
+    </div>
+</div>
+{{template "base/footer" .}}
\ No newline at end of file