some changes ;]]]]]

This commit is contained in:
Aleksander Cynarski 2020-01-15 20:40:53 +01:00
parent f24c030d8f
commit ab802985e9
12 changed files with 449 additions and 204 deletions

256
Godeps/Godeps.json generated
View File

@ -1,130 +1,130 @@
{ {
"ImportPath": "gitea-issue", "ImportPath": "gitea-issue",
"GoVersion": "go1.13", "GoVersion": "go1.13",
"GodepVersion": "v80", "GodepVersion": "v80",
"Deps": [ "Deps": [
{ {
"ImportPath": "code.gitea.io/sdk/gitea", "ImportPath": "code.gitea.io/sdk/gitea",
"Rev": "b374d7ccc4b455f1e1829ab3ff0808de80bc39e0" "Rev": "b374d7ccc4b455f1e1829ab3ff0808de80bc39e0"
}, },
{ {
"ImportPath": "github.com/caarlos0/env", "ImportPath": "github.com/caarlos0/env",
"Comment": "v6.1.0-4-g6cef6a7", "Comment": "v6.1.0-4-g6cef6a7",
"Rev": "6cef6a7f8b01a4288118f1a4c5c303f117c1c7f0" "Rev": "6cef6a7f8b01a4288118f1a4c5c303f117c1c7f0"
}, },
{ {
"ImportPath": "github.com/gin-contrib/cors", "ImportPath": "github.com/gin-contrib/cors",
"Comment": "v1.3.0-1-g2a8f489", "Comment": "v1.3.0-1-g2a8f489",
"Rev": "2a8f489b42294786cbf465c1f20588b49117f1fe" "Rev": "2a8f489b42294786cbf465c1f20588b49117f1fe"
}, },
{ {
"ImportPath": "github.com/gin-contrib/sse", "ImportPath": "github.com/gin-contrib/sse",
"Comment": "v0.1.0-1-g43f0f29", "Comment": "v0.1.0-1-g43f0f29",
"Rev": "43f0f29dbd2b3f0feeebff91fd9cda22db296938" "Rev": "43f0f29dbd2b3f0feeebff91fd9cda22db296938"
}, },
{ {
"ImportPath": "github.com/gin-gonic/gin", "ImportPath": "github.com/gin-gonic/gin",
"Comment": "v1.4.0-57-g517eacb", "Comment": "v1.4.0-57-g517eacb",
"Rev": "517eacb4f9ca7276511841c63e2911d6ec94c22a" "Rev": "517eacb4f9ca7276511841c63e2911d6ec94c22a"
}, },
{ {
"ImportPath": "github.com/gin-gonic/gin/binding", "ImportPath": "github.com/gin-gonic/gin/binding",
"Comment": "v1.4.0-57-g517eacb", "Comment": "v1.4.0-57-g517eacb",
"Rev": "517eacb4f9ca7276511841c63e2911d6ec94c22a" "Rev": "517eacb4f9ca7276511841c63e2911d6ec94c22a"
}, },
{ {
"ImportPath": "github.com/gin-gonic/gin/internal/json", "ImportPath": "github.com/gin-gonic/gin/internal/json",
"Comment": "v1.4.0-57-g517eacb", "Comment": "v1.4.0-57-g517eacb",
"Rev": "517eacb4f9ca7276511841c63e2911d6ec94c22a" "Rev": "517eacb4f9ca7276511841c63e2911d6ec94c22a"
}, },
{ {
"ImportPath": "github.com/gin-gonic/gin/render", "ImportPath": "github.com/gin-gonic/gin/render",
"Comment": "v1.4.0-57-g517eacb", "Comment": "v1.4.0-57-g517eacb",
"Rev": "517eacb4f9ca7276511841c63e2911d6ec94c22a" "Rev": "517eacb4f9ca7276511841c63e2911d6ec94c22a"
}, },
{ {
"ImportPath": "github.com/go-playground/locales", "ImportPath": "github.com/go-playground/locales",
"Comment": "v0.13.0", "Comment": "v0.13.0",
"Rev": "9f105231d3a5f6877a2bf8321dfa15ea6f844b1b" "Rev": "9f105231d3a5f6877a2bf8321dfa15ea6f844b1b"
}, },
{ {
"ImportPath": "github.com/go-playground/locales/currency", "ImportPath": "github.com/go-playground/locales/currency",
"Comment": "v0.13.0", "Comment": "v0.13.0",
"Rev": "9f105231d3a5f6877a2bf8321dfa15ea6f844b1b" "Rev": "9f105231d3a5f6877a2bf8321dfa15ea6f844b1b"
}, },
{ {
"ImportPath": "github.com/go-playground/universal-translator", "ImportPath": "github.com/go-playground/universal-translator",
"Comment": "v0.16.0-1-g7120149", "Comment": "v0.16.0-1-g7120149",
"Rev": "71201497bace774495daed26a3874fd339e0b538" "Rev": "71201497bace774495daed26a3874fd339e0b538"
}, },
{ {
"ImportPath": "github.com/golang/protobuf/proto", "ImportPath": "github.com/golang/protobuf/proto",
"Comment": "v1.3.2-4-ged6926b3", "Comment": "v1.3.2-4-ged6926b3",
"Rev": "ed6926b37a637426117ccab59282c3839528a700" "Rev": "ed6926b37a637426117ccab59282c3839528a700"
}, },
{ {
"ImportPath": "github.com/jinzhu/gorm", "ImportPath": "github.com/jinzhu/gorm",
"Comment": "v1.9.11-20-g179760d", "Comment": "v1.9.11-20-g179760d",
"Rev": "179760d8343ed15cbb9f071ca824bc35ee7784bb" "Rev": "179760d8343ed15cbb9f071ca824bc35ee7784bb"
}, },
{ {
"ImportPath": "github.com/jinzhu/gorm/dialects/sqlite", "ImportPath": "github.com/jinzhu/gorm/dialects/sqlite",
"Comment": "v1.9.11-20-g179760d", "Comment": "v1.9.11-20-g179760d",
"Rev": "179760d8343ed15cbb9f071ca824bc35ee7784bb" "Rev": "179760d8343ed15cbb9f071ca824bc35ee7784bb"
}, },
{ {
"ImportPath": "github.com/jinzhu/inflection", "ImportPath": "github.com/jinzhu/inflection",
"Comment": "v1.0.0", "Comment": "v1.0.0",
"Rev": "f5c5f50e6090ae76a29240b61ae2a90dd810112e" "Rev": "f5c5f50e6090ae76a29240b61ae2a90dd810112e"
}, },
{ {
"ImportPath": "github.com/json-iterator/go", "ImportPath": "github.com/json-iterator/go",
"Comment": "1.1.8-2-gdc11f49", "Comment": "1.1.8-2-gdc11f49",
"Rev": "dc11f49689fd1c9a6de20749def70bd889bf0d42" "Rev": "dc11f49689fd1c9a6de20749def70bd889bf0d42"
}, },
{ {
"ImportPath": "github.com/leodido/go-urn", "ImportPath": "github.com/leodido/go-urn",
"Comment": "v1.2.0", "Comment": "v1.2.0",
"Rev": "a0f5013415294bb94553821ace21a1a74c0298cc" "Rev": "a0f5013415294bb94553821ace21a1a74c0298cc"
}, },
{ {
"ImportPath": "github.com/mattn/go-isatty", "ImportPath": "github.com/mattn/go-isatty",
"Comment": "v0.0.10-1-g0e9ddb7", "Comment": "v0.0.10-1-g0e9ddb7",
"Rev": "0e9ddb7c0c0aef74fa25eaba4141e6b5ab7aca2a" "Rev": "0e9ddb7c0c0aef74fa25eaba4141e6b5ab7aca2a"
}, },
{ {
"ImportPath": "github.com/mattn/go-sqlite3", "ImportPath": "github.com/mattn/go-sqlite3",
"Comment": "v1.11.0-15-g3f45aef", "Comment": "v1.11.0-15-g3f45aef",
"Rev": "3f45aefa8dc8bbdd97619dd2765f864972f3310e" "Rev": "3f45aefa8dc8bbdd97619dd2765f864972f3310e"
}, },
{ {
"ImportPath": "github.com/modern-go/concurrent", "ImportPath": "github.com/modern-go/concurrent",
"Comment": "1.0.3", "Comment": "1.0.3",
"Rev": "bacd9c7ef1dd9b15be4a9909b8ac7a4e313eec94" "Rev": "bacd9c7ef1dd9b15be4a9909b8ac7a4e313eec94"
}, },
{ {
"ImportPath": "github.com/modern-go/reflect2", "ImportPath": "github.com/modern-go/reflect2",
"Comment": "v1.0.1", "Comment": "v1.0.1",
"Rev": "94122c33edd36123c84d5368cfb2b69df93a0ec8" "Rev": "94122c33edd36123c84d5368cfb2b69df93a0ec8"
}, },
{ {
"ImportPath": "github.com/ugorji/go/codec", "ImportPath": "github.com/ugorji/go/codec",
"Comment": "codec/codecgen/v1.1.7-29-g42bc974", "Comment": "codec/codecgen/v1.1.7-29-g42bc974",
"Rev": "42bc974514ff101a54c6b72a0e4dee29d96c0b26" "Rev": "42bc974514ff101a54c6b72a0e4dee29d96c0b26"
}, },
{ {
"ImportPath": "golang.org/x/sys/unix", "ImportPath": "golang.org/x/sys/unix",
"Rev": "f43be2a4598cf3a47be9f94f0c28197ed9eae611" "Rev": "f43be2a4598cf3a47be9f94f0c28197ed9eae611"
}, },
{ {
"ImportPath": "gopkg.in/go-playground/validator.v9", "ImportPath": "gopkg.in/go-playground/validator.v9",
"Comment": "v9.30.0", "Comment": "v9.30.0",
"Rev": "cd1bd581694a11c089aea885a26247c5f783a4cf" "Rev": "cd1bd581694a11c089aea885a26247c5f783a4cf"
}, },
{ {
"ImportPath": "gopkg.in/yaml.v2", "ImportPath": "gopkg.in/yaml.v2",
"Comment": "v2.2.4", "Comment": "v2.2.4",
"Rev": "f221b8435cfb71e54062f6c6e99e9ade30b124d5" "Rev": "f221b8435cfb71e54062f6c6e99e9ade30b124d5"
} }
] ]
} }

