summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/test/java/com/google/gerrit/server/project/Util.java
blob: 6604641153a5aacc048180bd2ad15179e01c0952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.project;

import com.google.gerrit.common.data.AccessSection;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.common.data.LabelFunction;
import com.google.gerrit.common.data.LabelType;
import com.google.gerrit.common.data.LabelValue;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.common.data.PermissionRange;
import com.google.gerrit.common.data.PermissionRule;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.git.ProjectConfig;
import java.util.Arrays;

public class Util {
  public static final AccountGroup.UUID ADMIN = new AccountGroup.UUID("test.admin");
  public static final AccountGroup.UUID DEVS = new AccountGroup.UUID("test.devs");

  public static final LabelType codeReview() {
    return category(
        "Code-Review",
        value(2, "Looks good to me, approved"),
        value(1, "Looks good to me, but someone else must approve"),
        value(0, "No score"),
        value(-1, "I would prefer this is not merged as is"),
        value(-2, "This shall not be merged"));
  }

  public static final LabelType verified() {
    return category("Verified", value(1, "Verified"), value(0, "No score"), value(-1, "Fails"));
  }

  public static final LabelType patchSetLock() {
    LabelType label =
        category("Patch-Set-Lock", value(1, "Patch Set Locked"), value(0, "Patch Set Unlocked"));
    label.setFunction(LabelFunction.PATCH_SET_LOCK);
    return label;
  }

  public static LabelValue value(int value, String text) {
    return new LabelValue((short) value, text);
  }

  public static LabelType category(String name, LabelValue... values) {
    return new LabelType(name, Arrays.asList(values));
  }

  public static PermissionRule newRule(ProjectConfig project, AccountGroup.UUID groupUUID) {
    GroupReference group = new GroupReference(groupUUID, groupUUID.get());
    group = project.resolve(group);

    return new PermissionRule(group);
  }

  public static PermissionRule allow(
      ProjectConfig project,
      String permissionName,
      int min,
      int max,
      AccountGroup.UUID group,
      String ref) {
    PermissionRule rule = newRule(project, group);
    rule.setMin(min);
    rule.setMax(max);
    return grant(project, permissionName, rule, ref);
  }

  public static PermissionRule block(
      ProjectConfig project,
      String permissionName,
      int min,
      int max,
      AccountGroup.UUID group,
      String ref) {
    PermissionRule rule = newRule(project, group);
    rule.setMin(min);
    rule.setMax(max);
    PermissionRule r = grant(project, permissionName, rule, ref);
    r.setBlock();
    return r;
  }

  public static PermissionRule allow(
      ProjectConfig project, String permissionName, AccountGroup.UUID group, String ref) {
    return grant(project, permissionName, newRule(project, group), ref);
  }

  public static PermissionRule allow(
      ProjectConfig project,
      String permissionName,
      AccountGroup.UUID group,
      String ref,
      boolean exclusive) {
    return grant(project, permissionName, newRule(project, group), ref, exclusive);
  }

  public static PermissionRule allow(
      ProjectConfig project, String capabilityName, AccountGroup.UUID group) {
    PermissionRule rule = newRule(project, group);
    project
        .getAccessSection(AccessSection.GLOBAL_CAPABILITIES, true)
        .getPermission(capabilityName, true)
        .add(rule);
    if (GlobalCapability.hasRange(capabilityName)) {
      PermissionRange.WithDefaults range = GlobalCapability.getRange(capabilityName);
      if (range != null) {
        rule.setRange(range.getDefaultMin(), range.getDefaultMax());
      }
    }
    return rule;
  }

  public static PermissionRule remove(
      ProjectConfig project, String capabilityName, AccountGroup.UUID group) {
    PermissionRule rule = newRule(project, group);
    project
        .getAccessSection(AccessSection.GLOBAL_CAPABILITIES, true)
        .getPermission(capabilityName, true)
        .remove(rule);
    return rule;
  }

  public static PermissionRule remove(
      ProjectConfig project, String permissionName, AccountGroup.UUID group, String ref) {
    PermissionRule rule = newRule(project, group);
    project.getAccessSection(ref, true).getPermission(permissionName, true).remove(rule);
    return rule;
  }

  public static PermissionRule block(
      ProjectConfig project, String capabilityName, AccountGroup.UUID group) {
    PermissionRule rule = newRule(project, group);
    project
        .getAccessSection(AccessSection.GLOBAL_CAPABILITIES, true)
        .getPermission(capabilityName, true)
        .add(rule);
    return rule;
  }

  public static PermissionRule block(
      ProjectConfig project, String permissionName, AccountGroup.UUID group, String ref) {
    PermissionRule r = grant(project, permissionName, newRule(project, group), ref);
    r.setBlock();
    return r;
  }

  public static PermissionRule blockLabel(
      ProjectConfig project, String labelName, AccountGroup.UUID group, String ref) {
    return blockLabel(project, labelName, -1, 1, group, ref);
  }

  public static PermissionRule blockLabel(
      ProjectConfig project,
      String labelName,
      int min,
      int max,
      AccountGroup.UUID group,
      String ref) {
    PermissionRule r = grant(project, Permission.LABEL + labelName, newRule(project, group), ref);
    r.setBlock();
    r.setRange(min, max);
    return r;
  }

  public static PermissionRule deny(
      ProjectConfig project, String permissionName, AccountGroup.UUID group, String ref) {
    PermissionRule r = grant(project, permissionName, newRule(project, group), ref);
    r.setDeny();
    return r;
  }

  public static void doNotInherit(ProjectConfig project, String permissionName, String ref) {
    project
        .getAccessSection(ref, true) //
        .getPermission(permissionName, true) //
        .setExclusiveGroup(true);
  }

  private static PermissionRule grant(
      ProjectConfig project, String permissionName, PermissionRule rule, String ref) {
    return grant(project, permissionName, rule, ref, false);
  }

  private static PermissionRule grant(
      ProjectConfig project,
      String permissionName,
      PermissionRule rule,
      String ref,
      boolean exclusive) {
    Permission permission = project.getAccessSection(ref, true).getPermission(permissionName, true);
    if (exclusive) {
      permission.setExclusiveGroup(exclusive);
    }
    permission.add(rule);
    return rule;
  }

  private Util() {}
}