|
| 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 | +} |
0 commit comments