|
| 1 | +// |
| 2 | +// Copyright (C) 2021 The Qt Company |
| 3 | +// |
| 4 | + |
| 5 | +package com.googlesource.gerrit.plugins.qtcodereview; |
| 6 | + |
| 7 | +import com.google.common.base.MoreObjects; |
| 8 | +import com.google.common.base.Strings; |
| 9 | +import com.google.common.collect.ImmutableMap; |
| 10 | +import com.google.common.flogger.FluentLogger; |
| 11 | +import com.google.gerrit.common.data.ParameterizedString; |
| 12 | +import com.google.gerrit.extensions.api.changes.SubmitInput; |
| 13 | +import com.google.gerrit.extensions.restapi.RestApiException; |
| 14 | +import com.google.gerrit.extensions.restapi.RestModifyView; |
| 15 | +import com.google.gerrit.extensions.restapi.AuthException; |
| 16 | +import com.google.gerrit.extensions.restapi.Response; |
| 17 | +import com.google.gerrit.extensions.webui.UiAction; |
| 18 | +import com.google.gerrit.entities.Change; |
| 19 | +import com.google.gerrit.server.change.RevisionResource; |
| 20 | +import com.google.gerrit.server.config.GerritServerConfig; |
| 21 | +import com.google.gerrit.server.permissions.LabelPermission; |
| 22 | +import com.google.gerrit.server.permissions.PermissionBackend; |
| 23 | +import com.google.gerrit.server.permissions.PermissionBackendException; |
| 24 | +import com.google.gerrit.server.permissions.LabelPermission.ForUser; |
| 25 | +import com.google.gerrit.server.query.change.ChangeData; |
| 26 | +import com.google.gerrit.server.update.UpdateException; |
| 27 | +import com.google.inject.Inject; |
| 28 | +import com.google.inject.Singleton; |
| 29 | +import org.eclipse.jgit.lib.Config; |
| 30 | +import org.eclipse.jgit.lib.ObjectId; |
| 31 | +import org.eclipse.jgit.errors.RepositoryNotFoundException; |
| 32 | +import org.eclipse.jgit.errors.ConfigInvalidException; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +@Singleton |
| 37 | +public class QtPreCheck |
| 38 | + implements RestModifyView<RevisionResource, SubmitInput>, UiAction<RevisionResource> { |
| 39 | + |
| 40 | + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); |
| 41 | + private static final String DEFAULT_TOOLTIP = "Trigger a precheck integration"; |
| 42 | + private static final String LABEL_CODE_REVIEW = "Code-Review"; |
| 43 | + private static final short LABEL_CODE_REVIEW_VALUE = 2; |
| 44 | + |
| 45 | + public static class Output { |
| 46 | + transient Change change; |
| 47 | + |
| 48 | + private Output(Change c) { |
| 49 | + change = c; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private final PermissionBackend permissionBackend; |
| 54 | + private final ChangeData.Factory changeDataFactory; |
| 55 | + private final QtUtil qtUtil; |
| 56 | + private final String label; |
| 57 | + private final ParameterizedString titlePattern; |
| 58 | + |
| 59 | + private Change change; |
| 60 | + |
| 61 | + @Inject |
| 62 | + QtPreCheck( |
| 63 | + PermissionBackend permissionBackend, |
| 64 | + @GerritServerConfig Config cfg, |
| 65 | + ChangeData.Factory changeDataFactory, |
| 66 | + QtUtil qtUtil) { |
| 67 | + |
| 68 | + this.permissionBackend = permissionBackend; |
| 69 | + this.changeDataFactory = changeDataFactory; |
| 70 | + this.qtUtil = qtUtil; |
| 71 | + this.label = "PreCheck"; |
| 72 | + this.titlePattern = |
| 73 | + new ParameterizedString( |
| 74 | + MoreObjects.firstNonNull( |
| 75 | + cfg.getString("precheck", null, "precheckTooltip"), DEFAULT_TOOLTIP)); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Response<Output> apply(RevisionResource rsrc, SubmitInput input) |
| 80 | + throws RestApiException, RepositoryNotFoundException, IOException, PermissionBackendException, |
| 81 | + UpdateException, ConfigInvalidException { |
| 82 | + logger.atInfo().log("qtcodereview: precheck request for %s", rsrc.getChange().toString()); |
| 83 | + boolean canReview; |
| 84 | + |
| 85 | + canReview = rsrc.permissions().test(new LabelPermission. |
| 86 | + WithValue(ForUser.SELF, LABEL_CODE_REVIEW, LABEL_CODE_REVIEW_VALUE)); |
| 87 | + |
| 88 | + if (!canReview) { |
| 89 | + throw new AuthException(String.format("Precheck request from user %s without permission, %s", |
| 90 | + rsrc.getUser().getUserName(), rsrc.getChange().toString())); |
| 91 | + } |
| 92 | + |
| 93 | + Change change = rsrc.getChange(); |
| 94 | + Output output; |
| 95 | + output = new Output(change); |
| 96 | + this.qtUtil.postChangePreCheckEvent(change, rsrc.getPatchSet()); |
| 97 | + return Response.ok(output); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public UiAction.Description getDescription(RevisionResource resource) { |
| 102 | + boolean canReview; |
| 103 | + try { |
| 104 | + canReview = resource.permissions().test(new LabelPermission |
| 105 | + .WithValue(ForUser.SELF, LABEL_CODE_REVIEW, LABEL_CODE_REVIEW_VALUE)); |
| 106 | + } catch (PermissionBackendException e) { |
| 107 | + logger.atInfo().log(e.getMessage()); |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + if (!canReview) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + Change change = resource.getChange(); |
| 116 | + if (!change.getStatus().isOpen() |
| 117 | + || !resource.isCurrent()) { |
| 118 | + return null; // precheck not visible |
| 119 | + } |
| 120 | + |
| 121 | + ObjectId revId = resource.getPatchSet().commitId(); |
| 122 | + Map<String, String> params = |
| 123 | + ImmutableMap.of( |
| 124 | + "patchSet", String.valueOf(resource.getPatchSet().number()), |
| 125 | + "branch", change.getDest().shortName(), |
| 126 | + "commit", revId.abbreviate(7).name()); |
| 127 | + return new UiAction.Description() |
| 128 | + .setLabel(this.label) |
| 129 | + .setTitle(Strings.emptyToNull(titlePattern.replace(params))) |
| 130 | + .setVisible(true) |
| 131 | + .setEnabled(true); |
| 132 | + } |
| 133 | +} |
0 commit comments