use of com.google.gerrit.entities.Change in project gerrit by GerritCodeReview.
the class CatServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
String keyStr = req.getPathInfo();
// We shouldn't have to do this extra decode pass, but somehow we
// are now receiving our "^1" suffix as "%5E1", which confuses us
// downstream. Other times we get our embedded "," as "%2C", which
// is equally bad. And yet when these happen a "%2F" is left as-is,
// rather than escaped as "%252F", which makes me feel really really
// uncomfortable with a blind decode right here.
//
keyStr = Url.decode(keyStr);
if (!keyStr.startsWith("/")) {
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
keyStr = keyStr.substring(1);
final Patch.Key patchKey;
final int side;
{
final int c = keyStr.lastIndexOf('^');
if (c == 0) {
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (c < 0) {
side = 0;
} else {
try {
side = Integer.parseInt(keyStr.substring(c + 1));
keyStr = keyStr.substring(0, c);
} catch (NumberFormatException e) {
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
}
try {
patchKey = Patch.Key.parse(keyStr);
} catch (NumberFormatException e) {
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
}
final Change.Id changeId = patchKey.patchSetId().changeId();
String revision;
try {
ChangeNotes notes = changeNotesFactory.createCheckedUsingIndexLookup(changeId);
permissionBackend.currentUser().change(notes).check(ChangePermission.READ);
projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName())).checkStatePermitsRead();
if (patchKey.patchSetId().get() == 0) {
// change edit
Optional<ChangeEdit> edit = changeEditUtil.byChange(notes);
if (edit.isPresent()) {
revision = ObjectId.toString(edit.get().getEditCommit());
} else {
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
} else {
PatchSet patchSet = psUtil.get(notes, patchKey.patchSetId());
if (patchSet == null) {
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
revision = patchSet.commitId().name();
}
} catch (ResourceConflictException | NoSuchChangeException | AuthException e) {
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (PermissionBackendException | IOException e) {
getServletContext().log("Cannot query database", e);
rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
String path = patchKey.fileName();
String restUrl = String.format("%s/changes/%d/revisions/%s/files/%s/download?parent=%d", req.getContextPath(), changeId.get(), revision, Url.encode(path), side);
rsp.sendRedirect(restUrl);
}
use of com.google.gerrit.entities.Change in project gerrit by GerritCodeReview.
the class StarredChangesUtil method byAccountId.
public ImmutableSet<Change.Id> byAccountId(Account.Id accountId, String label) {
try (Repository repo = repoManager.openRepository(allUsers)) {
ImmutableSet.Builder<Change.Id> builder = ImmutableSet.builder();
for (Ref ref : repo.getRefDatabase().getRefsByPrefix(RefNames.REFS_STARRED_CHANGES)) {
Account.Id currentAccountId = Account.Id.fromRef(ref.getName());
// Skip all refs that don't correspond with accountId.
if (currentAccountId == null || !currentAccountId.equals(accountId)) {
continue;
}
// Skip all refs that don't contain the required label.
StarRef starRef = readLabels(repo, ref.getName());
if (!starRef.labels().contains(label)) {
continue;
}
// Skip invalid change ids.
Change.Id changeId = Change.Id.fromAllUsersRef(ref.getName());
if (changeId == null) {
continue;
}
builder.add(changeId);
}
return builder.build();
} catch (IOException e) {
throw new StorageException(String.format("Get starred changes for account %d failed", accountId.get()), e);
}
}
use of com.google.gerrit.entities.Change in project gerrit by GerritCodeReview.
the class PatchSetUtil method isPatchSetLocked.
/**
* Is the current patch set locked against state changes?
*/
public boolean isPatchSetLocked(ChangeNotes notes) {
Change change = notes.getChange();
if (change.isMerged()) {
return false;
}
ProjectState projectState = projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName()));
ApprovalsUtil approvalsUtil = approvalsUtilProvider.get();
for (PatchSetApproval ap : approvalsUtil.byPatchSet(notes, change.currentPatchSetId())) {
Optional<LabelType> type = projectState.getLabelTypes(notes).byLabel(ap.label());
if (type.isPresent() && ap.value() == 1 && type.get().getFunction() == LabelFunction.PATCH_SET_LOCK) {
return true;
}
}
return false;
}
use of com.google.gerrit.entities.Change in project gerrit by GerritCodeReview.
the class ChangeIdHandler method parseArguments.
@Override
public final int parseArguments(Parameters params) throws CmdLineException {
String token = params.getParameter(0);
List<String> tokens = Splitter.on(',').splitToList(token);
if (tokens.size() != 3) {
throw new CmdLineException(owner, localizable("change should be specified as <project>,<branch>,<change-id>"));
}
try {
Change.Key key = Change.Key.parse(tokens.get(2));
Project.NameKey project = Project.nameKey(tokens.get(0));
BranchNameKey branch = BranchNameKey.create(project, tokens.get(1));
List<ChangeData> changes = queryProvider.get().byBranchKey(branch, key);
if (!changes.isEmpty()) {
if (changes.size() > 1) {
String msg = "\"%s\": resolves to multiple changes";
logger.atSevere().log(msg, token);
throw new CmdLineException(owner, localizable(msg), token);
}
setter.addValue(changes.get(0).getId());
return 1;
}
} catch (IllegalArgumentException e) {
throw new CmdLineException(owner, localizable("Change-Id is not valid: %s"), e.getMessage());
} catch (StorageException e) {
throw new CmdLineException(owner, localizable("Database error: %s"), e.getMessage());
}
throw new CmdLineException(owner, localizable("\"%s\": change not found"), token);
}
use of com.google.gerrit.entities.Change in project gerrit by GerritCodeReview.
the class AddReviewersOp method updateChange.
@Override
public boolean updateChange(ChangeContext ctx) throws RestApiException, IOException {
change = ctx.getChange();
if (!accountIds.isEmpty()) {
if (state == CC) {
addedCCs = approvalsUtil.addCcs(ctx.getNotes(), ctx.getUpdate(change.currentPatchSetId()), accountIds, forGroup);
} else {
addedReviewers = approvalsUtil.addReviewers(ctx.getNotes(), ctx.getUpdate(change.currentPatchSetId()), projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject())).getLabelTypes(change.getDest()), change, accountIds);
}
}
ReviewerStateInternal internalState = ReviewerStateInternal.fromReviewerState(state);
// TODO(dborowitz): This behavior should live in ApprovalsUtil or something, like addCcs does.
ImmutableSet<Address> existing = ctx.getNotes().getReviewersByEmail().byState(internalState);
ImmutableList<Address> addressesToAdd = addresses.stream().filter(a -> !existing.contains(a)).collect(toImmutableList());
if (state == CC) {
addedCCsByEmail = addressesToAdd;
} else {
addedReviewersByEmail = addressesToAdd;
}
for (Address a : addressesToAdd) {
ctx.getUpdate(change.currentPatchSetId()).putReviewerByEmail(a, internalState);
}
if (addedCCs.isEmpty() && addedReviewers.isEmpty() && addressesToAdd.isEmpty()) {
return false;
}
checkAdded();
if (patchSet == null) {
patchSet = requireNonNull(psUtil.current(ctx.getNotes()));
}
return true;
}
Aggregations