Line data Source code
1 : // Copyright (C) 2016 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.account; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.entities.NotifyConfig.NotifyType; 19 : import com.google.gerrit.extensions.client.ProjectWatchInfo; 20 : import com.google.gerrit.extensions.restapi.BadRequestException; 21 : import com.google.gerrit.extensions.restapi.Response; 22 : import com.google.gerrit.extensions.restapi.RestApiException; 23 : import com.google.gerrit.extensions.restapi.RestModifyView; 24 : import com.google.gerrit.index.query.QueryParseException; 25 : import com.google.gerrit.index.query.QueryParser; 26 : import com.google.gerrit.server.IdentifiedUser; 27 : import com.google.gerrit.server.UserInitiated; 28 : import com.google.gerrit.server.account.AccountResource; 29 : import com.google.gerrit.server.account.AccountsUpdate; 30 : import com.google.gerrit.server.account.ProjectWatches; 31 : import com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey; 32 : import com.google.gerrit.server.permissions.GlobalPermission; 33 : import com.google.gerrit.server.permissions.PermissionBackend; 34 : import com.google.gerrit.server.permissions.PermissionBackendException; 35 : import com.google.gerrit.server.restapi.project.ProjectsCollection; 36 : import com.google.inject.Inject; 37 : import com.google.inject.Provider; 38 : import com.google.inject.Singleton; 39 : import java.io.IOException; 40 : import java.util.EnumSet; 41 : import java.util.HashMap; 42 : import java.util.List; 43 : import java.util.Map; 44 : import java.util.Set; 45 : import org.eclipse.jgit.errors.ConfigInvalidException; 46 : 47 : /** 48 : * REST endpoint to set project watches for an account. 49 : * 50 : * <p>This REST endpoint handles {@code POST /accounts/<account-identifier>/watched.projects} 51 : * requests. 52 : */ 53 : @Singleton 54 : public class PostWatchedProjects 55 : implements RestModifyView<AccountResource, List<ProjectWatchInfo>> { 56 : private final Provider<IdentifiedUser> self; 57 : private final PermissionBackend permissionBackend; 58 : private final GetWatchedProjects getWatchedProjects; 59 : private final ProjectsCollection projectsCollection; 60 : private final Provider<AccountsUpdate> accountsUpdateProvider; 61 : 62 : @Inject 63 : public PostWatchedProjects( 64 : Provider<IdentifiedUser> self, 65 : PermissionBackend permissionBackend, 66 : GetWatchedProjects getWatchedProjects, 67 : ProjectsCollection projectsCollection, 68 148 : @UserInitiated Provider<AccountsUpdate> accountsUpdateProvider) { 69 148 : this.self = self; 70 148 : this.permissionBackend = permissionBackend; 71 148 : this.getWatchedProjects = getWatchedProjects; 72 148 : this.projectsCollection = projectsCollection; 73 148 : this.accountsUpdateProvider = accountsUpdateProvider; 74 148 : } 75 : 76 : @Override 77 : public Response<List<ProjectWatchInfo>> apply(AccountResource rsrc, List<ProjectWatchInfo> input) 78 : throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException { 79 16 : if (!self.get().hasSameAccountId(rsrc.getUser())) { 80 3 : permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER); 81 : } 82 : 83 16 : Map<ProjectWatchKey, Set<NotifyType>> projectWatches = asMap(input); 84 16 : accountsUpdateProvider 85 16 : .get() 86 16 : .update( 87 : "Update Project Watches via API", 88 16 : rsrc.getUser().getAccountId(), 89 16 : u -> u.updateProjectWatches(projectWatches)); 90 16 : return getWatchedProjects.apply(rsrc); 91 : } 92 : 93 : private Map<ProjectWatchKey, Set<NotifyType>> asMap(List<ProjectWatchInfo> input) 94 : throws RestApiException, IOException, PermissionBackendException { 95 16 : Map<ProjectWatchKey, Set<NotifyType>> m = new HashMap<>(); 96 16 : for (ProjectWatchInfo info : input) { 97 16 : if (info.project == null || info.project.trim().isEmpty()) { 98 1 : throw new BadRequestException("project name must be specified"); 99 : } 100 : 101 16 : if (!Strings.isNullOrEmpty(info.filter)) { 102 : try { 103 7 : QueryParser.parse(info.filter); 104 0 : } catch (QueryParseException e) { 105 0 : throw new BadRequestException( 106 0 : String.format( 107 0 : "invalid filter expression for project %s: %s", info.project, e.getMessage()), 108 : e); 109 7 : } 110 : } 111 : 112 16 : ProjectWatchKey key = 113 16 : ProjectWatchKey.create(projectsCollection.parse(info.project).getNameKey(), info.filter); 114 16 : if (m.containsKey(key)) { 115 1 : throw new BadRequestException( 116 1 : "duplicate entry for project " + format(info.project, info.filter)); 117 : } 118 : 119 16 : Set<NotifyType> notifyValues = EnumSet.noneOf(NotifyType.class); 120 16 : if (toBoolean(info.notifyAbandonedChanges)) { 121 11 : notifyValues.add(NotifyType.ABANDONED_CHANGES); 122 : } 123 16 : if (toBoolean(info.notifyAllComments)) { 124 13 : notifyValues.add(NotifyType.ALL_COMMENTS); 125 : } 126 16 : if (toBoolean(info.notifyNewChanges)) { 127 14 : notifyValues.add(NotifyType.NEW_CHANGES); 128 : } 129 16 : if (toBoolean(info.notifyNewPatchSets)) { 130 5 : notifyValues.add(NotifyType.NEW_PATCHSETS); 131 : } 132 16 : if (toBoolean(info.notifySubmittedChanges)) { 133 2 : notifyValues.add(NotifyType.SUBMITTED_CHANGES); 134 : } 135 : 136 16 : m.put(key, notifyValues); 137 16 : } 138 16 : return m; 139 : } 140 : 141 : private boolean toBoolean(Boolean b) { 142 16 : return b == null ? false : b; 143 : } 144 : 145 : private static String format(String project, String filter) { 146 1 : return project 147 1 : + (filter != null && !ProjectWatches.FILTER_ALL.equals(filter) 148 0 : ? " and filter " + filter 149 1 : : ""); 150 : } 151 : }