From 35d9f224f28cec4319ae09a806649d349d665989 Mon Sep 17 00:00:00 2001 From: "cory.murphy" Date: Sun, 20 Oct 2024 07:56:14 -0400 Subject: [PATCH] add support for status checks --- charts/argobot/Chart.yaml | 4 ++-- charts/argobot/values.yaml | 3 ++- pkg/cli/start.go | 2 +- pkg/env/config.go | 3 ++- pkg/events/issue_comment_handler.go | 24 ++++++++++++++++++++++-- pkg/github/vsc_client.go | 10 ++++++++-- pkg/server/server_test.go | 19 +++++++++++++------ version | 2 +- 8 files changed, 51 insertions(+), 16 deletions(-) diff --git a/charts/argobot/Chart.yaml b/charts/argobot/Chart.yaml index e68e05b..e4f43f7 100644 --- a/charts/argobot/Chart.yaml +++ b/charts/argobot/Chart.yaml @@ -2,5 +2,5 @@ apiVersion: v2 name: argobot description: Helm chart for the corymurphy/argobot app type: application -version: 0.16.5 -appVersion: 0.16.5 +version: 0.17.0 +appVersion: 0.17.0 diff --git a/charts/argobot/values.yaml b/charts/argobot/values.yaml index 71d0983..cf429d9 100644 --- a/charts/argobot/values.yaml +++ b/charts/argobot/values.yaml @@ -45,7 +45,8 @@ config: | integration_id: 77423 privateKeyFilePath: /app/key.pem - argoCdUrl: http://argocd-server.argocd.svc.cluster.local:80 + argoCdApiUrl: http://argocd-server.argocd.svc.cluster.local:80 + argoCdWebUrl: http://localhost:8081 webServer: logLevel: info diff --git a/pkg/cli/start.go b/pkg/cli/start.go index 6f57ef7..f67c218 100644 --- a/pkg/cli/start.go +++ b/pkg/cli/start.go @@ -49,7 +49,7 @@ var run = &cobra.Command{ } argoClient := &argocd.ApplicationsClient{ - BaseUrl: config.ArgoCdUrl, + BaseUrl: config.ArgoCdApiUrl, } if apiKey, exists := os.LookupEnv("ARGOBOT_ARGOCD_API_KEY"); exists { diff --git a/pkg/env/config.go b/pkg/env/config.go index 270a2bd..d6b8183 100644 --- a/pkg/env/config.go +++ b/pkg/env/config.go @@ -6,6 +6,7 @@ type Config struct { Server HTTPConfig `yaml:"server"` Github githubapp.Config `yaml:"github"` - ArgoCdUrl string `yaml:"argoCdUrl"` + ArgoCdApiUrl string `yaml:"argoCdApiUrl"` + ArgoCdWebUrl string `yaml:"argoCdWebUrl"` PrivateKeyFilePath string `yaml:"privateKeyFilePath"` } diff --git a/pkg/events/issue_comment_handler.go b/pkg/events/issue_comment_handler.go index 1a83150..1d3dd61 100644 --- a/pkg/events/issue_comment_handler.go +++ b/pkg/events/issue_comment_handler.go @@ -39,6 +39,7 @@ func (h *IssueCommentHandler) Handle(ctx context.Context, eventType string, deli installationID := githubapp.GetInstallationIDFromEvent(&issue) client, err := h.NewInstallationClient(installationID) + vscClient := vsc.NewClient(client, h.Log) if err != nil { return err } @@ -84,7 +85,7 @@ func (h *IssueCommentHandler) Handle(ctx context.Context, eventType string, deli } } - commenter := vsc.NewCommenter(client, h.Log, ctx) + commenter := vsc.NewCommenter(client, h.Log, context.TODO()) if comment.Command.Name == command.Plan { planner := argocd.NewPlanner(&h.ArgoClient, h.Log) @@ -112,6 +113,15 @@ func (h *IssueCommentHandler) Handle(ctx context.Context, eventType string, deli if err != nil { h.Log.Err(err, fmt.Sprintf("error while planning %s", app)) } + + status := fmt.Sprintf("argobot/plan %s", app) + description := "planned successfully" + url := fmt.Sprintf("http://localhost:8081/applications/argocd/%s", app) // TODO + + err = vscClient.SetStatusCheck(context.TODO(), event, vsc.SuccessCommitState, status, description, url) + if err != nil { + h.Log.Err(err, fmt.Sprintf("error while setting status check for %s", app)) + } } return nil } @@ -123,7 +133,7 @@ func (h *IssueCommentHandler) Handle(ctx context.Context, eventType string, deli return nil } if !comment.Command.ExplicitApplication { - h.Log.Info("apply does not supply auto discovery. an application must be explicitly defined.") + h.Log.Info("apply does not support auto discovery. an application must be explicitly defined.") return nil } go func() { @@ -136,6 +146,16 @@ func (h *IssueCommentHandler) Handle(ctx context.Context, eventType string, deli h.Log.Err(err, "unable to apply") return } + + status := fmt.Sprintf("argobot/apply %s", app) + description := "applied successfully" + url := fmt.Sprintf("%s/applications/argocd/%s", h.Config.ArgoCdWebUrl, app) // TODO + + err = vscClient.SetStatusCheck(context.TODO(), event, vsc.SuccessCommitState, status, description, url) + if err != nil { + h.Log.Err(err, fmt.Sprintf("error while setting status check for %s", app)) + } + comment := fmt.Sprintf("apply result for `%s`\n\n", app) + "```\n" + response.Message + "\n```" err = commenter.Comment(&event, &comment) if err != nil { diff --git a/pkg/github/vsc_client.go b/pkg/github/vsc_client.go index 9dabf4f..6a9738e 100644 --- a/pkg/github/vsc_client.go +++ b/pkg/github/vsc_client.go @@ -12,9 +12,15 @@ type VscClient struct { logger logging.SimpleLogging } -func (v *VscClient) SetStatusCheck(ctx context.Context, event Event, state CommitState, context string, description string) error { +func NewClient(client *github.Client, logger logging.SimpleLogging) *VscClient { + return &VscClient{ + client: client, + logger: logger, + } +} + +func (v *VscClient) SetStatusCheck(ctx context.Context, event Event, state CommitState, context string, description string, url string) error { - url := "" status := &github.RepoStatus{ State: github.String(state.String()), Description: github.String(description), diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go index 6690ff8..7dccb85 100644 --- a/pkg/server/server_test.go +++ b/pkg/server/server_test.go @@ -40,12 +40,13 @@ func Test_HealthCheck(t *testing.T) { func Test_PRCommentHandler(t *testing.T) { requests := map[string]bool{ - "/app/installations/345345345/access_tokens": false, - "/repos/atlas8518/argocd-data/issues/1/comments": false, - "/repos/atlas8518/argocd-data/pulls/1": false, - "/api/v1/applications/testapp/managed-resources": false, - "/api/v1/applications/testapp": false, - "/repos/atlas8518/argocd-data/issues/comments/456456/reactions": false, + "/app/installations/345345345/access_tokens": false, + "/repos/atlas8518/argocd-data/issues/1/comments": false, + "/repos/atlas8518/argocd-data/pulls/1": false, + "/api/v1/applications/testapp/managed-resources": false, + "/api/v1/applications/testapp": false, + "/repos/atlas8518/argocd-data/issues/comments/456456/reactions": false, + "/repos/atlas8518/argocd-data/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e": false, } mu := sync.Mutex{} mockServer := mockServer(requests, &mu, t) @@ -244,6 +245,12 @@ func mockServer(requests map[string]bool, mu *sync.Mutex, t *testing.T) *httptes mu.Unlock() fmt.Fprint(w, string("")) }) + router.HandleFunc("/repos/atlas8518/argocd-data/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e", func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + requests["/repos/atlas8518/argocd-data/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e"] = true + mu.Unlock() + fmt.Fprint(w, string("")) + }) router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { t.Error(r) }) diff --git a/version b/version index 1927038..c5523bd 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.16.5 +0.17.0