/
config.go
110 lines (93 loc) · 3.04 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package config
import (
"context"
"errors"
"os"
"strconv"
"strings"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
gha "github.com/launchdarkly/find-code-references-in-pull-request/internal/github_actions"
)
type Config struct {
LdProject string
LdEnvironment string
LdInstance string
Owner string
Repo string
ApiToken string
Workspace string
GHClient *github.Client
MaxFlags int
PlaceholderComment bool
IncludeArchivedFlags bool
CheckExtinctions bool
CreateFlagLinks bool
}
func ValidateInputandParse(ctx context.Context) (*Config, error) {
// mask tokens
if accessToken := os.Getenv("INPUT_ACCESS-TOKEN"); len(accessToken) > 0 {
gha.MaskInput(accessToken)
}
if repoToken := os.Getenv("INPUT_REPO-TOKEN"); len(repoToken) > 0 {
gha.MaskInput(repoToken)
}
// init config with defaults
config := Config{
MaxFlags: 5,
IncludeArchivedFlags: true,
CheckExtinctions: true,
}
config.LdProject = os.Getenv("INPUT_PROJECT-KEY")
if config.LdProject == "" {
return nil, errors.New("`project-key` is required")
}
if envKey := os.Getenv("INPUT_ENVIRONMENT-KEY"); len(envKey) == 0 {
return nil, errors.New("`environment-key` is required")
} else if strings.Contains(envKey, ",") {
return nil, errors.New("only one `environment-key` is allowed")
} else {
config.LdEnvironment = envKey
}
config.LdInstance = os.Getenv("INPUT_BASE-URI")
if config.LdInstance == "" {
return nil, errors.New("`base-uri` is required.")
}
config.Owner = os.Getenv("GITHUB_REPOSITORY_OWNER")
config.Repo = strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[1]
config.ApiToken = os.Getenv("INPUT_ACCESS-TOKEN")
if config.ApiToken == "" {
return nil, errors.New("`access-token` is required")
}
config.Workspace = os.Getenv("GITHUB_WORKSPACE")
maxFlags, err := strconv.ParseInt(os.Getenv("INPUT_MAX-FLAGS"), 10, 32)
if err != nil {
return nil, err
}
config.MaxFlags = int(maxFlags)
if placholderComment, err := strconv.ParseBool(os.Getenv("INPUT_PLACEHOLDER-COMMENT")); err == nil {
// ignore error - default is false
config.PlaceholderComment = placholderComment
}
if includeArchivedFlags, err := strconv.ParseBool(os.Getenv("INPUT_INCLUDE-ARCHIVED-FLAGS")); err == nil {
// ignore error - default is true
config.IncludeArchivedFlags = includeArchivedFlags
}
if checkExtinctions, err := strconv.ParseBool(os.Getenv("INPUT_CHECK-EXTINCTIONS")); err == nil {
// ignore error - default is true
config.CheckExtinctions = checkExtinctions
}
if createFlagLinks, err := strconv.ParseBool(os.Getenv("INPUT_CREATE-FLAG-LINKS")); err == nil {
// ignore error - default is false
config.CreateFlagLinks = createFlagLinks
}
config.GHClient = getGithubClient(ctx)
return &config, nil
}
func getGithubClient(ctx context.Context) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc)
}