-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
114 lines (89 loc) · 2.85 KB
/
plugin.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
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
110
111
112
113
114
package main
import (
"encoding/json"
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/google/go-github/v44/github"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
)
const pluginName = "github.com/nickofthyme/pull-request-protector-buildkite-plugin"
type Plugin struct {
Users *[]string `json:"users,omitempty"`
Teams *[]string `json:"teams,omitempty"`
Member bool `json:"member"`
Collaborator bool `json:"collaborator"`
Verified bool `json:"verified"`
Files []string `json:"files"`
BlockStep BlockStep `json:"block_step"`
GHTokenKey string `json:"gh_token_env"`
Debug bool `json:"debug"`
GHAuthKey *string `json:"gh_auth_env"`
}
var defaultBlockStep = BlockStep{
Prompt: github.String("Default block step"),
Type: "block",
}
func getPluginS() *Plugin {
pluginMappings := []map[string]interface{}{}
pluginStr, _ := getEnvString("BUILDKITE_PLUGINS", true)
json.Unmarshal([]byte(pluginStr), &pluginMappings)
prpPlugin := &Plugin{
Debug: false,
Member: true,
Collaborator: true,
Verified: true,
GHTokenKey: "GITHUB_TOKEN",
Files: []string{".buildkite/**"},
BlockStep: defaultBlockStep,
}
for _, plugins := range pluginMappings {
for key, plugin := range plugins {
if strings.HasPrefix(key, pluginName) {
pluginBlob, errMarshal := json.Marshal(plugin)
if errMarshal != nil {
log.Fatalln(errors.Wrap(errMarshal, "failed to marshal pull-request-protector plugin configuration"))
}
err := json.Unmarshal(pluginBlob, &prpPlugin)
if err != nil {
log.Fatalln(errors.Wrap(err, "failed to parse pull-request-protector plugin configuration"))
}
validateBlockStep(prpPlugin.BlockStep)
break
}
}
}
if prpPlugin == nil {
log.Fatalln("Error parsing pull-request-protector plugin configuration")
}
return prpPlugin
}
func validateBlockStep(blockConfig BlockStep) {
stepJsonBlob, err := json.Marshal(blockConfig)
prettyJson(blockConfig, "Block step config")
if err != nil {
log.Fatalln(errors.Wrap(err, "Error attemptting to Marshal block step blob"))
}
stepStr := string(stepJsonBlob)
schemaLoader := gojsonschema.NewReferenceLoader("https://raw.githubusercontent.com/buildkite/pipeline-schema/master/schema.json#/definitions/blockStep")
documentLoader := gojsonschema.NewStringLoader(stepStr)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
log.Fatalln(errors.Wrap(err, "Error attemptting to validate block step from schema"))
}
if !result.Valid() {
log.Error("❌ The block_step provided fails schema validation, see errors below:\n")
for _, desc := range result.Errors() {
log.Errorf(" - %s\n", desc)
}
os.Exit(1)
}
}
var plugin *Plugin
func getPlugin() *Plugin {
if plugin == nil {
plugin = getPluginS()
}
return plugin
}