monitorjbl / pr-harmony

Extra pull request workflows for Stash
GNU General Public License v3.0
33 stars 17 forks source link

Basic port to bitbucket 6.0 APIs #77

Open mkoegler opened 5 years ago

mkoegler commented 5 years ago
diff --git a/pom.xml b/pom.xml
index 65f5713..80d14c1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.monitorjbl.plugins</groupId>
   <artifactId>pr-harmony</artifactId>
-  <version>2.5.0</version>
+  <version>2.6.0</version>
   <packaging>atlassian-plugin</packaging>

   <name>PR Harmony</name>
@@ -33,7 +33,7 @@
       <groupId>com.atlassian.bitbucket.server</groupId>
       <artifactId>bitbucket-api</artifactId>
       <scope>provided</scope>
-      <version>4.11.1</version>
+      <version>6.0.0</version>
     </dependency>
     <dependency>
       <groupId>com.atlassian.bitbucket.server</groupId>
@@ -205,8 +205,8 @@
   </profiles>

   <properties>
-    <bitbucket.version>5.0.1</bitbucket.version>
-    <bitbucket.data.version>5.0.1</bitbucket.data.version>
+    <bitbucket.version>6.0.1</bitbucket.version>
+    <bitbucket.data.version>6.0.1</bitbucket.data.version>
     <amps.version>6.3.0</amps.version>
     <plugin.testrunner.version>1.2.3</plugin.testrunner.version>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
diff --git a/src/main/java/com/monitorjbl/plugins/CommitBlockerHook.java b/src/main/java/com/monitorjbl/plugins/CommitBlockerHook.java
index 133e97f..6d70573 100644
--- a/src/main/java/com/monitorjbl/plugins/CommitBlockerHook.java
+++ b/src/main/java/com/monitorjbl/plugins/CommitBlockerHook.java
@@ -1,9 +1,12 @@
 package com.monitorjbl.plugins;

+import com.atlassian.bitbucket.hook.repository.PreRepositoryHook;
+import com.atlassian.bitbucket.hook.repository.PreRepositoryHookContext;
+import com.atlassian.bitbucket.hook.repository.RepositoryHook;
+import com.atlassian.bitbucket.hook.repository.RepositoryHookRequest;
+import com.atlassian.bitbucket.hook.repository.RepositoryHookResult;
 import com.atlassian.sal.api.user.UserManager;
 import com.atlassian.sal.api.user.UserProfile;
-import com.atlassian.bitbucket.hook.HookResponse;
-import com.atlassian.bitbucket.hook.PreReceiveHook;
 import com.atlassian.bitbucket.repository.RefChange;
 import com.atlassian.bitbucket.repository.Repository;
 import com.monitorjbl.plugins.config.Config;
@@ -12,10 +15,12 @@ import com.monitorjbl.plugins.config.ConfigDao;
 import java.util.Collection;
 import java.util.Set;

+import javax.annotation.Nonnull;
+
 import static com.google.common.collect.Iterables.concat;
 import static com.google.common.collect.Sets.newHashSet;

