qa: Can export to gitlab
This commit is contained in:
parent
0a22d09947
commit
60a34d3ced
13 changed files with 463 additions and 3 deletions
97
qa/api/admin.go
Normal file
97
qa/api/admin.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var adminLink string
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&adminLink, "admin-link", adminLink, "URL to admin interface, to use its features as replacement")
|
||||
}
|
||||
|
||||
func fwdAdmin(method, urlpath string, body io.Reader, out interface{}) error {
|
||||
u, err := url.Parse(adminLink)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var user, pass string
|
||||
if u.User != nil {
|
||||
user = u.User.Username()
|
||||
pass, _ = u.User.Password()
|
||||
u.User = nil
|
||||
}
|
||||
|
||||
u.Path = path.Join(u.Path, urlpath)
|
||||
|
||||
r, err := http.NewRequest(method, u.String(), body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(user) != 0 || len(pass) != 0 {
|
||||
r.SetBasicAuth(user, pass)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
return dec.Decode(&out)
|
||||
}
|
||||
|
||||
func fwdAdminRequest(c *gin.Context, urlpath string) {
|
||||
u, err := url.Parse(adminLink)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var user, pass string
|
||||
if u.User != nil {
|
||||
user = u.User.Username()
|
||||
pass, _ = u.User.Password()
|
||||
u.User = nil
|
||||
}
|
||||
|
||||
if len(urlpath) > 0 {
|
||||
u.Path = path.Join(u.Path, urlpath)
|
||||
} else {
|
||||
u.Path = path.Join(u.Path, c.Request.URL.Path)
|
||||
}
|
||||
|
||||
r, err := http.NewRequest(c.Request.Method, u.String(), c.Request.Body)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if len(user) != 0 || len(pass) != 0 {
|
||||
r.SetBasicAuth(user, pass)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadGateway, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
headers := map[string]string{}
|
||||
for key := range resp.Header {
|
||||
headers[key] = resp.Header.Get(key)
|
||||
}
|
||||
|
||||
c.DataFromReader(resp.StatusCode, resp.ContentLength, resp.Header.Get("Content-Type"), resp.Body, headers)
|
||||
}
|
Reference in a new issue