Skip to content

Commit

Permalink
Merge pull request #9 from k8sdeploy/associateAccount
Browse files Browse the repository at this point in the history
associate account
  • Loading branch information
Keloran committed Nov 5, 2021
2 parents c194567 + 798ae0d commit 3b6ad7d
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
build-args: |
version={{version}}
build=${{ github.sha }}
platforms: linux/arm64
platforms: linux/arm64,linux/amd64
file: ./k8s/Dockerfile
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Expand Down
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/vault/sdk v0.3.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.1.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.8.1 // indirect
github.com/jackc/pgx/v4 v4.13.0 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
Expand All @@ -52,7 +60,7 @@ require (
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/grpc v1.41.0 // indirect
Expand Down
118 changes: 118 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Local struct {
Development bool `env:"DEVELOPMENT" envDefault:"true"`
Port int `env:"LOCAL_PORT" envDefault:"3000"`
VaultAddress string `env:"VAULT_ADDRESS" envDefault:"http://vault.vault:8200"`
RDSAddress string `env:"RDS_ADDRESS" envDefault:"postgres.postgres"`
RDSAddress string `env:"RDS_ADDRESS" envDefault:"postgres.postgres:5432?sslmode=disable"`
}

type Config struct {
Expand Down
47 changes: 47 additions & 0 deletions internal/tygon/associate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tygon

import (
"fmt"

bugLog "github.com/bugfixes/go-bugfixes/logs"
"github.com/jackc/pgx/v4"
)

func (t *Tygon) getConnection() (*pgx.Conn, error) {
conn, err := pgx.Connect(
t.Context,
fmt.Sprintf(
"postgres://%s:%s@%s/%s",
t.Config.Username,
t.Config.Password,
t.Config.RDSAddress,
t.Config.Database))
if err != nil {
return nil, bugLog.Error(err)
}

return conn, nil
}

func (t *Tygon) associateAccountWithOrganization(orgName string) error {
conn, err := t.getConnection()
if err != nil {
return bugLog.Error(err)
}
defer func() {
if err := conn.Close(t.Context); err != nil {
bugLog.Debugf("failed to close associate: %+v", err)
}
}()

_, err = conn.Exec(
t.Context,
`SELECT account_id FROM account WHERE organization_name = $1`,
orgName,
t.Account.ID)
if err != nil {
return bugLog.Error(err)
}

return nil
}
26 changes: 25 additions & 1 deletion internal/tygon/event_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,34 @@ func isPackageEvent(payload interface{}) (bool, *github.PackageEvent) {
pe := github.PackageEvent{}

if err := mapstructure.Decode(payload, &pe); err == nil {
if pe.Package.GetName() != ""{
if pe.Package.GetName() != "" {
return true, &pe
}
}

return false, nil
}

func isPullRequestEvent(payload interface{}) (bool, *github.PullRequestEvent) {
pe := github.PullRequestEvent{}

if err := mapstructure.Decode(payload, &pe); err != nil {
if pe.PullRequest.GetMergeable() {
return true, &pe
}
}

return false, nil
}

func isReleaseEvent(payload interface{}) (bool, *github.ReleaseEvent) {
re := github.ReleaseEvent{}

if err := mapstructure.Decode(payload, &re); err != nil {
if re.Release.GetTagName() != "" {
return true, &re
}
}

return false, nil
}
20 changes: 19 additions & 1 deletion internal/tygon/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func jsonError(w http.ResponseWriter, msg string, errs error) {
}
}

//nolint:gocyclo
func (t Tygon) ParsePayload(w http.ResponseWriter, r *http.Request) {
var unknownPayload interface{}
if err := json.NewDecoder(r.Body).Decode(&unknownPayload); err != nil {
Expand All @@ -32,21 +33,38 @@ func (t Tygon) ParsePayload(w http.ResponseWriter, r *http.Request) {
// return
// }

// test different types
// test if ping event
if ok, parsedPayload := isPingEvent(unknownPayload); ok {
if err := t.handlePingEvent(parsedPayload); err != nil {
jsonError(w, "handlePingEvent failed", err)
return
}
}

// test if package event
if ok, parsedPayload := isPackageEvent(unknownPayload); ok {
if err := t.handlePackageEvent(parsedPayload); err != nil {
jsonError(w, "handlePackageEvent failed", err)
return
}
}

// test if pull request event
if ok, parsedPayload := isPullRequestEvent(unknownPayload); ok {
if err := t.handlePullRequestEvent(parsedPayload); err != nil {
jsonError(w, "handlePullRequestEvent failed", err)
return
}
}

// test if release event
if ok, parsedPayload := isReleaseEvent(unknownPayload); ok {
if err := t.handleReleaseEvent(parsedPayload); err != nil {
jsonError(w, "handleReleaseEvent failed", err)
return
}
}

for name, values := range r.Header {
for _, value := range values {
bugLog.Infof("Header: %s: %s", name, value)
Expand Down
42 changes: 31 additions & 11 deletions internal/tygon/tygon.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package tygon

import (
"context"
"strings"

bugLog "github.com/bugfixes/go-bugfixes/logs"
"github.com/google/go-github/v39/github"
"github.com/k8sdeploy/tygon/internal/config"
"github.com/mitchellh/mapstructure"
)

type Account struct {
Name string
ID int
}

type Tygon struct {
Config *config.Config
Context context.Context
EventConfig *EventConfig
Account *Account
}

// const GithubOrgs = "https://api.github.com/orgs/"
const GithubOrgs = "https://api.github.com/orgs/"

// func getOrg(org string) string {
// stripedStart := strings.Trim(org, GithubOrgs)
// split := strings.Split(stripedStart, "/")
// return strings.ToLower(split[0])
// }
//nolint:staticcheck
func getOrg(org string) string {
stripedStart := strings.Trim(org, GithubOrgs)
split := strings.Split(stripedStart, "/")
return strings.ToLower(split[0])
}

func NewTygon(cfg *config.Config) *Tygon {
return &Tygon{
Config: cfg,
Config: cfg,
Context: context.Background(),
}
}

Expand All @@ -31,10 +43,10 @@ func (t *Tygon) handlePingEvent(p *github.PingEvent) error {
return bugLog.Error(err)
}

// org := getOrg(*p.Hook.URL)
// fmt.Sprintf(org)

// https://api.github.com/orgs/BugFixes/hooks/326833658
org := getOrg(*p.Hook.URL)
if err := t.associateAccountWithOrganization(org); err != nil {
return bugLog.Errorf("failed to associate the ping: %+v", err)
}

return nil
}
Expand All @@ -43,6 +55,14 @@ func (t *Tygon) handlePackageEvent(p *github.PackageEvent) error {
return nil
}

func (t *Tygon) handlePullRequestEvent(p *github.PullRequestEvent) error {
return nil
}

func (t *Tygon) handleReleaseEvent(p *github.ReleaseEvent) error {
return nil
}

// func (t *Tygon) validateSecret(secretHash string, payload []byte) (bool, error) {
// gHash := hmac.New(sha256.New, []byte("abaf8d42-a9b0-401a-a7bb-f32a074f9e3d"))
// gHash.Write(payload)
Expand Down

0 comments on commit 3b6ad7d

Please sign in to comment.