-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh.go
78 lines (71 loc) · 1.49 KB
/
gh.go
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
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"os"
"strings"
)
func dl(fullURLFile string) chan string {
l := log.New(os.Stdout, "[dwnload] "+fullURLFile+": ", 0)
r := make(chan string)
go func() {
//? https://golangdocs.com/golang-download-files
client := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
}
resp, err := client.Get(fullURLFile)
if err != nil {
l.Fatal(err)
}
l.Printf("got %d", resp.StatusCode)
defer resp.Body.Close()
s, err := io.ReadAll(resp.Body)
if err != nil {
l.Fatal(err)
}
r <- string(s)
}()
return r
}
type GhCommit struct {
Modified []string
}
type GhResp struct {
Ref string
Commits []GhCommit
}
func gh(req *http.Request) int {
if req.Header.Get("X-Hub-Signature") != os.Getenv("GH_WEBHOOK_SECRET") {
return 403
}
if req.Header.Get("X-GitHub-Event") != "push" {
return 400
}
decoder := json.NewDecoder(req.Body)
var resp GhResp
decoder.Decode(&resp)
if resp.Ref != "refs/heads/main" { // for the time being
return 204
}
var files []string
for _, commit := range resp.Commits {
files = append(files, commit.Modified...)
}
var chls []chan string
for _, file := range files {
if !strings.HasSuffix(file, "/pat") {
continue
}
chls = append(chls, dl("https://raw.githubusercontent.com/terrapkg/packages/"+resp.Ref+"/"+file))
}
for _, chl := range chls {
content := <-chl
g_anitya_ch <- content
}
return 204
}