Skip to content

The Unified and Autonomous Development Platform

Add/remove branch in database while adding/removing via git push.

robin shinebdf024855746
diff --git a/commons.git/src/main/java/com/pmease/commons/git/Commit.java b/commons.git/src/main/java/com/pmease/commons/git/Commit.java
index 089e048290..1cff3a5e61 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/Commit.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/Commit.java
@@ -49,5 +49,5 @@ public class Commit extends BriefCommit {
public List<FileChange> getFileChanges() {
return Collections.unmodifiableList(fileChanges);
}
-
+
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfTouchesSpecifiedDirectories.java b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfTouchesSpecifiedDirectories.java
index 0b4e0a60d6..b0ba1a2b5a 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfTouchesSpecifiedDirectories.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfTouchesSpecifiedDirectories.java
@@ -8,6 +8,7 @@ import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.pmease.commons.editable.annotation.Editable;
+import com.pmease.commons.git.Commit;
import com.pmease.commons.util.pattern.WildcardUtils;
import com.pmease.gitop.core.editable.DirectoryChoice;
import com.pmease.gitop.model.PullRequest;
@@ -38,9 +39,19 @@ public class IfTouchesSpecifiedDirectories extends FileGateKeeper {
public CheckResult doCheck(PullRequest request) {
for (int i=0; i<request.getEffectiveUpdates().size(); i++) {
PullRequestUpdate update = request.getEffectiveUpdates().get(i);
- Collection<String> touchedFiles = request.getTarget().getProject().code().listChangedFiles(
- update.getBaseCommit(), update.getHeadCommit());
-
+
+ Collection<String> touchedFiles;
+ if (!update.getHeadCommit().startsWith(Commit.ZERO_HASH)) {
+ touchedFiles = request.getTarget().getProject().code().listChangedFiles(
+ update.getBaseCommit(), update.getHeadCommit());
+ } else {
+ touchedFiles = new ArrayList<>();
+ String path = update.getHeadCommit().substring(Commit.ZERO_HASH.length());
+ if (path.length() != 0) // test if a certain file can be touched
+ touchedFiles.add(path);
+ else // test if the branch can be deleted
+ return accepted("Touched directory '" + directories.get(0) + "'.");
+ }
for (String file: touchedFiles) {
for (String each: directories) {
if (WildcardUtils.matchPath(each + "/**", file)) {
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfTouchesSpecifiedFilePatterns.java b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfTouchesSpecifiedFilePatterns.java
index 5fd9f16eee..2bc4e241ef 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfTouchesSpecifiedFilePatterns.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfTouchesSpecifiedFilePatterns.java
@@ -1,10 +1,12 @@
package com.pmease.gitop.core.gatekeeper;
+import java.util.ArrayList;
import java.util.Collection;
import org.hibernate.validator.constraints.NotEmpty;
import com.pmease.commons.editable.annotation.Editable;
+import com.pmease.commons.git.Commit;
import com.pmease.commons.util.pattern.WildcardUtils;
import com.pmease.gitop.model.PullRequest;
import com.pmease.gitop.model.PullRequestUpdate;
@@ -39,8 +41,19 @@ public class IfTouchesSpecifiedFilePatterns extends FileGateKeeper {
public CheckResult doCheck(PullRequest request) {
for (int i=0; i<request.getEffectiveUpdates().size(); i++) {
PullRequestUpdate update = request.getEffectiveUpdates().get(i);
- Collection<String> touchedFiles = request.getTarget().getProject().code().listChangedFiles(
- update.getBaseCommit(), update.getHeadCommit());
+
+ Collection<String> touchedFiles;
+ if (!update.getHeadCommit().startsWith(Commit.ZERO_HASH)) {
+ touchedFiles = request.getTarget().getProject().code().listChangedFiles(
+ update.getBaseCommit(), update.getHeadCommit());
+ } else {
+ touchedFiles = new ArrayList<>();
+ String path = update.getHeadCommit().substring(Commit.ZERO_HASH.length());
+ if (path.length() != 0) // test if a certain file can be touched
+ touchedFiles.add(path);
+ else // test if the branch can be deleted
+ return accepted("Touched files match pattern '" + getFilePatterns() + "'.");
+ }
for (String file: touchedFiles) {
if (WildcardUtils.matchPath(getFilePatterns(), file)) {
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfVerifiedByBuilds.java b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfVerifiedByBuilds.java
index cd32025753..3aba8c581b 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfVerifiedByBuilds.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/IfVerifiedByBuilds.java
@@ -6,6 +6,7 @@ import javax.validation.constraints.Min;
import com.google.common.base.Preconditions;
import com.pmease.commons.editable.annotation.Editable;
+import com.pmease.commons.git.Commit;
import com.pmease.gitop.core.Gitop;
import com.pmease.gitop.core.manager.BuildResultManager;
import com.pmease.gitop.model.BuildResult;
@@ -58,7 +59,7 @@ public class IfVerifiedByBuilds extends CommonGateKeeper {
}
public CheckResult doCheck(PullRequest request) {
- BuildResultManager verificationManager = Gitop.getInstance(BuildResultManager.class);
+ BuildResultManager BuildResultManager = Gitop.getInstance(BuildResultManager.class);
Preconditions.checkNotNull(request.getMergePrediction());
@@ -70,32 +71,41 @@ public class IfVerifiedByBuilds extends CommonGateKeeper {
} else {
commit = request.getLatestUpdate().getHeadCommit();
}
- Collection<BuildResult> verifications = verificationManager.findBy(commit);
- for (BuildResult each: verifications) {
- if (!each.isPassed())
- return rejected("At least one build is failed for the merged commit.");
- }
- int lacks = buildCount - verifications.size();
-
- String prefix;
- if (request.getId() == null)
- prefix = "Not ";
- else
- prefix = "To be ";
- if (lacks > 0) {
- if (blockMode) {
- if (buildCount > 1)
- return blocked(prefix + "verified by " + lacks + " more build(s)", new NoneCanVote());
- else
- return blocked(prefix + "verified by build", new NoneCanVote());
+ if (commit.equals(Commit.ZERO_HASH)) { // delete branch
+ return accepted("Build is no longer necessary.");
+ } else if (commit.startsWith(Commit.ZERO_HASH)) { // check if a certain file can be touched
+ if (blockMode)
+ return blocked("Not verified by build.", new NoneCanVote());
+ else
+ return pending("Not verified by build.", new NoneCanVote());
+ } else {
+ Collection<BuildResult> buildResults = BuildResultManager.findBy(commit);
+ for (BuildResult each: buildResults) {
+ if (!each.isPassed())
+ return rejected("At least one build is failed for the merged commit.");
+ }
+ int lacks = buildCount - buildResults.size();
+
+ String prefix;
+ if (request.getId() == null)
+ prefix = "Not ";
+ else
+ prefix = "To be ";
+ if (lacks > 0) {
+ if (blockMode) {
+ if (buildCount > 1)
+ return blocked(prefix + "verified by " + lacks + " more build(s)", new NoneCanVote());
+ else
+ return blocked(prefix + "verified by build", new NoneCanVote());
+ } else {
+ if (buildCount > 1)
+ return pending(prefix + "verified by " + lacks + " more build(s)", new NoneCanVote());
+ else
+ return pending(prefix + "verified by build", new NoneCanVote());
+ }
} else {
- if (buildCount > 1)
- return pending(prefix + "verified by " + lacks + " more build(s)", new NoneCanVote());
- else
- return pending(prefix + "verified by build", new NoneCanVote());
+ return accepted("Builds passed");
}
- } else {
- return accepted("Builds passed");
}
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PostReceiveServlet.java b/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PostReceiveServlet.java
index f0cc734365..3b32a76c41 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PostReceiveServlet.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PostReceiveServlet.java
@@ -42,23 +42,27 @@ public class PostReceiveServlet extends CallbackServlet {
protected void callback(Project project, String callbackData, Output output) {
List<String> splitted = StringUtils.splitAndTrim(callbackData, " ");
- String oldCommitHash = splitted.get(0);
String branchName = Branch.getName(splitted.get(2));
+ if (branchName != null) {
+ String oldCommitHash = splitted.get(0);
+ String newCommitHash = splitted.get(1);
- // User with write permission can create new branch
- if (oldCommitHash.equals(Commit.ZERO_HASH)) {
- Branch branch = new Branch();
- branch.setProject(project);
- branch.setName(branchName);
- branchManager.save(branch);
- } else {
- logger.info("Executing post-receive hook against branch {}...", branchName);
-
- Branch branch = branchManager.findBy(project, branchName);
- Preconditions.checkNotNull(branch);
- eventBus.post(new BranchRefUpdateEvent(branch));
+ if (oldCommitHash.equals(Commit.ZERO_HASH)) {
+ Branch branch = new Branch();
+ branch.setProject(project);
+ branch.setName(branchName);
+ branchManager.save(branch);
+ } else if (newCommitHash.equals(Commit.ZERO_HASH)) {
+ Branch branch = branchManager.findBy(project, branchName);
+ Preconditions.checkNotNull(branch);
+ branchManager.delete(branch);
+ } else {
+ logger.info("Executing post-receive hook against branch {}...", branchName);
+
+ Branch branch = branchManager.findBy(project, branchName);
+ Preconditions.checkNotNull(branch);
+ eventBus.post(new BranchRefUpdateEvent(branch));
+ }
}
-
}
-
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PreReceiveServlet.java b/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PreReceiveServlet.java
index fe5e0bc1b6..f20e378353 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PreReceiveServlet.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/PreReceiveServlet.java
@@ -1,10 +1,12 @@
package com.pmease.gitop.core.hookcallback;
+import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
+import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -15,15 +17,14 @@ import com.pmease.gitop.core.manager.BranchManager;
import com.pmease.gitop.core.manager.ProjectManager;
import com.pmease.gitop.core.manager.UserManager;
import com.pmease.gitop.model.Branch;
-import com.pmease.gitop.model.MergePrediction;
import com.pmease.gitop.model.Project;
import com.pmease.gitop.model.PullRequest;
-import com.pmease.gitop.model.PullRequestUpdate;
import com.pmease.gitop.model.User;
import com.pmease.gitop.model.gatekeeper.GateKeeper;
import com.pmease.gitop.model.gatekeeper.checkresult.Accepted;
import com.pmease.gitop.model.gatekeeper.checkresult.CheckResult;
import com.pmease.gitop.model.gatekeeper.checkresult.Rejected;
+import com.pmease.gitop.model.permission.ObjectPermission;
@SuppressWarnings("serial")
@Singleton
@@ -44,61 +45,73 @@ public class PreReceiveServlet extends CallbackServlet {
this.branchManager = branchManager;
this.userManager = userManager;
}
-
+
+ private void error(Output output, String... messages) {
+ output.markError();
+ output.writeLine();
+ output.writeLine("*******************************************************");
+ output.writeLine("*");
+ for (String message: messages)
+ output.writeLine("* " + message);
+ output.writeLine("*");
+ output.writeLine("*******************************************************");
+ output.writeLine();
+ }
+
@Override
protected void callback(Project project, String callbackData, Output output) {
List<String> splitted = StringUtils.splitAndTrim(callbackData, " ");
-
- String oldCommitHash = splitted.get(0);
-
- // User with write permission can create new branch
- if (oldCommitHash.equals(Commit.ZERO_HASH))
- return;
-
- String newCommitHash = splitted.get(1);
- String branchName = Branch.getName(splitted.get(2));
-
- logger.info("Executing pre-receive hook against branch {}...", branchName);
-
- Branch branch = branchManager.findBy(project, branchName);
- Preconditions.checkNotNull(branch);
-
- User user = userManager.getCurrent();
- Preconditions.checkNotNull(user, "User pushing commits is unknown.");
-
- PullRequest request = new PullRequest();
- request.setTarget(branch);
- request.setSource(new Branch());
- request.getSource().setProject(new Project());
- request.getSource().getProject().setOwner(user);
- request.setTitle("Faked pull request to check against push gatekeeper");
- request.setMergePrediction(new MergePrediction(oldCommitHash, newCommitHash, newCommitHash));
-
- PullRequestUpdate update = new PullRequestUpdate();
- update.setRequest(request);
- update.setHeadCommit(newCommitHash);
- request.getUpdates().add(update);
-
- GateKeeper gateKeeper = project.getCompositeGateKeeper();
- CheckResult checkResult = gateKeeper.check(request);
-
- if (!(checkResult instanceof Accepted)) {
- output.markError();
- output.writeLine();
- output.writeLine("*******************************************************");
- output.writeLine("*");
- for (String each: checkResult.getReasons()) {
- output.writeLine("* " + each);
+ String refName = splitted.get(2);
+ if (refName.startsWith(Project.REFS_GITOP)) {
+ if (!SecurityUtils.getSubject().isPermitted(ObjectPermission.ofProjectAdmin(project)))
+ error(output, "Only project administrators can update gitop refs.");
+ } else {
+ String branchName = Branch.getName(refName);
+ if (branchName != null) {
+ String oldCommitHash = splitted.get(0);
+
+ if (!oldCommitHash.equals(Commit.ZERO_HASH)) {
+ Branch branch = branchManager.findBy(project, branchName);
+ Preconditions.checkNotNull(branch);
+
+ logger.info("Executing pre-receive hook against branch {}...", branchName);
+
+ User user = userManager.getCurrent();
+ Preconditions.checkNotNull(user);
+
+ String newCommitHash = splitted.get(1);
+
+ GateKeeper gateKeeper = project.getCompositeGateKeeper();
+ CheckResult checkResult = gateKeeper.checkCommit(user, branch, newCommitHash);
+
+ if (!(checkResult instanceof Accepted)) {
+ List<String> messages = new ArrayList<>();
+ for (String each: checkResult.getReasons())
+ messages.add(each);
+ if (!newCommitHash.equals(Commit.ZERO_HASH) && !(checkResult instanceof Rejected)) {
+ messages.add("");
+ messages.add("----------------------------------------------------");
+ messages.add("You may submit a pull request instead.");
+ }
+ error(output, messages.toArray(new String[messages.size()]));
+ } else {
+ for (PullRequest request: branch.getIngoingRequests()) {
+ if (request.isOpen()) {
+ error(output, "There are unclosed pull requests targeting this branch.",
+ "Please close them before continue.");
+ return;
+ }
+ }
+ for (PullRequest request: branch.getOutgoingRequests()) {
+ if (request.isOpen()) {
+ error(output, "There are unclosed pull requests originating from this branch.",
+ "Please close them before continue.");
+ return;
+ }
+ }
+ }
+ }
}
- if (!(checkResult instanceof Rejected)) {
- output.writeLine("*");
- output.writeLine("* ----------------------------------------------------");
- output.writeLine("* You may submit a pull request instead.");
- }
- output.writeLine("*");
- output.writeLine("*******************************************************");
- output.writeLine();
}
}
-
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java
index 716b161f3e..afb6c45c50 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java
@@ -51,4 +51,13 @@ public interface BranchManager extends GenericDao<Branch> {
* to be deleted
*/
public void deleteRefs(Branch branch);
+
+ /**
+ * Persist specified branch to database. Note that this won't update the git
+ * repository.
+ *
+ * @param branch
+ * branch to be saved
+ */
+ public void save(Branch branch);
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java
index 0893ab66d1..b5cb17dbae 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java
@@ -64,13 +64,8 @@ public class DefaultBranchManager extends AbstractGenericDao<Branch> implements
public void syncWithGit(Project project) {
Collection<String> branchesInGit = project.code().listBranches();
for (Branch branch: project.getBranches()) {
- if (!branchesInGit.contains(branch.getName())) {
- for (PullRequest request: branch.getIngoingRequests())
- pullRequestManager.deleteRefs(request);
- for (PullRequest request: branch.getOutgoingRequests())
- pullRequestManager.deleteRefs(request);
+ if (!branchesInGit.contains(branch.getName()))
delete(branch);
- }
}
for (String branchInGit: branchesInGit) {
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultProjectManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultProjectManager.java
index e364c1e602..8b806e4b13 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultProjectManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultProjectManager.java
@@ -23,6 +23,7 @@ import com.pmease.commons.util.FileUtils;
import com.pmease.commons.util.StringUtils;
import com.pmease.gitop.core.hookcallback.PostReceiveServlet;
import com.pmease.gitop.core.hookcallback.PreReceiveServlet;
+import com.pmease.gitop.core.manager.BranchManager;
import com.pmease.gitop.core.manager.ProjectManager;
import com.pmease.gitop.core.setting.ServerConfig;
import com.pmease.gitop.model.Project;
@@ -36,16 +37,20 @@ public class DefaultProjectManager extends AbstractGenericDao<Project> implement
private static final Logger logger = LoggerFactory.getLogger(DefaultProjectManager.class);
private final StorageManager storageManager;
-
+
+ private final BranchManager branchManager;
+
private final ServerConfig serverConfig;
private final String hookTemplate;
@Inject
- public DefaultProjectManager(GeneralDao generalDao, StorageManager storageManager, ServerConfig serverConfig) {
+ public DefaultProjectManager(GeneralDao generalDao, StorageManager storageManager,
+ BranchManager branchManager, ServerConfig serverConfig) {
super(generalDao);
this.storageManager = storageManager;
+ this.branchManager = branchManager;
this.serverConfig = serverConfig;
try (InputStream is = getClass().getClassLoader().getResourceAsStream("git-hook-template")) {
@@ -90,6 +95,8 @@ public class DefaultProjectManager extends AbstractGenericDao<Project> implement
String.format(hookTemplate, urlRoot + PostReceiveServlet.PATH + "/" + project.getId()));
postReceiveHook.setExecutable(true);
}
+
+ branchManager.syncWithGit(project);
} else {
super.save(project);
}
diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/Branch.java b/gitop.model/src/main/java/com/pmease/gitop/model/Branch.java
index e51090eb27..750bd8b33c 100644
--- a/gitop.model/src/main/java/com/pmease/gitop/model/Branch.java
+++ b/gitop.model/src/main/java/com/pmease/gitop/model/Branch.java
@@ -13,7 +13,6 @@ import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import com.google.common.base.Objects;
-import com.google.common.base.Preconditions;
import com.pmease.commons.git.Git;
import com.pmease.commons.hibernate.AbstractEntity;
@@ -30,7 +29,7 @@ public class Branch extends AbstractEntity {
@Column(nullable=false)
private String name;
-
+
@OneToMany(mappedBy="target", cascade=CascadeType.REMOVE)
private Collection<AutoPull> autoPullSources = new ArrayList<AutoPull>();
@@ -133,13 +132,16 @@ public class Branch extends AbstractEntity {
* @param refName
* name of the git reference
* @return
- * name of the branch
+ * name of the branch, or <tt>null</tt> if specified ref
+ * does not represent a branch
*/
public static String getName(String refName) {
- Preconditions.checkArgument(refName.startsWith(Git.REFS_HEADS));
- return refName.substring(Git.REFS_HEADS.length());
+ if (refName.startsWith(Git.REFS_HEADS))
+ return refName.substring(Git.REFS_HEADS.length());
+ else
+ return null;
}
-
+
@Override
public String toString() {
return Objects.toStringHelper(this)
diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/Project.java b/gitop.model/src/main/java/com/pmease/gitop/model/Project.java
index 8bc409c2df..f131dfb336 100644
--- a/gitop.model/src/main/java/com/pmease/gitop/model/Project.java
+++ b/gitop.model/src/main/java/com/pmease/gitop/model/Project.java
@@ -41,6 +41,8 @@ import com.pmease.gitop.model.validation.ProjectName;
@SuppressWarnings("serial")
@Editable
public class Project extends AbstractEntity implements UserBelonging {
+
+ public static final String REFS_GITOP = "refs/gitop/";
@ManyToOne
@JoinColumn(nullable=false)
diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/PullRequest.java b/gitop.model/src/main/java/com/pmease/gitop/model/PullRequest.java
index 931f31c3f2..5e1977c3d7 100644
--- a/gitop.model/src/main/java/com/pmease/gitop/model/PullRequest.java
+++ b/gitop.model/src/main/java/com/pmease/gitop/model/PullRequest.java
@@ -226,11 +226,11 @@ public class PullRequest extends AbstractEntity {
}
public String getHeadRef() {
- return "refs/gitop/pulls/" + getId() + "/head";
+ return Project.REFS_GITOP + "pulls/" + getId() + "/head";
}
public String getMergeRef() {
- return "refs/gitop/pulls/" + getId() + "/merge";
+ return Project.REFS_GITOP + "pulls/" + getId() + "/merge";
};
/**
diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestUpdate.java b/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestUpdate.java
index dbfc13150b..743de839e3 100644
--- a/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestUpdate.java
+++ b/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestUpdate.java
@@ -71,11 +71,11 @@ public class PullRequestUpdate extends AbstractEntity {
}
public String getBaseRef() {
- return "refs/gitop/updates/" + getId() + "/base";
+ return Project.REFS_GITOP + "updates/" + getId() + "/base";
}
public String getHeadRef() {
- return "refs/gitop/updates/" + getId() + "/head";
+ return Project.REFS_GITOP + "updates/" + getId() + "/head";
}
/**
diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/User.java b/gitop.model/src/main/java/com/pmease/gitop/model/User.java
index 01b66c4dbd..866c4d79b2 100644
--- a/gitop.model/src/main/java/com/pmease/gitop/model/User.java
+++ b/gitop.model/src/main/java/com/pmease/gitop/model/User.java
@@ -163,7 +163,7 @@ public class User extends AbstractUser implements ProtectedObject {
return null;
}
-
+
@Override
public String toString() {
return getName();
diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/AbstractGateKeeper.java b/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/AbstractGateKeeper.java
index 453e7af5ef..2655549e0e 100644
--- a/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/AbstractGateKeeper.java
+++ b/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/AbstractGateKeeper.java
@@ -7,8 +7,13 @@ import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
import com.pmease.commons.editable.annotation.Editable;
+import com.pmease.commons.git.Commit;
+import com.pmease.gitop.model.Branch;
+import com.pmease.gitop.model.MergePrediction;
import com.pmease.gitop.model.Project;
import com.pmease.gitop.model.PullRequest;
+import com.pmease.gitop.model.PullRequestUpdate;
+import com.pmease.gitop.model.User;
import com.pmease.gitop.model.gatekeeper.checkresult.Accepted;
import com.pmease.gitop.model.gatekeeper.checkresult.Blocked;
import com.pmease.gitop.model.gatekeeper.checkresult.CheckResult;
@@ -38,11 +43,46 @@ public abstract class AbstractGateKeeper implements GateKeeper {
return accepted("Gate keeper is disabled.");
}
+ @Override
+ public CheckResult checkFile(User user, Branch branch, String file) {
+ return checkCommit(user, branch, Commit.ZERO_HASH + file);
+ }
+
+
+ @Override
+ public CheckResult checkCommit(User user, Branch branch, String commit) {
+ PullRequest request = new PullRequest();
+ request.setTarget(branch);
+ request.setSource(new Branch());
+ request.getSource().setProject(new Project());
+ request.getSource().getProject().setOwner(user);
+ request.setTitle("Faked Pull Request");
+ request.setMergePrediction(new MergePrediction(null, commit, commit));
+
+ PullRequestUpdate update = new PullRequestUpdate();
+ update.setRequest(request);
+ update.setHeadCommit(request.getMergePrediction().getRequestHead());
+ request.getUpdates().add(update);
+
+ return check(request);
+ }
+
/**
* Check the gate keeper without considering enable flag.
*
* @param request
- * pull request to be checked
+ * pull request to be checked. Gitop will create faked pull request in some
+ * special cases explained below:
+ * <ul>
+ * <li> When determine whether or not a push operation should be accepted.
+ * <li> When determine whether or not a branch can be deleted. In this
+ * case, the commit hash of the update will be {@link Commit#ZERO_HASH}.
+ * <li> When determine whether or not a file can be touched. In this
+ * case, the commit hash of the update will be {@link Commit#ZERO_HASH}
+ * concatenated with full path of the file.
+ * </ul>
+ * For a faked pull request created in these special cases can be identified by
+ * checking whether or not <tt>request.getId()</tt> returns <tt>null</tt>
* @return
* result of the check
*/
diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/GateKeeper.java b/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/GateKeeper.java
index 1b95d5c189..3c9d98a509 100644
--- a/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/GateKeeper.java
+++ b/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/GateKeeper.java
@@ -4,7 +4,9 @@ import java.io.Serializable;
import com.pmease.commons.editable.annotation.Editable;
import com.pmease.commons.util.trimmable.Trimmable;
+import com.pmease.gitop.model.Branch;
import com.pmease.gitop.model.PullRequest;
+import com.pmease.gitop.model.User;
import com.pmease.gitop.model.gatekeeper.checkresult.CheckResult;
@Editable(name="Misc")
@@ -19,8 +21,39 @@ public interface GateKeeper extends Trimmable, Serializable {
* this case, we should not invite any users to vote for the
* request
* @return
+ * check result
*/
CheckResult check(PullRequest request);
+
+ /**
+ * Check if specified user can modify specified file in specified branch.
+ *
+ * @param user
+ * user to be checked
+ * @param branch
+ * branch to be checked
+ * @param file
+ * file to be checked
+ * @return
+ * check result
+ *
+ */
+ CheckResult checkFile(User user, Branch branch, String file);
+
+ /**
+ * Check if specified user can push specified commit to specified branch.
+ *
+ * @param user
+ * user to be checked
+ * @param branch
+ * branch to be checked
+ * @param commit
+ * commit to be checked
+ * @return
+ * check result
+ *
+ */
+ CheckResult checkCommit(User user, Branch branch, String commit);
boolean isEnabled();
}
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/settings/gatekeeper/GateKeeperPanel.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/settings/gatekeeper/GateKeeperPanel.java
index 72bb5c097c..ec5da15836 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/settings/gatekeeper/GateKeeperPanel.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/settings/gatekeeper/GateKeeperPanel.java
@@ -167,7 +167,7 @@ public abstract class GateKeeperPanel extends Panel {
}
@Override
- protected void onSave(AjaxRequestTarget target, GateKeeper gatekeeper) {
+ protected void onSave(AjaxRequestTarget target, GateKeeper gateKeeper) {
close(target);
fragment.replace(new WebMarkupContainer("childEditor").setOutputMarkupPlaceholderTag(true).setVisible(true));
andOrGateKeeper.getGateKeepers().add(gateKeeper);
@@ -219,7 +219,7 @@ public abstract class GateKeeperPanel extends Panel {
}
@Override
- protected void onSave(AjaxRequestTarget target, GateKeeper gatekeeper) {
+ protected void onSave(AjaxRequestTarget target, GateKeeper gateKeeper) {
close(target);
fragment.replace(new WebMarkupContainer("gateKeeperEditor").setOutputMarkupPlaceholderTag(true).setVisible(true));
notGateKeeper.setGateKeeper(gateKeeper);
@@ -293,7 +293,7 @@ public abstract class GateKeeperPanel extends Panel {
}
@Override
- protected void onSave(AjaxRequestTarget target, GateKeeper gatekeeper) {
+ protected void onSave(AjaxRequestTarget target, GateKeeper gateKeeper) {
close(target);
fragment.replace(new WebMarkupContainer("ifEditor").setOutputMarkupPlaceholderTag(true).setVisible(true));
ifThenGateKeeper.setIfGate(gateKeeper);
@@ -363,7 +363,7 @@ public abstract class GateKeeperPanel extends Panel {
}
@Override
- protected void onSave(AjaxRequestTarget target, GateKeeper gatekeeper) {
+ protected void onSave(AjaxRequestTarget target, GateKeeper gateKeeper) {
close(target);
fragment.replace(new WebMarkupContainer("thenEditor").setOutputMarkupPlaceholderTag(true).setVisible(true));
ifThenGateKeeper.setThenGate(gateKeeper);