Skip to content

Commit 54e0bdb

Browse files
committed
project_create
1 parent 5fc08ca commit 54e0bdb

File tree

3 files changed

+58
-63
lines changed

3 files changed

+58
-63
lines changed

lib/api_organizations/src/projects.rs

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ use bencher_endpoint::{
22
CorsResponse, Endpoint, Get, Post, ResponseCreated, ResponseOk, TotalCount,
33
};
44
use bencher_json::{
5-
project::{
6-
measure::built_in::default::{Latency, Throughput},
7-
ProjectRole,
8-
},
9-
DateTime, JsonDirection, JsonNewProject, JsonPagination, JsonProject, JsonProjects, ResourceId,
5+
project::measure::built_in::default::{Latency, Throughput},
6+
JsonDirection, JsonNewProject, JsonPagination, JsonProject, JsonProjects, ResourceId,
107
ResourceName, Search,
118
};
129
use bencher_rbac::organization::Permission;
@@ -15,16 +12,15 @@ use bencher_schema::model::organization::plan::PlanKind;
1512
use bencher_schema::{
1613
conn_lock,
1714
context::ApiContext,
18-
error::{forbidden_error, resource_conflict_err, resource_not_found_err},
15+
error::{resource_conflict_err, resource_not_found_err},
1916
model::{
2017
organization::QueryOrganization,
2118
project::{
2219
branch::InsertBranch,
2320
measure::{InsertMeasure, QueryMeasure},
24-
project_role::InsertProjectRole,
2521
testbed::{InsertTestbed, QueryTestbed},
2622
threshold::InsertThreshold,
27-
InsertProject, QueryProject,
23+
QueryProject,
2824
},
2925
user::auth::{AuthUser, BearerToken},
3026
},
@@ -230,41 +226,9 @@ async fn post_inner(
230226
.await?;
231227
}
232228

233-
// Create the project
234-
let insert_project =
235-
InsertProject::from_json(conn_lock!(context), &query_organization, json_project)?;
236-
slog::debug!(log, "Creating project: {insert_project:?}");
237-
238-
// Check to see if user has permission to create a project within the organization
239-
context
240-
.rbac
241-
.is_allowed_organization(auth_user, Permission::Create, &insert_project)
242-
.map_err(forbidden_error)?;
243-
244-
diesel::insert_into(schema::project::table)
245-
.values(&insert_project)
246-
.execute(conn_lock!(context))
247-
.map_err(resource_conflict_err!(Project, insert_project))?;
248-
let query_project = schema::project::table
249-
.filter(schema::project::uuid.eq(&insert_project.uuid))
250-
.first::<QueryProject>(conn_lock!(context))
251-
.map_err(resource_not_found_err!(Project, insert_project))?;
252-
slog::debug!(log, "Created project: {query_project:?}");
253-
254-
let timestamp = DateTime::now();
255-
// Connect the user to the project as a `Maintainer`
256-
let insert_proj_role = InsertProjectRole {
257-
user_id: auth_user.id(),
258-
project_id: query_project.id,
259-
role: ProjectRole::Maintainer,
260-
created: timestamp,
261-
modified: timestamp,
262-
};
263-
diesel::insert_into(schema::project_role::table)
264-
.values(&insert_proj_role)
265-
.execute(conn_lock!(context))
266-
.map_err(resource_conflict_err!(ProjectRole, insert_proj_role))?;
267-
slog::debug!(log, "Added project role: {insert_proj_role:?}");
229+
// Create a new project
230+
let query_project =
231+
QueryProject::create(log, context, auth_user, &query_organization, json_project).await?;
268232

269233
// Add a `main` branch to the project
270234
let query_branch = InsertBranch::main(log, context, query_project.id).await?;
@@ -318,8 +282,5 @@ async fn post_inner(
318282
)?;
319283
slog::debug!(log, "Added project threshold: {threshold_id}");
320284

321-
#[cfg(feature = "plus")]
322-
context.update_index(log, &query_project).await;
323-
324285
query_project.into_json(conn_lock!(context))
325286
}

lib/api_run/src/run.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,18 @@ async fn post_inner(
5656
json_run.project.as_ref(),
5757
) {
5858
(Some(auth_user), Some(organization), Some(project)) => {
59-
let query_project =
60-
QueryProject::get_or_create(context, &auth_user, organization, project)
61-
.await
62-
.map_err(|e| forbidden_error(e.to_string()))?;
59+
let query_project = QueryProject::get_or_create_organization_project(
60+
log,
61+
context,
62+
&auth_user,
63+
organization,
64+
project,
65+
)
66+
.await
67+
.map_err(|e| forbidden_error(e.to_string()))?;
6368
(auth_user, query_project)
6469
},
70+
(Some(auth_user), Some(organization), None) => return Err(bad_request_error("todo")),
6571
_ => return Err(bad_request_error("Not yet supported")),
6672
};
6773

lib/bencher_schema/src/model/project/mod.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use std::string::ToString;
22

33
use 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
};
88
use bencher_rbac::{project::Permission, Organization, Project};
99
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
1010
use dropshot::HttpError;
11+
use project_role::InsertProjectRole;
12+
use slog::Logger;
1113

1214
use 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

Comments
 (0)