Initial commit

This commit is contained in:
Aleksander Cynarski 2019-12-03 21:14:14 +01:00
commit 46a6b93ded
Signed by: paramah
GPG Key ID: C4340BA42B9C173A
7 changed files with 301 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
database.db
vendor
Makefile

125
Godeps/Godeps.json generated Normal file
View File

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

5
Godeps/Readme generated Normal file
View File

@ -0,0 +1,5 @@
This directory tree is generated automatically by godep.
Please do not edit.
See https://github.com/tools/godep for more information.

0
README.md Normal file
View File

View File

@ -0,0 +1,69 @@
package giteaClient
import (
"code.gitea.io/sdk/gitea"
"fmt"
)
var(
giteaClient *gitea.Client
giteaOwner string
giteaRepo string
)
func SetUp(config GiteaConfig){
giteaClient = gitea.NewClient(config.GiteaUrl, config.GiteaToken)
giteaOwner = config.GiteaOwner
giteaRepo = config.GiteaRepo
}
func GetUserInfo() (*gitea.User, error){
usr, err := giteaClient.GetMyUserInfo()
if(err != nil){
return nil, err
}
return usr, nil
}
func GetServerInfo() (string, error) {
ver, err := giteaClient.ServerVersion()
if(err != nil){
return "", err
}
return ver, nil
}
func GetIssues() ([]*gitea.Issue, error){
opt := gitea.ListIssueOption{
Page: 0,
State: "open",
}
giteaIssues, err := giteaClient.ListRepoIssues(giteaOwner, giteaRepo, opt)
if( err != nil){
fmt.Println(fmt.Sprintf("Gitea error: %+v", err.Error()))
return nil, err
}
return giteaIssues, nil
}
func GetIssue(issueID int64) (*gitea.Issue, error) {
giteaIssue, err := giteaClient.GetIssue(giteaOwner, giteaRepo, issueID)
if( err != nil){
fmt.Println(fmt.Sprintf("GetIssue gitea error: %+v", err.Error()))
return nil, err
}
return giteaIssue, nil
}
func GetIssueComments(issueID int64) ([]*gitea.Comment, error) {
giteaIssueComments, err := giteaClient.ListIssueComments(giteaOwner, giteaRepo, issueID)
if( err != nil){
fmt.Println(fmt.Sprintf("Gitea error: %+v", err.Error()))
return nil, err
}
return giteaIssueComments, nil
}
func CreateIssue(issue gitea.Issue){
}

View File

@ -0,0 +1,8 @@
package giteaClient
type GiteaConfig struct {
GiteaUrl string `env:"GITEA_URL" envDefault:"https://git.cynarski.pl"`
GiteaToken string `env:"GITEA_TOKEN,required"`
GiteaOwner string `env:"GITEA_OWNER,required"`
GiteaRepo string `env:"GITEA_REPO,required"`
}

91
main.go Normal file
View File

@ -0,0 +1,91 @@
package main
import (
"fmt"
"gitea-issue/giteaClient"
"github.com/caarlos0/env"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"net/http"
"strconv"
)
var (
db *gorm.DB
giteaConfig giteaClient.GiteaConfig
)
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 {
panic("ENV error")
}
giteaClient.SetUp(giteaConfig)
db.AutoMigrate(&userModel{})
}
func main() {
router := gin.Default()
v1 := router.Group("/api/v1/issues")
{
v1.GET("/", getIssues)
v1.GET("/:id", getIssue) //Migrate the schema
v1.GET("/:id/comments", getIssue)
}
_ = router.Run()
}
type (
// todoModel describes a todoModel type
userModel struct {
gorm.Model
Email string `json:"email"`
Token int `json:"token"`
}
)
func getIssues(c *gin.Context) {
issues, err := giteaClient.GetIssues();
if(err != nil){
c.AbortWithStatus(http.StatusNotFound)
}
c.AsciiJSON(http.StatusOK, issues)
}
func getIssue(c *gin.Context) {
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())
}
issue, err := giteaClient.GetIssue(issueId)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
}
c.AsciiJSON(http.StatusOK, issue)
}
func getIssueComments(c *gin.Context) {
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())
}
issueComments, err := giteaClient.GetIssueComments(issueId)
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
}
c.AsciiJSON(http.StatusOK, issueComments)
}