-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (75 loc) · 1.91 KB
/
main.go
File metadata and controls
90 lines (75 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/go-github/v73/github"
"github.com/pufferpanel/github-runner-scaler/env"
"github.com/redis/go-redis/v9"
"log"
"net/http"
"os"
)
var Label = env.Get("github.label")
var rdb = redis.NewClient(&redis.Options{
Addr: env.Get("redis.host"),
Password: env.Get("redis.password"),
DB: 0, // use default DB
})
var GithubSecretToken = []byte(env.Get("github.secret"))
var webLogger = log.New(os.Stdout, "[Web] ", log.LstdFlags|log.Lmicroseconds)
func main() {
r := gin.Default()
r.POST("/queue", func(c *gin.Context) {
payload, err := github.ValidatePayload(c.Request, GithubSecretToken)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
event, err := github.ParseWebHook(github.WebHookType(c.Request), payload)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
switch event := event.(type) {
case *github.WorkflowJobEvent:
onWorkflowJob(event)
}
c.Status(http.StatusAccepted)
})
StartWorkers()
err := r.Run()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}
func onWorkflowJob(request *github.WorkflowJobEvent) {
if request.WorkflowJob == nil {
return
}
if !contains(request.WorkflowJob.Labels, Label) {
return
}
var queue = ""
if *request.Action == "queued" {
queue = QueueName
} else if *request.Action == "completed" {
queue = DeleteQueueName
}
if queue == "" {
return
}
//this is a job we care about, let's start our queue stuff
//push it to redis, it will handle the queue
webLogger.Printf("Adding %d to queue", *request.WorkflowJob.RunID)
rdb.RPush(context.Background(), queue, fmt.Sprintf("%d", *request.WorkflowJob.RunID))
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}