Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor, move auto-tag from legacy to a new component teambit.workspace/modules/auto-tag #8463

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .bitmap
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,14 @@
"mainFile": "index.ts",
"rootDir": "scopes/scope/models/scope-model"
},
"modules/auto-tag": {
"name": "modules/auto-tag",
"scope": "",
"version": "",
"defaultScope": "teambit.workspace",
"mainFile": "index.ts",
"rootDir": "scopes/workspace/modules/auto-tag"
},
"modules/babel-compiler": {
"name": "modules/babel-compiler",
"scope": "teambit.compilation",
Expand Down
2 changes: 1 addition & 1 deletion scopes/component/merging/merging.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ComponentID, ComponentIdList } from '@teambit/component-id';
import { BitError } from '@teambit/bit-error';
import GeneralError from '@teambit/legacy/dist/error/general-error';
import { LaneId } from '@teambit/lane-id';
import { AutoTagResult } from '@teambit/legacy/dist/scope/component-ops/auto-tag';
import { AutoTagResult } from '@teambit/workspace.modules.auto-tag';
import { UnmergedComponent } from '@teambit/legacy/dist/scope/lanes/unmerged-components';
import { Lane, ModelComponent } from '@teambit/legacy/dist/scope/models';
import { Ref } from '@teambit/legacy/dist/scope/objects';
Expand Down
2 changes: 1 addition & 1 deletion scopes/component/snapping/snapping.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
getArtifactsFiles,
} from '@teambit/legacy/dist/consumer/component/sources/artifact-files';
import { VersionNotFound, ComponentNotFound } from '@teambit/legacy/dist/scope/exceptions';
import { AutoTagResult } from '@teambit/legacy/dist/scope/component-ops/auto-tag';
import { AutoTagResult } from '@teambit/workspace.modules.auto-tag';
import DependenciesAspect, { DependenciesMain } from '@teambit/dependencies';
import { SourceFile } from '@teambit/legacy/dist/consumer/component/sources';
import Version, { DepEdge, DepEdgeType, Log } from '@teambit/legacy/dist/scope/models/version';
Expand Down
17 changes: 13 additions & 4 deletions scopes/component/snapping/tag-model-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Component } from '@teambit/component';
import deleteComponentsFiles from '@teambit/legacy/dist/consumer/component-ops/delete-component-files';
import logger from '@teambit/legacy/dist/logger/logger';
import { sha1 } from '@teambit/legacy/dist/utils';
import { AutoTagResult, getAutoTagInfo } from '@teambit/legacy/dist/scope/component-ops/auto-tag';
import { AutoTagResult, getAutoTagInfo } from '@teambit/workspace.modules.auto-tag';
import { getValidVersionOrReleaseType } from '@teambit/legacy/dist/utils/semver-helper';
import { BuilderMain, OnTagOpts } from '@teambit/builder';
import { Log } from '@teambit/legacy/dist/scope/models/version';
Expand Down Expand Up @@ -234,9 +234,11 @@ export async function tagModelComponent({
// them as dependencies.
const idsToTriggerAutoTag = idsToTag.filter((id) => id.hasVersion());
const autoTagData =
skipAutoTag || !consumer ? [] : await getAutoTagInfo(consumer, ComponentIdList.fromArray(idsToTriggerAutoTag));
skipAutoTag || !workspace ? [] : await getAutoTagInfo(workspace, ComponentIdList.fromArray(idsToTriggerAutoTag));
const autoTagComponents = autoTagData.map((autoTagItem) => autoTagItem.component);
const autoTagComponentsFiltered = autoTagComponents.filter((c) => !idsToTag.has(c.id));
const autoTagConsumerComponents = autoTagComponents.map((c) => c.state._consumer) as ConsumerComponent[];
const autoTagComponentsFiltered = autoTagConsumerComponents.filter((c) => !idsToTag.has(c.id));

const autoTagIds = ComponentIdList.fromArray(autoTagComponentsFiltered.map((autoTag) => autoTag.id));
const allComponentsToTag = [...componentsToTag, ...autoTagComponentsFiltered];

Expand Down Expand Up @@ -296,7 +298,14 @@ export async function tagModelComponent({
// go through all dependencies and update their versions
updateDependenciesVersions(allComponentsToTag, dependencyResolver);

await addLogToComponents(componentsToTag, autoTagComponents, persist, message, messagePerId, copyLogFromPreviousSnap);
await addLogToComponents(
componentsToTag,
autoTagConsumerComponents,
persist,
message,
messagePerId,
copyLogFromPreviousSnap
);
// don't move it down. otherwise, it'll be empty and we don't know which components were during merge.
// (it's being deleted in snapping.main.runtime - `_addCompToObjects` method)
const unmergedComps = workspace ? await workspace.listComponentsDuringMerge() : [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@ import graphlib, { Graph } from 'graphlib';
import semver from 'semver';
import { ComponentIdList } from '@teambit/component-id';
import { isTag } from '@teambit/component-version';
import { Consumer } from '../../consumer';
import Component from '../../consumer/component/consumer-component';
import { Dependency } from '../../consumer/component/dependencies';
// import Component from '@teambit/legacy/dist/consumer/component/consumer-component';
import { Dependency } from '@teambit/legacy/dist/consumer/component/dependencies';
import { Workspace } from '@teambit/workspace';
import { Component } from '@teambit/component';

export async function getAutoTagPending(consumer: Consumer, changedComponents: ComponentIdList): Promise<Component[]> {
const autoTagInfo = await getAutoTagInfo(consumer, changedComponents);
export async function getAutoTagPending(
workspace: Workspace,
changedComponents: ComponentIdList
): Promise<Component[]> {
const autoTagInfo = await getAutoTagInfo(workspace, changedComponents);
return autoTagInfo.map((a) => a.component);
}

export type AutoTagResult = { component: Component; triggeredBy: ComponentIdList };

export async function getAutoTagInfo(consumer: Consumer, changedComponents: ComponentIdList): Promise<AutoTagResult[]> {
export async function getAutoTagInfo(
workspace: Workspace,
changedComponents: ComponentIdList
): Promise<AutoTagResult[]> {
if (!changedComponents.length) return [];
const potentialComponents = potentialComponentsForAutoTagging(consumer, changedComponents);
const potentialComponents = potentialComponentsForAutoTagging(workspace, changedComponents);
const idsToLoad = new ComponentIdList(...potentialComponents, ...changedComponents);
const { components } = await consumer.loadComponents(idsToLoad, false);
const { components } = await workspace.componentLoader.getMany(idsToLoad, undefined, false);
const graph = buildGraph(components);

const autoTagResults: AutoTagResult[] = [];
components.forEach((component) => {
const bitId = component.componentId;
const bitId = component.id;
const idStr = bitId.toStringWithoutVersion();
if (!graph.hasNode(idStr)) return;
// preorder gets all dependencies and dependencies of dependencies and so on.
Expand Down Expand Up @@ -70,7 +77,7 @@ function buildGraph(components: Component[]): Graph {

components.forEach((component) => {
const idStr = component.id.toStringWithoutVersion();
component.getAllDependencies().forEach((dependency: Dependency) => {
component.state._consumer.getAllDependencies().forEach((dependency: Dependency) => {
if (componentsIds.searchWithoutVersion(dependency.id)) {
const depId = dependency.id.toStringWithoutVersion();
// save the full ComponentID of a string id to be able to retrieve it later with no confusion
Expand All @@ -83,8 +90,8 @@ function buildGraph(components: Component[]): Graph {
return graph;
}

function potentialComponentsForAutoTagging(consumer: Consumer, modifiedComponents: ComponentIdList): ComponentIdList {
const candidateComponentsIds = consumer.bitMap.getAllBitIds();
function potentialComponentsForAutoTagging(workspace: Workspace, modifiedComponents: ComponentIdList): ComponentIdList {
const candidateComponentsIds = workspace.consumer.bitMap.getAllBitIds();
// if a modified component is in candidates array, remove it from the array as it will be already
// tagged with the correct version
const idsWithoutModified = candidateComponentsIds.filter(
Expand Down
1 change: 1 addition & 0 deletions scopes/workspace/modules/auto-tag/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getAutoTagInfo, getAutoTagPending, AutoTagResult } from './auto-tag';
7 changes: 3 additions & 4 deletions scopes/workspace/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { Lane, Version } from '@teambit/legacy/dist/scope/models';
import { LaneNotFound } from '@teambit/legacy/dist/api/scope/lib/exceptions/lane-not-found';
import { ScopeNotFoundOrDenied } from '@teambit/legacy/dist/remotes/exceptions/scope-not-found-or-denied';
import { isHash } from '@teambit/component-version';
import { getAutoTagPending } from '@teambit/workspace.modules.auto-tag';
import { GlobalConfigMain } from '@teambit/global-config';
import { ComponentConfigFile } from './component-config-file';
import {
Expand Down Expand Up @@ -340,10 +341,8 @@ export class Workspace implements ComponentFactory {
const modifiedComponents = (await this.modified()).map((c) => c.id);
const newComponents = (await componentsList.listNewComponents()) as ComponentIdList;
if (!modifiedComponents || !modifiedComponents.length) return [];
const autoTagPending = await this.consumer.listComponentsForAutoTagging(
ComponentIdList.fromArray(modifiedComponents)
);
const comps = autoTagPending.filter((autoTagComp) => !newComponents.has(autoTagComp.componentId));
const autoTagPending = await getAutoTagPending(this, ComponentIdList.fromArray(modifiedComponents));
const comps = autoTagPending.filter((autoTagComp) => !newComponents.has(autoTagComp.id));
return comps.map((c) => c.id);
}

Expand Down
5 changes: 0 additions & 5 deletions src/consumer/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from '../constants';
import logger from '../logger/logger';
import { Scope } from '../scope';
import { getAutoTagPending } from '../scope/component-ops/auto-tag';
import { ComponentNotFound } from '../scope/exceptions';
import { Lane, ModelComponent, Version } from '../scope/models';
import { generateRandomStr, sortObject } from '../utils';
Expand Down Expand Up @@ -292,10 +291,6 @@ export default class Consumer {
return this.componentLoader.loadMany(ids, throwOnFailure, loadOpts);
}

async listComponentsForAutoTagging(modifiedComponents: ComponentIdList): Promise<Component[]> {
return getAutoTagPending(this, modifiedComponents);
}

/**
* Check whether a model representation and file-system representation of the same component is the same.
* The way how it is done is by converting the file-system representation of the component into
Expand Down
17 changes: 0 additions & 17 deletions src/scope/component-ops/auto-tag.spec.ts

This file was deleted.