use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.
the class CreateTag method apply.
@Override
public Response<TagInfo> apply(ProjectResource resource, IdString id, TagInput input) throws RestApiException, IOException, PermissionBackendException, NoSuchProjectException {
String ref = id.get();
if (input == null) {
input = new TagInput();
}
if (input.ref != null && !ref.equals(input.ref)) {
throw new BadRequestException("ref must match URL");
}
if (input.revision != null) {
input.revision = input.revision.trim();
}
if (Strings.isNullOrEmpty(input.revision)) {
input.revision = Constants.HEAD;
}
ref = RefUtil.normalizeTagRef(ref);
PermissionBackend.ForRef perm = permissionBackend.currentUser().project(resource.getNameKey()).ref(ref);
try (Repository repo = repoManager.openRepository(resource.getNameKey())) {
ObjectId revid = RefUtil.parseBaseRevision(repo, resource.getNameKey(), input.revision);
RevWalk rw = RefUtil.verifyConnected(repo, revid);
// Reachability through tags does not influence a commit's visibility, so no need to check for
// visibility.
RevObject object = rw.parseAny(revid);
rw.reset();
boolean isAnnotated = Strings.emptyToNull(input.message) != null;
boolean isSigned = isAnnotated && input.message.contains("-----BEGIN PGP SIGNATURE-----\n");
if (isSigned) {
throw new MethodNotAllowedException("Cannot create signed tag \"" + ref + "\"");
} else if (isAnnotated) {
if (!check(perm, RefPermission.CREATE_TAG)) {
throw new AuthException("Cannot create annotated tag \"" + ref + "\"");
}
} else {
perm.check(RefPermission.CREATE);
}
if (repo.getRefDatabase().exactRef(ref) != null) {
throw new ResourceConflictException("tag \"" + ref + "\" already exists");
}
try (Git git = new Git(repo)) {
TagCommand tag = git.tag().setObjectId(object).setName(ref.substring(R_TAGS.length())).setAnnotated(isAnnotated).setSigned(isSigned);
if (isAnnotated) {
tag.setMessage(input.message).setTagger(resource.getUser().asIdentifiedUser().newCommitterIdent(TimeUtil.now(), TimeZone.getDefault()));
}
Ref result = tag.call();
tagCache.updateFastForward(resource.getNameKey(), ref, ObjectId.zeroId(), result.getObjectId());
referenceUpdated.fire(resource.getNameKey(), ref, ObjectId.zeroId(), result.getObjectId(), resource.getUser().asIdentifiedUser().state());
try (RevWalk w = new RevWalk(repo)) {
return Response.created(ListTags.createTagInfo(perm, result, w, resource.getProjectState(), links));
}
}
} catch (InvalidRevisionException e) {
throw new BadRequestException("Invalid base revision", e);
} catch (GitAPIException e) {
logger.atSevere().withCause(e).log("Cannot create tag \"%s\"", ref);
throw new IOException(e);
}
}
use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.
the class AgreementsIT method agreementsDisabledList.
@Test
public void agreementsDisabledList() throws Exception {
assume().that(isContributorAgreementsEnabled()).isFalse();
MethodNotAllowedException thrown = assertThrows(MethodNotAllowedException.class, () -> gApi.accounts().self().listAgreements());
assertThat(thrown).hasMessageThat().contains("contributor agreements disabled");
}
use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.
the class GetArchiveIT method zipFormatIsDisabled.
@Test
public void zipFormatIsDisabled() throws Exception {
MethodNotAllowedException ex = assertThrows(MethodNotAllowedException.class, () -> gApi.changes().id(changeId).current().getArchive(ArchiveFormat.ZIP));
assertThat(ex).hasMessageThat().isEqualTo("zip format is disabled");
}
use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.
the class RevisionReviewers method parse.
@Override
public ReviewerResource parse(RevisionResource rsrc, IdString id) throws ResourceNotFoundException, AuthException, MethodNotAllowedException, IOException, ConfigInvalidException {
if (!rsrc.isCurrent()) {
throw new MethodNotAllowedException("Cannot access on non-current patch set");
}
Address address = Address.tryParse(id.get());
Account.Id accountId = null;
try {
accountId = accounts.parse(TopLevelResource.INSTANCE, id).getUser().getAccountId();
} catch (ResourceNotFoundException e) {
if (address == null) {
throw e;
}
}
Collection<Account.Id> reviewers = approvalsUtil.getReviewers(rsrc.getNotes()).all();
// See if the id exists as a reviewer for this change
if (reviewers.contains(accountId)) {
return resourceFactory.create(rsrc, accountId);
}
// See if the address exists as a reviewer on the change
if (address != null && rsrc.getNotes().getReviewersByEmail().all().contains(address)) {
return new ReviewerResource(rsrc, address);
}
throw new ResourceNotFoundException(id);
}
use of com.google.gerrit.extensions.restapi.MethodNotAllowedException in project gerrit by GerritCodeReview.
the class MergeUtil method createCherryPickFromCommit.
public CodeReviewCommit createCherryPickFromCommit(ObjectInserter inserter, Config repoConfig, RevCommit mergeTip, RevCommit originalCommit, PersonIdent cherryPickCommitterIdent, String commitMsg, CodeReviewRevWalk rw, int parentIndex, boolean ignoreIdenticalTree, boolean allowConflicts) throws IOException, MergeIdenticalTreeException, MergeConflictException, MethodNotAllowedException, InvalidMergeStrategyException {
ThreeWayMerger m = newThreeWayMerger(inserter, repoConfig);
m.setBase(originalCommit.getParent(parentIndex));
DirCache dc = DirCache.newInCore();
if (allowConflicts && m instanceof ResolveMerger) {
// The DirCache must be set on ResolveMerger before calling
// ResolveMerger#merge(AnyObjectId...) otherwise the entries in DirCache don't get populated.
((ResolveMerger) m).setDirCache(dc);
}
ObjectId tree;
ImmutableSet<String> filesWithGitConflicts;
if (m.merge(mergeTip, originalCommit)) {
filesWithGitConflicts = null;
tree = m.getResultTreeId();
if (tree.equals(mergeTip.getTree()) && !ignoreIdenticalTree) {
throw new MergeIdenticalTreeException("identical tree");
}
} else {
if (!allowConflicts) {
throw new MergeConflictException(String.format("merge conflict while merging commits %s and %s", mergeTip.toObjectId(), originalCommit.toObjectId()));
}
if (!useContentMerge) {
// conflict markers.
throw new MethodNotAllowedException("Cherry-pick with allow conflicts requires that content merge is enabled.");
}
// For merging with conflict markers we need a ResolveMerger, double-check that we have one.
checkState(m instanceof ResolveMerger, "allow conflicts is not supported");
Map<String, MergeResult<? extends Sequence>> mergeResults = ((ResolveMerger) m).getMergeResults();
filesWithGitConflicts = mergeResults.entrySet().stream().filter(e -> e.getValue().containsConflicts()).map(Map.Entry::getKey).collect(toImmutableSet());
tree = mergeWithConflicts(rw, inserter, dc, "HEAD", mergeTip, "CHANGE", originalCommit, mergeResults);
}
CommitBuilder cherryPickCommit = new CommitBuilder();
cherryPickCommit.setTreeId(tree);
cherryPickCommit.setParentId(mergeTip);
cherryPickCommit.setAuthor(originalCommit.getAuthorIdent());
cherryPickCommit.setCommitter(cherryPickCommitterIdent);
cherryPickCommit.setMessage(commitMsg);
matchAuthorToCommitterDate(project, cherryPickCommit);
CodeReviewCommit commit = rw.parseCommit(inserter.insert(cherryPickCommit));
commit.setFilesWithGitConflicts(filesWithGitConflicts);
return commit;
}
Aggregations