Line data Source code
1 : // Copyright (C) 2012 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.change; 16 : 17 : import com.google.gerrit.entities.Project; 18 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 19 : import com.google.gerrit.extensions.restapi.RestApiException; 20 : import com.google.gerrit.server.CurrentUser; 21 : import com.google.gerrit.server.account.AccountState; 22 : import com.google.gerrit.server.config.ChangeCleanupConfig; 23 : import com.google.gerrit.server.notedb.StoreSubmitRequirementsOp; 24 : import com.google.gerrit.server.plugincontext.PluginItemContext; 25 : import com.google.gerrit.server.query.change.ChangeData; 26 : import com.google.gerrit.server.update.BatchUpdate; 27 : import com.google.gerrit.server.update.UpdateException; 28 : import com.google.gerrit.server.util.time.TimeUtil; 29 : import com.google.inject.Inject; 30 : import com.google.inject.Singleton; 31 : import java.util.Collection; 32 : 33 : @Singleton 34 : public class BatchAbandon { 35 : private final AbandonOp.Factory abandonOpFactory; 36 : private final ChangeCleanupConfig cfg; 37 : private final PluginItemContext<AccountPatchReviewStore> accountPatchReviewStore; 38 : private final StoreSubmitRequirementsOp.Factory storeSubmitRequirementsOpFactory; 39 : 40 : @Inject 41 : BatchAbandon( 42 : AbandonOp.Factory abandonOpFactory, 43 : ChangeCleanupConfig cfg, 44 : PluginItemContext<AccountPatchReviewStore> accountPatchReviewStore, 45 138 : StoreSubmitRequirementsOp.Factory storeSubmitRequirementsOpFactory) { 46 138 : this.abandonOpFactory = abandonOpFactory; 47 138 : this.cfg = cfg; 48 138 : this.accountPatchReviewStore = accountPatchReviewStore; 49 138 : this.storeSubmitRequirementsOpFactory = storeSubmitRequirementsOpFactory; 50 138 : } 51 : 52 : /** 53 : * If an extension has more than one changes to abandon that belong to the same project, they 54 : * should use the batch instead of abandoning one by one. 55 : * 56 : * <p>It's the caller's responsibility to ensure that all jobs inside the same batch have the 57 : * matching project from its ChangeData. Violations will result in a ResourceConflictException. 58 : */ 59 : public void batchAbandon( 60 : BatchUpdate.Factory updateFactory, 61 : Project.NameKey project, 62 : CurrentUser user, 63 : Collection<ChangeData> changes, 64 : String msgTxt, 65 : NotifyResolver.Result notify) 66 : throws RestApiException, UpdateException { 67 1 : if (changes.isEmpty()) { 68 0 : return; 69 : } 70 1 : AccountState accountState = user.isIdentifiedUser() ? user.asIdentifiedUser().state() : null; 71 1 : try (BatchUpdate u = updateFactory.create(project, user, TimeUtil.now())) { 72 1 : u.setNotify(notify); 73 1 : for (ChangeData change : changes) { 74 1 : if (!project.equals(change.project())) { 75 1 : throw new ResourceConflictException( 76 1 : String.format( 77 : "Project name \"%s\" doesn't match \"%s\"", 78 1 : change.project().get(), project.get())); 79 : } 80 1 : u.addOp(change.getId(), abandonOpFactory.create(accountState, msgTxt)); 81 1 : u.addOp( 82 1 : change.getId(), 83 1 : storeSubmitRequirementsOpFactory.create(change.submitRequirements().values(), change)); 84 1 : } 85 1 : u.execute(); 86 : 87 1 : if (cfg.getCleanupAccountPatchReview()) { 88 0 : cleanupAccountPatchReview(changes); 89 : } 90 : } 91 1 : } 92 : 93 : public void batchAbandon( 94 : BatchUpdate.Factory updateFactory, 95 : Project.NameKey project, 96 : CurrentUser user, 97 : Collection<ChangeData> changes, 98 : String msgTxt) 99 : throws RestApiException, UpdateException { 100 1 : batchAbandon(updateFactory, project, user, changes, msgTxt, NotifyResolver.Result.all()); 101 1 : } 102 : 103 : public void batchAbandon( 104 : BatchUpdate.Factory updateFactory, 105 : Project.NameKey project, 106 : CurrentUser user, 107 : Collection<ChangeData> changes) 108 : throws RestApiException, UpdateException { 109 0 : batchAbandon(updateFactory, project, user, changes, "", NotifyResolver.Result.all()); 110 0 : } 111 : 112 : private void cleanupAccountPatchReview(Collection<ChangeData> changes) { 113 0 : for (ChangeData change : changes) { 114 0 : accountPatchReviewStore.run(s -> s.clearReviewed(change.getId())); 115 0 : } 116 0 : } 117 : }