2021-10-26 13:49:08 +05:30
|
|
|
package routers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-27 12:56:00 +05:30
|
|
|
"fmt"
|
2021-11-01 10:53:41 +05:30
|
|
|
"io"
|
|
|
|
"io/fs"
|
2021-10-26 13:49:08 +05:30
|
|
|
"net/http"
|
2021-11-01 10:53:41 +05:30
|
|
|
"path/filepath"
|
2021-10-26 13:49:08 +05:30
|
|
|
"strconv"
|
|
|
|
|
2021-10-27 12:56:00 +05:30
|
|
|
"gitea.com/gitea/pr-deployer/pkgs/services"
|
|
|
|
"gitea.com/gitea/pr-deployer/pkgs/settings"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
|
|
|
"gitea.com/go-chi/session"
|
2021-11-15 18:57:14 +05:30
|
|
|
logger "github.com/chi-middleware/logrus-logger"
|
2021-10-26 13:49:08 +05:30
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/google/go-github/v39/github"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/unrolled/render"
|
|
|
|
)
|
|
|
|
|
|
|
|
var rnd *render.Render
|
|
|
|
|
2021-11-01 10:53:41 +05:30
|
|
|
type tmplFS struct {
|
|
|
|
fs.FS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tfs tmplFS) Walk(root string, walkFn filepath.WalkFunc) error {
|
|
|
|
return fs.WalkDir(tfs, root, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
info, err := d.Info()
|
|
|
|
return walkFn(path, info, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tfs tmplFS) ReadFile(filename string) ([]byte, error) {
|
|
|
|
f, err := tfs.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
return io.ReadAll(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertFS(templateFS fs.FS) render.FileSystem {
|
|
|
|
return tmplFS{templateFS}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Web(publicFS, templateFS fs.FS) {
|
2021-10-27 12:56:00 +05:30
|
|
|
rnd = render.New(render.Options{
|
2021-11-01 10:53:41 +05:30
|
|
|
Directory: ".",
|
2021-10-27 12:56:00 +05:30
|
|
|
IsDevelopment: true,
|
2021-11-01 10:53:41 +05:30
|
|
|
FileSystem: convertFS(templateFS),
|
2021-10-27 12:56:00 +05:30
|
|
|
})
|
2021-10-26 13:49:08 +05:30
|
|
|
|
|
|
|
c := chi.NewRouter()
|
2021-10-27 12:56:00 +05:30
|
|
|
c.Use(session.Sessioner())
|
2021-11-15 18:57:14 +05:30
|
|
|
c.Use(logger.Logger("router", log.StandardLogger()))
|
2021-10-27 12:56:00 +05:30
|
|
|
|
|
|
|
c.Get("/", Home)
|
|
|
|
c.Get("/callback", OAuth2Callback)
|
2021-10-26 13:49:08 +05:30
|
|
|
c.Get("/prs", ListPRs)
|
|
|
|
c.Post("/pr/{index}/run", RunPR)
|
|
|
|
c.Post("/pr/{index}/stop", StopPR)
|
|
|
|
c.Post("/webhook", Webhook)
|
2021-11-01 10:53:41 +05:30
|
|
|
|
|
|
|
fs := http.StripPrefix("/public", http.FileServer(http.FS(publicFS)))
|
|
|
|
|
2021-10-28 21:10:41 +05:30
|
|
|
c.Get("/public/*", func(w http.ResponseWriter, r *http.Request) {
|
2021-11-01 10:53:41 +05:30
|
|
|
fs.ServeHTTP(w, r)
|
2021-10-28 21:10:41 +05:30
|
|
|
})
|
2021-10-27 12:56:00 +05:30
|
|
|
|
2021-10-26 13:49:08 +05:30
|
|
|
http.ListenAndServe(":3001", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListPRs(w http.ResponseWriter, r *http.Request) {
|
2021-10-27 12:56:00 +05:30
|
|
|
sess := session.GetSession(r)
|
|
|
|
pToken := sess.Get("token")
|
|
|
|
if pToken == nil {
|
|
|
|
http.Redirect(w, r, "/", 302)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:08 +05:30
|
|
|
p, _ := strconv.Atoi(r.FormValue("p"))
|
|
|
|
if p < 1 {
|
|
|
|
p = 1
|
|
|
|
}
|
2021-10-27 12:56:00 +05:30
|
|
|
|
|
|
|
token := pToken.(*oauth2.Token)
|
2021-10-28 21:10:41 +05:30
|
|
|
fmt.Printf("%#v\n", token)
|
|
|
|
c, err := services.NewClient(token)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pulls, err := c.GetPullRequests(p)
|
2021-10-26 13:49:08 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:56:00 +05:30
|
|
|
user := sess.Get("user")
|
|
|
|
|
2021-10-26 13:49:08 +05:30
|
|
|
if err := rnd.HTML(w, http.StatusOK, "pulls", map[string]interface{}{
|
|
|
|
"pulls": pulls,
|
2021-10-27 12:56:00 +05:30
|
|
|
"user": user,
|
2021-10-26 13:49:08 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunPR(w http.ResponseWriter, r *http.Request) {
|
|
|
|
idx := chi.URLParam(r, "index")
|
|
|
|
i, _ := strconv.Atoi(idx)
|
|
|
|
if i <= 0 {
|
2021-10-28 21:10:41 +05:30
|
|
|
log.Errorf("start failed: %s is not a valid index", idx)
|
2021-10-26 13:49:08 +05:30
|
|
|
return
|
|
|
|
}
|
2021-10-27 12:56:00 +05:30
|
|
|
ctx := context.Background()
|
2021-10-28 21:10:41 +05:30
|
|
|
|
|
|
|
sess := session.GetSession(r)
|
|
|
|
pToken := sess.Get("token")
|
|
|
|
if pToken == nil {
|
|
|
|
http.Redirect(w, r, "/", 302)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token := pToken.(*oauth2.Token)
|
|
|
|
fmt.Printf("%#v\n", token)
|
|
|
|
client, err := services.NewClient(token)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := services.UpdateAndStartPullRequest(ctx, client, i, ""); err != nil {
|
|
|
|
log.Error(err)
|
2021-10-26 13:49:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopPR(w http.ResponseWriter, r *http.Request) {
|
|
|
|
idx := chi.URLParam(r, "index")
|
|
|
|
i, _ := strconv.Atoi(idx)
|
|
|
|
if i <= 0 {
|
|
|
|
log.Error("stop failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := services.StopPullRequest(i); err != nil {
|
|
|
|
log.Error("stop failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Webhook(w http.ResponseWriter, r *http.Request) {
|
|
|
|
payload, err := github.ValidatePayload(r, settings.WebhookSecretKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
event, err := github.ParseWebHook(github.WebHookType(r), payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch evt := event.(type) {
|
|
|
|
case *github.PullRequestEvent:
|
2021-10-27 12:56:00 +05:30
|
|
|
switch *evt.Action {
|
|
|
|
case "synchronize":
|
|
|
|
ctx := context.Background()
|
2021-10-28 21:10:41 +05:30
|
|
|
// FIXME: get token
|
|
|
|
client, err := services.NewClient(nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := services.UpdateAndStartPullRequest(ctx, client, *evt.Number, *evt.After); err != nil {
|
2021-10-27 12:56:00 +05:30
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Trace("pull request %d is %s", *evt.Number, *evt.Action)
|
2021-10-26 13:49:08 +05:30
|
|
|
}
|
2021-10-27 12:56:00 +05:30
|
|
|
|
2021-10-26 13:49:08 +05:30
|
|
|
default:
|
|
|
|
log.Warn("received %v type event, ignored", event)
|
|
|
|
}
|
|
|
|
}
|