Skip to content

Commit

Permalink
add support for status checks
Browse files Browse the repository at this point in the history
  • Loading branch information
corymurphy committed Oct 20, 2024
1 parent 3d87aa0 commit 35d9f22
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 16 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.16.5
appVersion: 0.16.5
version: 0.17.0
appVersion: 0.17.0
3 changes: 2 additions & 1 deletion charts/argobot/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
24 changes: 22 additions & 2 deletions pkg/events/issue_comment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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() {
Expand All @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions pkg/github/vsc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
19 changes: 13 additions & 6 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.16.5
0.17.0

0 comments on commit 35d9f22

Please sign in to comment.