Skip to content

Commit

Permalink
feat: clone token
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Oct 12, 2024
1 parent 37b30a1 commit e84299d
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 109 deletions.
28 changes: 27 additions & 1 deletion api/auth/get_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package auth

import (
"errors"
"fmt"
"net/http"

Expand Down Expand Up @@ -75,7 +76,24 @@ func GetAuthToken(c *gin.Context) {

// GitHub App and OAuth share the same callback URL,
// so we need to differentiate between the two using setup_action
if c.Request.FormValue("setup_action") == "install" {
setupAction := c.Request.FormValue("setup_action")
switch setupAction {
case "install":
case "update":
installID := c.Request.FormValue("installation_id")
if len(installID) == 0 {
retErr := errors.New("setup_action is install but installation_id is missing")

util.HandleError(c, http.StatusBadRequest, retErr)

return
}

// todo: if the repo is already added, then redirecting to the install url will try to add ALL repos...

// todo: on "install" we also need to check if it was just a regular github ui manual installation
// todo: on "update" this might just be a regular ui update to the github app
// todo: we need to capture the installation ID and sync all the vela repos for that installation
redirect, err := api.GetAppInstallRedirectURL(ctx, l, m, c.Request.URL.Query())
if err != nil {
retErr := fmt.Errorf("unable to get app install redirect URL: %w", err)
Expand All @@ -85,9 +103,17 @@ func GetAuthToken(c *gin.Context) {
return
}

if len(redirect) == 0 {
c.JSON(http.StatusOK, "installation completed")

return
}

c.Redirect(http.StatusTemporaryRedirect, redirect)

return
case "":
break
}

// capture the OAuth state if present
Expand Down
1 change: 1 addition & 0 deletions api/build/compile_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func CompileAndPublish(
WithRepo(repo).
WithUser(u).
WithLabels(cfg.Labels).
WithSCM(scm).
Compile(ctx, pipelineFile)
if err != nil {
// format the error message with extra information
Expand Down
14 changes: 11 additions & 3 deletions api/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func Install(c *gin.Context) {
return
}

// first, check if the org installation exists.
// if it does, just add the repo manually using the api and be done with it
// if it doesn't, then we need to start the installation flow
// but this came from the browser... it has NO auth to contact github api

// type cannot be empty
t := util.FormParameter(c, "type")
if len(t) == 0 {
Expand All @@ -102,10 +107,12 @@ func Install(c *gin.Context) {

// capture query params
ri := &types.RepoInstall{
Type: t,
Port: p,
OrgSCMID: int64(orgSCMID),
RepoSCMID: int64(repoSCMID),
InstallCallback: types.InstallCallback{
Type: t,
Port: p,
},
}

// construct the repo installation url
Expand Down Expand Up @@ -141,7 +148,8 @@ func GetAppInstallRedirectURL(ctx context.Context, l *logrus.Entry, m *internal.

// default redirect location if a user ended up here
// by providing an unsupported type
r := fmt.Sprintf("%s/install", m.Vela.Address)
// this is ignored when empty
r := ""

switch t {
// cli auth flow
Expand Down
12 changes: 6 additions & 6 deletions api/repo/install_html_url.go → api/repo/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ import (
// schema:
// "$ref": "#/definitions/Error"

// GetInstallHTMLURL represents the API handler to retrieve the
// GetInstallInfo represents the API handler to retrieve the
// SCM installation HTML URL for a particular repo and Vela server.
func GetInstallHTMLURL(c *gin.Context) {
func GetInstallInfo(c *gin.Context) {
// capture middleware values
m := c.MustGet("metadata").(*internal.Metadata)
l := c.MustGet("logger").(*logrus.Entry)
u := user.Retrieve(c)
r := repo.Retrieve(c)
scm := scm.FromContext(c)

l.Debug("constructing repo install url")
l.Debug("retrieving repo install information")

ri, err := scm.GetRepoInstallInfo(c.Request.Context(), u, r.GetOrg(), r.GetName())
ri, err := scm.GetRepoInstallInfo(c.Request.Context(), u, r)
if err != nil {
retErr := fmt.Errorf("unable to get repo scm install info %s: %w", u.GetName(), err)

Expand All @@ -76,11 +76,11 @@ func GetInstallHTMLURL(c *gin.Context) {
}

// todo: use url.values etc
appInstallURL := fmt.Sprintf(
ri.InstallURL = fmt.Sprintf(
"%s/install?org_scm_id=%d&repo_scm_id=%d",
m.Vela.Address,
ri.OrgSCMID, ri.RepoSCMID,
)

c.JSON(http.StatusOK, fmt.Sprintf("%s", appInstallURL))
c.JSON(http.StatusOK, ri)
}
18 changes: 12 additions & 6 deletions api/types/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import (
)

// RepoInstall is the configuration for installing a repo into the SCM.
//
// swagger:model RepoInstall
type RepoInstall struct {
Type string
Port string
OrgSCMID int64
RepoSCMID int64
OrgSCMID int64
RepoSCMID int64
AppInstalled bool
RepoAdded bool
InstallURL string
InstallCallback
}

// InstallCallback is the callback configuration for the installation.
type InstallCallback struct {
Type string
Port string
}

// Repo is the API representation of a repo.
Expand Down
2 changes: 2 additions & 0 deletions compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/api/types/settings"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/scm"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/go-vela/types/raw"
Expand Down Expand Up @@ -147,6 +148,7 @@ type Engine interface {
// WithLabel defines a function that sets
// the label(s) in the Engine.
WithLabels([]string) Engine
WithSCM(scm.Service) Engine
// WithPrivateGitHub defines a function that sets
// the private github client in the Engine.
WithPrivateGitHub(context.Context, string, string) Engine
Expand Down
Loading

0 comments on commit e84299d

Please sign in to comment.