11use std:: string:: ToString ;
22
33use bencher_json:: {
4- project:: { JsonProjectPatch , JsonProjectPatchNull , JsonUpdateProject , Visibility } ,
4+ project:: { JsonProjectPatch , JsonProjectPatchNull , JsonUpdateProject , ProjectRole , Visibility } ,
55 DateTime , JsonNewProject , JsonProject , NameId , NameIdKind , ProjectUuid , ResourceId ,
66 ResourceName , Slug , Url ,
77} ;
88use bencher_rbac:: { project:: Permission , Organization , Project } ;
99use diesel:: { ExpressionMethods , QueryDsl , RunQueryDsl } ;
1010use dropshot:: HttpError ;
11+ use project_role:: InsertProjectRole ;
12+ use slog:: Logger ;
1113
1214use crate :: {
1315 conn_lock,
@@ -72,7 +74,8 @@ impl QueryProject {
7274 Project
7375 ) ;
7476
75- pub async fn get_or_create (
77+ pub async fn get_or_create_organization_project (
78+ log : & Logger ,
7679 context : & ApiContext ,
7780 auth_user : & AuthUser ,
7881 organization : & ResourceId ,
@@ -98,13 +101,13 @@ impl QueryProject {
98101 {
99102 query_project
100103 } else {
101- let new_project = JsonNewProject {
104+ let json_project = JsonNewProject {
102105 name : slug. clone ( ) . into ( ) ,
103106 slug : Some ( slug. clone ( ) ) ,
104107 url : None ,
105108 visibility : None ,
106109 } ;
107- Self :: create ( context, & query_organization, new_project , auth_user ) . await ?
110+ Self :: create ( log , context, auth_user , & query_organization, json_project ) . await ?
108111 }
109112 } ,
110113 NameIdKind :: Name ( name) => {
@@ -115,28 +118,29 @@ impl QueryProject {
115118 {
116119 query_project
117120 } else {
118- let new_project = JsonNewProject {
121+ let json_project = JsonNewProject {
119122 name,
120123 slug : None ,
121124 url : None ,
122125 visibility : None ,
123126 } ;
124- Self :: create ( context, & query_organization, new_project , auth_user ) . await ?
127+ Self :: create ( log , context, auth_user , & query_organization, json_project ) . await ?
125128 }
126129 } ,
127130 } ;
128131
129132 Ok ( query_project)
130133 }
131134
132- async fn create (
135+ pub async fn create (
136+ log : & Logger ,
133137 context : & ApiContext ,
134- query_organization : & QueryOrganization ,
135- new_project : JsonNewProject ,
136138 auth_user : & AuthUser ,
139+ query_organization : & QueryOrganization ,
140+ json_project : JsonNewProject ,
137141 ) -> Result < Self , HttpError > {
138142 let insert_project =
139- InsertProject :: from_json ( conn_lock ! ( context) , query_organization, new_project ) ?;
143+ InsertProject :: from_json ( conn_lock ! ( context) , query_organization, json_project ) ?;
140144
141145 // Check to see if user has permission to create a project within the organization
142146 context
@@ -148,12 +152,36 @@ impl QueryProject {
148152 )
149153 . map_err ( forbidden_error) ?;
150154
151- let conn = conn_lock ! ( context) ;
152155 diesel:: insert_into ( project_table:: table)
153156 . values ( & insert_project)
154- . execute ( conn )
157+ . execute ( conn_lock ! ( context ) )
155158 . map_err ( resource_conflict_err ! ( Project , & insert_project) ) ?;
156- Self :: from_uuid ( conn, query_organization. id , insert_project. uuid )
159+ let query_project = Self :: from_uuid (
160+ conn_lock ! ( context) ,
161+ query_organization. id ,
162+ insert_project. uuid ,
163+ ) ?;
164+ slog:: debug!( log, "Created project: {query_project:?}" ) ;
165+
166+ let timestamp = DateTime :: now ( ) ;
167+ // Connect the user to the project as a `Maintainer`
168+ let insert_proj_role = InsertProjectRole {
169+ user_id : auth_user. id ( ) ,
170+ project_id : query_project. id ,
171+ role : ProjectRole :: Maintainer ,
172+ created : timestamp,
173+ modified : timestamp,
174+ } ;
175+ diesel:: insert_into ( schema:: project_role:: table)
176+ . values ( & insert_proj_role)
177+ . execute ( conn_lock ! ( context) )
178+ . map_err ( resource_conflict_err ! ( ProjectRole , insert_proj_role) ) ?;
179+ slog:: debug!( log, "Added project role: {insert_proj_role:?}" ) ;
180+
181+ #[ cfg( feature = "plus" ) ]
182+ context. update_index ( log, & query_project) . await ;
183+
184+ Ok ( query_project)
157185 }
158186
159187 pub fn is_public ( & self ) -> bool {
0 commit comments