-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from michpetrov/set-317
SET-317: check if PR is merged in the future branch
- Loading branch information
Showing
4 changed files
with
147 additions
and
4 deletions.
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
135 changes: 135 additions & 0 deletions
135
src/main/java/org/jboss/set/assist/CommitHomeService.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,135 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2020, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.jboss.set.assist; | ||
|
||
import org.jboss.set.aphrodite.Aphrodite; | ||
import org.jboss.set.aphrodite.domain.Commit; | ||
import org.jboss.set.aphrodite.domain.PullRequest; | ||
import org.jboss.set.aphrodite.simplecontainer.SimpleContainer; | ||
import org.jboss.set.aphrodite.spi.NotFoundException; | ||
|
||
import javax.naming.NameNotFoundException; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.LocalTime; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class CommitHomeService { | ||
private static final Logger logger = Logger.getLogger(CommitHomeService.class.getCanonicalName()); | ||
private static Aphrodite aphrodite; | ||
private static Store store; | ||
|
||
private static final String FUTURE = "-future"; | ||
private static final String EAP_REPO = "jbossas/jboss-eap7"; | ||
|
||
private static final long MONTH_MILLI = 2629800000L; // milliseconds in a month | ||
|
||
static { | ||
try { | ||
aphrodite = SimpleContainer.instance().lookup(Aphrodite.class.getSimpleName(), Aphrodite.class); | ||
} catch (NameNotFoundException e) { | ||
logger.log(Level.SEVERE, "Can not get aphrodite service due to : ", e); | ||
} | ||
} | ||
|
||
private static Store store() { | ||
if (store == null) { | ||
store = new Store(); | ||
} | ||
return store; | ||
} | ||
|
||
private static List<Commit> getLastCommits(PullRequest pullRequest) { | ||
if (!isEAP7PR(pullRequest)) { | ||
return null; | ||
} | ||
|
||
String branch = pullRequest.getCodebase().getName() + FUTURE; | ||
if (store().getCommits(branch) == null || store().isStale()) { | ||
try { | ||
long since = Instant.now().toEpochMilli() - 5 * MONTH_MILLI; // 5 months in the past | ||
List<Commit> list = aphrodite.getCommitsSince(pullRequest.getRepository().getURL(), branch, since); | ||
store().putCommits(branch, list); | ||
store().update(); | ||
} catch (NotFoundException nfe) { | ||
logger.warning("Could not retrieve commits from " + pullRequest.getRepository().getURL()); | ||
} | ||
} | ||
|
||
return store().getCommits(branch); | ||
} | ||
|
||
|
||
private static boolean isEAP7PR(PullRequest pr) { | ||
return pr.getURL().toString().contains(EAP_REPO) && pr.getCodebase().getName().startsWith("7."); | ||
} | ||
|
||
/** | ||
* Checks all commits (SHA or message) of the given PR against last commits from a future branch | ||
* (only considers PRs submitted to a 7.<y>.x branch) | ||
* | ||
* @param pullRequest | ||
* @return false if PR is not submitted to 7.x or if one or more commits are missing in the future branch, true otherwise | ||
*/ | ||
public static boolean isMergedInFutureBranch(PullRequest pullRequest) { | ||
if (!isEAP7PR(pullRequest)) { | ||
return false; | ||
} | ||
|
||
List<Commit> lastCommits = getLastCommits(pullRequest); | ||
for (Commit prCommit : pullRequest.getCommits()) { | ||
boolean found = lastCommits.stream() | ||
.anyMatch(commit -> prCommit.getSha().equals(commit.getSha()) || pullRequest.getTitle().equals(commit.getMessage())); | ||
if (!found) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static class Store { | ||
private Map<String, List<Commit>> commitMap = new HashMap<>(); | ||
private LocalTime lastUpdate = LocalTime.now(); | ||
|
||
public List<Commit> getCommits(String branch) { | ||
return commitMap.get(branch); | ||
} | ||
|
||
public void putCommits(String branch, List<Commit> commits) { | ||
commitMap.put(branch, commits); | ||
} | ||
|
||
public void update() { | ||
lastUpdate = isStale() ? LocalTime.now() : lastUpdate; | ||
} | ||
|
||
public boolean isStale() { | ||
return Duration.between(lastUpdate, LocalTime.now()).toHours() >= 2; | ||
} | ||
} | ||
} |
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