Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support env vars for options in server config #364

Merged
merged 4 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions config/policy-bot.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ server:
logging:
# If true, logs are printed in human-readable form. We recommend using
# "false" to output JSON-formatted logs in production
# Can also be set by the POLICYBOT_LOG_PRETTY environment variable.
text: false
# Set a minimum logging level threshold
# Choose from: debug, info, warn, error
# Can also be set by the POLICYBOT_LOG_LEVEL environment variable.
level: debug

# Options for the GitHub response cache. When the cache reaches max_size, the
Expand Down Expand Up @@ -78,17 +80,21 @@ sessions:
# Options for application behavior. The defaults are shown below.
#
# options:
# # The path to the policy file in a repository.
# # The path to the policy file in a repository. Can also be set by the
# # POLICYBOT_POLICY_PATH environment variable.
# policy_path: .policy.yml
#
# # The name of an organization repository to look in for a shared policy if
# # a repository does not define a policy file.
# # a repository does not define a policy file. Can also be set by the
# # POLICYBOT_SHARED_REPOSITORY environment variable.
# shared_repository: .github
#
# # The path to the policy file in the shared organization repository.
# # Can also be set by the POLICYBOT_SHARED_POLICY_PATH environment variable.
# shared_policy_path: policy.yml
#
# # The context prefix for status checks created by the bot.
# # The context prefix for status checks created by the bot. Can also be set by the
# # POLICYBOT_STATUS_CHECK_CONTEXT environment variable.
# status_check_context: policy-bot

# Options for locating the frontend files. By default, the server uses appropriate
Expand Down
2 changes: 1 addition & 1 deletion server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func ParseConfig(bytes []byte) (*Config, error) {
return nil, errors.Wrapf(err, "failed unmarshalling yaml")
}

c.Options.FillDefaults()
c.Options.SetValuesFromEnv("POLICYBOT_OPTIONS_")
c.Server.SetValuesFromEnv("POLICYBOT_")
c.Github.SetValuesFromEnv("")

Expand Down
19 changes: 18 additions & 1 deletion server/handler/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"math/rand"
"os"
"strings"
"time"

Expand Down Expand Up @@ -75,7 +76,7 @@ type PullEvaluationOptions struct {
Deprecated_AppName string `yaml:"app_name"`
}

func (p *PullEvaluationOptions) FillDefaults() {
func (p *PullEvaluationOptions) fillDefaults() {
if p.PolicyPath == "" {
p.PolicyPath = DefaultPolicyPath
}
Expand All @@ -91,6 +92,14 @@ func (p *PullEvaluationOptions) FillDefaults() {
}
}

func (p *PullEvaluationOptions) SetValuesFromEnv(prefix string) {
setStringFromEnv("POLICY_PATH", prefix, &p.PolicyPath)
setStringFromEnv("SHARED_REPOSITORY", prefix, &p.SharedRepository)
setStringFromEnv("SHARED_POLICY_PATH", prefix, &p.SharedPolicyPath)
setStringFromEnv("STATUS_CHECK_CONTEXT", prefix, &p.StatusCheckContext)
p.fillDefaults()
}
Copy link
Contributor Author

@devinburnette devinburnette Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think maybe this is the better way mirroring your pattern from go-baseapp and go-githubapp, but i felt like these options weren't suited to live in those codebases. so i put them here instead.

only thing i felt weird about was setting the defaults here, probably fine, but let me know what you think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's a bit weird, but I think it's fine since this is only used in one place.


func (b *Base) PostStatus(ctx context.Context, prctx pull.Context, client *github.Client, state, message string) {
logger := zerolog.Ctx(ctx)

Expand Down Expand Up @@ -381,3 +390,11 @@ func selectionToReviewersRequest(s reviewer.Selection) github.ReviewersRequest {

return req
}

func setStringFromEnv(key, prefix string, value *string) bool {
if v, ok := os.LookupEnv(prefix + key); ok {
*value = v
return true
}
return false
}