Skip to content

Commit e0134cf

Browse files
committed
Add delete github integration api
1 parent 8f6d6bf commit e0134cf

9 files changed

+100
-63
lines changed

app/controllers/github_integration_controller.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package controllers
22

33
import (
4+
"ai-developer/app/config"
45
"ai-developer/app/services/integrations"
6+
"fmt"
57
"github.com/gin-gonic/gin"
68
"go.uber.org/zap"
79
"net/http"
@@ -22,6 +24,16 @@ func (gic *GithubIntegrationController) Authorize(c *gin.Context) {
2224
c.Redirect(http.StatusTemporaryRedirect, authCodeUrl)
2325
}
2426

27+
func (gic *GithubIntegrationController) DeleteIntegration(c *gin.Context) {
28+
userId, _ := c.Get("user_id")
29+
err := gic.githubIntegrationService.DeleteIntegration(uint64(userId.(int)))
30+
if err != nil {
31+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
32+
return
33+
}
34+
c.JSON(http.StatusOK, gin.H{"message": "Integration deleted successfully"})
35+
}
36+
2537
func (gic *GithubIntegrationController) CheckIfIntegrationExists(c *gin.Context) {
2638
userId, _ := c.Get("user_id")
2739
hasIntegration, err := gic.githubIntegrationService.HasGithubIntegration(uint64(userId.(int)))
@@ -60,14 +72,9 @@ func (gic *GithubIntegrationController) HandleCallback(c *gin.Context) {
6072
zap.String("state", state),
6173
)
6274

63-
err := gic.githubIntegrationService.GenerateAndSaveAccessToken(code, state)
64-
if err != nil {
65-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
66-
return
67-
} else {
68-
c.JSON(http.StatusOK, gin.H{"message": "Integration successful"})
69-
return
70-
}
75+
_ = gic.githubIntegrationService.GenerateAndSaveAccessToken(code, state)
76+
redirectUrl := fmt.Sprintf("%s/settings?page=integrations", config.GithubFrontendURL())
77+
c.Redirect(http.StatusTemporaryRedirect, redirectUrl)
7178
}
7279

7380
func NewGithubIntegrationController(

app/controllers/project_controller.go

+8-38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controllers
22

33
import (
4+
"ai-developer/app/models"
45
"ai-developer/app/services"
56
"ai-developer/app/types/request"
67
"net/http"
@@ -56,8 +57,8 @@ func (controller *ProjectController) GetProjectById(context *gin.Context) {
5657
context.JSON(http.StatusOK, project)
5758
}
5859

59-
func (controller *ProjectController) CreateProjectFromGit(context *gin.Context) {
60-
var createProjectRequest request.CreateProjectFromGitRequest
60+
func (controller *ProjectController) CreateProject(context *gin.Context) {
61+
var createProjectRequest request.CreateProjectRequest
6162
if err := context.ShouldBindJSON(&createProjectRequest); err != nil {
6263
context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
6364
return
@@ -73,44 +74,13 @@ func (controller *ProjectController) CreateProjectFromGit(context *gin.Context)
7374
return
7475
}
7576

76-
project, err := controller.projectService.CreateProjectFromGit(user.ID, user.OrganisationID, createProjectRequest)
77-
if err != nil {
78-
context.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
79-
return
77+
var project *models.Project
78+
if createProjectRequest.Repository == nil {
79+
project, err = controller.projectService.CreateProject(int(user.OrganisationID), createProjectRequest)
80+
} else {
81+
project, err = controller.projectService.CreateProjectFromGit(user.ID, user.OrganisationID, createProjectRequest)
8082
}
8183

82-
context.JSON(
83-
http.StatusOK,
84-
gin.H{
85-
"project_id": project.ID,
86-
"project_url": project.Url,
87-
"project_name": project.Name,
88-
"project_frontend_url": project.FrontendURL,
89-
"project_backend_url": project.BackendURL,
90-
"project_framework": project.BackendFramework,
91-
"project_frontend_framework": project.FrontendFramework,
92-
},
93-
)
94-
return
95-
}
96-
97-
func (controller *ProjectController) CreateProject(context *gin.Context) {
98-
var createProjectRequest request.CreateProjectRequest
99-
if err := context.ShouldBindJSON(&createProjectRequest); err != nil {
100-
context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
101-
return
102-
}
103-
email, _ := context.Get("email")
104-
user, err := controller.userService.GetUserByEmail(email.(string))
105-
if err != nil {
106-
context.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
107-
return
108-
}
109-
if user == nil {
110-
context.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "User not found"})
111-
return
112-
}
113-
project, err := controller.projectService.CreateProject(int(user.OrganisationID), createProjectRequest)
11484
if err != nil {
11585
context.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
11686
return

app/repositories/integrations.go

+16
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ func (ir *IntegrationsRepository) FindIntegrationIdByUserIdAndType(userId uint64
2424
return
2525
}
2626

27+
func (ir *IntegrationsRepository) DeleteIntegration(userId uint64, integrationType string) (err error) {
28+
ir.logger.Info(
29+
"Deleting integration",
30+
zap.Uint64("userId", userId),
31+
zap.String("integrationType", integrationType),
32+
)
33+
err = ir.db.Unscoped().Where(&models.Integration{
34+
UserId: userId,
35+
IntegrationType: integrationType,
36+
}).Delete(&models.Integration{
37+
UserId: userId,
38+
IntegrationType: integrationType,
39+
}).Error
40+
return
41+
}
42+
2743
func (ir *IntegrationsRepository) AddOrUpdateIntegration(
2844
userId uint64,
2945
integrationType string,

app/services/integrations/github_integration_service.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"ai-developer/app/models/dtos/integrations"
66
"ai-developer/app/utils"
77
"context"
8+
"errors"
89
"fmt"
910
"github.com/google/go-github/github"
1011
"go.uber.org/zap"
1112
"golang.org/x/oauth2"
1213
githubOAuth "golang.org/x/oauth2/github"
14+
"gorm.io/gorm"
1315
"strconv"
1416
)
1517

@@ -22,12 +24,20 @@ type GithubIntegrationService struct {
2224
integrationService *IntegrationService
2325
}
2426

27+
func (gis *GithubIntegrationService) DeleteIntegration(userId uint64) (err error) {
28+
err = gis.integrationService.DeleteIntegration(userId, GithubIntegrationType)
29+
return
30+
}
31+
2532
func (gis *GithubIntegrationService) GetRedirectUrl(userId uint64) string {
2633
return gis.oauthConfig.AuthCodeURL(fmt.Sprintf("%d", userId), oauth2.AccessTypeOnline)
2734
}
2835

2936
func (gis *GithubIntegrationService) HasGithubIntegration(userId uint64) (hasIntegration bool, err error) {
3037
integration, err := gis.integrationService.FindIntegrationIdByUserIdAndType(userId, GithubIntegrationType)
38+
if errors.Is(err, gorm.ErrRecordNotFound) {
39+
return false, nil
40+
}
3141
if err != nil {
3242
return
3343
}
@@ -37,7 +47,11 @@ func (gis *GithubIntegrationService) HasGithubIntegration(userId uint64) (hasInt
3747

3848
func (gis *GithubIntegrationService) GetRepositories(userId uint64) (repos []*github.Repository, err error) {
3949
integration, err := gis.integrationService.FindIntegrationIdByUserIdAndType(userId, GithubIntegrationType)
40-
if err != nil || integration == nil {
50+
if errors.Is(err, gorm.ErrRecordNotFound) {
51+
return make([]*github.Repository, 0), nil
52+
}
53+
54+
if err != nil {
4155
return
4256
}
4357

app/services/integrations/integration_service.go

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ func (is *IntegrationService) FindIntegrationIdByUserIdAndType(userId uint64, in
1616
return is.integrationsRepository.FindIntegrationIdByUserIdAndType(userId, integrationType)
1717
}
1818

19+
func (is *IntegrationService) DeleteIntegration(userId uint64, integrationType string) (err error) {
20+
return is.integrationsRepository.DeleteIntegration(userId, integrationType)
21+
}
22+
1923
func (is *IntegrationService) AddOrUpdateIntegration(
2024
userId uint64,
2125
integrationType string,

app/services/project_service.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (s *ProjectService) GetProjectDetailsById(projectId int) (*models.Project,
8484
return project, nil
8585
}
8686

87-
func (s *ProjectService) CreateProjectFromGit(userId uint, orgId uint, requestData request.CreateProjectFromGitRequest) (project *models.Project, err error) {
87+
func (s *ProjectService) CreateProjectFromGit(userId uint, orgId uint, requestData request.CreateProjectRequest) (project *models.Project, err error) {
8888
integrationDetails, err := s.githubIntegrationService.GetGithubIntegrationDetails(uint64(userId))
8989
if err != nil {
9090
s.logger.Error("Error getting github integration details", zap.Error(err))
@@ -132,7 +132,7 @@ func (s *ProjectService) CreateProjectFromGit(userId uint, orgId uint, requestDa
132132
_, err = s.workspaceServiceClient.ImportGitRepository(
133133
&request.ImportGitRepository{
134134
WorkspaceId: hashID,
135-
Repository: requestData.Repository,
135+
Repository: *requestData.Repository,
136136
Username: integrationDetails.GithubUserId,
137137
Password: integrationDetails.AccessToken,
138138
RemoteURL: remoteGitURL,
+5-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
package request
22

33
type CreateProjectRequest struct {
4-
Name string `json:"name"`
5-
Framework string `json:"framework"`
6-
FrontendFramework string `json:"frontend_framework"`
7-
Description string `json:"description"`
8-
}
9-
10-
type CreateProjectFromGitRequest struct {
11-
Name string `json:"name"`
12-
Framework string `json:"framework"`
13-
FrontendFramework string `json:"frontend_framework"`
14-
Description string `json:"description"`
15-
Repository string `json:"repository"`
4+
Name string `json:"name"`
5+
Framework string `json:"framework"`
6+
FrontendFramework string `json:"frontend_framework"`
7+
Description string `json:"description"`
8+
Repository *string `json:"repository"`
169
}

server.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,6 @@ func main() {
479479
projects.POST("", projectsController.CreateProject)
480480
projects.PUT("", projectsController.UpdateProject)
481481

482-
projects.POST("/import", projectsController.CreateProjectFromGit)
483-
484482
projects.GET("/", projectsController.GetAllProjects)
485483
projects.POST("/", projectsController.CreateProject)
486484
projects.PUT("/", projectsController.UpdateProject)
@@ -550,7 +548,10 @@ func main() {
550548
integrations := api.Group("/integrations", middleware.AuthenticateJWT())
551549

552550
githubIntegration := integrations.Group("/github")
551+
553552
githubIntegration.GET("", githubIntegrationController.CheckIfIntegrationExists)
553+
githubIntegration.DELETE("", githubIntegrationController.DeleteIntegration)
554+
554555
githubIntegration.GET("/repos", githubIntegrationController.GetRepositories)
555556
githubIntegration.GET("/authorize", githubIntegrationController.Authorize)
556557
githubIntegration.GET("/callback", githubIntegrationController.HandleCallback)

worker.go

+32
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"ai-developer/app/client/workspace"
77
"ai-developer/app/config"
88
"ai-developer/app/constants"
9+
"ai-developer/app/controllers"
910
"ai-developer/app/monitoring"
1011
"ai-developer/app/repositories"
1112
"ai-developer/app/services"
1213
"ai-developer/app/services/git_providers"
14+
"ai-developer/app/services/integrations"
1315
"ai-developer/app/services/s3_providers"
1416
"ai-developer/app/tasks"
1517
"context"
@@ -266,6 +268,36 @@ func main() {
266268
}, nil)
267269
})
268270

271+
// Integration
272+
{
273+
if err = c.Provide(repositories.NewIntegrationsRepository); err != nil {
274+
config.Logger.Error("Error providing IntegrationsRepository", zap.Error(err))
275+
panic(err)
276+
}
277+
if err = c.Provide(integrations.NewIntegrationService); err != nil {
278+
config.Logger.Error("Error providing IntegrationService", zap.Error(err))
279+
panic(err)
280+
}
281+
}
282+
283+
// Github Integration
284+
{
285+
if err = c.Provide(config.NewGithubIntegrationConfig); err != nil {
286+
config.Logger.Error("Error providing GithubIntegrationConfig", zap.Error(err))
287+
panic(err)
288+
}
289+
290+
if err = c.Provide(integrations.NewGithubIntegrationService); err != nil {
291+
config.Logger.Error("Error providing GithubIntegrationService", zap.Error(err))
292+
panic(err)
293+
}
294+
295+
if err = c.Provide(controllers.NewGithubIntegrationController); err != nil {
296+
config.Logger.Error("Error providing GithubIntegrationController", zap.Error(err))
297+
panic(err)
298+
}
299+
}
300+
269301
err = c.Provide(func(
270302
deleteWorkspaceTaskHandler *tasks.DeleteWorkspaceTaskHandler,
271303
createExecutionJobTaskHandler *tasks.CreateExecutionJobTaskHandler,

0 commit comments

Comments
 (0)