Skip to content

Commit

Permalink
feat: webhook handlers, debug code cleanup, installation redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Oct 23, 2024
1 parent 69e89a1 commit 9aca5ff
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 36 deletions.
11 changes: 2 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ FROM alpine:3.20.3@sha256:beefdbd8a1da6d2915566fde36db9db0b524eb737fc57cd1367eff

RUN apk add --update --no-cache ca-certificates

# FROM scratch
FROM golang
FROM scratch

COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

Expand All @@ -15,10 +14,4 @@ ENV GODEBUG=netdns=go

ADD release/vela-server /bin/

# CMD ["/bin/vela-server"]

# dlv wrapper

EXPOSE 4000
RUN CGO_ENABLED=0 go install -ldflags "-s -w -extldflags '-static'" github.com/go-delve/delve/cmd/dlv@latest
CMD [ "/go/bin/dlv", "--listen=:4000", "--headless=true", "--log=true", "--accept-multiclient", "--api-version=2", "exec", "/bin/vela-server" ]
CMD ["/bin/vela-server"]
25 changes: 22 additions & 3 deletions api/auth/get_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package auth
import (
"fmt"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

"github.com/go-vela/server/api/types"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/internal/token"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/util"
Expand Down Expand Up @@ -67,7 +67,6 @@ import (
func GetAuthToken(c *gin.Context) {
// capture middleware values
tm := c.MustGet("token-manager").(*token.Manager)
m := c.MustGet("metadata").(*internal.Metadata)
l := c.MustGet("logger").(*logrus.Entry)

ctx := c.Request.Context()
Expand All @@ -77,8 +76,28 @@ func GetAuthToken(c *gin.Context) {

var err error

// handle scm setup events
// setup_action==install represents the GitHub App installation callback redirect
if c.Request.FormValue("setup_action") == "install" {
c.Redirect(http.StatusTemporaryRedirect, "https://"+m.Source.Host)
installID, err := strconv.ParseInt(c.Request.FormValue("installation_id"), 10, 0)
if err != nil {
retErr := fmt.Errorf("unable to parse installation_id: %w", err)

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

return
}

r, err := scm.FromContext(c).FinishInstallation(ctx, c.Request, installID)
if err != nil {
retErr := fmt.Errorf("unable to finish installation: %w", err)

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

return
}

c.Redirect(http.StatusTemporaryRedirect, r)

return
}
Expand Down
2 changes: 1 addition & 1 deletion api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func PostWebhook(c *gin.Context) {
return
}

c.JSON(http.StatusOK, "handled installation event!")
c.JSON(http.StatusOK, "installation processed successfully")

return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/vela-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func server(c *cli.Context) error {
metadata.Vela.OpenIDIssuer = oidcIssuer
tm.Issuer = oidcIssuer

jitter := wait.Jitter(0*time.Second, 2.0)
jitter := wait.Jitter(5*time.Second, 2.0)

logrus.Infof("retrieving initial platform settings after %v delay", jitter)

Expand Down
2 changes: 0 additions & 2 deletions compiler/native/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,11 @@ func environment(b *api.Build, m *internal.Metadata, r *api.Repo, u *api.User, n
env["VELA_HOST"] = notImplemented
env["VELA_NETRC_MACHINE"] = notImplemented
env["VELA_NETRC_PASSWORD"] = netrcPassword
logrus.Infof("using netrc password: %s", netrcPassword)
env["VELA_NETRC_USERNAME"] = "x-oauth-basic"
env["VELA_QUEUE"] = notImplemented
env["VELA_RUNTIME"] = notImplemented
env["VELA_SOURCE"] = notImplemented
env["VELA_VERSION"] = notImplemented
env["VELA_VADER"] = "yes"
env["CI"] = "true"

// populate environment variables from metadata
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ services:
restart: always
ports:
- '8080:8080'
# dlv
- '4000:4000'
depends_on:
postgres:
condition: service_healthy
Expand Down
2 changes: 1 addition & 1 deletion scm/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (c *client) newGithubAppClient(ctx context.Context) (*github.Client, error)
}

// helper function to return the GitHub App installation token.
func (c *client) newGithubAppInstallationToken(ctx context.Context, r *api.Repo, repos []string, permissions []string) (string, error) {
func (c *client) newGithubAppInstallationRepoToken(ctx context.Context, r *api.Repo, repos []string, permissions []string) (string, error) {
// create a github client based off the existing GitHub App configuration
client, err := github.NewClient(
&http.Client{Transport: c.AppsTransport}).
Expand Down
41 changes: 27 additions & 14 deletions scm/github/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package github

import (
"context"
"fmt"
"net/http"
"strings"
"time"

"github.com/go-vela/server/api/types"
Expand All @@ -20,14 +22,14 @@ import (
func (c *client) ProcessInstallation(ctx context.Context, request *http.Request, webhook *internal.Webhook, db database.Interface) error {
c.Logger.Tracef("processing GitHub App installation")

errs := []error{}
errs := []string{}

// set install_id for repos added to the installation
for _, repo := range webhook.Installation.RepositoriesAdded {
r, err := db.GetRepoForOrg(ctx, webhook.Installation.Org, repo)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
errs = append(errs, err)
errs = append(errs, fmt.Sprintf("%s:%s", repo, err.Error()))
}

// skip repos that dont exist in vela
Expand All @@ -36,7 +38,7 @@ func (c *client) ProcessInstallation(ctx context.Context, request *http.Request,

err = updateRepoInstallationID(ctx, webhook, r, db, webhook.Installation.ID)
if err != nil {
errs = append(errs, err)
errs = append(errs, fmt.Sprintf("%s:%s", repo, err.Error()))
}
}

Expand All @@ -45,7 +47,7 @@ func (c *client) ProcessInstallation(ctx context.Context, request *http.Request,
r, err := db.GetRepoForOrg(ctx, webhook.Installation.Org, repo)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
errs = append(errs, err)
errs = append(errs, fmt.Sprintf("%s:%s", repo, err.Error()))
}

// skip repos that dont exist in vela
Expand All @@ -54,21 +56,13 @@ func (c *client) ProcessInstallation(ctx context.Context, request *http.Request,

err = updateRepoInstallationID(ctx, webhook, r, db, 0)
if err != nil {
errs = append(errs, err)
errs = append(errs, fmt.Sprintf("%s:%s", repo, err.Error()))
}
}

// combine all errors
if len(errs) > 0 {
var combined error
for _, e := range errs {
if combined == nil {
combined = e
} else {
combined = errors.Wrap(combined, e.Error())
}
}
return combined
return errors.New(strings.Join(errs, ", "))
}

return nil
Expand Down Expand Up @@ -142,3 +136,22 @@ func updateRepoInstallationID(ctx context.Context, webhook *internal.Webhook, r

return nil
}

// FinishInstallation completes the web flow for a GitHub App installation, returning a redirect to the app installation page.
func (c *client) FinishInstallation(ctx context.Context, request *http.Request, installID int64) (string, error) {
c.Logger.Tracef("finishing GitHub App installation")

githubAppClient, err := c.newGithubAppClient(ctx)
if err != nil {
return "", err
}

install, _, err := githubAppClient.Apps.GetInstallation(ctx, installID)
if err != nil {
return "", err
}

r := install.GetHTMLURL()

return r, nil
}
6 changes: 3 additions & 3 deletions scm/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ func (c *client) GetBranch(ctx context.Context, r *api.Repo, branch string) (str
// CreateChecks defines a function that does stuff...
func (c *client) CreateChecks(ctx context.Context, r *api.Repo, commit, step, event string) (int64, error) {
// create client from GitHub App
t, err := c.newGithubAppInstallationToken(ctx, r, []string{}, []string{})
t, err := c.newGithubAppInstallationRepoToken(ctx, r, []string{}, []string{})
if err != nil {
return 0, err
}
Expand All @@ -709,7 +709,7 @@ func (c *client) CreateChecks(ctx context.Context, r *api.Repo, commit, step, ev
// UpdateChecks defines a function that does stuff...
func (c *client) UpdateChecks(ctx context.Context, r *api.Repo, s *library.Step, commit, event string) error {
// create client from GitHub App
t, err := c.newGithubAppInstallationToken(ctx, r, []string{}, []string{})
t, err := c.newGithubAppInstallationRepoToken(ctx, r, []string{}, []string{})
if err != nil {
return err
}
Expand Down Expand Up @@ -806,7 +806,7 @@ func (c *client) GetNetrcPassword(ctx context.Context, u *api.User, r *api.Repo,
// todo: pass in THIS repo to only get access to that repo
// https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation
// maybe take an optional list of repos and permission set that is driven by yaml
t, err := c.newGithubAppInstallationToken(ctx, r, repositories, []string{})
t, err := c.newGithubAppInstallationRepoToken(ctx, r, repositories, []string{})
if err != nil {
logrus.Errorf("unable to get github app installation token: %v", err)
}
Expand Down
3 changes: 3 additions & 0 deletions scm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ type Service interface {
// ProcessInstallation defines a function that
// processes an installation event.
ProcessInstallation(context.Context, *http.Request, *internal.Webhook, database.Interface) error
// ProcessInstallation defines a function that
// finishes an installation event and returns a web redirect.
FinishInstallation(context.Context, *http.Request, int64) (string, error)

// TODO: Add convert functions to interface?
}

0 comments on commit 9aca5ff

Please sign in to comment.