Skip to content

Commit 8bb3f40

Browse files
FEAT[DXTA-297]: Create workflow for github installation data provisioning
1 parent b0b2623 commit 8bb3f40

File tree

17 files changed

+457
-62
lines changed

17 files changed

+457
-62
lines changed

cmd/internal-api/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func main() {
129129

130130
defer temporalClient.Close()
131131

132-
usersHandler := handler.NewUsers(temporalClient, *cfg)
132+
temporalHandler := handler.SetupOnboardingTemporal(temporalClient, *cfg)
133133

134134
r.Route("/tenant", func(r chi.Router) {
135135
if os.Getenv("ENABLE_JWT_AUTH") == "true" {
@@ -147,8 +147,8 @@ func main() {
147147
r.Post("/teams", handler.CreateTeam)
148148
r.Post("/teams/{team_id}/members/{member_id}", handler.AddMemberToTeam)
149149
r.Post("/members", handler.CreateMember)
150+
r.Get("/provision-github-installations/{installation_id}", temporalHandler.ProvisionGithubInstallationData)
150151
})
151-
152152
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
153153
w.Write([]byte(`OK`))
154154
})
@@ -157,7 +157,7 @@ func main() {
157157
w.Write([]byte(`OK`))
158158
})
159159

160-
r.Get("/users-count", usersHandler.UsersCount)
160+
r.Get("/users-count", temporalHandler.UsersCount)
161161

