This repository was archived by the owner on Nov 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdreck.go
More file actions
109 lines (93 loc) · 2.47 KB
/
dreck.go
File metadata and controls
109 lines (93 loc) · 2.47 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package dreck
import (
"bufio"
"bytes"
"strings"
"github.com/miekg/dreck/log"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
yaml "gopkg.in/yaml.v2"
)
// Dreck is a plugin that handles Github Issues and Pull Requests for you.
type Dreck struct {
Next httpserver.Handler
clientID string
key string
owners string
secret string
path string // when should dreck trigger, default to '/dreck'
hmac bool // validate HMAC on the webhook
strategy string // how to merge when we merge
user string // user to use to exec commands
env map[string]string // environment to give to commands
}
// New returns a new, initialized Dreck.
func New() Dreck {
d := Dreck{}
d.owners = ".dreck.yaml"
d.path = "/dreck"
d.strategy = mergeSquash
d.env = make(map[string]string)
return d
}
func (d Dreck) getConfig(owner string, repository string) (*DreckConfig, error) {
var config DreckConfig
buf, err := githubFile(owner, repository, d.owners)
if err != nil {
// failing to grab this file is not an error
log.Infof("Failing to grab %s file: %s", d.owners, err)
} else {
if err := yaml.Unmarshal(buf, &config); err != nil {
return nil, err
}
}
// grap toplevel CODEOWNERS file and parse that
buf, err = githubFile(owner, repository, "CODEOWNERS")
if err != nil {
return nil, err
}
config.CodeOwners, err = parseOwners(buf)
return &config, err
}
func parseOwners(buf []byte) ([]string, error) {
// simple line, by line based format
//
// # In this example, @doctocat owns any files in the build/logs
// # directory at the root of the repository and any of its
// # subdirectories.
// /build/logs/ @doctocat
scanner := bufio.NewScanner(bytes.NewReader(buf))
users := map[string]struct{}{}
for scanner.Scan() {
text := scanner.Text()
if len(text) == 0 {
continue
}
if text[0] == '#' {
continue
}
ele := strings.Fields(text)
if len(ele) == 0 {
continue
}
// ok ele[0] is the path, the rest are (in our case) github usernames prefixed with @
for _, s := range ele[1:] {
if len(s) <= 1 {
continue
}
users[s[1:]] = struct{}{}
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
u := []string{}
for k := range users {
u = append(u, k)
}
return u, nil
}
const (
Aliases = "aliases" // Aliases enables alias expansion.
Exec = "exec" // Exec enables the exec command.
Trigger = "/" // Trigger is the prefix that triggers action from this bot.
)