Search in sources :

Example 41 with PatchSetApproval

use of com.google.gerrit.reviewdb.client.PatchSetApproval in project gerrit by GerritCodeReview.

the class ImpersonationIT method submitOnBehalfOf.

@Test
public void submitOnBehalfOf() throws Exception {
    allowSubmitOnBehalfOf();
    PushOneCommit.Result r = createChange();
    String changeId = project.get() + "~master~" + r.getChangeId();
    gApi.changes().id(changeId).current().review(ReviewInput.approve());
    SubmitInput in = new SubmitInput();
    in.onBehalfOf = admin2.email;
    gApi.changes().id(changeId).current().submit(in);
    ChangeData cd = r.getChange();
    assertThat(cd.change().getStatus()).isEqualTo(Change.Status.MERGED);
    PatchSetApproval submitter = approvalsUtil.getSubmitter(db, cd.notes(), cd.change().currentPatchSetId());
    assertThat(submitter.getAccountId()).isEqualTo(admin2.id);
    assertThat(submitter.getRealAccountId()).isEqualTo(admin.id);
}
Also used : SubmitInput(com.google.gerrit.extensions.api.changes.SubmitInput) ChangeData(com.google.gerrit.server.query.change.ChangeData) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 42 with PatchSetApproval

use of com.google.gerrit.reviewdb.client.PatchSetApproval in project gerrit by GerritCodeReview.

the class MergedByPushOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws OrmException, IOException {
    change = ctx.getChange();
    correctBranch = refName.equals(change.getDest().get());
    if (!correctBranch) {
        return false;
    }
    if (patchSetProvider != null) {
        // Caller might have also arranged for construction of a new patch set
        // that is not present in the old notes so we can't use PatchSetUtil.
        patchSet = patchSetProvider.get();
    } else {
        patchSet = checkNotNull(psUtil.get(ctx.getDb(), ctx.getNotes(), psId), "patch set %s not found", psId);
    }
    info = getPatchSetInfo(ctx);
    ChangeUpdate update = ctx.getUpdate(psId);
    Change.Status status = change.getStatus();
    if (status == Change.Status.MERGED) {
        return true;
    }
    if (status.isOpen()) {
        change.setCurrentPatchSet(info);
        change.setStatus(Change.Status.MERGED);
        // we cannot reconstruct the submit records for when this change was
        // submitted, this is why we must fix the status
        update.fixStatus(Change.Status.MERGED);
        update.setCurrentPatchSet();
    }
    StringBuilder msgBuf = new StringBuilder();
    msgBuf.append("Change has been successfully pushed");
    if (!refName.equals(change.getDest().get())) {
        msgBuf.append(" into ");
        if (refName.startsWith(Constants.R_HEADS)) {
            msgBuf.append("branch ");
            msgBuf.append(Repository.shortenRefName(refName));
        } else {
            msgBuf.append(refName);
        }
    }
    msgBuf.append(".");
    ChangeMessage msg = ChangeMessagesUtil.newMessage(psId, ctx.getUser(), ctx.getWhen(), msgBuf.toString(), ChangeMessagesUtil.TAG_MERGED);
    cmUtil.addChangeMessage(ctx.getDb(), update, msg);
    PatchSetApproval submitter = ApprovalsUtil.newApproval(change.currentPatchSetId(), ctx.getUser(), LabelId.legacySubmit(), 1, ctx.getWhen());
    update.putApproval(submitter.getLabel(), submitter.getValue());
    ctx.getDb().patchSetApprovals().upsert(Collections.singleton(submitter));
    return true;
}
Also used : ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) Change(com.google.gerrit.reviewdb.client.Change) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 43 with PatchSetApproval

use of com.google.gerrit.reviewdb.client.PatchSetApproval in project gerrit by GerritCodeReview.

the class MergeUtil method createDetailedCommitMessage.

/**
   * Adds footers to existing commit message based on the state of the change.
   *
   * <p>This adds the following footers if they are missing:
   *
   * <ul>
   *   <li>Reviewed-on: <i>url</i>
   *   <li>Reviewed-by | Tested-by | <i>Other-Label-Name</i>: <i>reviewer</i>
   *   <li>Change-Id
   * </ul>
   *
   * @param n
   * @param ctl
   * @param psId
   * @return new message
   */
