Skip to content

Commit

Permalink
Merge branch 'release-0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
williamphan committed Jan 13, 2015
2 parents a3d7924 + 3813982 commit cb44a83
Show file tree
Hide file tree
Showing 13 changed files with 496 additions and 334 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Changelog
All notable changes to this project will be documented in this file.

## 0.4 - 2015-01-13
### Added
- ReviewAssistant is now configurable.
- Maximum reviewers, configuration parameter.
- Auto add reviewer, configuration parameter.
- If +2 user is required, configuration parameter.
- Modifier for total review time, configuration parameter.
- Load balancing, configuration parameter.
- Age and limit in the +2 query, configuration parameter.

### Fixed
- Query only looks for changes with +2 code review.
- ReviewAssistant will now try to fill with reviewers until maxReviewers is reached.

## 0.3 - 2015-01-05
### Added
- Automatic adding of reviewer with merge rights.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.github.nexception.reviewassistant</groupId>
<artifactId>reviewassistant</artifactId>
<packaging>jar</packaging>
<version>0.3</version>
<version>0.4</version>
<name>reviewassistant</name>

<properties>
Expand Down
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);
}
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;
}
}
Loading

0 comments on commit cb44a83

Please sign in to comment.