|
| 1 | +package macro |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/SoureCode/kyx/project" |
| 6 | + "github.com/SoureCode/kyx/shell" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +func SentryDeploysNew(repo string, startedAt, stoppedAt time.Time) { |
| 14 | + p := project.GetProject() |
| 15 | + dir := p.GetDirectory() |
| 16 | + logger := shell.GetLogger() |
| 17 | + |
| 18 | + if !filepath.IsAbs(repo) { |
| 19 | + repo = filepath.Clean(filepath.Join(dir, repo)) |
| 20 | + } |
| 21 | + |
| 22 | + cmd := shell.NewGitCommand("-C", repo, "rev-parse", "HEAD"). |
| 23 | + WithLogger(logger). |
| 24 | + WithLogLevel(3) |
| 25 | + |
| 26 | + if err := cmd.Run(); err != nil { |
| 27 | + logger.Errorln("Error getting git commit hash:", err) |
| 28 | + os.Exit(1) |
| 29 | + } |
| 30 | + |
| 31 | + hash := strings.TrimSpace(cmd.Stdout()) |
| 32 | + env := p.GetEnvironment() |
| 33 | + |
| 34 | + // Only run if Sentry is configured |
| 35 | + sentryUrl, ok := env.Lookup("SENTRY_URL") |
| 36 | + if !ok { |
| 37 | + logger.Infoln("Sentry url not configured, skipping sentry integration") |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if strings.HasSuffix(sentryUrl, "/") { |
| 42 | + sentryUrl = strings.TrimSuffix(sentryUrl, "/") |
| 43 | + } |
| 44 | + |
| 45 | + sentryOrg, ok := env.Lookup("SENTRY_ORG") |
| 46 | + if !ok { |
| 47 | + logger.Infoln("Sentry organization not configured, skipping sentry integration") |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + sentryProject, ok := env.Lookup("SENTRY_PROJECT") |
| 52 | + if !ok { |
| 53 | + logger.Infoln("Sentry project not configured, skipping sentry integration") |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + _, ok = env.Lookup("SENTRY_AUTH_TOKEN") |
| 58 | + if !ok { |
| 59 | + logger.Infoln("Sentry auth token not configured, skipping sentry integration") |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + appEnv, ok := env.Lookup("APP_ENV") |
| 64 | + if !ok { |
| 65 | + logger.Infoln("APP_ENV not configured, skipping sentry integration") |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + cmd = shell.NewSentryCommand( |
| 70 | + "--url="+sentryUrl, |
| 71 | + "releases", "list", |
| 72 | + "--raw", |
| 73 | + "--org="+sentryOrg, |
| 74 | + "--project="+sentryProject, |
| 75 | + ). |
| 76 | + WithLogger(logger). |
| 77 | + WithLogLevel(0) |
| 78 | + |
| 79 | + if err := cmd.Run(); err != nil { |
| 80 | + logger.Errorln("Error listing Sentry releases:", err) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + |
| 84 | + releases := strings.TrimSpace(cmd.Stdout()) |
| 85 | + |
| 86 | + if !strings.Contains(releases, hash) { |
| 87 | + cmd = shell.NewSentryCommand( |
| 88 | + "releases", "new", |
| 89 | + "--url="+sentryUrl, |
| 90 | + "--org="+sentryOrg, |
| 91 | + "--project="+sentryProject, |
| 92 | + hash, |
| 93 | + ). |
| 94 | + WithLogger(logger). |
| 95 | + WithLogLevel(0) |
| 96 | + |
| 97 | + if err := cmd.Run(); err != nil { |
| 98 | + logger.Errorln("Error creating new Sentry release:", err) |
| 99 | + os.Exit(1) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + cmd = shell.NewSentryCommand( |
| 104 | + "--url="+sentryUrl, |
| 105 | + "deploys", "new", |
| 106 | + // "--url="+filepath.Base(filepath.Dir(dir)), // @todo strategy to get the URL |
| 107 | + "--org="+sentryOrg, |
| 108 | + "--project="+sentryProject, |
| 109 | + "--started="+startedAt.Format(time.RFC3339), |
| 110 | + "--finished="+stoppedAt.Format(time.RFC3339), |
| 111 | + "--release="+hash, |
| 112 | + "--env="+appEnv, |
| 113 | + ). |
| 114 | + WithLogger(logger). |
| 115 | + WithLogLevel(0) |
| 116 | + |
| 117 | + if err := cmd.Run(); err != nil { |
| 118 | + fmt.Fprintf(os.Stderr, "Error executing sentry-cli: %v\n", err) |
| 119 | + os.Exit(cmd.ExitCode()) |
| 120 | + } |
| 121 | +} |
0 commit comments