Skip to content

Commit 97254f5

Browse files
Options can't be configured as ENV var (#359)
1 parent f0f4f60 commit 97254f5

4 files changed

Lines changed: 108 additions & 31 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,9 @@ A sample configuration file is provided at `config/bulldozer.example.yml`.
370370
Certain values may also be set by environment variables; these are noted in the
371371
comments in the sample configuration file. By default, the environment
372372
variables for server values are prefixed with `BULLDOZER_` (e.g.
373-
`BULLDOZER_PORT`). This prefix can be overridden by setting the
373+
`BULLDOZER_PORT`). For configuring options the prefix is
374+
`BULLDOZER_OPTIONS` (e.g. `BULLDOZER_OPTIONS_APP_NAME`)
375+
This prefix can be overridden by setting the
374376
`BULLDOZER_ENV_PREFIX` environment variable.
375377

376378
### GitHub App Configuration

config/bulldozer.example.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,31 @@ github:
6565
# Options for application behavior
6666
options:
6767
# The path within repositories to find the bulldozer.yml config file
68+
# Can also be set by the BULLDOZER_OPTIONS_CONFIGURATION_PATH environment variable.
6869
configuration_path: .bulldozer.yml
6970

7071
# The name of the application. This will affect the User-Agent header
7172
# when making requests to Github.
73+
# Can also be set by the BULLDOZER_OPTIONS_APP_NAME environment variable.
7274
app_name: bulldozer
7375

76+
# # The name of an organization repository to look in for a shared bulldozer file if
77+
# # a repository does not define a bulldozer file. Can also be set by the
78+
# # BULLDOZER_OPTIONS_SHARED_REPOSITORY environment variable.
79+
# shared_repository: .github
80+
81+
# # The path to the bulldozer file in the shared organization repository.
82+
# # Can also be set by the BULLDOZER_OPTIONS_SHARED_CONFIGURATION_PATH environment variable.
83+
# shared_policy_path: policy.yml
84+
85+
# # To reduce pressure on CI systems and Github, the update feature can be disabled at the
86+
# # server level by specifying the following server option:
87+
# # Can also be set by the BULLDOZER_OPTIONS_DISABLE_UPDATE_FEATURE environment variable.
88+
# disable_update_feature: true
89+
7490
# Deprecated: An optional personal access token associated with a GitHub user
7591
# that is used to merge pull requests into protected branches with push
76-
# restrictions. Can also be set by the BULLDOZER_PUSH_RESTRICTION_USER_TOKEN
92+
# restrictions. Can also be set by the BULLDOZER_OPTIONS_PUSH_RESTRICTION_USER_TOKEN
7793
# environment variable.
7894
#
7995
# This is not necessary on GitHub.com and GitHub Enterprise 2.20+, which

server/config.go

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"os"
1919

2020
"github.com/c2h5oh/datasize"
21-
"github.com/palantir/bulldozer/bulldozer"
21+
"github.com/palantir/bulldozer/server/handler"
2222
"github.com/palantir/go-baseapp/baseapp"
2323
"github.com/palantir/go-baseapp/baseapp/datadog"
2424
"github.com/palantir/go-githubapp/githubapp"
@@ -27,15 +27,13 @@ import (
2727
)
2828

2929
const (
30-
DefaultEnvPrefix = "BULLDOZER_"
31-
DefaultSharedRepository = ".github"
32-
DefaultSharedConfigurationPath = "bulldozer.yml"
30+
DefaultEnvPrefix = "BULLDOZER_"
3331
)
3432