162162
go func() {
163163
log.Printf("Listening on %s\n", srv.Addr)

cmd/onboarding-worker/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/dxta-dev/app/internal/onboarding"
88
"github.com/dxta-dev/app/internal/onboarding/activities"
9+
"github.com/dxta-dev/app/internal/onboarding/data"
910
"github.com/dxta-dev/app/internal/onboarding/workflows"
1011
"go.temporal.io/sdk/client"
1112
"go.temporal.io/sdk/worker"
@@ -17,6 +18,12 @@ func main() {
1718
log.Fatalln("Failed to load configuration:", err)
1819
}
1920

21+
err = data.LoadGithubConfig()
22+
23+
if err != nil {
24+
log.Fatalln("Failed to load github configuration:", err)
25+
}
26+
2027
temporalClient, err := client.Dial(client.Options{
2128
HostPort: cfg.TemporalHostPort,
2229
Namespace: cfg.TemporalOnboardingNamespace,
@@ -26,12 +33,19 @@ func main() {
2633
}
2734
defer temporalClient.Close()
2835

36+
err = data.InitAppClient()
37+
38+
if err != nil {
39+
log.Fatalf("Unable to init app client: %v", err)
40+
}
41+
2942
err = onboarding.RegisterNamespace(
3043
context.Background(),
3144
cfg.TemporalHostPort,
3245
cfg.TemporalOnboardingNamespace,
3346
30,
3447
)
48+
3549
if err != nil {
3650
log.Fatalln("Failed to register Temporal namespace:", err)
3751
}
@@ -45,6 +59,9 @@ func main() {
4559
w.RegisterWorkflow(workflows.CountUsers)
4660
w.RegisterActivity(userActivities)
4761

62+
/* w.RegisterWorkflow(workflow.ProvisionGithubInstallationData)
63+
w.RegisterActivity(activity.GetGithubInstallation) */
64+
4865
if err := w.Run(worker.InterruptCh()); err != nil {
4966
log.Fatalln("Worker failed to start", err)
5067
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020

2121
require (
2222
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 // indirect
23+
github.com/bradleyfalzon/ghinstallation/v2 v2.16.0 // indirect
2324
github.com/cenkalti/backoff/v5 v5.0.2 // indirect
2425
github.com/davecgh/go-spew v1.1.1 // indirect
2526
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
@@ -28,8 +29,12 @@ require (
2829
github.com/go-logr/logr v1.4.3 // indirect
2930
github.com/go-logr/stdr v1.2.2 // indirect
3031
github.com/goccy/go-json v0.10.3 // indirect
32+
github.com/gofri/go-github-ratelimit/v2 v2.0.2 // indirect
3133
github.com/gogo/protobuf v1.3.2 // indirect
34+
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
3235
github.com/golang/mock v1.6.0 // indirect
36+
github.com/google/go-github/v72 v72.0.0 // indirect
37+
github.com/google/go-querystring v1.1.0 // indirect
3338
github.com/google/uuid v1.6.0 // indirect
3439
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
3540
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.1 // indirect

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/XSAM/otelsql v0.39.0/go.mod h1:uMOXLUX+wkuAuP0AR3B45NXX7E9lJS2mERa8gq
55
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 h1:goHVqTbFX3AIo0tzGr14pgfAW2ZfPChKO21Z9MGf/gk=
66
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
77
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
8+
github.com/bradleyfalzon/ghinstallation/v2 v2.16.0 h1:B91r9bHtXp/+XRgS5aZm6ZzTdz3ahgJYmkt4xZkgDz8=
9+
github.com/bradleyfalzon/ghinstallation/v2 v2.16.0/go.mod h1:OeVe5ggFzoBnmgitZe/A+BqGOnv1DvU/0uiLQi1wutM=
810
github.com/cenkalti/backoff/v5 v5.0.2 h1:rIfFVxEf1QsI7E1ZHfp/B4DF/6QBAUhmgkxc0H7Zss8=
911
github.com/cenkalti/backoff/v5 v5.0.2/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
1012
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
@@ -54,8 +56,12 @@ github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
5456
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
5557
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
5658
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
59+
github.com/gofri/go-github-ratelimit/v2 v2.0.2 h1:gS8wAS1jTmlWGdTjAM7KIpsLjwY1S0S/gKK5hthfSXM=
60+
github.com/gofri/go-github-ratelimit/v2 v2.0.2/go.mod h1:YBQt4gTbdcbMjJFT05YFEaECwH78P5b0IwrnbLiHGdE=
5761
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
5862
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
63+
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
64+
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
5965
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
6066
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
6167
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
@@ -68,9 +74,14 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
6874
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
6975
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
7076
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
77+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7178
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7279
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
7380
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
81+
github.com/google/go-github/v72 v72.0.0 h1:FcIO37BLoVPBO9igQQ6tStsv2asG4IPcYFi655PPvBM=
82+
github.com/google/go-github/v72 v72.0.0/go.mod h1:WWtw8GMRiL62mvIquf1kO3onRHeWWKmK01qdCY8c5fg=
83+
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
84+
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
7485
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
7586
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
7687
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

internal/internal-api/data/tenantDB.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package data
22

33
import (
4+
"context"
45
"database/sql"
56
"fmt"
67
"os"
@@ -12,7 +13,7 @@ type TenantDB struct {
1213
DB *sql.DB
1314
}
1415

15-
func NewTenantDB(dbUrl string) (TenantDB, error) {
16+
func NewTenantDB(dbUrl string, ctx context.Context) (TenantDB, error) {
1617
driverName := otel.GetDriverName()
1718
devToken := os.Getenv("DXTA_DEV_GROUP_TOKEN")
1819

@@ -30,6 +31,10 @@ func NewTenantDB(dbUrl string) (TenantDB, error) {
3031
return TenantDB{}, err
3132
}
3233

34+
if err := tenantDB.PingContext(ctx); err != nil {
35+
return TenantDB{}, err
36+
}
37+
3338
return TenantDB{
3439
DB: tenantDB,
3540
}, nil

internal/internal-api/handler/add_member_to_team.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ type AddMemberToTeamResponse struct {
1818
func AddMemberToTeam(w http.ResponseWriter, r *http.Request) {
1919
ctx := r.Context()
2020

21-
apiState := ctx.Value(util.ApiStateCtxKey).(api.State)
21+
authId := ctx.Value(util.AuthIdCtxKey).(string)
22+
23+
apiState, err := api.InternalApiState(authId, ctx)
24+
25+
if err != nil {
26+
util.JSONError(w, util.ErrorParam{Error: "Internal Server Error"}, http.StatusInternalServerError)
27+
return
28+
}
2229

2330
teamId, err := strconv.ParseInt(chi.URLParam(r, "team_id"), 10, 64)
2431
if err != nil {

internal/internal-api/handler/create_member.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ func CreateMember(w http.ResponseWriter, r *http.Request) {
3434
util.JSONError(w, util.ErrorParam{Error: "Bad Request"}, http.StatusBadRequest)
3535
}
3636

37-
apiState := ctx.Value(util.ApiStateCtxKey).(api.State)
37+
authId := ctx.Value(util.AuthIdCtxKey).(string)
38+
39+
apiState, err := api.InternalApiState(authId, ctx)
40+
41+
if err != nil {
42+
util.JSONError(w, util.ErrorParam{Error: "Internal Server Error"}, http.StatusInternalServerError)
43+
return
44+
}
3845

3946
newMemberRes, err := apiState.DB.CreateMember(body.Name, body.Email, ctx)
4047

internal/internal-api/handler/create_team.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,26 @@ func CreateTeam(w http.ResponseWriter, r *http.Request) {
2828
return
2929
}
3030

31-
organizationId := ctx.Value(util.OrganizationIdCtxKey).(int64)
32-
33-
if organizationId == 0 || body.TeamName == "" {
34-
fmt.Printf(
35-
"No organization id or team name provided. Organization id: %d Team name: %s",
36-
organizationId,
37-
body.TeamName,
38-
)
31+
if body.TeamName == "" {
32+
fmt.Printf("No team name provided. Team name: %s", body.TeamName)
3933
util.JSONError(w, util.ErrorParam{Error: "Bad Request"}, http.StatusBadRequest)
4034
}
4135

42-
apiState := ctx.Value(util.ApiStateCtxKey).(api.State)
36+
authId := ctx.Value(util.AuthIdCtxKey).(string)
37+
38+
apiState, err := api.InternalApiState(authId, ctx)
39+
40+
if err != nil {
41+
util.JSONError(w, util.ErrorParam{Error: "Internal Server Error"}, http.StatusInternalServerError)
42+
return
43+
}
44+
45+
organizationId, err := apiState.DB.GetOrganizationIdByAuthId(authId, ctx)
46+
47+
if err != nil {
48+
util.JSONError(w, util.ErrorParam{Error: "Bad request"}, http.StatusBadRequest)
49+
return
50+
}
4351

4452
newTeamRes, err := apiState.DB.CreateTeam(body.TeamName, organizationId, ctx)
4553

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package handler
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"strconv"
8+
9+
"github.com/dxta-dev/app/internal/onboarding/workflows"
10+
"github.com/dxta-dev/app/internal/util"
11+
"github.com/go-chi/chi/v5"
12+
"github.com/google/go-github/v72/github"
13+
)
14+
15+
type Response struct {
16+
Installations *github.Installation `json:"installations"`
17+
}
18+
19+
func (t *OnboardingTemporal) ProvisionGithubInstallationData(w http.ResponseWriter, r *http.Request) {
20+
ctx := r.Context()
21+
22+
installationId, err := strconv.ParseInt(chi.URLParam(r, "installation_id"), 10, 64)
23+
if err != nil {
24+
fmt.Printf("Issue while parsing installation id URL param. Error: %s", err.Error())
25+
util.JSONError(w, util.ErrorParam{Error: "Bad Request"}, http.StatusBadRequest)
26+
return
27+
}
28+
29+
out, err := workflows.ExecuteGithubInstallationDataProvision(
30+
ctx,
31+
t.temporalClient,
32+
workflows.Args{
33+
TemporalOnboardingQueueName: t.config.TemporalOnboardingNamespace,
34+
InstallationId: installationId,
35+
})
36+
37+
if err != nil {
38+
fmt.Println(err.Error())
39+
util.JSONError(
40+
w,
41+
util.ErrorParam{Error: "Internal Server Error"},
42+
http.StatusInternalServerError,
43+
)
44+
}
45+
46+
w.Header().Set("Content-Type", "application/json")
47+
w.WriteHeader(http.StatusOK)
48+
49+
if err := json.NewEncoder(w).Encode(Response{Installations: out}); err != nil {
50+
fmt.Printf("Issue while formatting response. Error: %s", err.Error())
51+
util.JSONError(
52+
w,
53+
util.ErrorParam{Error: "Internal Server Error"},
54+
http.StatusInternalServerError,
55+
)
56+
return
57+
}
58+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package handler
2+
3+
import (
4+
"github.com/dxta-dev/app/internal/onboarding"
5+
"go.temporal.io/sdk/client"
6+
)
7+
8+
type OnboardingTemporal struct {
9+
temporalClient client.Client
10+
config onboarding.Config
11+
}
12+
13+
func SetupOnboardingTemporal(temporalClient client.Client, config onboarding.Config) *OnboardingTemporal {
14+
return &OnboardingTemporal{
15+
temporalClient: temporalClient,
16+
config: config,
17+
}
18+
}

0 commit comments

Comments
 (0)