Skip to content

The Unified and Autonomous Development Platform

Add the option of specifying branches when define directory protection setting.

robin shine37052b98ccf2
diff --git a/commons.editable/src/main/java/com/pmease/commons/editable/annotation/OmitNames.java b/commons.editable/src/main/java/com/pmease/commons/editable/annotation/OmitName.java
similarity index 64%
rename from commons.editable/src/main/java/com/pmease/commons/editable/annotation/OmitNames.java
rename to commons.editable/src/main/java/com/pmease/commons/editable/annotation/OmitName.java
index d93f4fb40d..d64589c38b 100644
--- a/commons.editable/src/main/java/com/pmease/commons/editable/annotation/OmitNames.java
+++ b/commons.editable/src/main/java/com/pmease/commons/editable/annotation/OmitName.java
@@ -6,6 +6,10 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.TYPE)
-public @interface OmitNames {
+@Target(ElementType.METHOD)
+public @interface OmitName {
+
+ public enum Place {ALL, EDITOR, VIEWER};
+
+ Place value() default Place.ALL;
}
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/editable/reflection/ReflectionBeanEditor.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/editable/reflection/ReflectionBeanEditor.java
index b136b625d0..a0de52034d 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/editable/reflection/ReflectionBeanEditor.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/editable/reflection/ReflectionBeanEditor.java
@@ -19,7 +19,7 @@ import org.apache.wicket.model.LoadableDetachableModel;
import com.pmease.commons.editable.EditContext;
import com.pmease.commons.editable.EditableUtils;
import com.pmease.commons.editable.PropertyEditContext;
-import com.pmease.commons.editable.annotation.OmitNames;
+import com.pmease.commons.editable.annotation.OmitName;
import com.pmease.commons.editable.annotation.TableLayout;
@SuppressWarnings("serial")
@@ -75,8 +75,12 @@ public class ReflectionBeanEditor extends Panel {
protected void populateItem(ListItem<PropertyEditContext> item) {
final PropertyEditContext propertyContext = item.getModelObject();
- item.add(new Label("name", EditableUtils.getName(propertyContext.getPropertyGetter()))
- .setVisible(editContext.getBeanClass().getAnnotation(OmitNames.class) == null));
+ Label nameLabel = new Label("name", EditableUtils.getName(propertyContext.getPropertyGetter()));
+ item.add(nameLabel);
+
+ OmitName omitName = propertyContext.getPropertyGetter().getAnnotation(OmitName.class);
+ if (omitName != null && omitName.value() != OmitName.Place.VIEWER)
+ nameLabel.setVisible(false);
String required;
if (propertyContext.isPropertyRequired())
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/editable/reflection/ReflectionBeanViewer.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/editable/reflection/ReflectionBeanViewer.java
index ea9d39d161..eec6a0b19d 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/editable/reflection/ReflectionBeanViewer.java
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/editable/reflection/ReflectionBeanViewer.java
@@ -1,5 +1,7 @@
package com.pmease.commons.wicket.editable.reflection;
+import java.lang.reflect.Method;
+
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
@@ -8,7 +10,7 @@ import org.apache.wicket.markup.html.panel.Panel;
import com.pmease.commons.editable.EditableUtils;
import com.pmease.commons.editable.PropertyEditContext;
-import com.pmease.commons.editable.annotation.OmitNames;
+import com.pmease.commons.editable.annotation.OmitName;
@SuppressWarnings("serial")
public class ReflectionBeanViewer extends Panel {
@@ -29,8 +31,13 @@ public class ReflectionBeanViewer extends Panel {
@Override
protected void populateItem(ListItem<PropertyEditContext> item) {
- item.add(new Label("name", EditableUtils.getName(item.getModelObject().getPropertyGetter()))
- .setVisible(editContext.getBeanClass().getAnnotation(OmitNames.class) == null));
+ Method propertyGetter = item.getModelObject().getPropertyGetter();
+ Label nameLabel = new Label("name", EditableUtils.getName(propertyGetter));
+ item.add(nameLabel);
+ OmitName omitName = propertyGetter.getAnnotation(OmitName.class);
+
+ if (omitName != null && omitName.value() != OmitName.Place.EDITOR)
+ nameLabel.setVisible(false);
item.add((Component)item.getModelObject().renderForView("value"));
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/BranchProtection.java b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/BranchProtection.java
index 22e30fb870..9c6b40c45e 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/BranchProtection.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/BranchProtection.java
@@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.pmease.commons.editable.annotation.Editable;
-import com.pmease.commons.editable.annotation.OmitNames;
+import com.pmease.commons.editable.annotation.OmitName;
import com.pmease.commons.util.trimmable.TrimUtils;
import com.pmease.commons.util.trimmable.Trimmable;
import com.pmease.gitop.core.Gitop;
@@ -30,7 +30,6 @@ import com.pmease.gitop.model.gatekeeper.checkresult.CheckResult;
@Editable(order=100, icon="icon-lock", description="By default, users with write permission of "
+ "the project can push code to all branches. Use this gate keeper to restrict write "
+ "access of certain branches to certain teams.")
-@OmitNames
public class BranchProtection extends CommonGateKeeper {
private List<Entry> entries = new ArrayList<Entry>();
@@ -44,6 +43,7 @@ public class BranchProtection extends CommonGateKeeper {
@Size(min=1, message="At least one entry has to be specified.")
@Valid
@NotNull
+ @OmitName
public List<Entry> getEntries() {
return entries;
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/DirectoryProtection.java b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/DirectoryProtection.java
index 0a14f2f734..2811ce1d8a 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/DirectoryProtection.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/gatekeeper/DirectoryProtection.java
@@ -2,6 +2,7 @@ package com.pmease.gitop.core.gatekeeper;
import java.io.Serializable;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
import javax.validation.Valid;
@@ -11,12 +12,14 @@ import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import com.pmease.commons.editable.annotation.Editable;
-import com.pmease.commons.editable.annotation.OmitNames;
+import com.pmease.commons.editable.annotation.OmitName;
import com.pmease.commons.util.trimmable.TrimUtils;
import com.pmease.commons.util.trimmable.Trimmable;
import com.pmease.gitop.core.Gitop;
+import com.pmease.gitop.core.editable.BranchChoice;
import com.pmease.gitop.core.editable.DirectoryChoice;
import com.pmease.gitop.core.editable.TeamChoice;
+import com.pmease.gitop.core.manager.BranchManager;
import com.pmease.gitop.core.manager.TeamManager;
import com.pmease.gitop.model.Project;
import com.pmease.gitop.model.PullRequest;
@@ -30,21 +33,35 @@ import com.pmease.gitop.model.gatekeeper.checkresult.CheckResult;
@SuppressWarnings("serial")
@Editable(order=200, icon="icon-lock", description="By default, users with write permission of "
+ "the project can write to all directories. Use this gate keeper to restrict write "
- + "access of certain directories to certain users.")
-@OmitNames
+ + "access of certain directories in specified branches to certain teams. Note that if "
+ + "branch is not specified, the directory restriction will apply to all branches.")
public class DirectoryProtection extends CommonGateKeeper {
+ private List<Long> branchIds = new ArrayList<Long>();
+
private List<Entry> entries = new ArrayList<Entry>();
public DirectoryProtection() {
Entry entry = new Entry();
entries.add(entry);
}
-
+
+ @Editable(name="Branches To Apply (Optionally)", order=50)
+ @BranchChoice
+ @NotNull
+ public List<Long> getBranchIds() {
+ return branchIds;
+ }
+
+ public void setBranchIds(List<Long> branchIds) {
+ this.branchIds = branchIds;
+ }
+
@Editable(name="Protected Directories", order=100)
@Valid
@Size(min=1, message="At least one entry has to be added.")
@NotNull
+ @OmitName(OmitName.Place.EDITOR)
public List<Entry> getEntries() {
return entries;
}
@@ -95,6 +112,11 @@ public class DirectoryProtection extends CommonGateKeeper {
@Override
public CheckResult doCheck(PullRequest request) {
AndGateKeeper andGateKeeper = new AndGateKeeper();
+ if (!branchIds.isEmpty()) {
+ IfSubmittedToSpecifiedBranches branchGate = new IfSubmittedToSpecifiedBranches();
+ branchGate.getBranchIds().addAll(branchIds);
+ andGateKeeper.getGateKeepers().add(branchGate);
+ }
for (Entry entry: entries) {
IfThenGateKeeper ifThenGateKeeper = new IfThenGateKeeper();
IfTouchesSpecifiedDirectories ifGate = new IfTouchesSpecifiedDirectories();
@@ -112,6 +134,14 @@ public class DirectoryProtection extends CommonGateKeeper {
@Override
protected GateKeeper trim(Project project) {
+ if (!branchIds.isEmpty()) {
+ for (Iterator<Long> it = branchIds.iterator(); it.hasNext();) {
+ if (Gitop.getInstance(BranchManager.class).get(it.next()) == null)
+ it.remove();
+ }
+ if (branchIds.isEmpty())
+ return null;
+ }
TrimUtils.trim(entries, project);
if (entries.isEmpty())
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 b0ba1a2b5a..3588cc2ae7 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
@@ -50,7 +50,7 @@ public class IfTouchesSpecifiedDirectories extends FileGateKeeper {
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) + "'.");
+ return accepted("Delete branch '" + request.getTarget().getName() + "'.");
}
for (String file: touchedFiles) {
for (String each: directories) {
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 2bc4e241ef..e34d09b43b 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
@@ -52,7 +52,7 @@ public class IfTouchesSpecifiedFilePatterns extends FileGateKeeper {
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() + "'.");
+ return accepted("Delete branch '" + request.getTarget().getName() + "'.");
}
for (String file: touchedFiles) {
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/GitUpdateCallback.java b/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/GitUpdateCallback.java
index 327e419cd5..d8504d246c 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/GitUpdateCallback.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/GitUpdateCallback.java
@@ -110,7 +110,7 @@ public class GitUpdateCallback extends HttpServlet {
User user = userManager.getCurrent();
Preconditions.checkNotNull(user);
- GateKeeper gateKeeper = project.getCompositeGateKeeper();
+ GateKeeper gateKeeper = project.aggregateGateKeepers();
CheckResult checkResult = gateKeeper.checkCommit(user, branch, newCommitHash);
if (!(checkResult instanceof Accepted)) {
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestManager.java
index 607f89f853..b9c4862eb1 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestManager.java
@@ -174,7 +174,7 @@ public class DefaultPullRequestManager extends AbstractGenericDao<PullRequest> i
}
}
- request.setCheckResult(request.getTarget().getProject().getCompositeGateKeeper().check(request));
+ request.setCheckResult(request.getTarget().getProject().aggregateGateKeepers().check(request));
for (VoteInvitation invitation : request.getVoteInvitations()) {
if (!request.getCheckResult().canVote(invitation.getVoter(), request))
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 1f10e9ff1f..69334eb756 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
@@ -28,6 +28,7 @@ import com.pmease.commons.loader.AppLoader;
import com.pmease.commons.util.FileUtils;
import com.pmease.gitop.model.gatekeeper.AndGateKeeper;
import com.pmease.gitop.model.gatekeeper.GateKeeper;
+import com.pmease.gitop.model.gatekeeper.checkresult.CheckResult;
import com.pmease.gitop.model.permission.object.ProtectedObject;
import com.pmease.gitop.model.permission.object.UserBelonging;
import com.pmease.gitop.model.storage.StorageManager;
@@ -222,11 +223,27 @@ public class Project extends AbstractEntity implements UserBelonging {
}
- public GateKeeper getCompositeGateKeeper() {
+ public GateKeeper aggregateGateKeepers() {
AndGateKeeper andGateKeeper = new AndGateKeeper();
for (GateKeeper each: getGateKeepers())
andGateKeeper.getGateKeepers().add(each);
return andGateKeeper;
}
+ /**
+ * Check if specified user can touch 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
+ */
+ public CheckResult checkFile(User user, Branch branch, String file) {
+ return aggregateGateKeepers().checkFile(user, branch, file);
+ }
+
}
diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/checkresult/CheckResult.java b/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/checkresult/CheckResult.java
index 7b4e6aa594..27e40a1dde 100644
--- a/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/checkresult/CheckResult.java
+++ b/gitop.model/src/main/java/com/pmease/gitop/model/gatekeeper/checkresult/CheckResult.java
@@ -57,4 +57,43 @@ public abstract class CheckResult implements Serializable {
return false;
}
+ /**
+ * Whether or not the check represents for a rejection.
+ *
+ * @return
+ * <tt>true</tt> if rejected
+ */
+ public boolean isRejected() {
+ return this instanceof Rejected;
+ }
+
+ /**
+ * Whether or not the check represents for an acceptance.
+ *
+ * @return
+ * <tt>true</tt> if accepted
+ */
+ public boolean isAccepted() {
+ return this instanceof Accepted;
+ }
+
+ /**
+ * Whether or not the check represents for a pending approval.
+ *
+ * @return
+ * <tt>true</tt> if pending approval
+ */
+ public boolean isPending() {
+ return this instanceof Pending;
+ }
+
+ /**
+ * Whether or not the check represents for a blocked pending approval.
+ *
+ * @return
+ * <tt>true</tt> if pending blocked approval
+ */
+ public boolean isBlocked() {
+ return this instanceof Blocked;
+ }
}
\ No newline at end of file