-public class CommitBlockerHook implements PreReceiveHook {
+public class CommitBlockerHook implements PreRepositoryHook<RepositoryHookRequest> {
   private final ConfigDao configDao;
   private final UserManager userManager;
   private final RegexUtils regexUtils;
@@ -28,26 +33,28 @@ public class CommitBlockerHook implements PreReceiveHook {
     this.userUtils = userUtils;
   }

+
+  @Nonnull
   @Override
-  public boolean onReceive(Repository repository, Collection<RefChange> collection, HookResponse hookResponse) {
-    Config config = configDao.getConfigForRepo(repository.getProject().getKey(), repository.getSlug());
+  public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext context,
+                   @Nonnull RepositoryHookRequest request) {
+    Config config = configDao.getConfigForRepo(request.getRepository().getProject().getKey(), request.getRepository().getSlug());

     UserProfile user = userManager.getRemoteUser();
-    for(RefChange ch : collection) {
+    for(RefChange ch : request.getRefChanges()) {
       String branch = regexUtils.formatBranchName(ch.getRef().getId());
       Set<String> excluded = newHashSet(concat(config.getExcludedUsers(), userUtils.dereferenceGroups(config.getExcludedGroups())));
       if(regexUtils.match(config.getBlockedCommits(), branch) && !excluded.contains(user.getUsername())) {
-        hookResponse.err().write("\n" +
+     return RepositoryHookResult.rejected("reject commit", "\n" +
                 "******************************\n" +
                 "*    !! Commit Rejected !!   *\n" +
                 "******************************\n\n" +
                 "Direct commits are not allowed\n" +
                 "to branch [" + branch + "].\n\n"
         );
-        return false;
       }
     }
-    return true;
+    return RepositoryHookResult.accepted();
   }

diff --git a/src/main/java/com/monitorjbl/plugins/MergeBlocker.java b/src/main/java/com/monitorjbl/plugins/MergeBlocker.java
index 332867b..7ed9d8d 100644
--- a/src/main/java/com/monitorjbl/plugins/MergeBlocker.java
+++ b/src/main/java/com/monitorjbl/plugins/MergeBlocker.java
@@ -1,11 +1,14 @@
 package com.monitorjbl.plugins;

+import com.atlassian.bitbucket.hook.repository.PreRepositoryHookContext;
+import com.atlassian.bitbucket.hook.repository.PullRequestMergeHookRequest;
+import com.atlassian.bitbucket.hook.repository.RepositoryHookResult;
+import com.atlassian.bitbucket.hook.repository.RepositoryMergeCheck;
 import com.atlassian.bitbucket.pull.PullRequest;
 import com.atlassian.bitbucket.pull.PullRequestParticipant;
 import com.atlassian.bitbucket.pull.PullRequestParticipantStatus;
 import com.atlassian.bitbucket.repository.Repository;
 import com.atlassian.bitbucket.scm.pull.MergeRequest;
-import com.atlassian.bitbucket.scm.pull.MergeRequestCheck;
 import com.google.common.base.Joiner;
 import com.monitorjbl.plugins.config.Config;
 import com.monitorjbl.plugins.config.ConfigDao;
@@ -13,7 +16,7 @@ import com.monitorjbl.plugins.config.ConfigDao;
 import javax.annotation.Nonnull;
 import java.util.Set;

-public class MergeBlocker implements MergeRequestCheck {
+public class MergeBlocker implements RepositoryMergeCheck {
   private final ConfigDao configDao;
   private final UserUtils userUtils;
   private final RegexUtils regexUtils;
@@ -24,30 +27,33 @@ public class MergeBlocker implements MergeRequestCheck {
     this.regexUtils = regexUtils;
   }

+  @Nonnull
   @Override
-  public void check(@Nonnull MergeRequest mergeRequest) {
-    PullRequest pr = mergeRequest.getPullRequest();
+  public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext context,
+                                        @Nonnull PullRequestMergeHookRequest request) {
+    PullRequest pr = request.getPullRequest();
     Repository repo = pr.getToRef().getRepository();
     final Config config = configDao.getConfigForRepo(repo.getProject().getKey(), repo.getSlug());

     String branch = regexUtils.formatBranchName(pr.getToRef().getId());
     if (regexUtils.match(config.getBlockedPRs(), branch)) {
-      mergeRequest.veto("Pull Request Blocked", "Pull requests have been disabled for branch [" + branch + "]");
+      return RepositoryHookResult.rejected("Pull Request Blocked", "Pull requests have been disabled for branch [" + branch + "]");
     } else {
       PullRequestApproval approval = new PullRequestApproval(config, userUtils);
       if (!approval.isPullRequestApproved(pr)) {
         Set<String> missing = approval.missingRevieiwersNames(pr);
-        mergeRequest.veto("Required reviewers must approve", (config.getRequiredReviews() - approval.seenReviewers(pr).size()) +
+        return RepositoryHookResult.rejected("Required reviewers must approve", (config.getRequiredReviews() - approval.seenReviewers(pr).size()) +
             " more approvals required from the following users: " + Joiner.on(", ").join(missing));
       } else {
         Boolean needsWork = config.getBlockMergeIfPrNeedsWork();
         final Boolean blockAutoMergeBecausePrNeedsWork = needsWork != null && needsWork && needsWork(pr);

         if (blockAutoMergeBecausePrNeedsWork) {
-          mergeRequest.veto("Needs work", "PR marked as Needs Work from reviewer(s)");
+          return RepositoryHookResult.rejected("Needs work", "PR marked as Needs Work from reviewer(s)");
         }
       }
     }
+    return RepositoryHookResult.accepted();
   }

   private boolean needsWork(final PullRequest pr) {
diff --git a/src/main/resources/atlassian-plugin.xml b/src/main/resources/atlassian-plugin.xml
index 53ace26..523f85b 100644
--- a/src/main/resources/atlassian-plugin.xml
+++ b/src/main/resources/atlassian-plugin.xml
@@ -39,10 +39,23 @@
     <resource type="download" name="pluginIcon.png" location="images/pluginIcon.png"/>
   </web-resource>

-  <pre-receive-hook key="commitBlocker" name="Commit Blocker" class="com.monitorjbl.plugins.CommitBlockerHook">
+  <repository-hook key="commitBlocker" name="Commit Blocker"
+                 class="com.monitorjbl.plugins.CommitBlockerHook">
     <description>Allows blocking of all direct commits</description>
-  </pre-receive-hook>
-  <merge-check key="mergeCheck" class="bean:mergeBlocker"/>
+    <icon>images/pluginIcon.png</icon>
+    <scopes>
+        <scope>project</scope>
+        <scope>repository</scope>
+    </scopes>
+  </repository-hook>
+  <repository-merge-check key="mergeCheck" name="merge Check" class="bean:mergeBlocker">
+    <description>PR Harmony merge check</description>
+    <icon>images/pluginIcon.png</icon>
+    <scopes>
+        <scope>project</scope>
+        <scope>repository</scope>
+    </scopes>
+  </repository-merge-check>

   <servlet key="pr-harmony-servlet" class="com.monitorjbl.plugins.config.ConfigServlet">
     <url-pattern>/pr-harmony/*</url-pattern>
@@ -59,12 +72,14 @@
     <dependency>com.atlassian.auiplugin:aui-flag</dependency>
   </web-resource>

+  <!--
   <web-resource key="bitbucket-pr-refresh-resources" name="PR Refresh Hook">
     <context>bitbucket.page.pullRequest.view</context>
     <resource type="download" name="pr-refresh-hook.js" location="pr-refresh-hook.js"/>
     <dependency>com.atlassian.bitbucket.server.bitbucket-web:global</dependency>
     <dependency>com.atlassian.auiplugin:aui-flag</dependency>
   </web-resource>
+-->

   <web-item key="pr-harmony-repo-config" weight="30" section="bitbucket.repository.settings.panel/repository-settings-addons-section">
     <label>PR Harmony</label>
RSEllip commented 5 years ago

@monitorjbl, what's the current status and plans for Bitbucket 6+ compatibility?