View File

@ -2,6 +2,7 @@ package controller
import ( import (
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"fmt"
"gitea-issue/giteaClient" "gitea-issue/giteaClient"
"gitea-issue/model" "gitea-issue/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -9,11 +10,11 @@ import (
"github.com/savaki/swag/swagger" "github.com/savaki/swag/swagger"
"net/http" "net/http"
"strconv" "strconv"
"fmt"
) )
func PostIssue(c *gin.Context) { func PostIssue(c *gin.Context) {
var issueProxy model.PostCreateIssueProxy var issueProxy model.PostCreateIssueProxy
if err := c.BindJSON(&issueProxy); err != nil { if err := c.BindJSON(&issueProxy); err != nil {
c.AsciiJSON(http.StatusBadRequest, gin.H{ c.AsciiJSON(http.StatusBadRequest, gin.H{
"message": "CREATE_ISSUE_ERROR", "message": "CREATE_ISSUE_ERROR",
@ -22,6 +23,10 @@ func PostIssue(c *gin.Context) {
return return
} }
sudo := c.GetHeader("X-Sudo")
if (sudo != "") {
giteaClient.SudoUser(sudo)
}
giteaIssue, err := issueProxy.TransformToGiteaCreateIssueOption() giteaIssue, err := issueProxy.TransformToGiteaCreateIssueOption()
if err != nil { if err != nil {
c.AsciiJSON(http.StatusBadRequest, gin.H{ c.AsciiJSON(http.StatusBadRequest, gin.H{
@ -39,16 +44,16 @@ func PostIssue(c *gin.Context) {
}) })
return return
} }
c.AsciiJSON(http.StatusOK, response) c.AsciiJSON(http.StatusOK, model.IssueTransformFromGitea(response))
} }
func PostIssueSwagger() (*swagger.Endpoint){ func PostIssueSwagger() (*swagger.Endpoint) {
return endpoint.New("post", "/issues", "Create issue", return endpoint.New("post", "/issues", "Create issue",
endpoint.Handler(PostIssue), endpoint.Handler(PostIssue),
endpoint.Description("Post new issue"), endpoint.Description("Post new issue"),
endpoint.Body(model.PostCreateIssueProxy{}, "Issue object", true), endpoint.Body(model.PostCreateIssueProxy{}, "Issue object", true),
endpoint.Tags("issues"), endpoint.Tags("issues"),
endpoint.Response(http.StatusOK, gitea.Issue{}, "Gitea Issue list"), endpoint.Response(http.StatusOK, model.GetCreateIssueProxy{}, "Gitea Issue list"),
) )
} }
@ -60,16 +65,17 @@ func GetIssues(c *gin.Context) {
proxyIssues = append(proxyIssues, model.IssueTransformFromGitea(issue)) proxyIssues = append(proxyIssues, model.IssueTransformFromGitea(issue))
} }
if(err != nil){ if (err != nil) {
c.AbortWithStatus(http.StatusNotFound) c.AbortWithStatus(http.StatusNotFound)
} }
c.AsciiJSON(http.StatusOK, proxyIssues) c.AsciiJSON(http.StatusOK, proxyIssues)
} }
func GetIssuesSwagger() (*swagger.Endpoint){ func GetIssuesSwagger() (*swagger.Endpoint) {
return endpoint.New("get", "/issues", "List project issues", return endpoint.New("get", "/issues", "List project issues",
endpoint.Handler(GetIssues), endpoint.Handler(GetIssues),
endpoint.Description("Get all issues"), endpoint.Description("Get all issues"),
endpoint.Security("token", "USER"),
endpoint.Tags("issues"), endpoint.Tags("issues"),
endpoint.Response(http.StatusOK, []model.GetCreateIssueProxy{}, "Gitea Issue list"), endpoint.Response(http.StatusOK, []model.GetCreateIssueProxy{}, "Gitea Issue list"),
) )
@ -85,13 +91,14 @@ func GetIssue(c *gin.Context) {
if err != nil { if err != nil {
c.AbortWithStatus(http.StatusNotFound) c.AbortWithStatus(http.StatusNotFound)
} }
c.AsciiJSON(http.StatusOK, issue) c.AsciiJSON(http.StatusOK, model.IssueTransformFromGitea(issue))
} }
func GetIssueSwagger() (*swagger.Endpoint){ func GetIssueSwagger() (*swagger.Endpoint) {
return endpoint.New("get", "/issue/:id", "Get one issue", return endpoint.New("get", "/issues/:id", "Get one issue",
endpoint.Handler(GetIssue), endpoint.Handler(GetIssue),
endpoint.Description("Get one issue"), endpoint.Description("Get one issue"),
endpoint.Security("token", "USER"),
endpoint.Tags("issues"), endpoint.Tags("issues"),
endpoint.Response(http.StatusOK, model.GetCreateIssueProxy{}, "Gitea Issue"), endpoint.Response(http.StatusOK, model.GetCreateIssueProxy{}, "Gitea Issue"),
) )
@ -104,17 +111,79 @@ func GetIssueComments(c *gin.Context) {
c.AsciiJSON(http.StatusNotFound, err.Error()) c.AsciiJSON(http.StatusNotFound, err.Error())
} }
issueComments, err := giteaClient.GetIssueComments(issueId) issueComments, err := giteaClient.GetIssueComments(issueId)
proxyComments := []model.GetCommentProxy{}
for _, comment := range issueComments {
proxyComments = append(proxyComments, model.CommentTransformFromGitea(comment))
}
if (err != nil) {
c.AbortWithStatus(http.StatusNotFound)
}
c.AsciiJSON(http.StatusOK, proxyComments)
if err != nil { if err != nil {
c.AbortWithStatus(http.StatusNotFound) c.AbortWithStatus(http.StatusNotFound)
} }
c.AsciiJSON(http.StatusOK, issueComments) c.AsciiJSON(http.StatusOK, issueComments)
} }
func GetIssueCommentsSwagger() (*swagger.Endpoint){ func GetIssueCommentsSwagger() (*swagger.Endpoint) {
return endpoint.New("get", "/issue/:id/comments", "Get issue comments", return endpoint.New("get", "/issues/:id/comments", "Get issue comments",
endpoint.Handler(GetIssue), endpoint.Handler(GetIssueComments),
endpoint.Description("Get issue comments"), endpoint.Description("Get issue comments"),
endpoint.Security("token", "USER"),
endpoint.Tags("issues"), endpoint.Tags("issues"),
endpoint.Response(http.StatusOK, []gitea.Comment{}, "Gitea issue comments"), endpoint.Response(http.StatusOK, []gitea.Comment{}, "Gitea issue comments"),
) )
} }
func PostIssueComment(c *gin.Context) {
var commentProxy model.PostCommentProxy
if err := c.BindJSON(&commentProxy); err != nil {
c.AsciiJSON(http.StatusBadRequest, gin.H{
"message": "CREATE_COMMENT_ERROR",
"error": err.Error(),
})
return
}
issueId, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
fmt.Println(fmt.Sprintf("ParseInt err: %+v", err))
c.AsciiJSON(http.StatusNotFound, err.Error())
}
giteaComment, err := commentProxy.TransformToGiteaComment()
if err != nil {
c.AsciiJSON(http.StatusBadRequest, gin.H{
"message": "CREATE_COMMENT_ERROR",
"error": err.Error(),
})
return
}
sudo := c.GetHeader("X-Sudo")
if (sudo != "") {
giteaClient.SudoUser(sudo)
}
response, err := giteaClient.CreateComment(issueId, giteaComment)
if err != nil {
c.AsciiJSON(http.StatusBadRequest, gin.H{
"message": "CREATE_ISSUE_ERROR",
"error": err.Error(),
})
return
}
c.AsciiJSON(http.StatusOK, model.CommentTransformFromGitea(response))
}
func PostIssueCommentSwagger() (*swagger.Endpoint) {
return endpoint.New("post", "/issues/:id/comments", "Create issue comment",
endpoint.Handler(PostIssueComment),
endpoint.Description("Add issue comment"),
endpoint.Body(model.PostCommentProxy{}, "Comment object", true),
endpoint.Security("token", "USER"),
endpoint.Tags("issues"),
endpoint.Response(http.StatusOK, model.GetCommentProxy{}, "Gitea issue comments"),
)
}

View File

@ -17,7 +17,7 @@ func GetLabels(c *gin.Context) {
c.AsciiJSON(http.StatusOK, labels) c.AsciiJSON(http.StatusOK, labels)
} }
func GetLabelsSwagger() (*swagger.Endpoint){ func GetLabelsSwagger() (*swagger.Endpoint) {
return endpoint.New("get", "/labels", "List project labels", return endpoint.New("get", "/labels", "List project labels",
endpoint.Handler(GetLabels), endpoint.Handler(GetLabels),
endpoint.Description("Get all labels"), endpoint.Description("Get all labels"),

42
controller/user.go Normal file
View File

@ -0,0 +1,42 @@
package controller
import (
"code.gitea.io/sdk/gitea"
"gitea-issue/giteaClient"
"gitea-issue/model"
"github.com/gin-gonic/gin"
"github.com/savaki/swag/endpoint"
"github.com/savaki/swag/swagger"
"net/http"
)
func PostUser(c *gin.Context) {
var userProxy model.PostUserProxy
if err := c.BindJSON(&userProxy); err != nil {
c.AsciiJSON(http.StatusBadRequest, gin.H{
"message": "CREATE_USER_ERROR",
"error": err.Error(),
})
return
}
response, err := giteaClient.CreateUser(userProxy.TransformToGiteaUser())
if err != nil {
c.AsciiJSON(http.StatusUnprocessableEntity, gin.H{
"message": "CREATE_USER_ERROR",
"error": err.Error(),
})
return
}
c.AsciiJSON(http.StatusOK, response)
}
func PostUserSwagger() (*swagger.Endpoint) {
return endpoint.New("post", "/user", "Create user",
endpoint.Handler(PostUser),
endpoint.Description("Post new user"),
endpoint.Body(model.PostUserProxy{}, "User object", true),
endpoint.Tags("user"),
endpoint.Response(http.StatusOK, gitea.User{}, "Gitea User"),
)
}

View File

@ -5,41 +5,55 @@ import (
"fmt" "fmt"
) )
var( var (
giteaClient *gitea.Client giteaClient *gitea.Client
giteaOwner string giteaOwner string
giteaRepo string giteaRepo string
) )
func SetUp(config GiteaConfig){ func SetUp(config GiteaConfig) {
giteaClient = gitea.NewClient(config.GiteaUrl, config.GiteaToken) giteaClient = gitea.NewClient(config.GiteaUrl, config.GiteaToken)
giteaOwner = config.GiteaOwner giteaOwner = config.GiteaOwner
giteaRepo = config.GiteaRepo giteaRepo = config.GiteaRepo
} }
func GetUserInfo() (*gitea.User, error){ func GetUserInfo() (*gitea.User, error) {
usr, err := giteaClient.GetMyUserInfo() usr, err := giteaClient.GetMyUserInfo()
if(err != nil){ if (err != nil) {
return nil, err return nil, err
} }
return usr, nil return usr, nil
} }
func SudoUser(username string) {
giteaClient.SetSudo(username)
}
func GetServerInfo() (string, error) { func GetServerInfo() (string, error) {
ver, err := giteaClient.ServerVersion() ver, err := giteaClient.ServerVersion()
if(err != nil){ if (err != nil) {
return "", err return "", err
} }
return ver, nil return ver, nil
} }
func GetIssues() ([]*gitea.Issue, error){ func CreateUser(user gitea.CreateUserOption) (*gitea.User, error) {
giteaUser, err := giteaClient.AdminCreateUser(user)
if (err != nil) {
fmt.Println(fmt.Sprintf("Gitea error: %+v", err.Error()))
return nil, err
}
return giteaUser, nil
}
func GetIssues() ([]*gitea.Issue, error) {
opt := gitea.ListIssueOption{ opt := gitea.ListIssueOption{
Page: 0, Page: 0,
State: "open", State: "closed",
} }
giteaIssues, err := giteaClient.ListRepoIssues(giteaOwner, giteaRepo, opt) giteaIssues, err := giteaClient.ListRepoIssues(giteaOwner, giteaRepo, opt)
if( err != nil){ if (err != nil) {
fmt.Println(fmt.Sprintf("Gitea error: %+v", err.Error())) fmt.Println(fmt.Sprintf("Gitea error: %+v", err.Error()))
return nil, err return nil, err
} }
@ -48,7 +62,7 @@ func GetIssues() ([]*gitea.Issue, error){
func GetIssue(issueID int64) (*gitea.Issue, error) { func GetIssue(issueID int64) (*gitea.Issue, error) {
giteaIssue, err := giteaClient.GetIssue(giteaOwner, giteaRepo, issueID) giteaIssue, err := giteaClient.GetIssue(giteaOwner, giteaRepo, issueID)
if( err != nil){ if (err != nil) {
fmt.Println(fmt.Sprintf("GetIssue gitea error: %+v", err.Error())) fmt.Println(fmt.Sprintf("GetIssue gitea error: %+v", err.Error()))
return nil, err return nil, err
} }
@ -57,7 +71,7 @@ func GetIssue(issueID int64) (*gitea.Issue, error) {
func GetIssueComments(issueID int64) ([]*gitea.Comment, error) { func GetIssueComments(issueID int64) ([]*gitea.Comment, error) {
giteaIssueComments, err := giteaClient.ListIssueComments(giteaOwner, giteaRepo, issueID) giteaIssueComments, err := giteaClient.ListIssueComments(giteaOwner, giteaRepo, issueID)
if( err != nil){ if (err != nil) {
fmt.Println(fmt.Sprintf("Gitea get isse comments error: %+v", err.Error())) fmt.Println(fmt.Sprintf("Gitea get isse comments error: %+v", err.Error()))
return nil, err return nil, err
} }
@ -66,18 +80,28 @@ func GetIssueComments(issueID int64) ([]*gitea.Comment, error) {
func GetLabels() ([]*gitea.Label, error) { func GetLabels() ([]*gitea.Label, error) {
giteaLabels, err := giteaClient.ListRepoLabels(giteaOwner, giteaRepo) giteaLabels, err := giteaClient.ListRepoLabels(giteaOwner, giteaRepo)
if( err != nil){ if (err != nil) {
fmt.Println(fmt.Sprintf("Gitea get labels error: %+v", err.Error())) fmt.Println(fmt.Sprintf("Gitea get labels error: %+v", err.Error()))
return nil, err return nil, err
} }
return giteaLabels, nil return giteaLabels, nil
} }
func CreateIssue(issue gitea.CreateIssueOption) (*gitea.Issue, error){ func CreateIssue(issue gitea.CreateIssueOption) (*gitea.Issue, error) {
giteaCreateIssue, err := giteaClient.CreateIssue(giteaOwner, giteaRepo, issue) giteaCreateIssue, err := giteaClient.CreateIssue(giteaOwner, giteaRepo, issue)
if( err != nil){ if (err != nil) {
fmt.Println(fmt.Sprintf("Gitea get labels error: %+v", err.Error())) fmt.Println(fmt.Sprintf("Gitea get labels error: %+v", err.Error()))
return nil, err return nil, err
} }
return giteaCreateIssue, nil return giteaCreateIssue, nil
} }
func CreateComment(id int64, option gitea.CreateIssueCommentOption) (*gitea.Comment, error) {
giteaComment, err := giteaClient.CreateIssueComment(giteaOwner, giteaRepo, id, option)
if (err != nil) {
fmt.Println(fmt.Sprintf("Gitea create comment error: %+v", err.Error()))
return nil, err
}
return giteaComment, nil
}

48
main.go
View File

@ -4,37 +4,27 @@ import (
"fmt" "fmt"
"gitea-issue/controller" "gitea-issue/controller"
"gitea-issue/giteaClient" "gitea-issue/giteaClient"
"gitea-issue/proxy"
"gitea-issue/security"
"github.com/caarlos0/env" "github.com/caarlos0/env"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite" _ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/savaki/swag"
"github.com/savaki/swag/swagger"
ginSwagger "github.com/swaggo/gin-swagger" ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles" "github.com/swaggo/gin-swagger/swaggerFiles"
"net/http" "net/http"
"strconv" "strconv"
"github.com/savaki/swag"
"github.com/savaki/swag/swagger"
) )
var ( var (
db *gorm.DB
giteaConfig giteaClient.GiteaConfig giteaConfig giteaClient.GiteaConfig
proxyConfig ProxyConfig proxyConfig proxy.ProxyConfig
) )
type ProxyConfig struct {
ProjectOrigin string `env:"PROJECT_ORIGIN,required"`
}
func init() { func init() {
//open a db connection
var err error
db, err := gorm.Open("sqlite3", "./database.db")
if err != nil {
panic("failed to connect database")
}
if err := env.Parse(&giteaConfig); err != nil { if err := env.Parse(&giteaConfig); err != nil {
panic(fmt.Sprintf("ENV error: %+v", err.Error())) panic(fmt.Sprintf("ENV error: %+v", err.Error()))
} }
@ -43,7 +33,8 @@ func init() {
} }
giteaClient.SetUp(giteaConfig) giteaClient.SetUp(giteaConfig)
db.AutoMigrate(&userModel{}) proxy.SetUp(proxyConfig)
} }
func main() { func main() {
@ -53,6 +44,7 @@ func main() {
config.AllowOrigins = []string{proxyConfig.ProjectOrigin} config.AllowOrigins = []string{proxyConfig.ProjectOrigin}
router.Use(gin.Logger()) router.Use(gin.Logger())
router.Use(cors.New(config)) router.Use(cors.New(config))
router.Use(gin.Recovery())
proxyApi := swag.New( proxyApi := swag.New(
swag.Title("Gitea issues proxy"), swag.Title("Gitea issues proxy"),
@ -62,37 +54,34 @@ func main() {
swag.Tag("issues", "Gitea issues proxy endpoints"), swag.Tag("issues", "Gitea issues proxy endpoints"),
swag.Tag("labels", "Gites labels for issue"), swag.Tag("labels", "Gites labels for issue"),
swag.Endpoints( swag.Endpoints(
controller.PostUserSwagger(),
controller.PostIssueSwagger(), controller.PostIssueSwagger(),
controller.PostIssueCommentSwagger(),
controller.GetIssuesSwagger(), controller.GetIssuesSwagger(),
controller.GetIssueSwagger(), controller.GetIssueSwagger(),
controller.GetIssueCommentsSwagger(), controller.GetIssueCommentsSwagger(),
controller.GetLabelsSwagger()), controller.GetLabelsSwagger()),
) )
authorizedApi := router.Group("/", security.BearerToken())
proxyApi.Walk(func(path string, endpoint *swagger.Endpoint) { proxyApi.Walk(func(path string, endpoint *swagger.Endpoint) {
h := endpoint.Handler.(func(c *gin.Context)) h := endpoint.Handler.(func(c *gin.Context))
path = swag.ColonPath(path) path = swag.ColonPath(path)
router.Handle(endpoint.Method, path, h) authorizedApi.Handle(endpoint.Method, path, h)
}) })
router.GET("/swagger.json", gin.WrapH(proxyApi.Handler(true))) docs := router.Group("/")
docs.GET("/swagger.json", gin.WrapH(proxyApi.Handler(true)))
url := ginSwagger.URL("/swagger.json") url := ginSwagger.URL("/swagger.json")
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url)) docs.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
_ = router.Run() _ = router.Run()
} }
type (
userModel struct {
gorm.Model
Email string `json:"email"`
Token int `json:"token"`
}
)
func getIssues(c *gin.Context) { func getIssues(c *gin.Context) {
issues, err := giteaClient.GetIssues(); issues, err := giteaClient.GetIssues();
if(err != nil){ if (err != nil) {
c.AbortWithStatus(http.StatusNotFound) c.AbortWithStatus(http.StatusNotFound)
} }
c.AsciiJSON(http.StatusOK, issues) c.AsciiJSON(http.StatusOK, issues)
@ -131,4 +120,3 @@ func getLabels(c *gin.Context) {
} }
c.AsciiJSON(http.StatusOK, labels) c.AsciiJSON(http.StatusOK, labels)
} }

35
model/commentProxy.go Normal file
View File

@ -0,0 +1,35 @@
package model
import (
"code.gitea.io/sdk/gitea"
"time"
)
type GetCommentProxy struct {
Id int64 `json:"id"`
Body string `json:"body"`
OriginalAuthor string `json:"original_author"`
CreatedAt time.Time `json:"created_at"`
}
type PostCommentProxy struct {
Body string `json:"body"`
}
func (c PostCommentProxy) TransformToGiteaComment() (gitea.CreateIssueCommentOption, error) {
giteaObject := gitea.CreateIssueCommentOption{
Body: c.Body,
}
return giteaObject, nil
}
func CommentTransformFromGitea(comment *gitea.Comment) (GetCommentProxy) {
giteaComment := GetCommentProxy{
Id: comment.ID,
Body: comment.Body,
OriginalAuthor: comment.OriginalAuthor,
CreatedAt: comment.Created,
}
return giteaComment
}

View File

@ -7,34 +7,35 @@ import (
) )
type GetCreateIssueProxy struct { type GetCreateIssueProxy struct {
Title string `json:"title"` Id int64 `json:"id"`
Body string `json:"body"` Title string `json:"title"`
Labels []*gitea.Label `json:"labels"` Body string `json:"body"`
Closed *time.Time `json:"closed"` Labels []*gitea.Label `json:"labels"`
Closed *time.Time `json:"closed"`
} }
type PostCreateIssueProxy struct { type PostCreateIssueProxy struct {
Title string `json:"title"` Title string `json:"title"`
Body string `json:"body"` Body string `json:"body"`
Labels []*gitea.Label `json:"labels"` Labels []*gitea.Label `json:"labels"`
} }
func (c PostCreateIssueProxy) TransformToGiteaCreateIssueOption() (gitea.CreateIssueOption, error){ func (c PostCreateIssueProxy) TransformToGiteaCreateIssueOption() (gitea.CreateIssueOption, error) {
giteaUser, err := giteaClient.GetUserInfo() giteaUser, err := giteaClient.GetUserInfo()
if( err != nil){ if (err != nil) {
return gitea.CreateIssueOption{}, err; return gitea.CreateIssueOption{}, err;
} }
labels := []int64{} labels := []int64{}
for _, label := range c.Labels{ for _, label := range c.Labels {
labels = append(labels, label.ID) labels = append(labels, label.ID)
} }
giteaObject := gitea.CreateIssueOption{ giteaObject := gitea.CreateIssueOption{
Title: c.Title, Title: c.Title,
Body: c.Body, Body: c.Body,
Assignee: giteaUser.UserName, Assignee: giteaUser.UserName,
Labels: labels, Labels: labels,
Closed: false, Closed: false,
} }
return giteaObject, nil return giteaObject, nil
@ -43,6 +44,7 @@ func (c PostCreateIssueProxy) TransformToGiteaCreateIssueOption() (gitea.CreateI
func IssueTransformFromGitea(issue *gitea.Issue) (GetCreateIssueProxy) { func IssueTransformFromGitea(issue *gitea.Issue) (GetCreateIssueProxy) {
giteaIssue := GetCreateIssueProxy{ giteaIssue := GetCreateIssueProxy{
Id: issue.Index,
Title: issue.Title, Title: issue.Title,
Body: issue.Body, Body: issue.Body,
Labels: issue.Labels, Labels: issue.Labels,

47
model/userProxy.go Normal file
View File

@ -0,0 +1,47 @@
package model
import (
"code.gitea.io/sdk/gitea"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"math/rand"
"unicode"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456!@#$%")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
type PostUserProxy struct {
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r)
}
func (c PostUserProxy) TransformToGiteaUser() (gitea.CreateUserOption) {
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
username, _, _ := transform.String(t, c.FirstName+c.LastName)
giteaObject := gitea.CreateUserOption{
SourceID: 0,
LoginName: c.Email,
Username: username,
FullName: c.FirstName + " " + c.LastName,
Email: c.Email,
Password: randSeq(12),
MustChangePassword: nil,
SendNotify: false,
}
return giteaObject
}

9
proxy/proxyClient.go Normal file
View File

@ -0,0 +1,9 @@
package proxy
var (
Bearer string
)
func SetUp(config ProxyConfig) {
Bearer = config.ProxyToken
}

6
proxy/proxyConfig.go Normal file
View File

@ -0,0 +1,6 @@
package proxy
type ProxyConfig struct {
ProjectOrigin string `env:"PROJECT_ORIGIN,required"`
ProxyToken string `env:"PROXY_TOKEN,required"`
}

View File

@ -0,0 +1,23 @@
package security
import (
"gitea-issue/proxy"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func BearerToken() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.GetHeader("Authorization")
if (token == "") {
c.AbortWithStatus(http.StatusBadRequest)
}
var tokenArr []string
tokenArr = strings.Split(token, " ")
if (tokenArr[1] != proxy.Bearer) {
c.AbortWithStatus(http.StatusUnauthorized)
}
c.Next()
}
}