Skip to content

Commit

Permalink
Annotate images based on name (#868)
Browse files Browse the repository at this point in the history
## What

## Why

## Notes
  • Loading branch information
vadim-kharin-codefresh authored Nov 22, 2024
1 parent 04b5e0a commit 7e49659
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 60 deletions.
30 changes: 3 additions & 27 deletions lib/interface/cli/commands/image/annotate.cmd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const Command = require('../../Command');
const { parseFamiliarName } = require('@codefresh-io/docker-reference');
const annotateRoot = require('../root/annotate.cmd');
const CFError = require('cf-errors');
const { sdk } = require('../../../../logic');
const annotationLogic = require('../annotation/annotation.logic');

const command = new Command({
Expand All @@ -27,32 +24,11 @@ const command = new Command({
array: true,
})
.example('codefresh annotate image 2dfacdaad466 -l coverage=75%', 'Annotate an image with a single label')
.example('codefresh annotate image 2dfacdaad466 -l coverage=75% -l tests_passed=true', 'Annotate an image with multiple labels');
.example('codefresh annotate image 2dfacdaad466 -l coverage=75% -l tests_passed=true', 'Annotate an image with multiple labels')
.example('codefresh annotate image codefresh/cli:latest -l coverage=75% -l tests_passed=true', 'Annotate an image by name with multiple labels');
},
handler: async (argv) => {
let dockerImageId = argv.id;
const useFullName = dockerImageId.includes(':');

if (useFullName) {
const { repository, tag } = parseFamiliarName(dockerImageId);
const results = await sdk.images.list({
imageDisplayNameRegex: repository,
tag,
select: 'internalImageId',
});

if (!results.length) {
throw new CFError('Image does not exist');
}

if (results.length > 1) {
throw new CFError(`Could not get image id. ${results.length} images found.`);
}

dockerImageId = results[0].internalImageId;
}

await annotationLogic.createAnnotations({ entityId: dockerImageId, entityType: 'image', labels: argv.label });
await annotationLogic.createAnnotations({ entityId: argv.id, entityType: 'image', labels: argv.label });
console.log('Annotations added successfully');
},
});
Expand Down
32 changes: 0 additions & 32 deletions lib/interface/cli/commands/image/image.sdk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,37 +98,5 @@ describe('image commands', () => {
await annotateCmd.handler(argv);
await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
});

it('should handle annotating given full image name', async () => {
const argv = { id: 'repo/name:tag', label: ['test=test'] };
const responses = [
{ statusCode: 200, body: [{ internalImageId: 'some id' }] },
DEFAULT_RESPONSE,
];
request.__queueResponses(responses);
await annotateCmd.handler(argv);
await verifyResponsesReturned(responses); // eslint-disable-line
});

it('should throw on no images found given image full name', async () => {
const argv = { id: 'repo/name:tag', label: ['test=test'] };
const response = { statusCode: 200, body: [] };
request.__setResponse(response);

await expect(annotateCmd.handler(argv)).rejects.toThrow();
await verifyResponsesReturned([response]); // eslint-disable-line
});

it('should throw on multiple images found given image full name', async () => {
const argv = { id: 'repo/name:tag', label: ['test=test'] };
const response = {
statusCode: 200,
body: [{ internalImageId: 'some id' }, { internalImageId: 'another id' }],
};
request.__setResponse(response);

await expect(annotateCmd.handler(argv)).rejects.toThrow();
await verifyResponsesReturned([response]); // eslint-disable-line
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codefresh",
"version": "0.88.1",
"version": "0.88.2",
"description": "Codefresh command line utility",
"main": "index.js",
"preferGlobal": true,
Expand Down

0 comments on commit 7e49659

Please sign in to comment.