-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
115 lines (100 loc) · 3.11 KB
/
main.go
File metadata and controls
115 lines (100 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Package main provides a sample test runner for live API testing.
// WARNING: DO NOT RUN - requires live credentials and makes real API calls
// to GitHub, Okta, and Slack. Use cmd/verify for offline testing instead.
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"github.com/joho/godotenv"
"github.com/cruxstack/github-ops-app/internal/app"
"github.com/cruxstack/github-ops-app/internal/config"
)
func main() {
logger := config.NewLogger()
ctx := context.Background()
envpath := filepath.Join(".env")
logger.Debug("loading environment", slog.String("path", envpath))
if _, err := os.Stat(envpath); err == nil {
_ = godotenv.Load(envpath)
}
cfg, err := config.NewConfig()
if err != nil {
logger.Error("failed to load config", slog.String("error", err.Error()))
os.Exit(1)
}
a, err := app.NewApp(ctx, cfg, logger)
if err != nil {
logger.Error("failed to initialize app", slog.String("error", err.Error()))
os.Exit(1)
}
router := a.Handler()
path := filepath.Join("fixtures", "samples.json")
raw, err := os.ReadFile(path)
if err != nil {
logger.Error("failed to read samples file", slog.String("error", err.Error()))
os.Exit(1)
}
var samples []map[string]any
if err := json.Unmarshal(raw, &samples); err != nil {
logger.Error("failed to parse samples json", slog.String("error", err.Error()))
os.Exit(1)
}
for i, sample := range samples {
eventType, ok := sample["event_type"].(string)
if !ok {
logger.Error("missing or invalid event_type", slog.Int("sample", i))
os.Exit(1)
}
switch eventType {
case "okta_sync":
reqPath := fmt.Sprintf("%s/scheduled/okta-sync", cfg.BasePath)
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, reqPath, nil)
if err != nil {
logger.Error("failed to construct http request",
slog.Int("sample", i),
slog.String("error", err.Error()))
os.Exit(1)
}
if cfg.AdminToken != "" {
httpReq.Header.Set("Authorization", "Bearer "+cfg.AdminToken)
}
rec := httptest.NewRecorder()
router.ServeHTTP(rec, httpReq)
if rec.Code >= 400 {
logger.Error("failed to process okta_sync sample",
slog.Int("sample", i),
slog.String("response", rec.Body.String()))
os.Exit(1)
}
case "pr_webhook":
payload, _ := json.Marshal(sample["payload"])
reqPath := fmt.Sprintf("%s/webhooks", cfg.BasePath)
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, reqPath, bytes.NewReader(payload))
if err != nil {
logger.Error("failed to construct http request",
slog.Int("sample", i),
slog.String("error", err.Error()))
os.Exit(1)
}
httpReq.Header.Set("X-GitHub-Event", "pull_request")
rec := httptest.NewRecorder()
router.ServeHTTP(rec, httpReq)
if rec.Code >= 400 {
logger.Error("failed to process pr_webhook sample",
slog.Int("sample", i),
slog.String("response", rec.Body.String()))
os.Exit(1)
}
default:
logger.Info("skipping unknown event type", slog.String("event_type", eventType))
}
logger.Info("processed sample successfully", slog.Int("sample", i))
}
}