-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
496 additions
and
334 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
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/github/nexception/reviewassistant/AdviceCache.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,25 @@ | ||
package com.github.nexception.reviewassistant; | ||
|
||
import com.github.nexception.reviewassistant.models.Calculation; | ||
import com.google.gerrit.server.change.RevisionResource; | ||
|
||
/** | ||
* The AdviceCache interface is used to store and fetch calculations. | ||
*/ | ||
public interface AdviceCache { | ||
|
||
/** | ||
* Caches the provided calculation. | ||
* | ||
* @param calculation the calculation object to be cached | ||
*/ | ||
public void storeCalculation(Calculation calculation); | ||
|
||
/** | ||
* Returns the calculation object with the matching RevisionResource. | ||
* | ||
* @param resource the RevisionResource to fetch calculation from cache | ||
* @return a Calculation object if one is found, null otherwise | ||
*/ | ||
public Calculation fetchCalculation(RevisionResource resource); | ||
} |
101 changes: 101 additions & 0 deletions
101
src/main/java/com/github/nexception/reviewassistant/AdviceCacheImpl.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,101 @@ | ||
package com.github.nexception.reviewassistant; | ||
|
||
import com.github.nexception.reviewassistant.models.Calculation; | ||
import com.google.gerrit.extensions.annotations.PluginData; | ||
import com.google.gerrit.extensions.api.GerritApi; | ||
import com.google.gerrit.extensions.api.changes.ChangeApi; | ||
import com.google.gerrit.extensions.common.ChangeInfo; | ||
import com.google.gerrit.extensions.restapi.RestApiException; | ||
import com.google.gerrit.server.change.RevisionResource; | ||
import com.google.gerrit.server.config.PluginConfigFactory; | ||
import com.google.gerrit.server.project.NoSuchProjectException; | ||
import com.google.gson.Gson; | ||
import com.google.inject.Inject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
|
||
/** | ||
* This implementation of AdviceCache writes to the plugin's data directory ({gerrit url}/plugins/ReviewAssistant). | ||
* The structure follows that of git's object directory, which means that the first two letters of the | ||
* commit's SHA-1 is used as name for the sub directory, and the rest of the SHA-1 is used as file name. | ||
*/ | ||
public class AdviceCacheImpl implements AdviceCache { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AdviceCacheImpl.class); | ||
private File dir; | ||
private GerritApi gApi; | ||
private PluginConfigFactory cfg; | ||
|
||
@Inject AdviceCacheImpl(@PluginData File dir, GerritApi gApi, PluginConfigFactory cfg) { | ||
this.dir = dir; | ||
this.gApi = gApi; | ||
this.cfg = cfg; | ||
} | ||
|
||
@Override public void storeCalculation(Calculation calculation) { | ||
File file = new File(dir, | ||
calculation.commitId.substring(0, 2) + File.separator + calculation.commitId | ||
.substring(2)); | ||
log.debug("Writing calculation to {}", file); | ||
file.getParentFile().mkdirs(); | ||
try (BufferedWriter writer = Files | ||
.newBufferedWriter(file.toPath(), Charset.forName("UTF-8"))) { | ||
Gson gson = new Gson(); | ||
String s = gson.toJson(calculation); | ||
writer.write(s, 0, s.length()); | ||
log.debug("Cached calculation in file {}", file); | ||
} catch (FileNotFoundException e) { | ||
log.error("Could not find file {}", file); | ||
log.error(e.toString()); | ||
} catch (IOException e) { | ||
log.error("Could not write to file {}", file); | ||
log.error(e.toString()); | ||
} | ||
} | ||
|
||
@Override public Calculation fetchCalculation(RevisionResource resource) { | ||
File file = new File(dir, | ||
resource.getPatchSet().getRevision().get().substring(0, 2) + File.separator + resource | ||
.getPatchSet().getRevision().get().substring(2)); | ||
Calculation calculation = null; | ||
log.debug("Loading calculation from {}", file); | ||
try (BufferedReader reader = Files | ||
.newBufferedReader(file.toPath(), Charset.forName("UTF-8"))) { | ||
Gson gson = new Gson(); | ||
calculation = gson.fromJson(reader.readLine(), Calculation.class); | ||
log.info("Returning Calculation {}", calculation.toString()); | ||
} catch (IOException e) { | ||
log.error("Could not read calculation file for {}", | ||
resource.getPatchSet().getRevision().get()); | ||
log.error(e.toString()); | ||
} | ||
|
||
if (calculation == null || calculation.totalReviewTime == 0) { | ||
log.debug("Corrupt or missing calculation. Will recalculate for {}", | ||
resource.getPatchSet().getRevision().get()); | ||
try { | ||
ChangeApi cApi = gApi.changes().id(resource.getChange().getChangeId()); | ||
ChangeInfo info = cApi.get(); | ||
double reviewTimeModifier = | ||
cfg.getProjectPluginConfigWithInheritance(resource.getChange().getProject(), | ||
"reviewassistant").getInt("time", "reviewTimeModifier", 100); | ||
calculation = ReviewAssistant.calculate(info, reviewTimeModifier / 100); | ||
storeCalculation(calculation); | ||
} catch (RestApiException e) { | ||
log.error("Could not get ChangeInfo for change {}", | ||
resource.getChange().getChangeId()); | ||
} catch (NoSuchProjectException e) { | ||
log.error(e.getMessage(), e); | ||
} | ||
} | ||
return calculation; | ||
} | ||
} |
Oops, something went wrong.