Skip to content

Commit

Permalink
fix: package installation actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mbystedt committed Dec 16, 2024
1 parent 6ab1ae3 commit 0b7dd17
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 32 deletions.
5 changes: 3 additions & 2 deletions src/intention/action.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,9 @@ export class ActionService {
const parsedVersion = this.parseActionVersion(action);
if (!this.actionUtil.isStrictSemver(parsedVersion)) {
return {
message:
'Package actions must specify a valid semver version. See: https://semver.org',
message: action.package?.version
? 'Package actions must specify a valid semver version. See: https://semver.org'
: 'No package version set. If using source intention, check action.source values.',
data: {
action: action.action,
action_id: action.id,
Expand Down
5 changes: 5 additions & 0 deletions src/intention/entity/action-source.embeddable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { ObjectId } from 'mongodb';

@Embeddable()
export class ActionSourceEmbeddable {
constructor(action: string, intention: ObjectId) {
this.action = action;
this.intention = intention;
}

@Property({ nullable: true })
action?: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TransactionEmbeddable } from './transaction.embeddable';
import { UserEmbeddable } from './user.embeddable';
import { PackageEmbeddable } from './package.embeddable';
import { ActionDto } from '../dto/action.dto';
import { ActionSourceEmbeddable } from './action-source.embeddable';

@Embeddable({ discriminatorValue: ACTION_NAMES.PACKAGE_INSTALLATION })
export class PackageInstallationActionEmbeddable extends ActionEmbeddable {
Expand All @@ -19,10 +20,12 @@ export class PackageInstallationActionEmbeddable extends ActionEmbeddable {
vaultEnvironment: ENVIRONMENT_NAMES | undefined,
trace: TransactionEmbeddable,
packageEmbed: PackageEmbeddable,
source: ActionSourceEmbeddable,
) {
super(action, actionUser, service, vaultEnvironment, trace);
this.action = ACTION_NAMES.PACKAGE_INSTALLATION;
this.package = packageEmbed;
this.source = source;
}

@Embedded({ entity: () => PackageEmbeddable, object: true })
Expand Down
2 changes: 1 addition & 1 deletion src/intention/entity/package.embeddable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PackageDto } from '../dto/package.dto';
export class PackageEmbeddable {
static merge(...theArgs: Array<PackageDto | PackageEmbeddable>) {
const rval = new PackageEmbeddable();
for (const arg of theArgs) {
for (const arg of theArgs.filter((arg) => arg)) {
rval.architecture = arg.architecture ?? rval.architecture;
rval.buildGuid = arg.buildGuid ?? rval.buildGuid;
rval.buildNumber = arg.buildNumber ?? rval.buildNumber;
Expand Down
63 changes: 37 additions & 26 deletions src/intention/intention.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import { ServiceDto } from '../persistence/dto/service.dto';
import { CloudObjectEmbeddable } from './entity/cloud-object.embeddable';
import { CloudEmbeddable } from './entity/cloud.embeddable';
import { ValidatorUtil } from '../util/validator.util';
import { ActionSourceEmbeddable } from './entity/action-source.embeddable';

export interface IntentionOpenResponse {
actions: {
Expand Down Expand Up @@ -229,15 +230,21 @@ export class IntentionService {
vaultEnvironment,
trace,
);
case 'package-installation':
case 'package-installation': {
const packageSource = await this.sourcePackageFromBuild(
action,
serviceEmbed,
);
return new PackageInstallationActionEmbeddable(
action,
actionUser,
serviceEmbed,
vaultEnvironment,
trace,
PackageEmbeddable.fromDto(action.package),
packageSource ? packageSource.package : undefined,
packageSource ? packageSource.source : undefined,
);
}
case 'package-provision':
return new PackageProvisionActionEmbeddable(
action,
Expand Down Expand Up @@ -477,6 +484,7 @@ export class IntentionService {
.reduce((pv, cv) => pv.concat(cv), []),
};
} catch (e) {
console.log(e);
throw new BadRequestException({
statusCode: 400,
message: 'Illegal search arguement',
Expand Down Expand Up @@ -512,13 +520,20 @@ export class IntentionService {
return artifactCombos;
}

private async annotateActionPackageFromExistingArtifact(
action: ActionEmbeddable,
) {
if (action.action !== 'package-installation' || !action.service.id) {
// Only annotates package installations
private async sourcePackageFromBuild(
action: ActionDto,
serviceEmbed: IntentionServiceEmbeddable,
): Promise<
{ source?: ActionSourceEmbeddable; package: PackageEmbeddable } | undefined
> {
const defaultVal = action.package
? {
package: PackageEmbeddable.fromDto(action.package),
}
: undefined;
if (!serviceEmbed.id) {
// Only existing services (with service.id set) will have artifacts
return;
return defaultVal;
}
let artifactSearchResult: ArtifactSearchResult;

Expand All @@ -542,7 +557,7 @@ export class IntentionService {
artifactSearchResult.data.length !== 1
) {
// Skip: Could not uniquely identify artifact based on source
return;
return defaultVal;
}
} else if (action.package?.name && action.package?.version) {
// Find latest artifact for this service with same package name and version
Expand All @@ -558,29 +573,25 @@ export class IntentionService {
limit: 1,
});
} else {
return;
return defaultVal;
}

if (artifactSearchResult.meta.total === 0) {
// Could not identify artifact
return;
return defaultVal;
}

action.package = PackageEmbeddable.merge(
artifactSearchResult.data[0].action.package ?? {},
artifactSearchResult.data[0].artifact,
action.package,
);
if (!action.source) {
action.source = {
intention: new ObjectId(
artifactSearchResult.data[0].intention.id.toString(),
),
};
}
// action.source.action = this.actionUtil.actionToIdString(
// artifactSearchResult.data[0].action,
// );
return {
source: new ActionSourceEmbeddable(
this.actionUtil.actionToIdString(artifactSearchResult.data[0].action),
new ObjectId(artifactSearchResult.data[0].intention.id),
),
package: PackageEmbeddable.merge(
artifactSearchResult.data[0].action.package ?? {},
artifactSearchResult.data[0].artifact,
action.package,
),
};
}

private async finalizeIntention(
Expand Down
9 changes: 6 additions & 3 deletions src/util/action.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ActionUtil {
const actionArr = action.split('#');
if (actionArr.length === 2) {
if (actionArr[0] !== '') {
if (actionArr[0] in ACTION_NAMES) {
if (ACTION_NAMES.includes(actionArr[0] as any)) {
actionOptions.action = actionArr[0] as ActionName;
} else {
throw new BadRequestException({
Expand All @@ -72,7 +72,10 @@ export class ActionUtil {
}
}
actionOptions.id = actionArr[1];
} else if (actionArr.length === 1 && actionArr[0] in ACTION_NAMES) {
} else if (
actionArr.length === 1 &&
ACTION_NAMES.includes(actionArr[0] as any)
) {
actionOptions.action = actionArr[0] as ActionName;
} else {
throw new BadRequestException({
Expand Down Expand Up @@ -102,7 +105,7 @@ export class ActionUtil {
});
}

public actionToIdString(action: ActionEmbeddable) {
public actionToIdString(action: ActionEmbeddable | ActionDto) {
return `${action.action}#${action.id}`;
}

Expand Down

0 comments on commit 0b7dd17

Please sign in to comment.