VerigreenNeededLogic { public VerigreenNeeded isVerigreenNeeded( final String parentBranchName, final String branchNameToVerify, final String commitId, final String committer) { String reason = ""; boolean ans; boolean shouldRejectCommit = true; ans= (VerigreenMap.get("_protectedBranches")).matches("(^|(.*,))"+parentBranchName+"((,.*)|$)") && !(VerigreenMap.get("_permittedUsers")).matches("(^|(.*,))"+committer+"((,.*)|$)"); if (ans) { CommitItem item = checkIfCommitItemExists(parentBranchName, branchNameToVerify, commitId); List<String> passCommit = VerigreenMap.get("_passCommit") != null? new LinkedList<>(Arrays.asList(VerigreenMap.get("_passCommit").split("\\s*,\\s*"))):null; if (item != null){ ans = false; VerificationStatus vs = item.getStatus(); if (vs.equals(VerificationStatus.PASSED) || vs.equals(VerificationStatus.FORCING_PUSH) || vs.equals(VerificationStatus.PASSED_BY_CHILD)){ shouldRejectCommit = false; } else if (vs.equals(VerificationStatus.GIT_FAILURE) || vs.equals(VerificationStatus.TRIGGER_FAILED) || vs.equals(VerificationStatus.TIMEOUT)){ ans = true; shouldRejectCommit = false; } else if(VerigreenMap.get("_fullPush").equals("enabled") && vs.equals(VerificationStatus.FAILED)) { ans = true; shouldRejectCommit = false; } reason = String.format( "branch [%s] already exists and its status is [%s]", branchNameToVerify, vs.toString()); } else if (passCommit != null){ String commitToBeDeleted = ""; for (String commit : passCommit) { if (!commit.equals("") && branchNameToVerify.contains(commit)){ commitToBeDeleted = commit; ans = false; shouldRejectCommit = false; reason = String.format( "Verigreen is not needed, Commit [%s] was created due to push by permitted user", commit); break; } } if (passCommit.contains(commitToBeDeleted)){ passCommit.remove(commitToBeDeleted); String passCommitString = VerigreenNeededLogic.VerigreenMap.get("_passCommit"); passCommitString = passCommitString.replace(commitToBeDeleted, ""); passCommitString = passCommitString.replace(",,", ","); if (passCommitString.startsWith(",")){ passCommitString = passCommitString.replace(",", ""); } VerigreenMap.put("_passCommit", passCommitString); } } } else { shouldRejectCommit = false; reason = (VerigreenMap.get("_permittedUsers")).contains(committer) ? String.format("[%s] is a permitted user", committer) : String.format( "[%s] isn't protected branch and therefore verigreen won't be executed", parentBranchName); } return new VerigreenNeeded(ans, shouldRejectCommit, reason); } private  VerigreenNeededLogic(String protectedBranches, String permittedUsers, String hashedPassword, String fullPush
			); void setGitRepositoryPath(); void sendEmailNotification(String subject, String messageText, String[] recipients, String signature); static String getSignature(); void setJenkinsProperties(); void setProtectedBranches(List<String> protectedBranches); void setPermittedUsers(List<String> permittedUsers); VerigreenNeeded isVerigreenNeeded(
			final String parentBranchName,
			final String branchNameToVerify,
			final String commitId,
			final String committer); static Map<String, String> checkJenkinsMode(CommitItem commitItem); static Map<String, String> commitItemAsParameterMap(CommitItem commitItem); static Properties properties; static Map<String,String> VerigreenMap; static Map<String,String> jenkinsParams; static Map<String,List<JSONObject>> history; }
@Test public void testIsVerigreenNeededBranchNotProtected() { VerigreenNeeded verigreenNeeded = CollectorApi.getVerigreenNeededLogic().isVerigreenNeeded( "not-protected", branch, "", "user1"); Assert.assertEquals(false, verigreenNeeded.getVerigreenNeeded()); Assert.assertEquals(false, verigreenNeeded.getShouldRejectCommit()); }
@Test public void testIsVerigreenNeededCommitPassed() { CommitItemFactory.create(VerificationStatus.PASSED, branch, null); VerigreenNeeded verigreenNeeded = CollectorApi.getVerigreenNeededLogic().isVerigreenNeeded( protectedBranch, branch, "", "user1"); Assert.assertEquals(false, verigreenNeeded.getVerigreenNeeded()); Assert.assertEquals(false, verigreenNeeded.getShouldRejectCommit()); }
@Test public void testIsVerigreenNeededCommitNull() { VerigreenNeeded verigreenNeeded = CollectorApi.getVerigreenNeededLogic().isVerigreenNeeded( protectedBranch, branch, "", "user1"); Assert.assertEquals(true, verigreenNeeded.getVerigreenNeeded()); Assert.assertEquals(true, verigreenNeeded.getShouldRejectCommit()); }
@Test public void testIsVerigreenNeededCommitRunning() { CommitItemFactory.create(VerificationStatus.RUNNING, branch, null); VerigreenNeeded verigreenNeeded = CollectorApi.getVerigreenNeededLogic().isVerigreenNeeded( protectedBranch, branch, "", "user1"); Assert.assertEquals(false, verigreenNeeded.getVerigreenNeeded()); Assert.assertEquals(true, verigreenNeeded.getShouldRejectCommit()); }
@Test public void testIsVerigreenNeededPermittedUser() { String pUser= VerigreenNeededLogic.properties.getProperty("git.permittedUsers"); VerigreenNeeded verigreenNeeded = CollectorApi.getVerigreenNeededLogic().isVerigreenNeeded( protectedBranch, branch, "", pUser); Assert.assertEquals(false, verigreenNeeded.getVerigreenNeeded()); Assert.assertEquals(false, verigreenNeeded.getShouldRejectCommit()); }