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

fix(merge-from-scope), push artifacts to original scopes #7119

Merged
merged 3 commits into from
Mar 4, 2023
Merged
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
17 changes: 17 additions & 0 deletions e2e/harmony/lanes/lanes.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,23 @@ describe('bit lane command', function () {
helper.command.expectStatusToBeClean();
});
});
describe('merging from scope', () => {
before(() => {
helper.scopeHelper.getClonedLocalScope(localScope);
helper.scopeHelper.getClonedRemoteScope(remoteScope);
helper.command.export();
const bareMerge = helper.scopeHelper.getNewBareScope('-bare-merge');
helper.scopeHelper.addRemoteScope(helper.scopes.remotePath, bareMerge.scopePath);
helper.scopeHelper.addRemoteScope(anotherRemotePath, bareMerge.scopePath);
helper.command.mergeLaneFromScope(bareMerge.scopePath, `${helper.scopes.remote}/dev`, '--push');
});
it('should push the artifacts to the original-scope', () => {
const artifacts = helper.command.getArtifacts(`${anotherRemote}/bar2@latest`, anotherRemotePath);
const pkgArtifacts = artifacts.find((a) => a.generatedBy === 'teambit.pkg/pkg');
const hash = pkgArtifacts.files[0].file;
expect(() => helper.command.catObject(hash, false, anotherRemotePath)).to.not.throw();
});
});
});
describe('multiple scopes when the components are new', () => {
let anotherRemote: string;
Expand Down
32 changes: 15 additions & 17 deletions scopes/lanes/merge-lanes/merge-lanes.main.runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BitError } from '@teambit/bit-error';
import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
import ImporterAspect, { ImporterMain } from '@teambit/importer';
import { LanesAspect, LanesMain } from '@teambit/lanes';
import MergingAspect, {
MergingMain,
Expand All @@ -19,11 +20,10 @@ import ScopeComponentsImporter from '@teambit/legacy/dist/scope/component-ops/sc
import { ComponentID } from '@teambit/component-id';
import { DEFAULT_LANE, LaneId } from '@teambit/lane-id';
import { Lane, Version } from '@teambit/legacy/dist/scope/models';
import { getRefsFromExtensions } from '@teambit/legacy/dist/consumer/component/sources/artifact-files';
import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
import { SnapsDistance } from '@teambit/legacy/dist/scope/component-ops/snaps-distance';
import { RemoveAspect, RemoveMain } from '@teambit/remove';
import { compact, uniq } from 'lodash';
import { compact } from 'lodash';
import { ExportAspect, ExportMain } from '@teambit/export';
import { BitObject } from '@teambit/legacy/dist/scope/objects';
import { getDivergeData } from '@teambit/legacy/dist/scope/component-ops/get-diverge-data';
Expand Down Expand Up @@ -56,7 +56,8 @@ export class MergeLanesMain {
private logger: Logger,
private remove: RemoveMain,
private scope: ScopeMain,
private exporter: ExportMain
private exporter: ExportMain,
private importer: ImporterMain
) {}

async mergeLane(
Expand Down Expand Up @@ -100,7 +101,7 @@ export class MergeLanesMain {
const getOtherLane = async () => {
if (isDefaultLane) {
if (!skipFetch) {
await this.lanes.importer.importObjectsFromMainIfExist(currentLane?.toBitIds().toVersionLatest() || []);
await this.importer.importObjectsFromMainIfExist(currentLane?.toBitIds().toVersionLatest() || []);
}
return undefined;
}
Expand All @@ -110,16 +111,7 @@ export class MergeLanesMain {
// don't assign `lane` to the result of this command. otherwise, if you have local snaps, it'll ignore them and use the remote-lane.
const otherLane = await this.lanes.fetchLaneWithItsComponents(otherLaneId);

// get all artifacts
const allIds = otherLane.toBitIds();
const laneComps = await consumer.scope.getManyConsumerComponents(allIds);
const allRefs = laneComps.map((comp) => getRefsFromExtensions(comp.extensions)).flat();
const allRefsUniq = uniq(allRefs.map((r) => r.toString()));
try {
await consumer.scope.scopeImporter.importManyObjects({ [otherLaneId.scope]: allRefsUniq });
} catch (err) {
this.logger.error(`failed fetching artifacts for lane ${otherLaneId.toString()}`, err);
}
await this.importer.importHeadArtifactsFromLane(otherLane, true);

lane = await consumer.scope.loadLane(otherLaneId);
}
Expand Down Expand Up @@ -300,6 +292,7 @@ export class MergeLanesMain {
ignoreMissingHead: true,
lane: toLaneObj,
});
await this.importer.importHeadArtifactsFromLane(fromLaneObj, true);
await this.throwIfNotUpToDate(fromLaneId, toLaneId);
const repo = this.scope.legacyScope.objects;
// loop through all components, make sure they're all ahead of main (it might not be on main yet).
Expand Down Expand Up @@ -359,6 +352,9 @@ export class MergeLanesMain {
// no need to export anything else other than the head. the normal calculation of what to export won't apply here
// as it is done from the scope.
exportHeadsOnly: shouldSquash,
// all artifacts must be pushed. they're all considered "external" in this case, because it's running from a
// bare-scope, but we don't want to ignore them, otherwise, they'll be missing from the component-scopes.
ignoreMissingExternalArtifacts: false,
});
exportedIds = exported.map((id) => id.toString());
}
Expand Down Expand Up @@ -388,22 +384,24 @@ ${compsNotUpToDate.map((s) => s.componentId.toString()).join('\n')}`);
RemoveAspect,
ScopeAspect,
ExportAspect,
ImporterAspect,
];
static runtime = MainRuntime;

static async provider([lanes, cli, workspace, merging, loggerMain, remove, scope, exporter]: [
static async provider([lanes, cli, workspace, merging, loggerMain, remove, scope, exporter, importer]: [
LanesMain,
CLIMain,
Workspace,
MergingMain,
LoggerMain,
RemoveMain,
ScopeMain,
ExportMain
ExportMain,
ImporterMain
]) {
const logger = loggerMain.createLogger(MergeLanesAspect.id);
const lanesCommand = cli.getCommand('lane');
const mergeLanesMain = new MergeLanesMain(workspace, merging, lanes, logger, remove, scope, exporter);
const mergeLanesMain = new MergeLanesMain(workspace, merging, lanes, logger, remove, scope, exporter, importer);
lanesCommand?.commands?.push(new MergeLaneCmd(mergeLanesMain));
cli.register(new MergeLaneFromScopeCmd(mergeLanesMain));
return mergeLanesMain;
Expand Down
5 changes: 4 additions & 1 deletion scopes/scope/export/export.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export class ExportMain {
idsWithFutureScope,
resumeExportId,
ignoreMissingArtifacts,
ignoreMissingExternalArtifacts = true,
isOnMain = true,
exportHeadsOnly, // relevant when exporting from bare-scope, especially when re-exporting existing versions, the normal calculation based on getDivergeData won't work
filterOutExistingVersions, // go to the remote and check whether the version exists there. if so, don't export it
Expand All @@ -215,6 +216,7 @@ export class ExportMain {
idsWithFutureScope: BitIds;
resumeExportId?: string | undefined;
ignoreMissingArtifacts?: boolean;
ignoreMissingExternalArtifacts?: boolean;
isOnMain?: boolean;
exportHeadsOnly?: boolean;
filterOutExistingVersions?: boolean;
Expand Down Expand Up @@ -351,7 +353,8 @@ export class ExportMain {
const objectItems = await modelComponent.collectVersionsObjects(
scope.objects,
refs.map((ref) => ref.toString()),
ignoreMissingArtifacts
ignoreMissingArtifacts,
ignoreMissingExternalArtifacts
);
const objectsList = await new ObjectList(objectItems).toBitObjects();
const componentAndObject = { component: modelComponent, objects: objectsList.getAll() };
Expand Down
19 changes: 19 additions & 0 deletions scopes/scope/importer/importer.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
import { DependencyResolverAspect, DependencyResolverMain } from '@teambit/dependency-resolver';
import WorkspaceAspect, { OutsideWorkspaceError, Workspace } from '@teambit/workspace';
import { CommunityAspect } from '@teambit/community';
import { uniq } from 'lodash';
import type { CommunityMain } from '@teambit/community';
import { Analytics } from '@teambit/legacy/dist/analytics/analytics';
import ConsumerComponent from '@teambit/legacy/dist/consumer/component';
Expand All @@ -13,6 +14,7 @@ import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
import ScopeAspect, { ScopeMain } from '@teambit/scope';
import { DEFAULT_LANE, LaneId } from '@teambit/lane-id';
import ScopeComponentsImporter from '@teambit/legacy/dist/scope/component-ops/scope-components-importer';
import { getRefsFromExtensions } from '@teambit/legacy/dist/consumer/component/sources/artifact-files';
import InstallAspect, { InstallMain } from '@teambit/install';
import loader from '@teambit/legacy/dist/cli/loader';
import { BitIds } from '@teambit/legacy/dist/bit-id';
Expand Down Expand Up @@ -80,6 +82,23 @@ export class ImporterMain {
return importComponents.importComponents();
}

/**
* given a lane object, load all components by their head on the lane, find the artifacts refs and import them from
* the lane scope
*/
async importHeadArtifactsFromLane(lane: Lane, throwIfMissing = false) {
const ids = lane.toBitIds();
const laneComps = await this.scope.legacyScope.getManyConsumerComponents(ids);
const allRefs = laneComps.map((comp) => getRefsFromExtensions(comp.extensions)).flat();
const allRefsUniq = uniq(allRefs.map((r) => r.toString()));
try {
await this.scope.legacyScope.scopeImporter.importManyObjects({ [lane.scope]: allRefsUniq });
} catch (err) {
this.logger.error(`failed fetching artifacts for lane ${lane.id.toString()}`, err);
if (throwIfMissing) throw err;
}
}

/**
* if on main, fetch main objects, if on lane, fetch lane objects.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/e2e-helper/e2e-command-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ export default class CommandHelper {
const component = lane.components.find((c) => c.id.name === componentName);
return component.head;
}
getArtifacts(id: string) {
const comp = this.catComponent(`${id}@latest`);
getArtifacts(id: string, cwd?: string) {
const comp = this.catComponent(`${id}@latest`, cwd);
const builderExt = comp.extensions.find((ext) => ext.name === 'teambit.pipelines/builder');
if (!builderExt) throw new Error(`unable to find builder data for ${id}`);
const artifacts = builderExt.data.artifacts;
Expand Down
7 changes: 4 additions & 3 deletions src/scope/models/model-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,8 @@ export default class Component extends BitObject {
async collectVersionsObjects(
repo: Repository,
versions: string[],
ignoreMissingArtifacts?: boolean
ignoreMissingLocalArtifacts = false,
ignoreMissingExternalArtifacts = true
): Promise<ObjectItem[]> {
const refsWithoutArtifacts: Ref[] = [];
const artifactsRefs: Ref[] = [];
Expand All @@ -739,7 +740,7 @@ export default class Component extends BitObject {
const refs = versionObject.refsWithOptions(false, false);
refsWithoutArtifacts.push(...refs);
const refsFromExtensions = getRefsFromExtensions(versionObject.extensions);
locallyChangedHashes.includes(versionObject.hash.toString())
locallyChangedHashes.includes(versionObject.hash.toString()) || !ignoreMissingExternalArtifacts
? artifactsRefs.push(...refsFromExtensions)
: artifactsRefsFromExportedVersions.push(...refsFromExtensions);
});
Expand All @@ -755,7 +756,7 @@ for a component "${this.id()}", versions: ${versions.join(', ')}`);
throw err;
}
try {
const loaded = ignoreMissingArtifacts
const loaded = ignoreMissingLocalArtifacts
? await repo.loadManyRawIgnoreMissing(artifactsRefs)
: await repo.loadManyRaw(artifactsRefs);
loadedRefs.push(...loaded);
Expand Down