3533
type Config struct {
3634
Server baseapp.HTTPConfig `yaml:"server"`
3735
Github githubapp.Config `yaml:"github"`
38-
Options Options `yaml:"options"`
36+
Options handler.Options `yaml:"options"`
3937
Logging LoggingConfig `yaml:"logging"`
4038
Datadog datadog.Config `yaml:"datadog"`
4139
Cache CacheConfig `yaml:"cache"`
@@ -56,20 +54,6 @@ type WorkerConfig struct {
5654
QueueSize int `yaml:"queue_size"`
5755
}
5856

59-
type Options struct {
60-
AppName string `yaml:"app_name"`
61-
PushRestrictionUserToken string `yaml:"push_restriction_user_token"`
62-
63-
ConfigurationPath string `yaml:"configuration_path"`
64-
SharedRepository string `yaml:"shared_repository"`
65-
SharedConfigurationPath string `yaml:"shared_configuration_path"`
66-
DefaultRepositoryConfig *bulldozer.Config `yaml:"default_repository_config"`
67-
68-
ConfigurationV0Paths []string `yaml:"configuration_v0_paths"`
69-
70-
DisableUpdateFeature bool `yaml:"disable_update_feature"`
71-
}
72-
7357
func ParseConfig(bytes []byte) (*Config, error) {
7458
var c Config
7559
if err := yaml.UnmarshalStrict(bytes, &c); err != nil {
@@ -84,16 +68,7 @@ func ParseConfig(bytes []byte) (*Config, error) {
8468
}
8569
c.Server.SetValuesFromEnv(envPrefix)
8670

87-
if v, ok := os.LookupEnv(envPrefix + "PUSH_RESTRICTION_USER_TOKEN"); ok {
88-
c.Options.PushRestrictionUserToken = v
89-
}
90-
91-
if c.Options.SharedRepository == "" {
92-
c.Options.SharedRepository = DefaultSharedRepository
93-
}
94-
if c.Options.SharedConfigurationPath == "" {
95-
c.Options.SharedConfigurationPath = DefaultSharedConfigurationPath
96-
}
71+
c.Options.SetValuesFromEnv(envPrefix + "OPTIONS_")
9772

9873
return &c, nil
9974
}

server/handler/options.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2022 Palantir Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package handler
16+
17+
import (
18+
"os"
19+
"strconv"
20+
21+
"github.com/palantir/bulldozer/bulldozer"
22+
)
23+
24+
const (
25+
DefaultSharedRepository = ".github"
26+
DefaultConfigurationPath = ".bulldozer.yml"
27+
DefaultSharedConfigurationPath = "bulldozer.yml"
28+
DefaultAppName = "bulldozer"
29+
)
30+
31+
type Options struct {
32+
AppName string `yaml:"app_name"`
33+
PushRestrictionUserToken string `yaml:"push_restriction_user_token"`
34+
35+
ConfigurationPath string `yaml:"configuration_path"`
36+
SharedRepository string `yaml:"shared_repository"`
37+
SharedConfigurationPath string `yaml:"shared_configuration_path"`
38+
DefaultRepositoryConfig *bulldozer.Config `yaml:"default_repository_config"`
39+
40+
ConfigurationV0Paths []string `yaml:"configuration_v0_paths"`
41+
42+
DisableUpdateFeature bool `yaml:"disable_update_feature"`
43+
}
44+
45+
func (o *Options) fillDefaults() {
46+
if o.ConfigurationPath == "" {
47+
o.ConfigurationPath = DefaultConfigurationPath
48+
}
49+
if o.AppName == "" {
50+
o.AppName = DefaultAppName
51+
}
52+
if o.SharedRepository == "" {
53+
o.SharedRepository = DefaultSharedRepository
54+
}
55+
if o.SharedConfigurationPath == "" {
56+
o.SharedConfigurationPath = DefaultSharedConfigurationPath
57+
}
58+
}
59+
60+
func (o *Options) SetValuesFromEnv(prefix string) {
61+
setStringFromEnv("CONFIGURATION_PATH", prefix, &o.ConfigurationPath)
62+
setStringFromEnv("APP_NAME", prefix, &o.AppName)
63+
setStringFromEnv("SHARED_REPOSITORY", prefix, &o.SharedRepository)
64+
setStringFromEnv("SHARED_CONFIGURATION_PATH", prefix, &o.SharedConfigurationPath)
65+
setBooleanFromEnv("DISABLE_UPDATE_FEATURE", prefix, &o.DisableUpdateFeature)
66+
setStringFromEnv("PUSH_RESTRICTION_USER_TOKEN", prefix, &o.PushRestrictionUserToken)
67+
o.fillDefaults()
68+
}
69+
70+
func setStringFromEnv(key, prefix string, value *string) bool {
71+
if v, ok := os.LookupEnv(prefix + key); ok {
72+
*value = v
73+
return true
74+
}
75+
return false
76+
}
77+
78+
func setBooleanFromEnv(key, prefix string, value *bool) bool {
79+
if v, ok := os.LookupEnv(prefix + key); ok {
80+
*value, _ = strconv.ParseBool(v)
81+
return true
82+
}
83+
return false
84+
}

0 commit comments

Comments
 (0)