aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java
blob: 301d0d2bf15878f35b99495c33d3ccd59ea1208a (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
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2013 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.sonar.server.issue;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import org.sonar.api.ServerComponent;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.IssueQuery;
import org.sonar.api.issue.IssueQueryResult;
import org.sonar.api.issue.action.Action;
import org.sonar.api.issue.action.Function;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.issue.internal.IssueChangeContext;
import org.sonar.api.web.UserRole;
import org.sonar.core.issue.IssueUpdater;
import org.sonar.core.issue.db.IssueStorage;
import org.sonar.server.user.UserSession;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

public class ActionService implements ServerComponent {

  private final DefaultIssueFinder finder;
  private final IssueStorage issueStorage;
  private final IssueUpdater updater;
  private final List<Action> actions;

  public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, List<Action> actions) {
    this.finder = finder;
    this.issueStorage = issueStorage;
    this.updater = updater;
    this.actions = actions;
  }

  public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater) {
    this(finder, issueStorage, updater, Collections.<Action>emptyList());
  }

  public List<Action> listAvailableActions(final Issue issue) {
    return newArrayList(Iterables.filter(actions, new Predicate<Action>() {
      @Override
      public boolean apply(Action action) {
        return action.supports(issue);
      }
    }));
  }

  public List<Action> listAvailableActions(String issueKey) {
    IssueQueryResult queryResult = loadIssue(issueKey);
    final DefaultIssue issue = (DefaultIssue) queryResult.first();
    if (issue == null) {
      throw new IllegalArgumentException("Issue is not found : " + issueKey);
    }

    return listAvailableActions(issue);
  }

  public Issue execute(String issueKey, String actionKey, UserSession userSession) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(actionKey), "Missing action");

    IssueQueryResult queryResult = loadIssue(issueKey);
    DefaultIssue issue = (DefaultIssue) queryResult.first();
    if (issue == null) {
      throw new IllegalArgumentException("Issue is not found : " + issueKey);
    }

    Action action = getAction(actionKey);
    if (action == null) {
      throw new IllegalArgumentException("Action is not found : " + actionKey);
    }
    if (!action.supports(issue)) {
      throw new IllegalStateException("A condition is not respected");
    }

    IssueChangeContext changeContext = IssueChangeContext.createUser(new Date(), userSession.login());
    FunctionContext functionContext = new FunctionContext(updater, issue, changeContext);
    for (Function function : action.functions()) {
      function.execute(functionContext);
    }
    issueStorage.save(issue);
    return issue;
  }

  public IssueQueryResult loadIssue(String issueKey) {
    IssueQuery query = IssueQuery.builder().issueKeys(newArrayList(issueKey)).requiredRole(UserRole.USER).build();
    return finder.find(query);
  }

  @CheckForNull
  private Action getAction(final String actionKey) {
    return Iterables.find(actions, new Predicate<Action>() {
      @Override
      public boolean apply(Action action) {
        return action.key().equals(actionKey);
      }
    }, null);
  }

  static class FunctionContext implements Function.Context {

    private final DefaultIssue issue;
    private final IssueUpdater updater;
    private final IssueChangeContext changeContext;

    FunctionContext(IssueUpdater updater, DefaultIssue issue, IssueChangeContext changeContext) {
      this.updater = updater;
      this.issue = issue;
      this.changeContext = changeContext;
    }

    @Override
    public Issue issue() {
      return issue;
    }

    @Override
    public Function.Context setAttribute(String key, @Nullable String value) {
      updater.setAttribute(issue, key, value, changeContext);
      return this;
    }

    @Override
    public Function.Context addComment(String text) {
      updater.addComment(issue, text, changeContext);
      return this;
    }
  }

}