Skip to content

Commit

Permalink
RELTEC-12262: refactor webhooks endpoint to vcs webhooks, support
Browse files Browse the repository at this point in the history
different vcs systems during user validation
  • Loading branch information
KRaffael committed Oct 29, 2024
1 parent 03f3342 commit e12d68f
Show file tree
Hide file tree
Showing 230 changed files with 89,023 additions and 1,372 deletions.
178 changes: 110 additions & 68 deletions README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions api-generator/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ function generate_apimodel {
-g go-autumrest
}

DOWNSTREAM_API_DIRECTORY=../internal/client

function generate_downstream {
P_DOWNSTREAM_NAME=$1
P_SPEC_FILE_NAME=$2
Expand All @@ -42,6 +44,7 @@ function generate_downstream {
}

generate_apimodel
generate_downstream bitbucket bitbucket-v8.19.json BuildsAndDeployments:PullRequests:Repository:User

# -------------------------------------- customization -----------------------------------------
# omit certain fields from yaml representations, which we use internally to save to files in git
Expand Down
32 changes: 32 additions & 0 deletions api/openapi-v3-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,38 @@ paths:
description: Internal Server Error
tags:
- webhook
/webhooks/vcs/{vcsKey}:
post:
tags:
- webhooks
operationId: postWebhook
parameters:
- name: vcsKey
in: path
required: true
schema:
type: string
requestBody:
required: true
content:
application/json:
schema:
type: object
responses:
'204':
description: Success
'400':
description: Bad Request
'404':
description: Deployment Repository or Git Reference Not found
'422':
description: Bad Deployment Repository
'500':
description: Internal Server Error
content:
"*/*":
schema:
"$ref": "#/components/schemas/ErrorDto"
components:
schemas:
OwnerDto:
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ require (
github.com/go-git/go-git/v5 v5.12.0
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/go-playground/webhooks/v6 v6.4.0
github.com/google/go-github/v66 v66.0.0
github.com/google/uuid v1.6.0
github.com/lestrrat-go/jwx/v2 v2.1.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.20.4
Expand Down Expand Up @@ -55,6 +57,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,15 @@ github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v66 v66.0.0 h1:ADJsaXj9UotwdgK8/iFZtv7MLc8E8WBl62WLd/D/9+M=
github.com/google/go-github/v66 v66.0.0/go.mod h1:+4SO9Zkuyf8ytMj0csN1NR/5OTR+MfqPp8P8dVlcvY4=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
21 changes: 21 additions & 0 deletions internal/acorn/config/customconfigint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import (
librepo "github.com/Interhyp/go-backend-service-common/acorns/repository"
)

type VCSPlatform int64

const (
VCSPlatformBitbucketDatacenter VCSPlatform = iota
VCSPlatformGitHub
VCSPlatformUnknown
)

type VCSConfig struct {
Platform VCSPlatform
APIBaseURL string
AccessToken string
}

type CustomConfiguration interface {
BasicAuthUsername() string
BasicAuthPassword() string
Expand Down Expand Up @@ -63,6 +77,10 @@ type CustomConfiguration interface {

AllowedFileCategories() []string

VCSConfigs() map[string]VCSConfig
WebhooksProcessAsync() bool
UserPrefix() string

Kafka() *kafka.Config
KafkaGroupIdOverride() string

Expand Down Expand Up @@ -125,4 +143,7 @@ const (
KeyRedisPassword = "REDIS_PASSWORD"
KeyPullRequestBuildUrl = "PULL_REQUEST_BUILD_URL"
KeyPullRequestBuildKey = "PULL_REQUEST_BUILD_KEY"
KeyVCSConfigs = "VCS_CONFIGS"
KeyWebhooksProcessAsync = "WEBHOOKS_PROCESS_ASYNC"
KeyUserPrefix = "USER_PREFIX"
)
44 changes: 44 additions & 0 deletions internal/acorn/errors/inputvalidationerror/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package inputvalidationerror

import (
"context"
"errors"
"fmt"
)

type ErrorInt interface {
Ctx() context.Context
IsInputValidationError() bool
}

// this also implements the error interface

type Error struct {
ctx context.Context
err error
}

func New(ctx context.Context, reason string) error {
return &Error{
ctx: ctx,
err: fmt.Errorf("input validation failed: %s", reason),
}
}

func (e *Error) Error() string {
return e.err.Error()
}

func (e *Error) Ctx() context.Context {
return e.ctx
}

// the presence of this method makes the interface unique and thus recognizable by a simple type check

func (e *Error) IsInputValidationError() bool {
return true
}

func Is(err error) bool {
return errors.As(err, new(*Error))
}
36 changes: 0 additions & 36 deletions internal/acorn/repository/bitbucketint.go

This file was deleted.

30 changes: 30 additions & 0 deletions internal/acorn/repository/vcsint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package repository

import "context"

type VcsPlugin interface {
SetCommitStatusInProgress(ctx context.Context, repoPath, repoName, commitID, url string, statusKey string) error

SetCommitStatusSucceeded(ctx context.Context, repoPath, repoName, commitID, url string, statusKey string) error

SetCommitStatusFailed(ctx context.Context, repoPath, repoName, commitID, url string, statusKey string) error

CreatePullRequestComment(ctx context.Context, repoPath, repoName, pullRequestID, text string) error

GetChangedFilesOnPullRequest(ctx context.Context, repoPath, repoName, pullRequestID, toRef string) ([]File, string, error)

GetUser(ctx context.Context, username string) (string, error)
}

type CommitBuildStatus string

const (
CommitBuildStatusSuccess CommitBuildStatus = "SUCCESSFUL"
CommitBuildStatusInProgress CommitBuildStatus = "INPROGRESS"
CommitBuildStatusFailed CommitBuildStatus = "FAILED"
)

type File struct {
Path string
Contents string
}
15 changes: 0 additions & 15 deletions internal/acorn/service/prvalidatorint.go

This file was deleted.

10 changes: 10 additions & 0 deletions internal/acorn/service/vcswebhookshandlerint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package service

import (
"context"
"net/http"
)

type VCSWebhooksHandler interface {
HandleEvent(ctx context.Context, vcsKey string, r *http.Request) error
}
Loading

0 comments on commit e12d68f

Please sign in to comment.