com.salesforce.dockerfileimageupdate.model.GitForkBranch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dockerfile-image-update Show documentation
Show all versions of dockerfile-image-update Show documentation
This tool provides a mechanism to make security updates to docker images at scale.
The tool searches github for declared docker images and sends pull requests to projects that are not using
the desired version of the requested docker image.
package com.salesforce.dockerfileimageupdate.model;
/**
* Converts an imageName to a branchName. Primary conversion necessary is : to -
* Also support backward compatible method of specifying a branch
*
* Container name rules: https://docs.docker.com/engine/reference/commandline/tag/#extended-description
* Branch Rules: https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
*/
public class GitForkBranch {
private final String branchPrefix;
private final String imageName;
private final String imageTag;
private final boolean specifiedBranchOverride;
public GitForkBranch(String imageName, String imageTag, String specifiedBranch) {
this.imageTag = (imageTag == null || imageTag.trim().isEmpty()) ? "" : imageTag.trim();
this.imageName = (imageName == null || imageName.trim().isEmpty()) ? "" : imageName.trim();
if (specifiedBranch == null || specifiedBranch.trim().isEmpty()) {
if (this.imageName.isEmpty()) {
throw new IllegalArgumentException("You must specify an imageName if not specifying a branch");
}
this.branchPrefix = this.imageName.replace(":", "-").toLowerCase();
this.specifiedBranchOverride = false;
} else {
this.branchPrefix = specifiedBranch;
this.specifiedBranchOverride = true;
}
}
/**
* If a specifiedBranch was specified, do a direct comparison. Otherwise, check for a
* translated imageName as a prefix.
*
* @param branchName branch name to check against our new image
* @return is the branch name equal to specifiedBranch or the normalized image name
*/
public boolean isSameBranchOrHasImageNamePrefix(String branchName) {
if (this.imageTag.equals("")) {
return this.getBranchName().equals(branchName);
} else if (branchName != null) {
String tempBranchName = branchName.trim();
return getBranchWithoutTag(tempBranchName).equals(this.branchPrefix);
}
return false;
}
private String getBranchWithoutTag(String branchName) {
int lastDash = branchName.lastIndexOf("-");
if (lastDash == -1) {
return branchName;
} else {
return branchName.substring(0, lastDash);
}
}
/**
* @return a branch was specified to override default behavior
*/
public boolean useSpecifiedBranchOverride() {
return this.specifiedBranchOverride;
}
/**
* Either the specified branch or a combo of normalized imageName-imageTag
*
* This essentially uses a cleansed and lowercase imageName as the branch prefix.
* If an imageTag exists, we append a dash and then the imageTag.
*
* @return cleansed branchPrefix[-imageTag]
*/
public String getBranchName() {
if (this.imageTag == null || this.imageTag.trim().isEmpty()) {
return this.branchPrefix;
} else {
return this.branchPrefix + "-" + this.imageTag;
}
}
public String getImageName() {
return imageName;
}
public String getImageTag() {
return imageTag;
}
}