private String createDetailedCommitMessage(RevCommit n, ChangeControl ctl, PatchSet.Id psId) {
    Change c = ctl.getChange();
    final List<FooterLine> footers = n.getFooterLines();
    final StringBuilder msgbuf = new StringBuilder();
    msgbuf.append(n.getFullMessage());
    if (msgbuf.length() == 0) {
        // WTF, an empty commit message?
        msgbuf.append("<no commit message provided>");
    }
    if (msgbuf.charAt(msgbuf.length() - 1) != '\n') {
        // Missing a trailing LF? Correct it (perhaps the editor was broken).
        msgbuf.append('\n');
    }
    if (footers.isEmpty()) {
        // Doesn't end in a "Signed-off-by: ..." style line? Add another line
        // break to start a new paragraph for the reviewed-by tag lines.
        //
        msgbuf.append('\n');
    }
    if (!contains(footers, FooterConstants.CHANGE_ID, c.getKey().get())) {
        msgbuf.append(FooterConstants.CHANGE_ID.getName());
        msgbuf.append(": ");
        msgbuf.append(c.getKey().get());
        msgbuf.append('\n');
    }
    final String siteUrl = urlProvider.get();
    if (siteUrl != null) {
        final String url = siteUrl + c.getId().get();
        if (!contains(footers, FooterConstants.REVIEWED_ON, url)) {
            msgbuf.append(FooterConstants.REVIEWED_ON.getName());
            msgbuf.append(": ");
            msgbuf.append(url);
            msgbuf.append('\n');
        }
    }
    PatchSetApproval submitAudit = null;
    for (final PatchSetApproval a : safeGetApprovals(ctl, psId)) {
        if (a.getValue() <= 0) {
            // Negative votes aren't counted.
            continue;
        }
        if (a.isLegacySubmit()) {
            //
            if (submitAudit == null || a.getGranted().compareTo(submitAudit.getGranted()) > 0) {
                submitAudit = a;
            }
            continue;
        }
        final Account acc = identifiedUserFactory.create(a.getAccountId()).getAccount();
        final StringBuilder identbuf = new StringBuilder();
        if (acc.getFullName() != null && acc.getFullName().length() > 0) {
            if (identbuf.length() > 0) {
                identbuf.append(' ');
            }
            identbuf.append(acc.getFullName());
        }
        if (acc.getPreferredEmail() != null && acc.getPreferredEmail().length() > 0) {
            if (isSignedOffBy(footers, acc.getPreferredEmail())) {
                continue;
            }
            if (identbuf.length() > 0) {
                identbuf.append(' ');
            }
            identbuf.append('<');
            identbuf.append(acc.getPreferredEmail());
            identbuf.append('>');
        }
        if (identbuf.length() == 0) {
            // Nothing reasonable to describe them by? Ignore them.
            continue;
        }
        final String tag;
        if (isCodeReview(a.getLabelId())) {
            tag = "Reviewed-by";
        } else if (isVerified(a.getLabelId())) {
            tag = "Tested-by";
        } else {
            final LabelType lt = project.getLabelTypes().byLabel(a.getLabelId());
            if (lt == null) {
                continue;
            }
            tag = lt.getName();
        }
        if (!contains(footers, new FooterKey(tag), identbuf.toString())) {
            msgbuf.append(tag);
            msgbuf.append(": ");
            msgbuf.append(identbuf);
            msgbuf.append('\n');
        }
    }
    return msgbuf.toString();
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) FooterLine(org.eclipse.jgit.revwalk.FooterLine) FooterKey(org.eclipse.jgit.revwalk.FooterKey) LabelType(com.google.gerrit.common.data.LabelType) Change(com.google.gerrit.reviewdb.client.Change) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval)

Example 44 with PatchSetApproval

use of com.google.gerrit.reviewdb.client.PatchSetApproval in project gerrit by GerritCodeReview.

the class EqualsLabelPredicate method match.

@Override
public boolean match(ChangeData object) throws OrmException {
    Change c = object.change();
    if (c == null) {
        //
        return false;
    }
    ProjectState project = projectCache.get(c.getDest().getParentKey());
    if (project == null) {
        //
        return false;
    }
    LabelType labelType = type(project.getLabelTypes(), label);
    if (labelType == null) {
        // Label is not defined by this project.
        return false;
    }
    boolean hasVote = false;
    for (PatchSetApproval p : object.currentApprovals()) {
        if (labelType.matches(p)) {
            hasVote = true;
            if (match(object, p.getValue(), p.getAccountId(), labelType)) {
                return true;
            }
        }
    }
    if (!hasVote && expVal == 0) {
        return true;
    }
    return false;
}
Also used : LabelType(com.google.gerrit.common.data.LabelType) ProjectState(com.google.gerrit.server.project.ProjectState) Change(com.google.gerrit.reviewdb.client.Change) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval)

