Skip to content

Commit

Permalink
fix(docker): Fix handling Docker Image name with digest (#7896) (#7918)
Browse files Browse the repository at this point in the history
* fix(docker): Fix handling Docker Image name with digest

* feat(docker): Fix digest support in manual docker trigger

* Fix feedback for DockerImageUtils

Co-Authored-By: Maggie Neterval <mneterval@google.com>

* fix(docker): Call "this.updateArtifact" in "lookupTypeChanged" instead of "this.props.updateCommand" in DockerTriggerTemplate

Co-authored-by: Maggie Neterval <mneterval@google.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

Co-authored-by: Ryuzo Yamamoto <ryuzo.yamamoto@gmail.com>
Co-authored-by: Maggie Neterval <mneterval@google.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
4 people committed Feb 19, 2020
1 parent b64c7bb commit 0e9ec86
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
50 changes: 48 additions & 2 deletions app/scripts/modules/docker/src/image/DockerImageUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('imageId parsing', () => {
});

it('parses image with organization and correctly distinguishes digest from tag', () => {
const imageId = 'organization/image:sha256:abc123';
const imageId = 'organization/image@sha256:abc123';
expect(DockerImageUtils.splitImageId(imageId)).toEqual({
organization: 'organization',
repository: 'organization/image',
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('imageId parsing', () => {
});

it('parses image with nested organization and correctly distinguishes digest from tag', () => {
const imageId = 'nested/organization/image:sha256:abc123';
const imageId = 'nested/organization/image@sha256:abc123';
expect(DockerImageUtils.splitImageId(imageId)).toEqual({
organization: 'nested/organization',
repository: 'nested/organization/image',
Expand All @@ -100,3 +100,49 @@ describe('imageId parsing', () => {
});
});
});

describe('imageId generating', () => {
it('generate imageId without repository', () => {
expect(
DockerImageUtils.generateImageId({
organization: 'gcr.io/project',
repository: '',
digest: undefined,
tag: undefined,
}),
).toEqual(undefined);
});

it('generate imageId with repository but no tag/digest', () => {
expect(
DockerImageUtils.generateImageId({
organization: 'gcr.io/project',
repository: 'gcr.io/project/my-image',
digest: undefined,
tag: undefined,
}),
).toEqual(undefined);
});

it('generate imageId with digest', () => {
expect(
DockerImageUtils.generateImageId({
organization: 'gcr.io/project',
repository: 'gcr.io/project/my-image',
digest: 'sha256:28f82eba',
tag: undefined,
}),
).toEqual('gcr.io/project/my-image@sha256:28f82eba');
});

it('generate imageId with tag', () => {
expect(
DockerImageUtils.generateImageId({
organization: 'gcr.io/project',
repository: 'gcr.io/project/my-image',
digest: undefined,
tag: 'v1.2',
}),
).toEqual('gcr.io/project/my-image:v1.2');
});
});
17 changes: 15 additions & 2 deletions app/scripts/modules/docker/src/image/DockerImageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ export interface IDockerImageParts {
export class DockerImageUtils {
// Split the image id up into the selectable parts to feed the UI
public static splitImageId(imageId = ''): IDockerImageParts {
const imageParts = imageId.split(':');
let imageParts: string[];
if (imageId.includes('@')) {
imageParts = imageId.split('@');
} else {
imageParts = imageId.split(':');
}
const repository = imageParts[0];
const repositoryParts = repository.split('/');
// Everything before the last slash is considered the organization
Expand All @@ -34,6 +39,14 @@ export class DockerImageUtils {
return undefined;
}

return `${parts.repository}:${parts.digest ? parts.digest : parts.tag}`;
let imageId: string;

if (parts.digest) {
imageId = `${parts.repository}@${parts.digest}`;
} else {
imageId = `${parts.repository}:${parts.tag}`;
}

return imageId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,33 @@ export class DockerTriggerTemplate extends React.Component<

private lookupTypeChanged = (o: Option<IDockerLookupType>) => {
const newType = o.value;
this.props.updateCommand('extraFields.tag', newType === 'tag' ? this.state.selectedTag : this.state.digest);
this.updateArtifact(this.props.command, newType === 'tag' ? this.state.selectedTag : this.state.digest);
this.setState({ lookupType: newType });
};

private updateArtifact(command: IPipelineCommand, tag: string) {
this.props.updateCommand('extraFields.tag', tag);
private updateArtifact(command: IPipelineCommand, tagOrDigest: string) {
this.props.updateCommand('extraFields.tag', tagOrDigest);
const trigger = command.trigger as IDockerTrigger;
if (trigger && trigger.repository) {
let imageName = '';
if (trigger.registry) {
imageName += trigger.registry + '/';
}
imageName += trigger.repository;

let imageReference = '';
if (this.state.lookupType === 'digest') {
imageReference = `${imageName}@${tagOrDigest}`;
} else {
imageReference = `${imageName}:${tagOrDigest}`;
}

this.props.updateCommand('extraFields.artifacts', [
{
type: 'docker/image',
name: imageName,
version: tag,
reference: imageName + ':' + tag,
version: tagOrDigest,
reference: imageReference,
},
]);
}
Expand Down

0 comments on commit 0e9ec86

Please sign in to comment.