-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathgithub_integration_controller.go
More file actions
88 lines (78 loc) · 2.66 KB
/
github_integration_controller.go
File metadata and controls
88 lines (78 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package controllers
import (
"ai-developer/app/config"
"ai-developer/app/services/integrations"
"fmt"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"net/http"
)
type GithubIntegrationController struct {
githubIntegrationService *integrations.GithubIntegrationService
logger *zap.Logger
}
func (gic *GithubIntegrationController) Authorize(c *gin.Context) {
userId, _ := c.Get("user_id")
gic.logger.Debug(
"Authorizing github integration",
zap.Any("user_id", userId),
)
authCodeUrl := gic.githubIntegrationService.GetRedirectUrl(uint64(userId.(int)))
c.Redirect(http.StatusTemporaryRedirect, authCodeUrl)
}
func (gic *GithubIntegrationController) DeleteIntegration(c *gin.Context) {
userId, _ := c.Get("user_id")
err := gic.githubIntegrationService.DeleteIntegration(uint64(userId.(int)))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Integration deleted successfully"})
}
func (gic *GithubIntegrationController) CheckIfIntegrationExists(c *gin.Context) {
userId, _ := c.Get("user_id")
hasIntegration, err := gic.githubIntegrationService.HasGithubIntegration(uint64(userId.(int)))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"integrated": hasIntegration})
}
func (gic *GithubIntegrationController) GetRepositories(c *gin.Context) {
userId, _ := c.Get("user_id")
repositories, err := gic.githubIntegrationService.GetRepositories(uint64(userId.(int)))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
response := make([]map[string]interface{}, 0)
for _, repo := range repositories {
response = append(response, map[string]interface{}{
"id": repo.GetID(),
"url": repo.GetCloneURL(),
"name": repo.GetFullName(),
})
}
c.JSON(http.StatusOK, gin.H{"repositories": response})
}
func (gic *GithubIntegrationController) HandleCallback(c *gin.Context) {
code := c.Query("code")
state := c.Query("state")
gic.logger.Debug(
"Handling github integration callback",
zap.String("code", code),
zap.String("state", state),
)
_ = gic.githubIntegrationService.GenerateAndSaveAccessToken(code, state)
redirectUrl := fmt.Sprintf("%s/settings?page=integrations", config.GithubFrontendURL())
c.Redirect(http.StatusTemporaryRedirect, redirectUrl)
}
func NewGithubIntegrationController(
githubIntegrationService *integrations.GithubIntegrationService,
logger *zap.Logger,
) *GithubIntegrationController {
return &GithubIntegrationController{
githubIntegrationService: githubIntegrationService,
logger: logger,
}
}