|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "ai-developer/app/services/integrations" |
| 5 | + "github.com/gin-gonic/gin" |
| 6 | + "go.uber.org/zap" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +type GithubIntegrationController struct { |
| 11 | + githubIntegrationService *integrations.GithubIntegrationService |
| 12 | + logger *zap.Logger |
| 13 | +} |
| 14 | + |
| 15 | +func (gic *GithubIntegrationController) Authorize(c *gin.Context) { |
| 16 | + userId, _ := c.Get("user_id") |
| 17 | + gic.logger.Debug( |
| 18 | + "Authorizing github integration", |
| 19 | + zap.Any("user_id", userId), |
| 20 | + ) |
| 21 | + authCodeUrl := gic.githubIntegrationService.GetRedirectUrl(uint64(userId.(int))) |
| 22 | + c.Redirect(http.StatusTemporaryRedirect, authCodeUrl) |
| 23 | +} |
| 24 | + |
| 25 | +func (gic *GithubIntegrationController) CheckIfIntegrationExists(c *gin.Context) { |
| 26 | + userId, _ := c.Get("user_id") |
| 27 | + hasIntegration, err := gic.githubIntegrationService.HasGithubIntegration(uint64(userId.(int))) |
| 28 | + if err != nil { |
| 29 | + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) |
| 30 | + return |
| 31 | + } |
| 32 | + c.JSON(http.StatusOK, gin.H{"integrated": hasIntegration}) |
| 33 | +} |
| 34 | + |
| 35 | +func (gic *GithubIntegrationController) GetRepositories(c *gin.Context) { |
| 36 | + userId, _ := c.Get("user_id") |
| 37 | + repositories, err := gic.githubIntegrationService.GetRepositories(uint64(userId.(int))) |
| 38 | + if err != nil { |
| 39 | + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) |
| 40 | + return |
| 41 | + } |
| 42 | + response := make([]map[string]interface{}, 0) |
| 43 | + for _, repo := range repositories { |
| 44 | + response = append(response, map[string]interface{}{ |
| 45 | + "id": repo.GetID(), |
| 46 | + "name": repo.GetName(), |
| 47 | + "url": repo.GetURL(), |
| 48 | + "full_name": repo.GetFullName(), |
| 49 | + }) |
| 50 | + } |
| 51 | + c.JSON(http.StatusOK, gin.H{"repositories": response}) |
| 52 | +} |
| 53 | + |
| 54 | +func (gic *GithubIntegrationController) HandleCallback(c *gin.Context) { |
| 55 | + code := c.Query("code") |
| 56 | + state := c.Query("state") |
| 57 | + |
| 58 | + gic.logger.Debug( |
| 59 | + "Handling github integration callback", |
| 60 | + zap.String("code", code), |
| 61 | + zap.String("state", state), |
| 62 | + ) |
| 63 | + |
| 64 | + err := gic.githubIntegrationService.GenerateAndSaveAccessToken(code, state) |
| 65 | + if err != nil { |
| 66 | + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) |
| 67 | + return |
| 68 | + } else { |
| 69 | + c.JSON(http.StatusOK, gin.H{"message": "Integration successful"}) |
| 70 | + return |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func NewGithubIntegrationController( |
| 75 | + githubIntegrationService *integrations.GithubIntegrationService, |
| 76 | + logger *zap.Logger, |
| 77 | +) *GithubIntegrationController { |
| 78 | + return &GithubIntegrationController{ |
| 79 | + githubIntegrationService: githubIntegrationService, |
| 80 | + logger: logger, |
| 81 | + } |
| 82 | +} |
0 commit comments