-
Notifications
You must be signed in to change notification settings - Fork 48
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
PR to fix https://github.com/salesforce/dockerfile-image-update/issues/236, to bring in support for sending PRs to docker-compose files #687
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
72893a9
Initial Commit to support https://github.com/salesforce/dockerfile-im…
pchugh17 a8e548f
Updated to read files types from command lines
pchugh17 9fb9df1
Merged parent
pchugh17 e0edaca
Updated to support docker-compose DFIU update
pchugh17 9fc5cad
Added unit tests
pchugh17 87f8fcc
Updated to support different branch name for different file type run
pchugh17 696e7e8
Uncommented the Unit tests
pchugh17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
...ge-update/src/main/java/com/salesforce/dockerfileimageupdate/model/ImageKeyValuePair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package com.salesforce.dockerfileimageupdate.model; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ImageKeyValuePair { | ||
private static final String IMAGE = "image"; | ||
private static final String INVALID_IMAGE_VALUE = "You have not provided a valid value for image key."; | ||
/** | ||
* The name of the base image | ||
*/ | ||
private final String baseImageName; | ||
/** | ||
* The tag of the base image | ||
*/ | ||
private final String tag; | ||
/** | ||
* As of writing, this could include {@code AS name} to run a multi-stage build | ||
*/ | ||
private final List<String> additionalParts; | ||
/** | ||
* Comment starting with # | ||
*/ | ||
private final String comments; | ||
|
||
/** | ||
* Accepts a FROM instruction line from a Dockerfile | ||
* See {@code isImageKeyValuePair} to ensure you're passing a valid line in. | ||
* | ||
* @param imageKeyValuePair a FROM instruction line from a Dockerfile | ||
*/ | ||
public ImageKeyValuePair(String imageKeyValuePair) { | ||
if (!isImageKeyValuePair(imageKeyValuePair)) { | ||
throw new IllegalArgumentException(INVALID_IMAGE_VALUE); | ||
} | ||
String lineWithoutComment = imageKeyValuePair; | ||
int commentIndex = imageKeyValuePair.indexOf("#"); | ||
if (commentIndex >= 0) { | ||
comments = imageKeyValuePair.substring(commentIndex); | ||
lineWithoutComment = imageKeyValuePair.substring(0, commentIndex); | ||
} else { | ||
comments = null; | ||
} | ||
// Remove "image:" from remaining string | ||
String lineWithoutImageKey = lineWithoutComment.trim(). | ||
replaceFirst(IMAGE, "").replaceFirst(":", ""). | ||
trim(); | ||
|
||
String[] imageAndTag = lineWithoutImageKey.split(":"); | ||
if (StringUtils.isNotEmpty(lineWithoutImageKey) && imageAndTag.length > 0) { | ||
baseImageName = imageAndTag[0]; | ||
if (imageAndTag.length > 1) { | ||
tag = imageAndTag[1]; | ||
} else { | ||
tag = null; | ||
} | ||
additionalParts = ImmutableList.of(); | ||
} else { | ||
baseImageName = null; | ||
tag = null; | ||
additionalParts = ImmutableList.of(); | ||
} | ||
} | ||
|
||
/** | ||
* Internal API to get a new ComposeImageValuePair from an existing object | ||
* @param baseImageName baseImageName to add | ||
* @param tag tag to add | ||
* @param additionalParts additionalParts to add | ||
* @param comments comments to add | ||
*/ | ||
private ImageKeyValuePair(String baseImageName, String tag, List<String> additionalParts, String comments) { | ||
this.baseImageName = baseImageName; | ||
this.tag = tag; | ||
this.additionalParts = ImmutableList.copyOf(additionalParts); | ||
this.comments = comments; | ||
} | ||
|
||
/** | ||
* Check if this {@code lineInFile} is a FROM instruction, | ||
* it is referencing {@code imageName} as a base image, | ||
* and the tag is not the same as {@code imageTag} (or there is no tag) | ||
* @param lineInFile Line a code file | ||
* @param imageName images name | ||
* @param imageTag tag for imageName | ||
* @return {@link Boolean} value isImageKeyValuePairWithThisImageAndOlderTag | ||
*/ | ||
public static boolean isImageKeyValuePairWithThisImageAndOlderTag(String lineInFile, String imageName, String imageTag) { | ||
if (ImageKeyValuePair.isImageKeyValuePair(lineInFile)) { | ||
ImageKeyValuePair ImageKeyValuePair = new ImageKeyValuePair(lineInFile); | ||
return ImageKeyValuePair.hasBaseImage(imageName) && ImageKeyValuePair.hasADifferentTag(imageTag); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Get a new {@code ComposeImageValuePair} the same as this but with the {@code tag} set as {@code newTag} | ||
* @param newTag the new image tag | ||
* @return a new FROM with the new image tag | ||
*/ | ||
public ImageKeyValuePair getImageKeyValuePairWithNewTag(String newTag) { | ||
return new ImageKeyValuePair(baseImageName, newTag, additionalParts, comments); | ||
} | ||
|
||
/** | ||
* Determines whether the line is a FROM instruction line in a Dockerfile | ||
* @param composeImageKeyValueLine a single line(key:value) from a Docker-compose.yaml | ||
* @return the line is a FROM instruction line or not | ||
*/ | ||
public static boolean isImageKeyValuePair(String composeImageKeyValueLine) { | ||
if (StringUtils.isNotBlank(composeImageKeyValueLine)) { | ||
return composeImageKeyValueLine.trim().startsWith(ImageKeyValuePair.IMAGE); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @return a String representation of a FROM instruction line in Dockerfile. No new line at the end | ||
*/ | ||
@Override | ||
public String toString() { | ||
StringBuilder stringBuilder = new StringBuilder(IMAGE); | ||
stringBuilder.append(": "); | ||
stringBuilder.append(baseImageName); | ||
if (hasTag()) { | ||
stringBuilder.append(String.format(":%s", tag.trim())); | ||
} | ||
for (String part : additionalParts) { | ||
if (StringUtils.isNotBlank(part)) { | ||
stringBuilder.append(String.format(" %s", part.trim())); | ||
} | ||
} | ||
|
||
if (hasComments()) { | ||
stringBuilder.append(String.format(" %s", comments)); | ||
} | ||
|
||
return stringBuilder.toString(); | ||
} | ||
|
||
public String getBaseImageName() { | ||
return baseImageName; | ||
} | ||
|
||
/** | ||
* Check to see if the {@code baseImageName} in this object is the {@code imageToFind} without | ||
* the other details (e.g. registry) | ||
* @param imageToFind the image name to search for | ||
* @return is {@code baseImageName} the same as {@code imageToFind} without extra things like registry | ||
*/ | ||
public boolean hasBaseImage(String imageToFind) { | ||
return baseImageName != null && | ||
imageToFind != null && | ||
baseImageName.endsWith(imageToFind); | ||
} | ||
|
||
/** | ||
* @return whether the {@code ComposeImageValuePair} has a {@code tag} | ||
*/ | ||
public boolean hasTag() { | ||
return tag != null; | ||
} | ||
|
||
/** | ||
* Determines whether the {@code tag} and {@code expectedTag} are the same | ||
* @param expectedTag the tag to compare against ComposeImageValuePair's {@code tag} | ||
* @return {@code true} if the 2 tags are different | ||
*/ | ||
public boolean hasADifferentTag(String expectedTag) { | ||
if (tag == null && expectedTag == null) { | ||
return false; | ||
} | ||
if (tag == null || expectedTag == null) { | ||
return true; | ||
} | ||
return !tag.trim().equals(expectedTag.trim()); | ||
} | ||
|
||
public String getTag() { | ||
return tag; | ||
} | ||
|
||
public List<String> getAdditionalParts() { | ||
return additionalParts; | ||
} | ||
|
||
public boolean hasComments() { | ||
return comments != null; | ||
} | ||
|
||
public String getComments() { | ||
return comments; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move these filenames to come from CLI where we have a default list of
"Dockerfile", "docker-compose"
there? Then this part of code would do search.filename on a listThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done