Skip to content

Commit 75a6fe4

Browse files
authored
Merge pull request #23 from MR5356/develop
add repository
2 parents 72d66aa + ceb9cdf commit 75a6fe4

File tree

11 files changed

+339
-11
lines changed

11 files changed

+339
-11
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,6 @@ coverage.out
138138
internal/server/static/*
139139
config-dev.yaml
140140

141-
/tmp
141+
/tmp
142+
143+
github_app_private_key.pem

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/MR5356/jietan v0.0.0-20240714044041-3c75753fbafb
99
github.com/antonfisher/nested-logrus-formatter v1.3.1
1010
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef
11+
github.com/bradleyfalzon/ghinstallation/v2 v2.16.0
1112
github.com/casbin/casbin/v2 v2.87.1
1213
github.com/casbin/gorm-adapter/v3 v3.24.0
1314
github.com/containerd/containerd v1.7.19
@@ -99,12 +100,14 @@ require (
99100
github.com/go-sql-driver/mysql v1.8.1 // indirect
100101
github.com/goccy/go-json v0.10.2 // indirect
101102
github.com/gogo/protobuf v1.3.2 // indirect
103+
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
102104
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
103105
github.com/golang-sql/sqlexp v0.1.0 // indirect
104106
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
105107
github.com/golang/protobuf v1.5.4 // indirect
106108
github.com/google/gnostic v0.5.7-v3refs // indirect
107-
github.com/google/go-cmp v0.6.0 // indirect
109+
github.com/google/go-cmp v0.7.0 // indirect
110+
github.com/google/go-github/v72 v72.0.0 // indirect
108111
github.com/google/go-querystring v1.1.0 // indirect
109112
github.com/google/gofuzz v1.2.0 // indirect
110113
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect

go.sum

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
5959
github.com/bitly/go-hostpool v0.1.0/go.mod h1:4gOCgp6+NZnVqlKyZ/iBZFTAJKembaVENUpMkpg42fw=
6060
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
6161
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
62+
github.com/bradleyfalzon/ghinstallation/v2 v2.16.0 h1:B91r9bHtXp/+XRgS5aZm6ZzTdz3ahgJYmkt4xZkgDz8=
63+
github.com/bradleyfalzon/ghinstallation/v2 v2.16.0/go.mod h1:OeVe5ggFzoBnmgitZe/A+BqGOnv1DvU/0uiLQi1wutM=
6264
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
6365
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
6466
github.com/bugsnag/bugsnag-go v1.0.5-0.20150529004307-13fd6b8acda0/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8=
@@ -221,6 +223,8 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
221223
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
222224
github.com/golang-jwt/jwt/v4 v4.4.3/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
223225
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
226+
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
227+
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
224228
github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
225229
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
226230
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
@@ -262,10 +266,12 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
262266
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
263267
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
264268
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
265-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
266-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
269+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
270+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
267271
github.com/google/go-github/v61 v61.0.0 h1:VwQCBwhyE9JclCI+22/7mLB1PuU9eowCXKY5pNlu1go=
268272
github.com/google/go-github/v61 v61.0.0/go.mod h1:0WR+KmsWX75G2EbpyGsGmradjo3IiciuI4BmdVCobQY=
273+
github.com/google/go-github/v72 v72.0.0 h1:FcIO37BLoVPBO9igQQ6tStsv2asG4IPcYFi655PPvBM=
274+
github.com/google/go-github/v72 v72.0.0/go.mod h1:WWtw8GMRiL62mvIquf1kO3onRHeWWKmK01qdCY8c5fg=
269275
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
270276
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
271277
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=

internal/config/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
const (
99
DefaultRetryCount = 3
1010
DefaultRetryDelay = time.Second * 0
11+
12+
ContextUserKey = "user"
13+
ContextUserIDKey = "userID"
1114
)
1215

1316
var config *Config
@@ -18,6 +21,7 @@ type Config struct {
1821
JWT JWT `json:"jwt" yaml:"jwt"`
1922
OAuthConfig map[string]OAuthConfig `json:"oauth" yaml:"oauth"`
2023
Email Email `json:"email" yaml:"email"`
24+
GithubApp GithubApp `json:"githubApp" yaml:"githubApp"`
2125
}
2226

2327
func Current(cfgs ...Cfg) *Config {
@@ -79,6 +83,11 @@ type Database struct {
7983
ConnMaxIdle time.Duration `json:"connMaxIdle" yaml:"connMaxIdle" default:"0s"`
8084
}
8185

86+
type GithubApp struct {
87+
AppID int64 `json:"appId" yaml:"appId" default:"1485539"`
88+
PrivateKey string `json:"privateKey" yaml:"privateKey" default:"./github_app_private_key.pem"`
89+
}
90+
8291
type Cfg func(c *Config)
8392

8493
func WithPort(port int) Cfg {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package module
2+
3+
import (
4+
"github.com/MR5356/aurora/internal/config"
5+
"github.com/MR5356/aurora/internal/response"
6+
"github.com/MR5356/aurora/pkg/util/ginutil"
7+
"github.com/gin-gonic/gin"
8+
"github.com/google/go-github/v61/github"
9+
"github.com/sirupsen/logrus"
10+
"net/http"
11+
"strconv"
12+
)
13+
14+
type Controller struct {
15+
service *Service
16+
}
17+
18+
func NewController() *Controller {
19+
return &Controller{
20+
service: GetService(),
21+
}
22+
}
23+
24+
func (c *Controller) handleGithubAppInstall(ctx *gin.Context) {
25+
payload, err := github.ValidatePayload(ctx.Request, []byte(""))
26+
if err != nil {
27+
logrus.Errorf("github.ValidatePayload: %v", err)
28+
response.ErrorWithMsg(ctx, response.CodeParamsError, "invalid payload")
29+
return
30+
}
31+
32+
eventType := github.WebHookType(ctx.Request)
33+
event, err := github.ParseWebHook(eventType, payload)
34+
if err != nil {
35+
logrus.Errorf("github.ParseWebHook: %v", err)
36+
response.ErrorWithMsg(ctx, response.CodeParamsError, "invalid webhook event")
37+
return
38+
}
39+
40+
var installationID int64 = 0
41+
var action string = ""
42+
switch e := event.(type) {
43+
case *github.InstallationEvent:
44+
installationID = e.Installation.GetID()
45+
action = e.GetAction()
46+
case *github.InstallationRepositoriesEvent:
47+
installationID = e.Installation.GetID()
48+
action = e.GetAction()
49+
}
50+
51+
logrus.Infof("installation: %+v", installationID)
52+
logrus.Infof("action: %s", action)
53+
if installationID == 0 {
54+
response.Success(ctx, nil)
55+
return
56+
}
57+
58+
if err = c.service.UpdateGithubModule(ctx, action, installationID); err != nil {
59+
logrus.Errorf("UpdateGithubModule failed: %v", err)
60+
response.ErrorWithMsg(ctx, response.CodeServerError, "failed to update module")
61+
return
62+
} else {
63+
logrus.Infof("UpdateGithubModule success, event: %s, installationID: %d", eventType, installationID)
64+
response.Success(ctx, nil)
65+
}
66+
}
67+
68+
func (c *Controller) handleGithubAppCallback(ctx *gin.Context) {
69+
installationIDStr := ctx.Query("installation_id")
70+
userID, ok := ctx.Get(config.ContextUserIDKey)
71+
if !ok {
72+
logrus.Error("user ID not found in context")
73+
response.ErrorWithMsg(ctx, response.CodeNoPermission, "user not authenticated")
74+
return
75+
}
76+
installationID, err := strconv.ParseInt(installationIDStr, 10, 64)
77+
if err != nil {
78+
logrus.Errorf("strconv.ParseInt: %v", err)
79+
response.ErrorWithMsg(ctx, response.CodeParamsError, "invalid installation ID")
80+
return
81+
}
82+
if c.service.RegisterInstallationID(ctx, installationID, userID.(string)) != nil {
83+
logrus.Errorf("RegisterInstallationID failed: %v", err)
84+
response.ErrorWithMsg(ctx, response.CodeServerError, "failed to register installation ID")
85+
return
86+
} else {
87+
logrus.Infof("RegisterInstallationID success, installationID: %d, user: %v", installationID, userID)
88+
ctx.Redirect(http.StatusFound, "/repository")
89+
}
90+
}
91+
92+
func (c *Controller) handleListModules(ctx *gin.Context) {
93+
page, size := ginutil.GetPageParams(ctx)
94+
userID, ok := ctx.Get(config.ContextUserIDKey)
95+
if !ok {
96+
logrus.Error("user ID not found in context")
97+
response.ErrorWithMsg(ctx, response.CodeNoPermission, "user not authenticated")
98+
return
99+
}
100+
res, err := c.service.PageModule(ctx, page, size, userID.(string))
101+
if err != nil {
102+
logrus.Errorf("PageModule failed: %v", err)
103+
response.ErrorWithMsg(ctx, response.CodeServerError, "failed to list modules")
104+
return
105+
}
106+
response.Success(ctx, res)
107+
}
108+
109+
func (c *Controller) RegisterRoute(group *gin.RouterGroup) {
110+
api := group.Group("/module")
111+
api.POST("/github/app/install", c.handleGithubAppInstall)
112+
api.GET("/github/app/callback", c.handleGithubAppCallback)
113+
api.GET("/list", c.handleListModules)
114+
}

internal/domain/module/model.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package module
2+
3+
import (
4+
"time"
5+
)
6+
7+
type Module struct {
8+
ID int64 `gorm:"primaryKey" json:"id"`
9+
Name string `gorm:"unique;not null;length:64" json:"name"`
10+
Owner string `gorm:"not null;length:64" json:"owner"`
11+
OwnerID int64 `gorm:"not null" json:"ownerID"`
12+
SCMType string `gorm:"not null;length:32" json:"scmType"`
13+
Description string `gorm:"length:1024" json:"description"`
14+
Language string `gorm:"length:128" json:"language"`
15+
Private bool `json:"private"`
16+
HtmlURL string `gorm:"length:256" json:"htmlURL"`
17+
CloneURL string `gorm:"length:256" json:"cloneURL"`
18+
SSHURL string `gorm:"length:256" json:"sshURL"`
19+
SVNURL string `gorm:"length:256" json:"svnURL"`
20+
InstallationID int64 `gorm:"not null" json:"installationID"`
21+
22+
CreatedAt time.Time `json:"createdAt" swaggerignore:"true"`
23+
UpdatedAt time.Time `json:"updatedAt" swaggerignore:"true"`
24+
}
25+
26+
type InstallationIDRelation struct {
27+
InstallationID int64 `gorm:"primaryKey;not null" json:"installationID"`
28+
Owner string `gorm:"not null;length:64" json:"owner"`
29+
}

0 commit comments

Comments
 (0)