|
| 1 | +package com.salesforce.dockerfileimageupdate.process; |
| 2 | + |
| 3 | +import com.salesforce.dockerfileimageupdate.model.FromInstruction; |
| 4 | +import com.salesforce.dockerfileimageupdate.model.GitForkBranch; |
| 5 | +import com.salesforce.dockerfileimageupdate.model.ShouldForkResult; |
| 6 | +import com.salesforce.dockerfileimageupdate.utils.DockerfileGitHubUtil; |
| 7 | +import org.kohsuke.github.GHContent; |
| 8 | +import org.kohsuke.github.GHRepository; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import java.io.BufferedReader; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.InputStreamReader; |
| 16 | + |
| 17 | +import static com.salesforce.dockerfileimageupdate.model.ShouldForkResult.shouldForkResult; |
| 18 | +import static com.salesforce.dockerfileimageupdate.model.ShouldForkResult.shouldNotForkResult; |
| 19 | + |
| 20 | +public class ForkableRepoValidator { |
| 21 | + private static final Logger log = LoggerFactory.getLogger(ForkableRepoValidator.class); |
| 22 | + |
| 23 | + public static final String REPO_IS_FORK = |
| 24 | + "it's a fork already. Sending a PR to a fork is unsupported at the moment."; |
| 25 | + public static final String REPO_IS_ARCHIVED = "it's archived."; |
| 26 | + public static final String REPO_IS_OWNED_BY_THIS_USER = "it is owned by this user."; |
| 27 | + public static final String COULD_NOT_CHECK_THIS_USER = |
| 28 | + "we could not determine fork status because we don't know the identity of the authenticated user."; |
| 29 | + public static final String CONTENT_PATH_NOT_IN_DEFAULT_BRANCH_TEMPLATE = |
| 30 | + "didn't find content path %s in default branch"; |
| 31 | + public static final String COULD_NOT_FIND_IMAGE_TO_UPDATE_TEMPLATE = |
| 32 | + "didn't find the image '%s' which required an update in path %s"; |
| 33 | + private final DockerfileGitHubUtil dockerfileGitHubUtil; |
| 34 | + |
| 35 | + public ForkableRepoValidator(DockerfileGitHubUtil dockerfileGitHubUtil) { |
| 36 | + this.dockerfileGitHubUtil = dockerfileGitHubUtil; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Check various conditions required for a repo to qualify for updates |
| 41 | + * |
| 42 | + * @param parentRepo parent repo we're checking |
| 43 | + * @param searchResultContent search result content we'll check against |
| 44 | + * @return should we fork the parentRepo? |
| 45 | + */ |
| 46 | + public ShouldForkResult shouldFork(GHRepository parentRepo, |
| 47 | + GHContent searchResultContent, |
| 48 | + GitForkBranch gitForkBranch) { |
| 49 | + return parentIsFork(parentRepo) |
| 50 | + .and(parentIsArchived(parentRepo) |
| 51 | + .and(thisUserIsNotOwner(parentRepo) |
| 52 | + .and(contentHasChangesInDefaultBranch(parentRepo, searchResultContent, gitForkBranch)))); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Check to see if the default branch of the parentRepo has the path we found in searchResultContent and |
| 57 | + * whether that content has a qualifying base image update |
| 58 | + * @param parentRepo parentRepo which we'd fork off of |
| 59 | + * @param searchResultContent search result with path to check in parent repo's default branch (where we'd PR) |
| 60 | + * @param gitForkBranch information about the imageName we'd like to update with the new tag |
| 61 | + */ |
| 62 | + protected ShouldForkResult contentHasChangesInDefaultBranch(GHRepository parentRepo, |
| 63 | + GHContent searchResultContent, |
| 64 | + GitForkBranch gitForkBranch) { |
| 65 | + try { |
| 66 | + String searchContentPath = searchResultContent.getPath(); |
| 67 | + GHContent content = |
| 68 | + dockerfileGitHubUtil.tryRetrievingContent(parentRepo, |
| 69 | + searchContentPath, parentRepo.getDefaultBranch()); |
| 70 | + if (content == null) { |
| 71 | + return shouldNotForkResult( |
| 72 | + String.format(CONTENT_PATH_NOT_IN_DEFAULT_BRANCH_TEMPLATE, searchContentPath)); |
| 73 | + } else { |
| 74 | + if (hasNoChanges(content, gitForkBranch)) { |
| 75 | + return shouldNotForkResult( |
| 76 | + String.format(COULD_NOT_FIND_IMAGE_TO_UPDATE_TEMPLATE, |
| 77 | + gitForkBranch.getImageName(), searchContentPath)); |
| 78 | + } |
| 79 | + } |
| 80 | + } catch (InterruptedException e) { |
| 81 | + log.warn("Couldn't get parent content to check for some reason for {}. Trying to proceed... exception: {}", |
| 82 | + parentRepo.getFullName(), e.getMessage()); |
| 83 | + } |
| 84 | + return shouldForkResult(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Check to see whether there are any changes in the specified content where the specified base image |
| 89 | + * in gitForkBranch needs an update |
| 90 | + * |
| 91 | + * @param content content to check |
| 92 | + * @param gitForkBranch information about the base image we'd like to update |
| 93 | + */ |
| 94 | + protected boolean hasNoChanges(GHContent content, GitForkBranch gitForkBranch) { |
| 95 | + try (InputStream stream = content.read(); |
| 96 | + InputStreamReader streamR = new InputStreamReader(stream); |
| 97 | + BufferedReader reader = new BufferedReader(streamR)) { |
| 98 | + String line; |
| 99 | + while ( (line = reader.readLine()) != null ) { |
| 100 | + if (FromInstruction.isFromInstructionWithThisImageAndOlderTag(line, |
| 101 | + gitForkBranch.getImageName(), gitForkBranch.getImageTag())) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + } catch (IOException exception) { |
| 106 | + log.warn("Failed while checking if there are changes in {}. Skipping... exception: {}", |
| 107 | + content.getPath(), exception.getMessage()); |
| 108 | + } |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Attempts to check to see if this user is the owner of the repo (don't fork your own repo). |
| 114 | + * If we can't tell because of a systemic error, don't attempt to fork (we could perhaps loosen this in the future). |
| 115 | + * If this user is the owner of the repo, do not fork. |
| 116 | + * |
| 117 | + * @param parentRepo parent repo we're checking |
| 118 | + */ |
| 119 | + protected ShouldForkResult thisUserIsNotOwner(GHRepository parentRepo) { |
| 120 | + try { |
| 121 | + if (dockerfileGitHubUtil.thisUserIsOwner(parentRepo)) { |
| 122 | + return shouldNotForkResult(REPO_IS_OWNED_BY_THIS_USER); |
| 123 | + } |
| 124 | + } catch (IOException ioException) { |
| 125 | + return shouldNotForkResult(COULD_NOT_CHECK_THIS_USER); |
| 126 | + } |
| 127 | + return shouldForkResult(); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Check if parentRepo is a fork. Do not fork a fork (for now, at least). |
| 132 | + * |
| 133 | + * @param parentRepo parent repo we're checking |
| 134 | + */ |
| 135 | + protected ShouldForkResult parentIsFork(GHRepository parentRepo) { |
| 136 | + return parentRepo.isFork() ? shouldNotForkResult(REPO_IS_FORK) : shouldForkResult(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Check if parentRepo is archived. We won't be able to update it, so do not fork. |
| 141 | + * |
| 142 | + * @param parentRepo parent repo we're checking |
| 143 | + */ |
| 144 | + protected ShouldForkResult parentIsArchived(GHRepository parentRepo) { |
| 145 | + return parentRepo.isArchived() ? shouldNotForkResult(REPO_IS_ARCHIVED) : shouldForkResult(); |
| 146 | + |
| 147 | + } |
| 148 | +} |
0 commit comments