Skip to content

Commit

Permalink
refactor to use separate handlers based on event type
Browse files Browse the repository at this point in the history
  • Loading branch information
corymurphy committed Oct 15, 2024
1 parent c33a0e3 commit 4f471d3
Show file tree
Hide file tree
Showing 23 changed files with 495 additions and 251 deletions.
4 changes: 2 additions & 2 deletions charts/argobot/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: argobot
description: Helm chart for the corymurphy/argobot app
type: application
version: 0.15.0
appVersion: 0.15.0
version: 0.16.0
appVersion: 0.16.0
2 changes: 2 additions & 0 deletions charts/argobot/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ spec:
readOnly: true
{{- end }}
env:
- name: ARGOBOT_LOG_LEVEL
value: {{ .Values.webServer.logLevel | default "info" }}
- name: ARGOBOT_GH_WEBHOOK_SECRET
valueFrom:
secretKeyRef:
Expand Down
1 change: 1 addition & 0 deletions charts/argobot/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ config: |
argoCdUrl: http://argocd-server.argocd.svc.cluster.local:80
webServer:
logLevel: info
resources:
requests:
cpu: 250m
Expand Down
48 changes: 48 additions & 0 deletions pkg/argocd/planner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package argocd

import (
"context"

"github.com/corymurphy/argobot/pkg/logging"
)

type Planner struct {
ArgoClient *ApplicationsClient
Log logging.SimpleLogging
}

func NewPlanner(client *ApplicationsClient, log logging.SimpleLogging) *Planner {
return &Planner{
ArgoClient: client,
Log: log,
}
}

func (p *Planner) Plan(ctx context.Context, name string, revision string) (string, bool, error) {
var plan string
var diff bool = false

resources, err := p.ArgoClient.ManagedResources(name)

if err != nil {
return plan, diff, err
}

live, err := p.ArgoClient.Get(name)

if err != nil {
return plan, diff, err
}

target, err := p.ArgoClient.GetManifest(name, revision)
if err != nil {
return plan, diff, err
}

settings, err := p.ArgoClient.GetSettings()
if err != nil {
return plan, diff, err
}

return Plan(ctx, &settings, live, resources, target, revision, p.Log)
}
10 changes: 8 additions & 2 deletions pkg/assert/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package assert
import (
"fmt"
"testing"

"github.com/pkg/errors"
)

type TestLogger struct {
Expand Down Expand Up @@ -32,6 +34,10 @@ func (t *TestLogger) Warn(format string, a ...interface{}) {
t.log(format, a...)
}

func (t *TestLogger) Err(format string, a ...interface{}) {
t.log(format, a...)
func (t *TestLogger) Err(err error, message string) {
t.t.Logf("%v", errors.Wrap(err, message))
}

// func (t *TestLogger) Err(format string, a ...interface{}) {
// t.log(format, a...)
// }
12 changes: 11 additions & 1 deletion pkg/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

const (
WebhookSecretEnvVar = "ARGOBOT_GH_WEBHOOK_SECRET"
LogLevelEnvVar = "ARGOBOT_LOG_LEVEL"
DefaultLogLevel = logging.Info
)

var (
Expand All @@ -38,6 +40,14 @@ var run = &cobra.Command{
config.Github.App.PrivateKey = string(content)
config.Github.App.WebhookSecret = os.Getenv(WebhookSecretEnvVar)

logLevel := DefaultLogLevel
if serializedLevel, exists := os.LookupEnv(LogLevelEnvVar); exists {
logLevel, err = logging.GetLogLevel(serializedLevel)
if err != nil {
panic(err)
}
}

argoClient := &argocd.ApplicationsClient{
BaseUrl: config.ArgoCdUrl,
}
Expand All @@ -48,7 +58,7 @@ var run = &cobra.Command{

server.NewServer(
config,
logging.NewLogger(logging.Info),
logging.NewLogger(logLevel),
argoClient,
).Start()
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/events/application_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewApplicationResolver(githubClient *gogithub.Client, argocdClient *argocd.
}
}

func (a *ApplicationResolver) FindApplicationNames(ctx context.Context, command *CommentCommand, event github.Event) ([]string, error) {
func (a *ApplicationResolver) FindApplicationNames(ctx context.Context, event github.Event) ([]string, error) {
var changedApps []string

modified, err := a.GetModifiedFiles(ctx, event)
Expand Down Expand Up @@ -57,7 +57,7 @@ func (a *ApplicationResolver) FindApplicationNames(ctx context.Context, command

// copied from atlantis
func (a *ApplicationResolver) GetModifiedFiles(ctx context.Context, event github.Event) ([]string, error) {
a.Log.Debug("Getting modified files for GitHub pull request %d")
a.Log.Debug("Getting modified files for GitHub pull request")
var files []string
nextPage := 0

Expand Down
4 changes: 2 additions & 2 deletions pkg/events/apply_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewApplyRunner(vcsClient *github.Client, config *env.Config, log logging.Si
}

// TODO: validate that the PR is in an approved/mergeable state
func (a *ApplyRunner) Run(ctx context.Context, cmd *CommentCommand, event vsc.Event) (CommentResponse, error) {
func (a *ApplyRunner) Run(ctx context.Context, app string, event vsc.Event) (CommentResponse, error) {
var resp CommentResponse

status, err := vsc.NewPullRequestStatusFetcher(ctx, a.Log, a.vcsClient).Fetch(event)
Expand All @@ -43,7 +43,7 @@ func (a *ApplyRunner) Run(ctx context.Context, cmd *CommentCommand, event vsc.Ev
return NewCommentResponse("pull request must be approved and in a mergeable state", event), nil
}

apply, err := a.ApplyClient.Apply(cmd.Application, event.Revision)
apply, err := a.ApplyClient.Apply(app, event.Revision)
if err != nil {
return resp, fmt.Errorf("argoclient failed while applying %w", err)
}
Expand Down
17 changes: 13 additions & 4 deletions pkg/events/comment_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ package events

import "github.com/corymurphy/argobot/pkg/command"

func NewAutoRunCommand(apps []string, name command.Name) *CommentCommand {
return &CommentCommand{
Flags: []string{},
Name: name,
Applications: apps,
ExplicitApplication: false,
}
}

type CommentCommand struct {
Flags []string
Name command.Name
Application string
Applications []string
Flags []string
Name command.Name
Applications []string
ExplicitApplication bool
}
194 changes: 0 additions & 194 deletions pkg/events/comment_handler.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/events/comment_handler_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/events/comment_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ func (c *CommentParser) Parse(event vsc.Event) *CommentParseResult {

return &CommentParseResult{
Command: &CommentCommand{
Name: name,
Application: app,
Applications: []string{app},
Name: name,
Applications: []string{app},
ExplicitApplication: true,
},
}
}
Expand Down
Loading

0 comments on commit 4f471d3

Please sign in to comment.