Skip to content

Annotate images based on name #868

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

Merged
merged 8 commits into from
Nov 22, 2024
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
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