mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-10-22 03:21:58 +00:00
load all things via middleware
This commit is contained in:
@@ -14,7 +14,6 @@ import (
|
||||
"github.com/drone/drone/engine/parser"
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/remote"
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/shared/httputil"
|
||||
"github.com/drone/drone/shared/token"
|
||||
"github.com/drone/drone/store"
|
||||
@@ -205,7 +204,7 @@ func PostHook(c *gin.Context) {
|
||||
// on status change notifications
|
||||
last, _ := store.GetBuildLastBefore(c, repo, build.Branch, build.ID)
|
||||
|
||||
engine_ := context.Engine(c)
|
||||
engine_ := engine.FromContext(c)
|
||||
go engine_.Schedule(c.Copy(), &engine.Task{
|
||||
User: user,
|
||||
Repo: repo,
|
||||
|
108
web/slack.go
108
web/slack.go
@@ -1,9 +1,113 @@
|
||||
package web
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/drone/drone/store"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
slashDeploy = "deploy"
|
||||
slashRestart = "restart"
|
||||
slashStatus = "status"
|
||||
)
|
||||
|
||||
// Slack is handler function that handles Slack slash commands.
|
||||
func Slack(c *gin.Context) {
|
||||
command := c.Param("command")
|
||||
text := c.PostForm("text")
|
||||
c.String(200, "received message %s", text)
|
||||
args := strings.Split(text, " ")
|
||||
|
||||
if command == "" {
|
||||
command = args[0]
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
switch command {
|
||||
case slashStatus:
|
||||
slackStatus(c, args)
|
||||
|
||||
case slashRestart:
|
||||
slackRestart(c, args)
|
||||
|
||||
case slashDeploy:
|
||||
slackDeploy(c, args)
|
||||
|
||||
default:
|
||||
c.String(200, "sorry, I didn't understand [%s]", text)
|
||||
}
|
||||
}
|
||||
|
||||
func slackDeploy(c *gin.Context, args []string) {
|
||||
if len(args) != 3 {
|
||||
c.String(200, "Invalid command. Please provide [repo] [build number] [environment]")
|
||||
return
|
||||
}
|
||||
var (
|
||||
repo = args[0]
|
||||
num = args[1]
|
||||
env = args[2]
|
||||
)
|
||||
owner, name, _ := parseRepoBranch(repo)
|
||||
|
||||
c.String(200, "deploying build %s/%s#%s to %s", owner, name, num, env)
|
||||
}
|
||||
|
||||
func slackRestart(c *gin.Context, args []string) {
|
||||
var (
|
||||
repo = args[0]
|
||||
num = args[1]
|
||||
)
|
||||
owner, name, _ := parseRepoBranch(repo)
|
||||
|
||||
c.String(200, "restarting build %s/%s#%s", owner, name, num)
|
||||
}
|
||||
|
||||
func slackStatus(c *gin.Context, args []string) {
|
||||
var (
|
||||
owner string
|
||||
name string
|
||||
branch string
|
||||
)
|
||||
if len(args) > 0 {
|
||||
owner, name, branch = parseRepoBranch(args[0])
|
||||
}
|
||||
|
||||
repo, err := store.GetRepoOwnerName(c, owner, name)
|
||||
if err != nil {
|
||||
c.String(200, "cannot find repository %s/%s", owner, name)
|
||||
return
|
||||
}
|
||||
if branch == "" {
|
||||
branch = repo.Branch
|
||||
}
|
||||
build, err := store.GetBuildLast(c, repo, branch)
|
||||
if err != nil {
|
||||
c.String(200, "cannot find status for %s/%s@%s", owner, name, branch)
|
||||
return
|
||||
}
|
||||
c.String(200, "%s@%s build number %d finished with status %s",
|
||||
repo.FullName,
|
||||
build.Branch,
|
||||
build.Number,
|
||||
build.Status,
|
||||
)
|
||||
}
|
||||
|
||||
func parseRepoBranch(repo string) (owner, name, branch string) {
|
||||
|
||||
parts := strings.Split(repo, "@")
|
||||
if len(parts) == 2 {
|
||||
branch = parts[1]
|
||||
repo = parts[0]
|
||||
}
|
||||
|
||||
parts = strings.Split(repo, "/")
|
||||
if len(parts) == 2 {
|
||||
owner = parts[0]
|
||||
name = parts[1]
|
||||
}
|
||||
return owner, name, branch
|
||||
}
|
||||
|
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"github.com/drone/drone/engine"
|
||||
"github.com/drone/drone/router/middleware/context"
|
||||
"github.com/drone/drone/router/middleware/session"
|
||||
"github.com/drone/drone/store"
|
||||
|
||||
@@ -20,7 +19,7 @@ import (
|
||||
// GetRepoEvents will upgrade the connection to a Websocket and will stream
|
||||
// event updates to the browser.
|
||||
func GetRepoEvents(c *gin.Context) {
|
||||
engine_ := context.Engine(c)
|
||||
engine_ := engine.FromContext(c)
|
||||
repo := session.Repo(c)
|
||||
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
||||
|
||||
@@ -55,7 +54,7 @@ func GetRepoEvents(c *gin.Context) {
|
||||
|
||||
func GetStream(c *gin.Context) {
|
||||
|
||||
engine_ := context.Engine(c)
|
||||
engine_ := engine.FromContext(c)
|
||||
repo := session.Repo(c)
|
||||
buildn, _ := strconv.Atoi(c.Param("build"))
|
||||
jobn, _ := strconv.Atoi(c.Param("number"))
|
||||
|
Reference in New Issue
Block a user