| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- package api
- import (
- "encoding/json"
- "fmt"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "github.com/go-chi/chi"
- "github.com/porter-dev/porter/internal/auth/token"
- "github.com/porter-dev/porter/internal/forms"
- "github.com/porter-dev/porter/internal/integrations/ci/actions"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/registry"
- )
- 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)
- return
- }
- 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) {
- projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
- if err != nil || projID == 0 {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- vals, err := url.ParseQuery(r.URL.RawQuery)
- name := vals["name"][0]
- namespace := vals["namespace"][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)
- }
- release, err := app.Repo.Release().ReadRelease(uint(clusterID), name, namespace)
- if err != nil {
- app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
- Code: ErrReleaseReadData,
- Errors: []string{"release not found"},
- }, w)
- }
- form := &forms.CreateGitAction{
- Release: release,
- ShouldGenerateOnly: false,
- }
- // decode from JSON to form value
- if err := json.NewDecoder(r.Body).Decode(form); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- gaExt, _ := app.createGitActionFromForm(projID, clusterID, name, form, w, r)
- w.WriteHeader(http.StatusCreated)
- if err := json.NewEncoder(w).Encode(gaExt); err != nil {
- app.handleErrorFormDecoding(err, ErrProjectDecode, w)
- return
- }
- }
- func (app *App) createGitActionFromForm(
- projID,
- clusterID uint64,
- name string,
- form *forms.CreateGitAction,
- w http.ResponseWriter,
- r *http.Request,
- ) (gaExt *models.GitActionConfigExternal, workflowYAML []byte) {
- // validate the form
- if err := app.validator.Struct(form); err != nil {
- app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
- return
- }
- // if the registry was provisioned through Porter, create a repository if necessary
- if form.RegistryID != 0 {
- // read the registry
- reg, err := app.Repo.Registry().ReadRegistry(form.RegistryID)
- if err != nil {
- app.handleErrorDataRead(err, w)
- return
- }
- _reg := registry.Registry(*reg)
- regAPI := &_reg
- // parse the name from the registry
- nameSpl := strings.Split(form.ImageRepoURI, "/")
- repoName := nameSpl[len(nameSpl)-1]
- err = regAPI.CreateRepository(app.Repo, repoName)
- if err != nil {
- app.handleErrorInternal(err, w)
- return
- }
- }
- repoSplit := strings.Split(form.GitRepo, "/")
- if len(repoSplit) != 2 {
- app.handleErrorFormDecoding(fmt.Errorf("invalid formatting of repo name"), ErrProjectDecode, w)
- return
- }
- session, err := app.Store.Get(r, app.ServerConf.CookieName)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- userID, _ := session.Values["user_id"].(uint)
- if userID == 0 {
- tok := app.getTokenFromRequest(r)
- if tok != nil && tok.IBy != 0 {
- userID = tok.IBy
- } else if tok == nil || tok.IBy == 0 {
- http.Error(w, "no user id found in request", http.StatusInternalServerError)
- return
- }
- }
- // generate porter jwt token
- jwt, _ := token.GetTokenForAPI(userID, uint(projID))
- encoded, err := jwt.EncodeToken(&token.TokenGeneratorConf{
- TokenSecret: app.ServerConf.TokenGeneratorSecret,
- })
- if err != nil {
- app.handleErrorInternal(err, w)
- return
- }
- // create the commit in the git repo
- gaRunner := &actions.GithubActions{
- ServerURL: app.ServerConf.ServerURL,
- GithubOAuthIntegration: nil,
- GithubAppID: app.GithubAppConf.AppID,
- GithubAppSecretPath: app.GithubAppConf.SecretPath,
- GithubInstallationID: form.GitRepoID,
- GitRepoName: repoSplit[1],
- GitRepoOwner: repoSplit[0],
- Repo: app.Repo,
- GithubConf: app.GithubProjectConf,
- ProjectID: uint(projID),
- ClusterID: uint(clusterID),
- ReleaseName: name,
- GitBranch: form.GitBranch,
- DockerFilePath: form.DockerfilePath,
- FolderPath: form.FolderPath,
- ImageRepoURL: form.ImageRepoURI,
- PorterToken: encoded,
- Version: updateAppActionVersion,
- ShouldGenerateOnly: form.ShouldGenerateOnly,
- ShouldCreateWorkflow: form.ShouldCreateWorkflow,
- }
- workflowYAML, err = gaRunner.Setup()
- if err != nil {
- app.handleErrorInternal(err, w)
- 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
- ga, err := app.Repo.GitActionConfig().CreateGitActionConfig(gitAction)
- if err != nil {
- app.handleErrorDataWrite(err, w)
- return
- }
- app.Logger.Info().Msgf("New git action created: %d", ga.ID)
- // update the release in the db with the image repo uri
- form.Release.ImageRepoURI = gitAction.ImageRepoURI
- _, err = app.Repo.Release().UpdateRelease(form.Release)
- if err != nil {
- app.handleErrorDataWrite(err, w)
- return
- }
- gaExt = ga.Externalize()
- return
- }
|