88 "ai-developer/app/models/dtos/asynq_task"
99 "ai-developer/app/repositories"
1010 "ai-developer/app/services/git_providers"
11+ "ai-developer/app/services/integrations"
1112 "ai-developer/app/types/request"
1213 "ai-developer/app/types/response"
1314 "ai-developer/app/utils"
@@ -22,16 +23,17 @@ import (
2223)
2324
2425type ProjectService struct {
25- redisRepo * repositories.ProjectConnectionsRepository
26- projectRepo * repositories.ProjectRepository
27- organisationRepository * repositories.OrganisationRepository
28- storyRepository * repositories.StoryRepository
29- pullRequestRepository * repositories.PullRequestRepository
30- gitnessService * git_providers.GitnessService
31- hashIdGenerator * utils.HashIDGenerator
32- workspaceServiceClient * workspace.WorkspaceServiceClient
33- asynqClient * asynq.Client
34- logger * zap.Logger
26+ redisRepo * repositories.ProjectConnectionsRepository
27+ projectRepo * repositories.ProjectRepository
28+ organisationRepository * repositories.OrganisationRepository
29+ storyRepository * repositories.StoryRepository
30+ pullRequestRepository * repositories.PullRequestRepository
31+ gitnessService * git_providers.GitnessService
32+ hashIdGenerator * utils.HashIDGenerator
33+ workspaceServiceClient * workspace.WorkspaceServiceClient
34+ asynqClient * asynq.Client
35+ githubIntegrationService * integrations.GithubIntegrationService
36+ logger * zap.Logger
3537}
3638
3739func (s * ProjectService ) GetAllProjectsOfOrganisation (organisationId int ) ([]response.GetAllProjectsResponse , error ) {
@@ -82,6 +84,87 @@ func (s *ProjectService) GetProjectDetailsById(projectId int) (*models.Project,
8284 return project , nil
8385}
8486
87+ func (s * ProjectService ) CreateProjectFromGit (userId uint , orgId uint , requestData request.CreateProjectFromGitRequest ) (project * models.Project , err error ) {
88+ integrationDetails , err := s .githubIntegrationService .GetGithubIntegrationDetails (uint64 (userId ))
89+ if err != nil {
90+ s .logger .Error ("Error getting github integration details" , zap .Error (err ))
91+ return nil , err
92+ }
93+
94+ hashID := s .hashIdGenerator .Generate () + "-" + uuid .New ().String ()
95+ url := "http://localhost:8081/?folder=/workspaces/" + hashID
96+ backend_url := "http://localhost:5000"
97+ frontend_url := "http://localhost:3000"
98+ env := config .Get ("app.env" )
99+ host := config .Get ("workspace.host" )
100+ if env == "production" {
101+ url = fmt .Sprintf ("https://%s.%s/?folder=/workspaces/%s" , hashID , host , hashID )
102+ backend_url = fmt .Sprintf ("https://be-%s.%s" , hashID , host )
103+ frontend_url = fmt .Sprintf ("https://fe-%s.%s" , hashID , host )
104+ }
105+ project = & models.Project {
106+ OrganisationID : orgId ,
107+ Name : requestData .Name ,
108+ BackendFramework : requestData .Framework ,
109+ FrontendFramework : requestData .FrontendFramework ,
110+ Description : requestData .Description ,
111+ HashID : hashID ,
112+ Url : url ,
113+ BackendURL : backend_url ,
114+ FrontendURL : frontend_url ,
115+ }
116+
117+ organisation , err := s .organisationRepository .GetOrganisationByID (uint (int (project .OrganisationID )))
118+ spaceOrProjectName := s .gitnessService .GetSpaceOrProjectName (organisation )
119+ repository , err := s .gitnessService .CreateRepository (spaceOrProjectName , project .Name , project .Description )
120+ if err != nil {
121+ s .logger .Error ("Error creating repository" , zap .Error (err ))
122+ return nil , err
123+ }
124+ httpPrefix := "https"
125+
126+ if config .AppEnv () == constants .Development {
127+ httpPrefix = "http"
128+ }
129+
130+ remoteGitURL := fmt .Sprintf ("%s://%s:%s@%s/git/%s/%s.git" , httpPrefix , config .GitnessUser (), config .GitnessToken (), config .GitnessHost (), spaceOrProjectName , project .Name )
131+ //Making Call to Workspace Service to create workspace on project level
132+ _ , err = s .workspaceServiceClient .ImportGitRepository (
133+ & request.ImportGitRepository {
134+ WorkspaceId : hashID ,
135+ Repository : requestData .Repository ,
136+ Username : integrationDetails .GithubUserId ,
137+ Password : integrationDetails .AccessToken ,
138+ RemoteURL : remoteGitURL ,
139+ GitnessUser : config .GitnessUser (),
140+ GitnessToken : config .GitnessToken (),
141+ },
142+ )
143+
144+ if err != nil {
145+ s .logger .Error ("Error creating workspace" , zap .Error (err ))
146+ return nil , err
147+ }
148+
149+ //Enqueue job to delete workspace with updated delay
150+ payloadBytes , err := json .Marshal (asynq_task.CreateDeleteWorkspaceTaskPayload {
151+ WorkspaceID : project .HashID ,
152+ })
153+ if err != nil {
154+ s .logger .Error ("Failed to marshal payload" , zap .Error (err ))
155+ return nil , err
156+ }
157+ _ , err = s .asynqClient .Enqueue (
158+ asynq .NewTask (constants .DeleteWorkspaceTaskType , payloadBytes ),
159+ asynq .ProcessIn (constants .ProjectConnectionTTL + 10 * time .Minute ),
160+ asynq .MaxRetry (3 ),
161+ asynq .TaskID ("delete:fallback:" + project .HashID ),
162+ )
163+
164+ s .logger .Info ("Project created successfully with repository" , zap .Any ("project" , project ), zap .Any ("repository" , repository ))
165+ return s .projectRepo .CreateProject (project )
166+ }
167+
85168func (s * ProjectService ) CreateProject (organisationID int , requestData request.CreateProjectRequest ) (* models.Project , error ) {
86169 hashID := s .hashIdGenerator .Generate () + "-" + uuid .New ().String ()
87170 url := "http://localhost:8081/?folder=/workspaces/" + hashID
@@ -314,21 +397,23 @@ func NewProjectService(projectRepo *repositories.ProjectRepository,
314397 storyRepository * repositories.StoryRepository ,
315398 pullRequestRepository * repositories.PullRequestRepository ,
316399 workspaceServiceClient * workspace.WorkspaceServiceClient ,
400+ githubIntegrationService * integrations.GithubIntegrationService ,
317401 repo * repositories.ProjectConnectionsRepository ,
318402 asynqClient * asynq.Client ,
319403 logger * zap.Logger ,
320404) * ProjectService {
321405 return & ProjectService {
322- projectRepo : projectRepo ,
323- gitnessService : gitnessService ,
324- organisationRepository : organisationRepository ,
325- storyRepository : storyRepository ,
326- pullRequestRepository : pullRequestRepository ,
327- workspaceServiceClient : workspaceServiceClient ,
328- redisRepo : repo ,
329- hashIdGenerator : utils .NewHashIDGenerator (5 ),
330- logger : logger .Named ("ProjectService" ),
331- asynqClient : asynqClient ,
406+ projectRepo : projectRepo ,
407+ gitnessService : gitnessService ,
408+ organisationRepository : organisationRepository ,
409+ storyRepository : storyRepository ,
410+ pullRequestRepository : pullRequestRepository ,
411+ workspaceServiceClient : workspaceServiceClient ,
412+ redisRepo : repo ,
413+ hashIdGenerator : utils .NewHashIDGenerator (5 ),
414+ logger : logger .Named ("ProjectService" ),
415+ asynqClient : asynqClient ,
416+ githubIntegrationService : githubIntegrationService ,
332417 }
333418}
334419
0 commit comments