-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
92 lines (70 loc) · 2.11 KB
/
config.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
package main
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
)
type Config struct {
DiscordWebhookUrl string `env:"DISCORD_WEBHOOK_URL" validate:"required,url"`
GitHubActor string `env:"GITHUB_ACTOR"`
GitHubJobName string `env:"GITHUB_JOB_NAME"`
GitHubJobStatus string `env:"GITHUB_JOB_STATUS" validate:"required"`
GitHubRef string `env:"GITHUB_REF"`
GitHubRepository string `env:"GITHUB_REPOSITORY" validate:"required"`
GitHubRunId string `env:"GITHUB_RUN_ID"`
GitHubServerUrl string `env:"GITHUB_SERVER_URL" validate:"required"`
GitHubSha string `env:"GITHUB_SHA" validate:"required"`
GitHubWorkflow string `env:"GITHUB_WORKFLOW" validate:"required"`
}
func NewConfig(v *Validator) (*Config, error) {
c := Config{}
v, err := NewValidator()
if err != nil {
return nil, err
}
t := reflect.TypeOf(c)
vp := reflect.ValueOf(&c)
for i := 0; i < t.NumField(); i++ {
ti := t.Field(i)
vpi := vp.Elem().Field(i)
envVarKey := ti.Tag.Get("env")
if envVarKey != "" {
envVarValue := os.Getenv(envVarKey)
vpi.SetString(envVarValue)
}
}
err = v.Validate.Struct(c)
if err != nil {
output := "There are errors with some environment variables:\n"
for fieldName, fieldMessage := range v.Map(err) {
structField, _ := t.FieldByName(fieldName)
output = output + fmt.Sprintf("* %s: %s\n", structField.Tag.Get("env"), fieldMessage)
}
return nil, errors.New(output)
}
return &c, nil
}
func (c *Config) GetRepositoryUrl() string {
return c.GitHubServerUrl + "/" + c.GitHubRepository
}
func (c *Config) GetRefUrl() string {
if strings.HasPrefix(c.GitHubRef, "refs/heads") {
s := strings.Split(c.GitHubRef, "/")
branchName := s[len(s)-1]
return c.GetRepositoryUrl() + "/tree/" + branchName
}
if strings.HasPrefix(c.GitHubRef, "refs/tags") {
s := strings.Split(c.GitHubRef, "/")
tagName := s[len(s)-1]
return c.GetRepositoryUrl() + "/releases/tags/" + tagName
}
return ""
}
func (c *Config) GetCommitUrl() string {
return c.GetRepositoryUrl() + "/commit/" + c.GitHubSha
}
func (c *Config) GetRunUrl() string {
return c.GetCommitUrl() + "/checks"
}