Skip to content

Commit

Permalink
fix(dal, sdf): Change contribute workflow to work for single modules …
Browse files Browse the repository at this point in the history
…only
  • Loading branch information
stack72 committed Jul 25, 2024
1 parent 33a2890 commit 40e68e5
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 157 deletions.
8 changes: 2 additions & 6 deletions app/web/src/api/sdf/dal/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SchemaId } from "./schema";
import { SchemaId, SchemaVariantId } from "./schema";

export type ModuleId = string;

Expand All @@ -15,11 +15,7 @@ export interface LatestModule {
}

export interface ModuleContributeRequest {
modules: ModuleContributeRequestItem[];
}

export interface ModuleContributeRequestItem {
name: string;
version: string;
schemaId: SchemaId;
schemaVariantId: SchemaVariantId;
}
23 changes: 13 additions & 10 deletions app/web/src/components/AssetCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,23 @@
import { computed, PropType, ref } from "vue";
import tinycolor from "tinycolor2";
import clsx from "clsx";
import { useTheme, Stack, Icon, ErrorMessage } from "@si/vue-lib/design-system";
import {
useTheme,
Modal,
Stack,
Icon,
ErrorMessage,
} from "@si/vue-lib/design-system";
import { useRouter } from "vue-router";
import { format as dateFormat } from "date-fns";
import { useAssetStore } from "@/store/asset.store";
import { SchemaVariantId, SchemaVariant } from "@/api/sdf/dal/schema";
import { getAssetIcon } from "@/store/components.store";
import { useModuleStore } from "@/store/module.store";
import { ModuleContributeRequest } from "@/api/sdf/dal/module";
import IconButton from "./IconButton.vue";
import EditingPill from "./EditingPill.vue";
import AssetContributeModal from "./AssetContributeModal.vue";
import { Modal } from "@si/vue-lib/design-system";
import { ModuleContributeRequest } from "@/api/sdf/dal/module";
import { format as dateFormat, parseISO } from "date-fns";
const props = defineProps({
titleCard: { type: Boolean },
Expand All @@ -155,16 +160,14 @@ const contributeAsset = () => contributeAssetModalRef.value?.open();
const onContributeAsset = () => contributeAssetSuccessModalRef.value?.open();
const contributeRequest = computed((): ModuleContributeRequest => {
const modules = [];
if (asset.value) {
const version = dateFormat(Date.now(), "yyyyMMddkkmmss");
modules.push({
return {
name: `${asset.value.schemaName} ${version}`,
version,
schemaId: asset.value.schemaId,
});
}
return { modules };
schemaVariantId: asset.value.schemaVariantId,
};
} else throw new Error("cannot contribute: no asset selected");
});
const editingVersionDoesNotExist = computed<boolean>(
Expand Down
17 changes: 4 additions & 13 deletions app/web/src/components/AssetContributeModal.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
<template>
<Modal ref="modalRef" title="Contribute Assets">
<Stack>
<ul
<div
v-if="enableContributeButton"
class="flex flex-col gap-2xs max-h-72 overflow-auto"
>
<li
v-for="module in props.contributeRequest.modules"
:key="module.schemaId"
class="flex px-1 items-center"
>
<span class="pr-2 select-none">•</span>
{{ module.name }}
</li>
</ul>
{{ props.contributeRequest.name }}
</div>
<ErrorMessage
v-if="contributeModuleReqStatus.isError"
:requestStatus="contributeModuleReqStatus"
Expand Down Expand Up @@ -51,12 +44,10 @@ import { ref, computed } from "vue";
import {
Modal,
VButton,
VormInput,
useModal,
Stack,
ErrorMessage,
} from "@si/vue-lib/design-system";
import { format as dateFormat, parseISO } from "date-fns";
import * as _ from "lodash-es";
import { useModuleStore } from "@/store/module.store";
import { ModuleContributeRequest } from "@/api/sdf/dal/module";
Expand Down Expand Up @@ -100,7 +91,7 @@ const isOpen = computed(() => modalRef.value?.isOpen);
defineExpose({ open, close, isOpen });
const enableContributeButton = computed(() => {
return props.contributeRequest.modules.length > 0;
return props.contributeRequest;
});
const contributeAssets = async () => {
Expand Down
45 changes: 0 additions & 45 deletions app/web/src/components/AssetListPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@
variant="simple"
@click="() => newAssetModalRef?.modal?.open()"
/>
<IconButton
v-if="canContribute || true"
:selected="contributeAssetModalRef?.isOpen || false"
class="hover:scale-125"
icon="cloud-upload"
size="sm"
tooltip="Contribute All"
tooltipPlacement="top"
variant="simple"
@click="contributeAsset"
/>
<IconButton
v-if="canUpdate"
class="hover:scale-125"
Expand Down Expand Up @@ -113,11 +102,6 @@
/>
</TreeNode>
</template>
<AssetContributeModal
ref="contributeAssetModalRef"
:contributeRequest="contributeRequest"
@contribute-success="onContributeAsset"
/>
<Modal
ref="contributeAssetSuccessModalRef"
size="sm"
Expand Down Expand Up @@ -147,19 +131,13 @@ import {
PillCounter,
} from "@si/vue-lib/design-system";
import { useRouter } from "vue-router";
import { format as dateFormat, parseISO } from "date-fns";
import SiSearch, { Filter } from "@/components/SiSearch.vue";
import {
ModuleContributeRequest,
ModuleContributeRequestItem,
} from "@/api/sdf/dal/module";
import { useAssetStore } from "@/store/asset.store";
import { SchemaVariant } from "@/api/sdf/dal/schema";
import { getAssetIcon } from "@/store/components.store";
import { useModuleStore } from "@/store/module.store";
import AssetNameModal from "./AssetNameModal.vue";
import AssetListItem from "./AssetListItem.vue";
import AssetContributeModal from "./AssetContributeModal.vue";
import SidebarSubpanelTitle from "./SidebarSubpanelTitle.vue";
import IconButton from "./IconButton.vue";
Expand All @@ -175,8 +153,6 @@ const loadAssetsReqStatus = assetStore.getRequestStatus(
);
const syncModulesReqStatus = moduleStore.getRequestStatus("SYNC");
const contributeAssetModalRef =
ref<InstanceType<typeof AssetContributeModal>>();
const contributeAssetSuccessModalRef = ref<InstanceType<typeof Modal>>();
const newAssetModalRef = ref<InstanceType<typeof AssetNameModal>>();
Expand All @@ -187,24 +163,6 @@ const onSearch = (search: string) => {
searchString.value = search.trim().toLocaleLowerCase();
};
const contributeRequest = computed((): ModuleContributeRequest => {
let modules = [] as ModuleContributeRequestItem[];
const filteredSchemaVariants = _.filter(
assetStore.schemaVariants,
(schemaVariant) => schemaVariant.canContribute,
);
const version = dateFormat(Date.now(), "yyyyMMddkkmmss");
modules = _.map(filteredSchemaVariants, (filteredSchemaVariant) => ({
name: `${filteredSchemaVariant.schemaName} ${version}`,
version,
schemaId: filteredSchemaVariant.schemaId,
}));
return { modules };
});
const canContribute = computed(() =>
assetList.value.some((a) => a.canContribute),
);
const canUpdate = computed(
() => Object.keys(moduleStore.upgradeableModules).length !== 0,
);
Expand Down Expand Up @@ -282,9 +240,6 @@ const updateAllAssets = () => {
});
};
const contributeAsset = () => contributeAssetModalRef.value?.open();
const onContributeAsset = () => contributeAssetSuccessModalRef.value?.open();
const filters = computed(() => [
assetList.value.filter((a) => a.canContribute),
assetList.value.filter(
Expand Down
6 changes: 5 additions & 1 deletion app/web/src/store/module.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ export const useModuleStore = () => {
return new ApiRequest({
method: "post",
url: `v2/workspaces/${workspaceId}/change-sets/${changeSetId}/modules/contribute`,
params: { ...visibility, ...request },
params: {
name: request.name,
version: request.version,
schemaVariantId: request.schemaVariantId,
},
});
},

Expand Down
20 changes: 15 additions & 5 deletions lib/dal/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,14 @@ impl Module {
fields(
name = name.as_ref(),
version = version.as_ref(),
%schema_id
%schema_variant_id
)
)]
pub async fn prepare_contribution(
ctx: &DalContext,
name: impl AsRef<str>,
version: impl AsRef<str>,
schema_id: SchemaId,
schema_variant_id: SchemaVariantId,
) -> ModuleResult<(String, String, Option<String>, Option<SchemaId>, Vec<u8>)> {
let user = match ctx.history_actor() {
HistoryActor::User(user_pk) => User::get_by_pk(ctx, *user_pk).await?,
Expand All @@ -561,14 +561,24 @@ impl Module {
));
}

// The frontend will send us the schema variant as this is what we care about from
// there. We can then use that schema variant to be able to understand the associated
// schema for it.
let variant = SchemaVariant::get_by_id_or_error(ctx, schema_variant_id).await?;
let associated_schema = variant.schema(ctx).await?;

// Create module payload.
let mut exporter =
PkgExporter::new_for_module_contribution(name, version, &created_by_email, schema_id);
let mut exporter = PkgExporter::new_for_module_contribution(
name,
version,
&created_by_email,
associated_schema.id(),
);
let module_payload = exporter.export_as_bytes(ctx).await.map_err(Box::new)?;

// Check if local information exists for contribution metadata.
let (local_module_based_on_hash, local_module_schema_id) =
match Module::find_for_member_id(ctx, schema_id).await? {
match Module::find_for_member_id(ctx, associated_schema.id()).await? {
Some(module) => (
Some(module.root_hash().to_string()),
module.schema_id().map(|id| id.into()),
Expand Down
13 changes: 9 additions & 4 deletions lib/dal/src/schema/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::layer_db_types::{
ContentTypeError, InputSocketContent, OutputSocketContent, SchemaVariantContent,
SchemaVariantContentV2,
};
use crate::module::Module;
use crate::prop::{PropError, PropPath};
use crate::schema::variant::root_prop::RootProp;
use crate::socket::input::InputSocketError;
Expand Down Expand Up @@ -115,6 +116,8 @@ pub enum SchemaVariantError {
LeafMapPropNotFound(PropId),
#[error("schema variant missing asset func id; schema_variant_id={0}")]
MissingAssetFuncId(SchemaVariantId),
#[error("module error: {0}")]
Module(String),
#[error("more than one schema found for schema variant: {0}")]
MoreThanOneSchemaFound(SchemaVariantId),
#[error("node weight error: {0}")]
Expand Down Expand Up @@ -777,14 +780,16 @@ impl SchemaVariant {
}
}

// NOTE(nick): we've determined that a module can be contributed if it is the default. For now, we are allowing
// unlocked variants to be contributed. Ask Paul for more information. I hate leaving Linear issues here since
// the community cannot see them, but ENG-2607 for context.
pub async fn can_be_contributed_by_id(
ctx: &DalContext,
id: SchemaVariantId,
) -> SchemaVariantResult<bool> {
Ok(Self::is_default_by_id(ctx, id).await?)
Ok(Self::is_default_by_id(ctx, id).await?
&& Self::is_locked_by_id(ctx, id).await?
&& Module::find_for_member_id(ctx, id)
.await
.map_err(|e| SchemaVariantError::Module(e.to_string()))?
.is_none())
}

pub async fn get_latest_for_schema(
Expand Down
8 changes: 7 additions & 1 deletion lib/dal/tests/integration_test/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,14 @@ async fn prepare_contribution_works(ctx: &DalContext) {
let name = "Paul's Test Pkg With Extra Spaces At The End ";
let version = " Version With Spaces At The Beginning 2019-06-03";

let default_variant_id = schema
.get_default_schema_variant_id(ctx)
.await
.expect("unable to get a default variant")
.expect("error getting the default variant id");

let (actual_name, actual_version, _, _, _) =
Module::prepare_contribution(ctx, name, version, schema.id())
Module::prepare_contribution(ctx, name, version, default_variant_id)
.await
.expect("could not prepare contribution");

Expand Down
19 changes: 3 additions & 16 deletions lib/sdf-server/src/server/service/v2/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,8 @@ mod sync;
pub enum ModulesAPIError {
#[error("axum http error: {0}")]
AxumHttp(#[from] axum::http::Error),
#[error("one or many module(s) were not contributed: {0:?}")]
ContributionPartialFailure(
Vec<(
frontend_types::ModuleContributeRequestItem,
module_index_client::ModuleIndexClientError,
)>,
),
#[error("all module(s) were not contributed: {0:?}")]
ContributionTotalFailure(
Vec<(
frontend_types::ModuleContributeRequestItem,
module_index_client::ModuleIndexClientError,
)>,
),
#[error("module not contributed: {0:?}")]
ContributionFailure(frontend_types::ModuleContributeRequest),
#[error("module error: {0}")]
Module(#[from] dal::module::ModuleError),
#[error("module index client error: {0}")]
Expand Down Expand Up @@ -60,8 +48,7 @@ impl IntoResponse for ModulesAPIError {
StatusCode::NOT_FOUND
}
Self::Module(dal::module::ModuleError::EmptyMetadata(_, _)) => StatusCode::BAD_REQUEST,
Self::ContributionPartialFailure(_) => StatusCode::MULTI_STATUS,
Self::ContributionTotalFailure(_) => StatusCode::BAD_GATEWAY,
Self::ContributionFailure(_) => StatusCode::BAD_REQUEST,
_ => ApiError::DEFAULT_ERROR_STATUS_CODE,
};

Expand Down
Loading

0 comments on commit 40e68e5

Please sign in to comment.