Skip to content

Commit

Permalink
Merge pull request #5193 from systeminit/chore(sdf)-Migrate-module-bu…
Browse files Browse the repository at this point in the history
…iltin-promotion-and-rejection-to-v2-endpoints

chore(sdf): Migrate module builtin promotion and rejection to v2 endpoints
  • Loading branch information
stack72 authored Dec 23, 2024
2 parents 711b283 + 72180ba commit eca2b42
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 176 deletions.
9 changes: 4 additions & 5 deletions app/web/src/store/module.store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineStore } from "pinia";
import * as _ from "lodash-es";
import { addStoreHooks, ApiRequest } from "@si/vue-lib/pinia";
import { addStoreHooks, ApiRequest, URLPattern } from "@si/vue-lib/pinia";
import { useWorkspacesStore } from "@/store/workspaces.store";
import { ChangeSetId } from "@/api/sdf/dal/change_set";
import router from "@/router";
Expand Down Expand Up @@ -133,7 +133,7 @@ export const useModuleStore = () => {
"change-sets",
{ changeSetId },
"modules",
];
] as URLPattern;

const WORKSPACE_API_PREFIX = ["v2", "workspaces", { workspaceId }];

Expand Down Expand Up @@ -399,7 +399,7 @@ export const useModuleStore = () => {
async REJECT_REMOTE_MODULE(moduleId: ModuleId) {
return new ApiRequest<{ success: true }>({
method: "post",
url: "/module/reject_module",
url: API_PREFIX.concat([{ moduleId }, "builtins", "reject"]),
params: { id: moduleId, ...getVisibilityParams() },
optimistic: () => {
// remove selection from URL
Expand All @@ -418,8 +418,7 @@ export const useModuleStore = () => {
async PROMOTE_TO_BUILTIN(moduleId: ModuleId) {
return new ApiRequest<{ success: true }>({
method: "post",
url: "/module/set_as_builtin",
params: { id: moduleId, ...getVisibilityParams() },
url: API_PREFIX.concat([{ moduleId }, "builtins", "promote"]),
optimistic: () => {
// remove selection from URL
router.replace({
Expand Down
7 changes: 0 additions & 7 deletions lib/sdf-server/src/service/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ const PKG_EXTENSION: &str = "sipkg";
const MAX_NAME_SEARCH_ATTEMPTS: usize = 100;

pub mod approval_process;
pub mod builtin_module_spec;
pub mod get_module;
pub mod import_workspace_vote;
pub mod install_module;
pub mod reject_module;
pub mod remote_module_spec;

#[remain::sorted]
Expand Down Expand Up @@ -247,11 +245,6 @@ pub fn routes() -> Router<AppState> {
"/remote_module_spec",
get(remote_module_spec::remote_module_spec),
)
.route(
"/set_as_builtin",
post(builtin_module_spec::promote_to_builtin),
)
.route("/reject_module", post(reject_module::reject_module))
.route(
"/begin_approval_process",
post(approval_process::begin_approval_process),
Expand Down
82 changes: 0 additions & 82 deletions lib/sdf-server/src/service/module/builtin_module_spec.rs

This file was deleted.

82 changes: 0 additions & 82 deletions lib/sdf-server/src/service/module/reject_module.rs

This file was deleted.

6 changes: 6 additions & 0 deletions lib/sdf-server/src/service/v2/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ use axum::{
routing::{get, post},
Router,
};
use dal::UserError;
use si_frontend_types as frontend_types;
use telemetry::prelude::*;
use thiserror::Error;

use crate::{service::ApiError, AppState};

mod builtins;
mod contribute;
mod list;
mod sync;
Expand All @@ -35,6 +37,8 @@ pub enum ModulesAPIError {
Transactions(#[from] dal::TransactionsError),
#[error("url parse error: {0}")]
UrlParse(#[from] url::ParseError),
#[error("user error: {0}")]
User(#[from] UserError),
}

impl IntoResponse for ModulesAPIError {
Expand All @@ -61,4 +65,6 @@ pub fn v2_routes() -> Router<AppState> {
.route("/contribute", post(contribute::contribute))
.route("/sync", get(sync::sync))
.route("/", get(list::list))
.route("/:module_id/builtins/reject", post(builtins::reject))
.route("/:module_id/builtins/promote", post(builtins::promote))
}
134 changes: 134 additions & 0 deletions lib/sdf-server/src/service/v2/module/builtins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use axum::{
extract::{Host, OriginalUri, Path},
Json,
};
use dal::{module::ModuleId, ChangeSetId, HistoryActor, User, WorkspacePk};
use module_index_client::ModuleIndexClient;
use serde::{Deserialize, Serialize};

use crate::{
extract::{AccessBuilder, HandlerContext, PosthogClient, RawAccessToken},
track,
};

use super::{ModuleAPIResult, ModulesAPIError};

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PromoteToBuiltinModuleResponse {
pub success: bool,
}

pub async fn promote(
HandlerContext(builder): HandlerContext,
AccessBuilder(request_ctx): AccessBuilder,
RawAccessToken(raw_access_token): RawAccessToken,
PosthogClient(posthog_client): PosthogClient,
OriginalUri(original_uri): OriginalUri,
Host(host_name): Host,
Path((_workspace_pk, change_set_id, module_id)): Path<(WorkspacePk, ChangeSetId, ModuleId)>,
) -> ModuleAPIResult<Json<PromoteToBuiltinModuleResponse>> {
let ctx = builder
.build(request_ctx.build(change_set_id.into()))
.await?;

let module_index_url = match ctx.module_index_url() {
Some(url) => url,
None => return Err(ModulesAPIError::ModuleIndexNotConfigured),
};

let user = match ctx.history_actor() {
HistoryActor::User(user_pk) => User::get_by_pk(&ctx, *user_pk).await?,
_ => None,
};

let (_, created_by_email) = user
.map(|user| (user.name().to_owned(), user.email().to_owned()))
.unwrap_or((
"unauthenticated user name".into(),
"unauthenticated user email".into(),
));

let module_index_client =
ModuleIndexClient::new(module_index_url.try_into()?, &raw_access_token);

module_index_client
.promote_to_builtin(module_id.into(), created_by_email.clone())
.await?;

track(
&posthog_client,
&ctx,
&original_uri,
&host_name,
"promote_to_builtin",
serde_json::json!({
"pkg_id": module_id,
"pkg_promoted_to_builtin_by": created_by_email,
}),
);

ctx.commit().await?;

Ok(Json(PromoteToBuiltinModuleResponse { success: true }))
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct RejectModuleResponse {
pub success: bool,
}

pub async fn reject(
HandlerContext(builder): HandlerContext,
AccessBuilder(request_ctx): AccessBuilder,
RawAccessToken(raw_access_token): RawAccessToken,
PosthogClient(posthog_client): PosthogClient,
OriginalUri(original_uri): OriginalUri,
Host(host_name): Host,
Path((_workspace_pk, change_set_id, module_id)): Path<(WorkspacePk, ChangeSetId, ModuleId)>,
) -> ModuleAPIResult<Json<RejectModuleResponse>> {
let ctx = builder
.build(request_ctx.build(change_set_id.into()))
.await?;

let module_index_url = match ctx.module_index_url() {
Some(url) => url,
None => return Err(ModulesAPIError::ModuleIndexNotConfigured),
};

let user = match ctx.history_actor() {
HistoryActor::User(user_pk) => User::get_by_pk(&ctx, *user_pk).await?,
_ => None,
};

let (_, created_by_email) = user
.map(|user| (user.name().to_owned(), user.email().to_owned()))
.unwrap_or((
"unauthenticated user name".into(),
"unauthenticated user email".into(),
));

let module_index_client =
ModuleIndexClient::new(module_index_url.try_into()?, &raw_access_token);

module_index_client
.reject_module(module_id.into(), created_by_email.clone())
.await?;

track(
&posthog_client,
&ctx,
&original_uri,
&host_name,
"reject_pkg",
serde_json::json!({
"pkg_id": module_id,
"pkg_rejected_by": created_by_email,
}),
);

ctx.commit().await?;

Ok(Json(RejectModuleResponse { success: true }))
}

0 comments on commit eca2b42

Please sign in to comment.