Example 45 with PatchSetApproval

use of com.google.gerrit.reviewdb.client.PatchSetApproval in project gerrit by GerritCodeReview.

the class ChangeJson method labelsForClosedChange.

private Map<String, LabelWithStatus> labelsForClosedChange(PermissionBackend.ForChange basePerm, ChangeData cd, LabelTypes labelTypes, boolean standard, boolean detailed) throws OrmException, PermissionBackendException {
    Set<Account.Id> allUsers = new HashSet<>();
    if (detailed) {
        // the latest patch set (in the next loop).
        for (PatchSetApproval psa : cd.approvals().values()) {
            allUsers.add(psa.getAccountId());
        }
    }
    Set<String> labelNames = new HashSet<>();
    SetMultimap<Account.Id, PatchSetApproval> current = MultimapBuilder.hashKeys().hashSetValues().build();
    for (PatchSetApproval a : cd.currentApprovals()) {
        allUsers.add(a.getAccountId());
        LabelType type = labelTypes.byLabel(a.getLabelId());
        if (type != null) {
            labelNames.add(type.getName());
            // Not worth the effort to distinguish between votable/non-votable for 0
            // values on closed changes, since they can't vote anyway.
            current.put(a.getAccountId(), a);
        }
    }
    Map<String, LabelWithStatus> labels;
    if (cd.change().getStatus() == Change.Status.MERGED) {
        // Since voting on merged changes is allowed all labels which apply to
        // the change must be returned. All applying labels can be retrieved from
        // the submit records, which is what initLabels does.
        // It's not possible to only compute the labels based on the approvals
        // since merged changes may not have approvals for all labels (e.g. if not
        // all labels are required for submit or if the change was auto-closed due
        // to direct push or if new labels were defined after the change was
        // merged).
        labels = initLabels(cd, labelTypes, standard);
        // it wouldn't be included in the submit records.
        for (String name : labelNames) {
            if (!labels.containsKey(name)) {
                labels.put(name, LabelWithStatus.create(new LabelInfo(), null));
            }
        }
    } else {
        // For abandoned changes return only labels for which approvals exist.
        // Other labels are not needed since voting on abandoned changes is not
        // allowed.
        labels = new TreeMap<>(labelTypes.nameComparator());
        for (String name : labelNames) {
            labels.put(name, LabelWithStatus.create(new LabelInfo(), null));
        }
    }
    if (detailed) {
        labels.entrySet().stream().filter(e -> labelTypes.byLabel(e.getKey()) != null).forEach(e -> setLabelValues(labelTypes.byLabel(e.getKey()), e.getValue()));
    }
    for (Account.Id accountId : allUsers) {
        Map<String, ApprovalInfo> byLabel = Maps.newHashMapWithExpectedSize(labels.size());
        Map<String, VotingRangeInfo> pvr = Collections.emptyMap();
        if (detailed) {
            PermissionBackend.ForChange perm = basePerm.user(userFactory.create(accountId));
            pvr = getPermittedVotingRanges(permittedLabels(perm, cd));
            for (Map.Entry<String, LabelWithStatus> entry : labels.entrySet()) {
                ApprovalInfo ai = approvalInfo(accountId, 0, null, null, null);
                byLabel.put(entry.getKey(), ai);
                addApproval(entry.getValue().label(), ai);
            }
        }
        for (PatchSetApproval psa : current.get(accountId)) {
            LabelType type = labelTypes.byLabel(psa.getLabelId());
            if (type == null) {
                continue;
            }
            short val = psa.getValue();
            ApprovalInfo info = byLabel.get(type.getName());
            if (info != null) {
                info.value = Integer.valueOf(val);
                info.permittedVotingRange = pvr.getOrDefault(type.getName(), null);
                info.date = psa.getGranted();
                info.tag = psa.getTag();
                if (psa.isPostSubmit()) {
                    info.postSubmit = true;
                }
            }
            if (!standard) {
                continue;
            }
            setLabelScores(type, labels.get(type.getName()), val, accountId);
        }
    }
    return labels;
}
Also used : REVIEWER_UPDATES(com.google.gerrit.extensions.client.ListChangesOption.REVIEWER_UPDATES) PushCertificateInfo(com.google.gerrit.extensions.common.PushCertificateInfo) LabelInfo(com.google.gerrit.extensions.common.LabelInfo) ProjectCache(com.google.gerrit.server.project.ProjectCache) MultimapBuilder(com.google.common.collect.MultimapBuilder) HashBasedTable(com.google.common.collect.HashBasedTable) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MergeUtil(com.google.gerrit.server.git.MergeUtil) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) WEB_LINKS(com.google.gerrit.extensions.client.ListChangesOption.WEB_LINKS) AnonymousUser(com.google.gerrit.server.AnonymousUser) LABELS(com.google.gerrit.extensions.client.ListChangesOption.LABELS) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) ApprovalInfo(com.google.gerrit.extensions.common.ApprovalInfo) DownloadScheme(com.google.gerrit.extensions.config.DownloadScheme) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) Set(java.util.Set) ReviewerStatusUpdate(com.google.gerrit.server.ReviewerStatusUpdate) SUBMITTABLE(com.google.gerrit.extensions.client.ListChangesOption.SUBMITTABLE) MESSAGES(com.google.gerrit.extensions.client.ListChangesOption.MESSAGES) AutoValue(com.google.auto.value.AutoValue) DETAILED_ACCOUNTS(com.google.gerrit.extensions.client.ListChangesOption.DETAILED_ACCOUNTS) Joiner(com.google.common.base.Joiner) ChangeMessagesUtil(com.google.gerrit.server.ChangeMessagesUtil) Singleton(com.google.inject.Singleton) Iterables(com.google.common.collect.Iterables) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Change(com.google.gerrit.reviewdb.client.Change) DOWNLOAD_COMMANDS(com.google.gerrit.extensions.client.ListChangesOption.DOWNLOAD_COMMANDS) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) CURRENT_COMMIT(com.google.gerrit.extensions.client.ListChangesOption.CURRENT_COMMIT) Lists(com.google.common.collect.Lists) Project(com.google.gerrit.reviewdb.client.Project) CURRENT_ACTIONS(com.google.gerrit.extensions.client.ListChangesOption.CURRENT_ACTIONS) DownloadCommand(com.google.gerrit.extensions.config.DownloadCommand) MoreObjects(com.google.common.base.MoreObjects) Throwables(com.google.common.base.Throwables) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) ReviewerUpdateInfo(com.google.gerrit.extensions.common.ReviewerUpdateInfo) SubmitRuleOptions(com.google.gerrit.server.project.SubmitRuleOptions) WebLinks(com.google.gerrit.server.WebLinks) VotingRangeInfo(com.google.gerrit.extensions.common.VotingRangeInfo) SubmitTypeRecord(com.google.gerrit.common.data.SubmitTypeRecord) WebLinkInfo(com.google.gerrit.extensions.common.WebLinkInfo) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) TreeMap(java.util.TreeMap) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ChangeIndexCollection(com.google.gerrit.server.index.change.ChangeIndexCollection) Repository(org.eclipse.jgit.lib.Repository) ChangeControl(com.google.gerrit.server.project.ChangeControl) OrmException(com.google.gwtorm.server.OrmException) ALL_REVISIONS(com.google.gerrit.extensions.client.ListChangesOption.ALL_REVISIONS) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) RevisionInfo(com.google.gerrit.extensions.common.RevisionInfo) PUSH_CERTIFICATES(com.google.gerrit.extensions.client.ListChangesOption.PUSH_CERTIFICATES) Assisted(com.google.inject.assistedinject.Assisted) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) GpgException(com.google.gerrit.server.GpgException) REVIEWED(com.google.gerrit.extensions.client.ListChangesOption.REVIEWED) CHECK(com.google.gerrit.extensions.client.ListChangesOption.CHECK) ChangedLines(com.google.gerrit.server.query.change.ChangeData.ChangedLines) ALL_FILES(com.google.gerrit.extensions.client.ListChangesOption.ALL_FILES) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) DETAILED_LABELS(com.google.gerrit.extensions.client.ListChangesOption.DETAILED_LABELS) CommitInfo(com.google.gerrit.extensions.common.CommitInfo) LabelType(com.google.gerrit.common.data.LabelType) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) FetchInfo(com.google.gerrit.extensions.common.FetchInfo) LabelTypes(com.google.gerrit.common.data.LabelTypes) Ref(org.eclipse.jgit.lib.Ref) Url(com.google.gerrit.extensions.restapi.Url) Optional(java.util.Optional) LabelValue(com.google.gerrit.common.data.LabelValue) LabelPermission(com.google.gerrit.server.permissions.LabelPermission) GpgApiAdapter(com.google.gerrit.server.api.accounts.GpgApiAdapter) PatchListNotAvailableException(com.google.gerrit.server.patch.PatchListNotAvailableException) AccountLoader(com.google.gerrit.server.account.AccountLoader) FixInput(com.google.gerrit.extensions.api.changes.FixInput) PluginDefinedAttributesFactory(com.google.gerrit.server.query.change.PluginDefinedAttributesFactory) QueryResult(com.google.gerrit.server.query.QueryResult) ReviewerState(com.google.gerrit.extensions.client.ReviewerState) Patch(com.google.gerrit.reviewdb.client.Patch) HashMap(java.util.HashMap) CommonConverters.toGitPerson(com.google.gerrit.server.CommonConverters.toGitPerson) SubmitRecord(com.google.gerrit.common.data.SubmitRecord) HashSet(java.util.HashSet) Account(com.google.gerrit.reviewdb.client.Account) CURRENT_REVISION(com.google.gerrit.extensions.client.ListChangesOption.CURRENT_REVISION) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ListChangesOption(com.google.gerrit.extensions.client.ListChangesOption) ProblemInfo(com.google.gerrit.extensions.common.ProblemInfo) CurrentUser(com.google.gerrit.server.CurrentUser) Logger(org.slf4j.Logger) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) ChangeMessageInfo(com.google.gerrit.extensions.common.ChangeMessageInfo) CHANGE_ACTIONS(com.google.gerrit.extensions.client.ListChangesOption.CHANGE_ACTIONS) Maps(com.google.common.collect.Maps) Ints(com.google.common.primitives.Ints) SetMultimap(com.google.common.collect.SetMultimap) ApprovalsUtil(com.google.gerrit.server.ApprovalsUtil) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeField(com.google.gerrit.server.index.change.ChangeField) Collectors.toList(java.util.stream.Collectors.toList) Address(com.google.gerrit.server.mail.Address) Provider(com.google.inject.Provider) CURRENT_FILES(com.google.gerrit.extensions.client.ListChangesOption.CURRENT_FILES) AccountInfoComparator(com.google.gerrit.server.api.accounts.AccountInfoComparator) COMMIT_FOOTERS(com.google.gerrit.extensions.client.ListChangesOption.COMMIT_FOOTERS) ALL_COMMITS(com.google.gerrit.extensions.client.ListChangesOption.ALL_COMMITS) Table(com.google.common.collect.Table) Collections(java.util.Collections) StarredChangesUtil(com.google.gerrit.server.StarredChangesUtil) Account(com.google.gerrit.reviewdb.client.Account) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) VotingRangeInfo(com.google.gerrit.extensions.common.VotingRangeInfo) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) LabelInfo(com.google.gerrit.extensions.common.LabelInfo) ApprovalInfo(com.google.gerrit.extensions.common.ApprovalInfo) LabelType(com.google.gerrit.common.data.LabelType) ObjectId(org.eclipse.jgit.lib.ObjectId) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)71 Change (com.google.gerrit.reviewdb.client.Change)37 Test (org.junit.Test)32 Account (com.google.gerrit.reviewdb.client.Account)17 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)17 LabelType (com.google.gerrit.common.data.LabelType)14 LabelId (com.google.gerrit.reviewdb.client.LabelId)13 ChangeMessage (com.google.gerrit.reviewdb.client.ChangeMessage)10 ChangeData (com.google.gerrit.server.query.change.ChangeData)10 ObjectId (org.eclipse.jgit.lib.ObjectId)10 RevId (com.google.gerrit.reviewdb.client.RevId)8 Timestamp (java.sql.Timestamp)8 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)7 HashMap (java.util.HashMap)7 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)6 LabelTypes (com.google.gerrit.common.data.LabelTypes)6 ArrayList (java.util.ArrayList)6 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 RequestId (com.google.gerrit.server.util.RequestId)5