mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2026-04-15 01:41:56 +00:00
didn't realize gin supports net.Context. Change to support Context pattern!
This commit is contained in:
+5
-7
@@ -4,7 +4,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/store"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -16,8 +16,7 @@ var (
|
||||
)
|
||||
|
||||
func GetBadge(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
repo, err := model.GetRepoName(db,
|
||||
repo, err := store.GetRepoOwnerName(c,
|
||||
c.Param("owner"),
|
||||
c.Param("name"),
|
||||
)
|
||||
@@ -38,7 +37,7 @@ func GetBadge(c *gin.Context) {
|
||||
branch = repo.Branch
|
||||
}
|
||||
|
||||
build, err := model.GetBuildLast(db, repo, branch)
|
||||
build, err := store.GetBuildLast(c, repo, branch)
|
||||
if err != nil {
|
||||
c.String(404, badgeNone)
|
||||
return
|
||||
@@ -59,8 +58,7 @@ func GetBadge(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetCC(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
repo, err := model.GetRepoName(db,
|
||||
repo, err := store.GetRepoOwnerName(c,
|
||||
c.Param("owner"),
|
||||
c.Param("name"),
|
||||
)
|
||||
@@ -69,7 +67,7 @@ func GetCC(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
builds, err := model.GetBuildList(db, repo)
|
||||
builds, err := store.GetBuildList(c, repo)
|
||||
if err != nil || len(builds) == 0 {
|
||||
c.AbortWithStatus(404)
|
||||
return
|
||||
|
||||
+22
-33
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/drone/drone/engine"
|
||||
"github.com/drone/drone/remote"
|
||||
"github.com/drone/drone/shared/httputil"
|
||||
"github.com/drone/drone/store"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/drone/drone/model"
|
||||
@@ -21,8 +22,7 @@ import (
|
||||
|
||||
func GetBuilds(c *gin.Context) {
|
||||
repo := session.Repo(c)
|
||||
db := context.Database(c)
|
||||
builds, err := model.GetBuildList(db, repo)
|
||||
builds, err := store.GetBuildList(c, repo)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
@@ -32,7 +32,6 @@ func GetBuilds(c *gin.Context) {
|
||||
|
||||
func GetBuild(c *gin.Context) {
|
||||
repo := session.Repo(c)
|
||||
db := context.Database(c)
|
||||
|
||||
num, err := strconv.Atoi(c.Param("number"))
|
||||
if err != nil {
|
||||
@@ -40,12 +39,12 @@ func GetBuild(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
build, err := model.GetBuildNumber(db, repo, num)
|
||||
build, err := store.GetBuildNumber(c, repo, num)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
jobs, _ := model.GetJobList(db, build)
|
||||
jobs, _ := store.GetJobList(c, build)
|
||||
|
||||
out := struct {
|
||||
*model.Build
|
||||
@@ -57,7 +56,6 @@ func GetBuild(c *gin.Context) {
|
||||
|
||||
func GetBuildLogs(c *gin.Context) {
|
||||
repo := session.Repo(c)
|
||||
db := context.Database(c)
|
||||
|
||||
// the user may specify to stream the full logs,
|
||||
// or partial logs, capped at 2MB.
|
||||
@@ -68,19 +66,19 @@ func GetBuildLogs(c *gin.Context) {
|
||||
num, _ := strconv.Atoi(c.Params.ByName("number"))
|
||||
seq, _ := strconv.Atoi(c.Params.ByName("job"))
|
||||
|
||||
build, err := model.GetBuildNumber(db, repo, num)
|
||||
build, err := store.GetBuildNumber(c, repo, num)
|
||||
if err != nil {
|
||||
c.AbortWithError(404, err)
|
||||
return
|
||||
}
|
||||
|
||||
job, err := model.GetJobNumber(db, build, seq)
|
||||
job, err := store.GetJobNumber(c, build, seq)
|
||||
if err != nil {
|
||||
c.AbortWithError(404, err)
|
||||
return
|
||||
}
|
||||
|
||||
r, err := model.GetLog(db, job)
|
||||
r, err := store.ReadLog(c, job)
|
||||
if err != nil {
|
||||
c.AbortWithError(404, err)
|
||||
return
|
||||
@@ -97,25 +95,24 @@ func GetBuildLogs(c *gin.Context) {
|
||||
func DeleteBuild(c *gin.Context) {
|
||||
engine_ := context.Engine(c)
|
||||
repo := session.Repo(c)
|
||||
db := context.Database(c)
|
||||
|
||||
// parse the build number and job sequence number from
|
||||
// the repquest parameter.
|
||||
num, _ := strconv.Atoi(c.Params.ByName("number"))
|
||||
seq, _ := strconv.Atoi(c.Params.ByName("job"))
|
||||
|
||||
build, err := model.GetBuildNumber(db, repo, num)
|
||||
build, err := store.GetBuildNumber(c, repo, num)
|
||||
if err != nil {
|
||||
c.AbortWithError(404, err)
|
||||
return
|
||||
}
|
||||
|
||||
job, err := model.GetJobNumber(db, build, seq)
|
||||
job, err := store.GetJobNumber(c, build, seq)
|
||||
if err != nil {
|
||||
c.AbortWithError(404, err)
|
||||
return
|
||||
}
|
||||
node, err := model.GetNode(db, job.NodeID)
|
||||
node, err := store.GetNode(c, job.NodeID)
|
||||
if err != nil {
|
||||
c.AbortWithError(404, err)
|
||||
return
|
||||
@@ -125,9 +122,8 @@ func DeleteBuild(c *gin.Context) {
|
||||
|
||||
func PostBuild(c *gin.Context) {
|
||||
|
||||
remote_ := context.Remote(c)
|
||||
remote_ := remote.FromContext(c)
|
||||
repo := session.Repo(c)
|
||||
db := context.Database(c)
|
||||
|
||||
num, err := strconv.Atoi(c.Param("number"))
|
||||
if err != nil {
|
||||
@@ -135,14 +131,14 @@ func PostBuild(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := model.GetUser(db, repo.UserID)
|
||||
user, err := store.GetUser(c, repo.UserID)
|
||||
if err != nil {
|
||||
log.Errorf("failure to find repo owner %s. %s", repo.FullName, err)
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
build, err := model.GetBuildNumber(db, repo, num)
|
||||
build, err := store.GetBuildNumber(c, repo, num)
|
||||
if err != nil {
|
||||
log.Errorf("failure to get build %d. %s", num, err)
|
||||
c.AbortWithError(404, err)
|
||||
@@ -155,7 +151,7 @@ func PostBuild(c *gin.Context) {
|
||||
if refresher, ok := remote_.(remote.Refresher); ok {
|
||||
ok, _ := refresher.Refresh(user)
|
||||
if ok {
|
||||
model.UpdateUser(db, user)
|
||||
store.UpdateUser(c, user)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,7 +163,7 @@ func PostBuild(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
key, _ := model.GetKey(db, repo)
|
||||
key, _ := store.GetKey(c, repo)
|
||||
netrc, err := remote_.Netrc(user, repo)
|
||||
if err != nil {
|
||||
log.Errorf("failure to generate netrc for %s. %s", repo.FullName, err)
|
||||
@@ -175,7 +171,7 @@ func PostBuild(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
jobs, err := model.GetJobList(db, build)
|
||||
jobs, err := store.GetJobList(c, build)
|
||||
if err != nil {
|
||||
log.Errorf("failure to get build %d jobs. %s", build.Number, err)
|
||||
c.AbortWithError(404, err)
|
||||
@@ -188,13 +184,8 @@ func PostBuild(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
c.AbortWithStatus(500)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
// todo move this to database tier
|
||||
// and wrap inside a transaction
|
||||
build.Status = model.StatusPending
|
||||
build.Started = 0
|
||||
build.Finished = 0
|
||||
@@ -205,25 +196,23 @@ func PostBuild(c *gin.Context) {
|
||||
job.Finished = 0
|
||||
job.ExitCode = 0
|
||||
job.Enqueued = build.Enqueued
|
||||
model.UpdateJob(db, job)
|
||||
store.UpdateJob(c, job)
|
||||
}
|
||||
|
||||
err = model.UpdateBuild(db, build)
|
||||
err = store.UpdateBuild(c, build)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(500)
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
c.JSON(202, build)
|
||||
|
||||
// get the previous build so taht we can send
|
||||
// on status change notifications
|
||||
last, _ := model.GetBuildLastBefore(db, repo, build.Branch, build.ID)
|
||||
last, _ := store.GetBuildLastBefore(c, repo, build.Branch, build.ID)
|
||||
|
||||
engine_ := context.Engine(c)
|
||||
go engine_.Schedule(&engine.Task{
|
||||
go engine_.Schedule(c.Copy(), &engine.Task{
|
||||
User: user,
|
||||
Repo: repo,
|
||||
Build: build,
|
||||
|
||||
+5
-10
@@ -6,14 +6,12 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/router/middleware/session"
|
||||
"github.com/drone/drone/shared/token"
|
||||
"github.com/drone/drone/store"
|
||||
)
|
||||
|
||||
func GetCommit(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
repo := session.Repo(c)
|
||||
|
||||
parsed, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
|
||||
@@ -34,7 +32,7 @@ func GetCommit(c *gin.Context) {
|
||||
branch = repo.Branch
|
||||
}
|
||||
|
||||
build, err := model.GetBuildCommit(db, repo, commit, branch)
|
||||
build, err := store.GetBuildCommit(c, repo, commit, branch)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
return
|
||||
@@ -44,7 +42,6 @@ func GetCommit(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetPullRequest(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
repo := session.Repo(c)
|
||||
refs := fmt.Sprintf("refs/pull/%s/head", c.Param("number"))
|
||||
|
||||
@@ -60,7 +57,7 @@ func GetPullRequest(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
build, err := model.GetBuildRef(db, repo, refs)
|
||||
build, err := store.GetBuildRef(c, repo, refs)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
return
|
||||
@@ -70,7 +67,6 @@ func GetPullRequest(c *gin.Context) {
|
||||
}
|
||||
|
||||
func RedirectSha(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
repo := session.Repo(c)
|
||||
|
||||
commit := c.Param("sha")
|
||||
@@ -79,7 +75,7 @@ func RedirectSha(c *gin.Context) {
|
||||
branch = repo.Branch
|
||||
}
|
||||
|
||||
build, err := model.GetBuildCommit(db, repo, commit, branch)
|
||||
build, err := store.GetBuildCommit(c, repo, commit, branch)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
return
|
||||
@@ -90,11 +86,10 @@ func RedirectSha(c *gin.Context) {
|
||||
}
|
||||
|
||||
func RedirectPullRequest(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
repo := session.Repo(c)
|
||||
refs := fmt.Sprintf("refs/pull/%s/head", c.Param("number"))
|
||||
|
||||
build, err := model.GetBuildRef(db, repo, refs)
|
||||
build, err := store.GetBuildRef(c, repo, refs)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
return
|
||||
|
||||
+11
-18
@@ -14,13 +14,13 @@ import (
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/shared/httputil"
|
||||
"github.com/drone/drone/shared/token"
|
||||
"github.com/drone/drone/store"
|
||||
"github.com/drone/drone/yaml"
|
||||
"github.com/drone/drone/yaml/matrix"
|
||||
)
|
||||
|
||||
func PostHook(c *gin.Context) {
|
||||
remote_ := context.Remote(c)
|
||||
db := context.Database(c)
|
||||
remote_ := remote.FromContext(c)
|
||||
|
||||
tmprepo, build, err := remote_.Hook(c.Request)
|
||||
if err != nil {
|
||||
@@ -46,7 +46,7 @@ func PostHook(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
repo, err := model.GetRepoName(db, tmprepo.Owner, tmprepo.Name)
|
||||
repo, err := store.GetRepoOwnerName(c, tmprepo.Owner, tmprepo.Name)
|
||||
if err != nil {
|
||||
log.Errorf("failure to find repo %s/%s from hook. %s", tmprepo.Owner, tmprepo.Name, err)
|
||||
c.AbortWithError(404, err)
|
||||
@@ -87,7 +87,7 @@ func PostHook(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := model.GetUser(db, repo.UserID)
|
||||
user, err := store.GetUser(c, repo.UserID)
|
||||
if err != nil {
|
||||
log.Errorf("failure to find repo owner %s. %s", repo.FullName, err)
|
||||
c.AbortWithError(500, err)
|
||||
@@ -103,7 +103,7 @@ func PostHook(c *gin.Context) {
|
||||
// a small number of people will probably be upset by this, I'm not sure
|
||||
// it is actually that big of a deal.
|
||||
if len(build.Email) == 0 {
|
||||
author, err := model.GetUserLogin(db, build.Author)
|
||||
author, err := store.GetUserLogin(c, build.Author)
|
||||
if err == nil {
|
||||
build.Email = author.Email
|
||||
}
|
||||
@@ -115,7 +115,7 @@ func PostHook(c *gin.Context) {
|
||||
if refresher, ok := remote_.(remote.Refresher); ok {
|
||||
ok, _ := refresher.Refresh(user)
|
||||
if ok {
|
||||
model.UpdateUser(db, user)
|
||||
store.UpdateUser(c, user)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ func PostHook(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
key, _ := model.GetKey(db, repo)
|
||||
key, _ := store.GetKey(c, repo)
|
||||
|
||||
// verify the branches can be built vs skipped
|
||||
yconfig, _ := yaml.Parse(string(raw))
|
||||
@@ -164,18 +164,12 @@ func PostHook(c *gin.Context) {
|
||||
c.AbortWithStatus(200)
|
||||
return
|
||||
}
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
log.Errorf("failure to begin database transaction", err)
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
// update some build fields
|
||||
build.Status = model.StatusPending
|
||||
build.RepoID = repo.ID
|
||||
|
||||
// and use a transaction
|
||||
var jobs []*model.Job
|
||||
for num, axis := range axes {
|
||||
jobs = append(jobs, &model.Job{
|
||||
@@ -185,13 +179,12 @@ func PostHook(c *gin.Context) {
|
||||
Environment: axis,
|
||||
})
|
||||
}
|
||||
err = model.CreateBuild(tx, build, jobs...)
|
||||
err = store.CreateBuild(c, build, jobs...)
|
||||
if err != nil {
|
||||
log.Errorf("failure to save commit for %s. %s", repo.FullName, err)
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
c.JSON(200, build)
|
||||
|
||||
@@ -203,10 +196,10 @@ func PostHook(c *gin.Context) {
|
||||
|
||||
// get the previous build so taht we can send
|
||||
// on status change notifications
|
||||
last, _ := model.GetBuildLastBefore(db, repo, build.Branch, build.ID)
|
||||
last, _ := store.GetBuildLastBefore(c, repo, build.Branch, build.ID)
|
||||
|
||||
engine_ := context.Engine(c)
|
||||
go engine_.Schedule(&engine.Task{
|
||||
go engine_.Schedule(c.Copy(), &engine.Task{
|
||||
User: user,
|
||||
Repo: repo,
|
||||
Build: build,
|
||||
|
||||
+9
-10
@@ -8,15 +8,15 @@ import (
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/remote"
|
||||
"github.com/drone/drone/shared/crypto"
|
||||
"github.com/drone/drone/shared/httputil"
|
||||
"github.com/drone/drone/shared/token"
|
||||
"github.com/drone/drone/store"
|
||||
)
|
||||
|
||||
func GetLogin(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
remote := context.Remote(c)
|
||||
remote := remote.FromContext(c)
|
||||
|
||||
// when dealing with redirects we may need
|
||||
// to adjust the content type. I cannot, however,
|
||||
@@ -36,9 +36,9 @@ func GetLogin(c *gin.Context) {
|
||||
}
|
||||
|
||||
// get the user from the database
|
||||
u, err := model.GetUserLogin(db, tmpuser.Login)
|
||||
u, err := store.GetUserLogin(c, tmpuser.Login)
|
||||
if err != nil {
|
||||
count, err := model.GetUserCount(db)
|
||||
count, err := store.CountUsers(c)
|
||||
if err != nil {
|
||||
log.Errorf("cannot register %s. %s", tmpuser.Login, err)
|
||||
c.Redirect(303, "/login?error=internal_error")
|
||||
@@ -64,7 +64,7 @@ func GetLogin(c *gin.Context) {
|
||||
u.Hash = crypto.Rand()
|
||||
|
||||
// insert the user into the database
|
||||
if err := model.CreateUser(db, u); err != nil {
|
||||
if err := store.CreateUser(c, u); err != nil {
|
||||
log.Errorf("cannot insert %s. %s", u.Login, err)
|
||||
c.Redirect(303, "/login?error=internal_error")
|
||||
return
|
||||
@@ -84,7 +84,7 @@ func GetLogin(c *gin.Context) {
|
||||
u.Email = tmpuser.Email
|
||||
u.Avatar = tmpuser.Avatar
|
||||
|
||||
if err := model.UpdateUser(db, u); err != nil {
|
||||
if err := store.UpdateUser(c, u); err != nil {
|
||||
log.Errorf("cannot update %s. %s", u.Login, err)
|
||||
c.Redirect(303, "/login?error=internal_error")
|
||||
return
|
||||
@@ -116,8 +116,7 @@ func GetLogout(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetLoginToken(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
remote := context.Remote(c)
|
||||
remote := remote.FromContext(c)
|
||||
|
||||
in := &tokenPayload{}
|
||||
err := c.Bind(in)
|
||||
@@ -132,7 +131,7 @@ func GetLoginToken(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := model.GetUserLogin(db, login)
|
||||
user, err := store.GetUserLogin(c, login)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
return
|
||||
|
||||
+8
-11
@@ -10,22 +10,21 @@ import (
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/router/middleware/session"
|
||||
"github.com/drone/drone/shared/token"
|
||||
"github.com/drone/drone/store"
|
||||
)
|
||||
|
||||
func GetNodes(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
nodes, err := model.GetNodeList(db)
|
||||
nodes, err := store.GetNodeList(c)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
c.String(400, err.Error())
|
||||
} else {
|
||||
c.IndentedJSON(http.StatusOK, nodes)
|
||||
c.JSON(200, nodes)
|
||||
}
|
||||
}
|
||||
|
||||
func ShowNodes(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
user := session.User(c)
|
||||
nodes, _ := model.GetNodeList(db)
|
||||
nodes, _ := store.GetNodeList(c)
|
||||
token, _ := token.New(token.CsrfToken, user.Login).Sign(user.Hash)
|
||||
c.HTML(http.StatusOK, "nodes.html", gin.H{"User": user, "Nodes": nodes, "Csrf": token})
|
||||
}
|
||||
@@ -35,7 +34,6 @@ func GetNode(c *gin.Context) {
|
||||
}
|
||||
|
||||
func PostNode(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
engine := context.Engine(c)
|
||||
|
||||
in := struct {
|
||||
@@ -64,7 +62,7 @@ func PostNode(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
err = model.InsertNode(db, node)
|
||||
err = store.CreateNode(c, node)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
@@ -74,16 +72,15 @@ func PostNode(c *gin.Context) {
|
||||
}
|
||||
|
||||
func DeleteNode(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
engine := context.Engine(c)
|
||||
|
||||
id, _ := strconv.Atoi(c.Param("node"))
|
||||
node, err := model.GetNode(db, int64(id))
|
||||
node, err := store.GetNode(c, int64(id))
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
err = model.DeleteNode(db, node)
|
||||
err = store.DeleteNode(c, node)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
+10
-13
@@ -9,15 +9,15 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/remote"
|
||||
"github.com/drone/drone/router/middleware/session"
|
||||
"github.com/drone/drone/shared/httputil"
|
||||
"github.com/drone/drone/shared/token"
|
||||
"github.com/drone/drone/store"
|
||||
)
|
||||
|
||||
func ShowIndex(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
remote := context.Remote(c)
|
||||
remote := remote.FromContext(c)
|
||||
user := session.User(c)
|
||||
if user == nil {
|
||||
c.Redirect(http.StatusSeeOther, "/login")
|
||||
@@ -43,7 +43,7 @@ func ShowIndex(c *gin.Context) {
|
||||
|
||||
// for each repository in the remote system we get
|
||||
// the intersection of those repostiories in Drone
|
||||
repos_, err := model.GetRepoListOf(db, repos)
|
||||
repos_, err := store.GetRepoListOf(c, repos)
|
||||
if err != nil {
|
||||
log.Errorf("Failure to get repository list for %s. %s.",
|
||||
user.Login, err)
|
||||
@@ -73,13 +73,12 @@ func ShowUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
func ShowUsers(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
user := session.User(c)
|
||||
if !user.Admin {
|
||||
c.AbortWithStatus(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
users, _ := model.GetUserList(db)
|
||||
users, _ := store.GetUserList(c)
|
||||
|
||||
token, _ := token.New(
|
||||
token.CsrfToken,
|
||||
@@ -94,11 +93,10 @@ func ShowUsers(c *gin.Context) {
|
||||
}
|
||||
|
||||
func ShowRepo(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
user := session.User(c)
|
||||
repo := session.Repo(c)
|
||||
|
||||
builds, _ := model.GetBuildList(db, repo)
|
||||
builds, _ := store.GetBuildList(c, repo)
|
||||
groups := []*model.BuildGroup{}
|
||||
|
||||
var curr *model.BuildGroup
|
||||
@@ -124,10 +122,10 @@ func ShowRepo(c *gin.Context) {
|
||||
}
|
||||
|
||||
func ShowRepoConf(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
|
||||
user := session.User(c)
|
||||
repo := session.Repo(c)
|
||||
key, _ := model.GetKey(db, repo)
|
||||
key, _ := store.GetKey(c, repo)
|
||||
|
||||
token, _ := token.New(
|
||||
token.CsrfToken,
|
||||
@@ -171,7 +169,6 @@ func ShowRepoBadges(c *gin.Context) {
|
||||
}
|
||||
|
||||
func ShowBuild(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
user := session.User(c)
|
||||
repo := session.Repo(c)
|
||||
num, _ := strconv.Atoi(c.Param("number"))
|
||||
@@ -180,13 +177,13 @@ func ShowBuild(c *gin.Context) {
|
||||
seq = 1
|
||||
}
|
||||
|
||||
build, err := model.GetBuildNumber(db, repo, num)
|
||||
build, err := store.GetBuildNumber(c, repo, num)
|
||||
if err != nil {
|
||||
c.AbortWithError(404, err)
|
||||
return
|
||||
}
|
||||
|
||||
jobs, err := model.GetJobList(db, build)
|
||||
jobs, err := store.GetJobList(c, build)
|
||||
if err != nil {
|
||||
c.AbortWithError(404, err)
|
||||
return
|
||||
|
||||
+11
-23
@@ -10,16 +10,16 @@ import (
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/remote"
|
||||
"github.com/drone/drone/router/middleware/session"
|
||||
"github.com/drone/drone/shared/crypto"
|
||||
"github.com/drone/drone/shared/httputil"
|
||||
"github.com/drone/drone/shared/token"
|
||||
"github.com/drone/drone/store"
|
||||
)
|
||||
|
||||
func PostRepo(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
remote := context.Remote(c)
|
||||
remote := remote.FromContext(c)
|
||||
user := session.User(c)
|
||||
owner := c.Param("owner")
|
||||
name := c.Param("name")
|
||||
@@ -45,7 +45,7 @@ func PostRepo(c *gin.Context) {
|
||||
}
|
||||
|
||||
// error if the repository already exists
|
||||
_, err = model.GetRepoName(db, owner, name)
|
||||
_, err = store.GetRepoOwnerName(c, owner, name)
|
||||
if err == nil {
|
||||
c.String(409, "Repository already exists.")
|
||||
return
|
||||
@@ -91,32 +91,23 @@ func PostRepo(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
// persist the repository
|
||||
err = model.CreateRepo(tx, r)
|
||||
err = store.CreateRepo(c, r)
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
keys.RepoID = r.ID
|
||||
err = model.CreateKey(tx, keys)
|
||||
err = store.CreateKey(c, keys)
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
c.JSON(200, r)
|
||||
}
|
||||
|
||||
func PatchRepo(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
repo := session.Repo(c)
|
||||
user := session.User(c)
|
||||
|
||||
@@ -152,7 +143,7 @@ func PatchRepo(c *gin.Context) {
|
||||
repo.Timeout = *in.Timeout
|
||||
}
|
||||
|
||||
err := model.UpdateRepo(db, repo)
|
||||
err := store.UpdateRepo(c, repo)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
@@ -166,9 +157,8 @@ func GetRepo(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetRepoKey(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
repo := session.Repo(c)
|
||||
keys, err := model.GetKey(db, repo)
|
||||
keys, err := store.GetKey(c, repo)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
} else {
|
||||
@@ -177,12 +167,11 @@ func GetRepoKey(c *gin.Context) {
|
||||
}
|
||||
|
||||
func DeleteRepo(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
remote := context.Remote(c)
|
||||
remote := remote.FromContext(c)
|
||||
repo := session.Repo(c)
|
||||
user := session.User(c)
|
||||
|
||||
err := model.DeleteRepo(db, repo)
|
||||
err := store.DeleteRepo(c, repo)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
@@ -193,7 +182,6 @@ func DeleteRepo(c *gin.Context) {
|
||||
}
|
||||
|
||||
func PostSecure(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
repo := session.Repo(c)
|
||||
|
||||
in, err := ioutil.ReadAll(c.Request.Body)
|
||||
@@ -215,7 +203,7 @@ func PostSecure(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
key, err := model.GetKey(db, repo)
|
||||
key, err := store.GetKey(c, repo)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
|
||||
+5
-10
@@ -1,10 +1,5 @@
|
||||
package controller
|
||||
|
||||
/*
|
||||
stream.Get("/:owner/:name", controller.GetRepoEvents)
|
||||
stream.Get("/:owner/:name/:build/:number", controller.GetStream)
|
||||
*/
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strconv"
|
||||
@@ -13,9 +8,9 @@ import (
|
||||
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"github.com/drone/drone/engine"
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/router/middleware/session"
|
||||
"github.com/drone/drone/store"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
|
||||
@@ -59,7 +54,7 @@ func GetRepoEvents(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetStream(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
|
||||
engine_ := context.Engine(c)
|
||||
repo := session.Repo(c)
|
||||
buildn, _ := strconv.Atoi(c.Param("build"))
|
||||
@@ -67,19 +62,19 @@ func GetStream(c *gin.Context) {
|
||||
|
||||
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
||||
|
||||
build, err := model.GetBuildNumber(db, repo, buildn)
|
||||
build, err := store.GetBuildNumber(c, repo, buildn)
|
||||
if err != nil {
|
||||
log.Debugln("stream cannot get build number.", err)
|
||||
c.AbortWithError(404, err)
|
||||
return
|
||||
}
|
||||
job, err := model.GetJobNumber(db, build, jobn)
|
||||
job, err := store.GetJobNumber(c, build, jobn)
|
||||
if err != nil {
|
||||
log.Debugln("stream cannot get job number.", err)
|
||||
c.AbortWithError(404, err)
|
||||
return
|
||||
}
|
||||
node, err := model.GetNode(db, job.NodeID)
|
||||
node, err := store.GetNode(c, job.NodeID)
|
||||
if err != nil {
|
||||
log.Debugln("stream cannot get node.", err)
|
||||
c.AbortWithError(404, err)
|
||||
|
||||
+6
-7
@@ -6,9 +6,10 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/remote"
|
||||
"github.com/drone/drone/router/middleware/session"
|
||||
"github.com/drone/drone/shared/token"
|
||||
"github.com/drone/drone/store"
|
||||
)
|
||||
|
||||
func GetSelf(c *gin.Context) {
|
||||
@@ -17,8 +18,7 @@ func GetSelf(c *gin.Context) {
|
||||
|
||||
func GetFeed(c *gin.Context) {
|
||||
user := session.User(c)
|
||||
db := context.Database(c)
|
||||
feed, err := model.GetUserFeed(db, user, 25, 0)
|
||||
feed, err := store.GetUserFeed(c, user, 25, 0)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
@@ -28,8 +28,7 @@ func GetFeed(c *gin.Context) {
|
||||
|
||||
func GetRepos(c *gin.Context) {
|
||||
user := session.User(c)
|
||||
remote := context.Remote(c)
|
||||
db := context.Database(c)
|
||||
remote := remote.FromContext(c)
|
||||
var repos []*model.RepoLite
|
||||
|
||||
// get the repository list from the cache
|
||||
@@ -47,7 +46,7 @@ func GetRepos(c *gin.Context) {
|
||||
|
||||
// for each repository in the remote system we get
|
||||
// the intersection of those repostiories in Drone
|
||||
repos_, err := model.GetRepoListOf(db, repos)
|
||||
repos_, err := store.GetRepoListOf(c, repos)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
@@ -59,7 +58,7 @@ func GetRepos(c *gin.Context) {
|
||||
|
||||
func GetRemoteRepos(c *gin.Context) {
|
||||
user := session.User(c)
|
||||
remote := context.Remote(c)
|
||||
remote := remote.FromContext(c)
|
||||
|
||||
reposv, ok := c.Get("repos")
|
||||
if ok {
|
||||
|
||||
+8
-13
@@ -6,14 +6,13 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/router/middleware/session"
|
||||
"github.com/drone/drone/shared/crypto"
|
||||
"github.com/drone/drone/store"
|
||||
)
|
||||
|
||||
func GetUsers(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
users, err := model.GetUserList(db)
|
||||
users, err := store.GetUserList(c)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
@@ -23,8 +22,7 @@ func GetUsers(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetUser(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
user, err := model.GetUserLogin(db, c.Param("login"))
|
||||
user, err := store.GetUserLogin(c, c.Param("login"))
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
@@ -35,7 +33,6 @@ func GetUser(c *gin.Context) {
|
||||
|
||||
func PatchUser(c *gin.Context) {
|
||||
me := session.User(c)
|
||||
db := context.Database(c)
|
||||
in := &model.User{}
|
||||
err := c.Bind(in)
|
||||
if err != nil {
|
||||
@@ -43,7 +40,7 @@ func PatchUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := model.GetUserLogin(db, c.Param("login"))
|
||||
user, err := store.GetUserLogin(c, c.Param("login"))
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
@@ -57,7 +54,7 @@ func PatchUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
err = model.UpdateUser(db, user)
|
||||
err = store.UpdateUser(c, user)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusConflict)
|
||||
return
|
||||
@@ -67,7 +64,6 @@ func PatchUser(c *gin.Context) {
|
||||
}
|
||||
|
||||
func PostUser(c *gin.Context) {
|
||||
db := context.Database(c)
|
||||
in := &model.User{}
|
||||
err := c.Bind(in)
|
||||
if err != nil {
|
||||
@@ -83,7 +79,7 @@ func PostUser(c *gin.Context) {
|
||||
user.Active = true
|
||||
user.Hash = crypto.Rand()
|
||||
|
||||
err = model.CreateUser(db, user)
|
||||
err = store.CreateUser(c, user)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
@@ -94,9 +90,8 @@ func PostUser(c *gin.Context) {
|
||||
|
||||
func DeleteUser(c *gin.Context) {
|
||||
me := session.User(c)
|
||||
db := context.Database(c)
|
||||
|
||||
user, err := model.GetUserLogin(db, c.Param("login"))
|
||||
user, err := store.GetUserLogin(c, c.Param("login"))
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
@@ -108,7 +103,7 @@ func DeleteUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
err = model.DeleteUser(db, user)
|
||||
err = store.DeleteUser(c, user)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user