|
|
@@ -63,7 +63,7 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- env, err := c.Repo().Environment().CreateEnvironment(&models.Environment{
|
|
|
+ env := &models.Environment{
|
|
|
ProjectID: project.ID,
|
|
|
ClusterID: cluster.ID,
|
|
|
GitInstallationID: uint(ga.InstallationID),
|
|
|
@@ -72,25 +72,20 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
|
|
|
GitRepoName: name,
|
|
|
Mode: request.Mode,
|
|
|
WebhookID: string(webhookUID),
|
|
|
- })
|
|
|
-
|
|
|
- if err != nil {
|
|
|
- c.deleteEnvAndReportError(w, r, env, err)
|
|
|
- return
|
|
|
}
|
|
|
|
|
|
// write Github actions files to the repo
|
|
|
client, err := getGithubClientFromEnvironment(c.Config(), env)
|
|
|
|
|
|
if err != nil {
|
|
|
- c.deleteEnvAndReportError(w, r, env, err)
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- webhookURL := fmt.Sprintf("%s/api/github/incoming_webhook/%s", c.Config().ServerConf.ServerURL, string(webhookUID))
|
|
|
+ webhookURL := getGithubWebhookURLFromUID(c.Config().ServerConf.ServerURL, string(webhookUID))
|
|
|
|
|
|
// create incoming webhook
|
|
|
- _, _, err = client.Repositories.CreateHook(
|
|
|
+ hook, _, err := client.Repositories.CreateHook(
|
|
|
r.Context(), owner, name, &github.Hook{
|
|
|
Config: map[string]interface{}{
|
|
|
"url": webhookURL,
|
|
|
@@ -103,7 +98,18 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
|
|
|
)
|
|
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "already exists on this repository") {
|
|
|
- c.deleteEnvAndReportError(w, r, env, err)
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
|
|
|
+ fmt.Errorf("error trying to create a new github repository webhook: %w", err), http.StatusConflict),
|
|
|
+ )
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ env.GithubWebhookID = hook.GetID()
|
|
|
+
|
|
|
+ env, err = c.Repo().Environment().CreateEnvironment(env)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -187,3 +193,7 @@ func getGithubClientFromEnvironment(config *config.Config, env *models.Environme
|
|
|
|
|
|
return github.NewClient(&http.Client{Transport: itr}), nil
|
|
|
}
|
|
|
+
|
|
|
+func getGithubWebhookURLFromUID(serverURL, webhookUID string) string {
|
|
|
+ return fmt.Sprintf("%s/api/github/incoming_webhook/%s", serverURL, string(webhookUID))
|
|
|
+}
|