|
|
@@ -20,6 +20,48 @@ const (
|
|
|
updateAppActionVersion = "v0.1.0"
|
|
|
)
|
|
|
|
|
|
+// HandleGenerateGitAction returns the Github action that will be created in a repository
|
|
|
+// for a given release
|
|
|
+func (app *App) HandleGenerateGitAction(w http.ResponseWriter, r *http.Request) {
|
|
|
+ projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 10, 64)
|
|
|
+
|
|
|
+ if err != nil || projID == 0 {
|
|
|
+ app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ vals, err := url.ParseQuery(r.URL.RawQuery)
|
|
|
+ name := vals["name"][0]
|
|
|
+
|
|
|
+ clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
|
|
|
+ Code: ErrReleaseReadData,
|
|
|
+ Errors: []string{"release not found"},
|
|
|
+ }, w)
|
|
|
+ }
|
|
|
+
|
|
|
+ form := &forms.CreateGitAction{
|
|
|
+ ShouldGenerateOnly: true,
|
|
|
+ }
|
|
|
+
|
|
|
+ // decode from JSON to form value
|
|
|
+ if err := json.NewDecoder(r.Body).Decode(form); err != nil {
|
|
|
+ app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ _, workflowYAML := app.createGitActionFromForm(projID, clusterID, name, form, w, r)
|
|
|
+
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
+
|
|
|
+ if _, err := w.Write(workflowYAML); err != nil {
|
|
|
+ app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// HandleCreateGitAction creates a new Github action in a repository for a given
|
|
|
// release
|
|
|
func (app *App) HandleCreateGitAction(w http.ResponseWriter, r *http.Request) {
|
|
|
@@ -53,7 +95,8 @@ func (app *App) HandleCreateGitAction(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
form := &forms.CreateGitAction{
|
|
|
- ReleaseID: release.Model.ID,
|
|
|
+ Release: release,
|
|
|
+ ShouldGenerateOnly: false,
|
|
|
}
|
|
|
|
|
|
// decode from JSON to form value
|
|
|
@@ -62,7 +105,7 @@ func (app *App) HandleCreateGitAction(w http.ResponseWriter, r *http.Request) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- gaExt := app.createGitActionFromForm(projID, release, name, form, w, r)
|
|
|
+ gaExt, _ := app.createGitActionFromForm(projID, clusterID, name, form, w, r)
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
|
|
@@ -73,17 +116,17 @@ func (app *App) HandleCreateGitAction(w http.ResponseWriter, r *http.Request) {
|
|
|
}
|
|
|
|
|
|
func (app *App) createGitActionFromForm(
|
|
|
- projID uint64,
|
|
|
- release *models.Release,
|
|
|
+ projID,
|
|
|
+ clusterID uint64,
|
|
|
name string,
|
|
|
form *forms.CreateGitAction,
|
|
|
w http.ResponseWriter,
|
|
|
r *http.Request,
|
|
|
-) *models.GitActionConfigExternal {
|
|
|
+) (gaExt *models.GitActionConfigExternal, workflowYAML []byte) {
|
|
|
// validate the form
|
|
|
if err := app.validator.Struct(form); err != nil {
|
|
|
app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
|
|
|
- return nil
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
// if the registry was provisioned through Porter, create a repository if necessary
|
|
|
@@ -93,7 +136,7 @@ func (app *App) createGitActionFromForm(
|
|
|
|
|
|
if err != nil {
|
|
|
app.handleErrorDataRead(err, w)
|
|
|
- return nil
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
_reg := registry.Registry(*reg)
|
|
|
@@ -107,30 +150,22 @@ func (app *App) createGitActionFromForm(
|
|
|
|
|
|
if err != nil {
|
|
|
app.handleErrorInternal(err, w)
|
|
|
- return nil
|
|
|
+ return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // convert the form to a git action config
|
|
|
- gitAction, err := form.ToGitActionConfig(updateAppActionVersion)
|
|
|
-
|
|
|
- if err != nil {
|
|
|
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- repoSplit := strings.Split(gitAction.GitRepo, "/")
|
|
|
+ repoSplit := strings.Split(form.GitRepo, "/")
|
|
|
|
|
|
if len(repoSplit) != 2 {
|
|
|
app.handleErrorFormDecoding(fmt.Errorf("invalid formatting of repo name"), ErrProjectDecode, w)
|
|
|
- return nil
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
session, err := app.Store.Get(r, app.ServerConf.CookieName)
|
|
|
|
|
|
if err != nil {
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
- return nil
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
userID, _ := session.Values["user_id"].(uint)
|
|
|
@@ -142,7 +177,7 @@ func (app *App) createGitActionFromForm(
|
|
|
userID = tok.IBy
|
|
|
} else if tok == nil || tok.IBy == 0 {
|
|
|
http.Error(w, "no user id found in request", http.StatusInternalServerError)
|
|
|
- return nil
|
|
|
+ return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -155,7 +190,7 @@ func (app *App) createGitActionFromForm(
|
|
|
|
|
|
if err != nil {
|
|
|
app.handleErrorInternal(err, w)
|
|
|
- return nil
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
// create the commit in the git repo
|
|
|
@@ -170,21 +205,35 @@ func (app *App) createGitActionFromForm(
|
|
|
Repo: *app.Repo,
|
|
|
GithubConf: app.GithubProjectConf,
|
|
|
ProjectID: uint(projID),
|
|
|
+ ClusterID: uint(clusterID),
|
|
|
ReleaseName: name,
|
|
|
- GitBranch: gitAction.GitBranch,
|
|
|
- DockerFilePath: gitAction.DockerfilePath,
|
|
|
- FolderPath: gitAction.FolderPath,
|
|
|
- ImageRepoURL: gitAction.ImageRepoURI,
|
|
|
+ GitBranch: form.GitBranch,
|
|
|
+ DockerFilePath: form.DockerfilePath,
|
|
|
+ FolderPath: form.FolderPath,
|
|
|
+ ImageRepoURL: form.ImageRepoURI,
|
|
|
PorterToken: encoded,
|
|
|
- ClusterID: release.ClusterID,
|
|
|
- Version: gitAction.Version,
|
|
|
+ Version: updateAppActionVersion,
|
|
|
+ ShouldGenerateOnly: form.ShouldGenerateOnly,
|
|
|
+ ShouldCreateWorkflow: form.ShouldCreateWorkflow,
|
|
|
}
|
|
|
|
|
|
- _, err = gaRunner.Setup()
|
|
|
+ workflowYAML, err = gaRunner.Setup()
|
|
|
|
|
|
if err != nil {
|
|
|
app.handleErrorInternal(err, w)
|
|
|
- return nil
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if form.Release == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // convert the form to a git action config
|
|
|
+ gitAction, err := form.ToGitActionConfig(gaRunner.Version)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
// handle write to the database
|
|
|
@@ -192,20 +241,22 @@ func (app *App) createGitActionFromForm(
|
|
|
|
|
|
if err != nil {
|
|
|
app.handleErrorDataWrite(err, w)
|
|
|
- return nil
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
app.Logger.Info().Msgf("New git action created: %d", ga.ID)
|
|
|
|
|
|
// update the release in the db with the image repo uri
|
|
|
- release.ImageRepoURI = gitAction.ImageRepoURI
|
|
|
+ form.Release.ImageRepoURI = gitAction.ImageRepoURI
|
|
|
|
|
|
- _, err = app.Repo.Release.UpdateRelease(release)
|
|
|
+ _, err = app.Repo.Release.UpdateRelease(form.Release)
|
|
|
|
|
|
if err != nil {
|
|
|
app.handleErrorDataWrite(err, w)
|
|
|
- return nil
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- return ga.Externalize()
|
|
|
+ gaExt = ga.Externalize()
|
|
|
+
|
|
|
+ return
|
|
|
}
|