{"action":"created","comment":{"url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments/27718800","id":27718800,"diff_hunk":"@@ -0,0 +1,164 @@\n+/*\n+ * SonarQube, open source software quality management tool.\n+ * Copyright (C) 2008-2014 SonarSource\n+ * mailto:contact AT sonarsource DOT com\n+ *\n+ * SonarQube is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU Lesser General Public\n+ * License as published by the Free Software Foundation; either\n+ * version 3 of the License, or (at your option) any later version.\n+ *\n+ * SonarQube is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n+ * Lesser General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU Lesser General Public License\n+ * along with this program; if not, write to the Free Software Foundation,\n+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n+ */\n+package org.sonar.server.qualityprofile.ws;\n+\n+import com.google.common.base.Predicate;\n+import com.google.common.collect.Collections2;\n+import com.google.common.collect.Iterables;\n+import com.google.common.collect.Lists;\n+import org.sonar.api.server.ws.Request;\n+import org.sonar.api.server.ws.Response;\n+import org.sonar.api.server.ws.WebService.NewAction;\n+import org.sonar.api.server.ws.WebService.NewController;\n+import org.sonar.api.utils.Paging;\n+import org.sonar.api.utils.text.JsonWriter;\n+import org.sonar.api.web.UserRole;\n+import org.sonar.core.persistence.DbSession;\n+import org.sonar.core.qualityprofile.db.ProjectQprofileAssociationDto;\n+import org.sonar.core.util.NonNullInputFunction;\n+import org.sonar.server.db.DbClient;\n+import org.sonar.server.exceptions.NotFoundException;\n+import org.sonar.server.user.UserSession;\n+\n+import java.util.Collection;\n+import java.util.List;\n+\n+public class QProfileProjectsAction implements BaseQProfileWsAction {\n+\n+  private static final String PARAM_KEY = \"key\";\n+  private static final String PARAM_SELECTED = \"selected\";\n+  private static final String PARAM_QUERY = \"query\";\n+  private static final String PARAM_PAGE_SIZE = \"pageSize\";\n+  private static final String PARAM_PAGE = \"page\";\n+\n+  private static final String SELECTION_ALL = \"all\";\n+  private static final String SELECTION_SELECTED = \"selected\";\n+  private static final String SELECTION_DESELECTED = \"deselected\";\n+\n+  private final DbClient dbClient;\n+\n+  public QProfileProjectsAction(DbClient dbClient) {\n+    this.dbClient = dbClient;\n+  }\n+\n+  @Override\n+  public void define(NewController controller) {\n+    NewAction projects = controller.createAction(\"projects\")\n+      .setSince(\"5.2\")\n+      .setHandler(this)\n+      .setDescription(\"List projects with their association status regarding a quality profile.\");\n+    projects.createParam(PARAM_KEY)\n+      .setDescription(\"A quality profile key.\")\n+      .setRequired(true)\n+      .setExampleValue(\"sonar-way-java-12345\");\n+    projects.createParam(PARAM_SELECTED)\n+      .setDescription(\"If specified, return only selected or deselected projects.\")\n+      .setPossibleValues(SELECTION_SELECTED, SELECTION_DESELECTED, SELECTION_ALL)\n+      .setDefaultValue(SELECTION_ALL);\n+    projects.createParam(PARAM_QUERY)\n+      .setDescription(\"If specified, return only projects whose name match the query.\");\n+    projects.createParam(PARAM_PAGE_SIZE)\n+      .setDescription(\"Size for the paging to apply\").setDefaultValue(100);\n+    projects.createParam(PARAM_PAGE)\n+      .setDescription(\"Index of the page to display\").setDefaultValue(1);\n+  }\n+\n+  @Override\n+  public void handle(Request request, Response response) throws Exception {\n+    String profileKey = request.mandatoryParam(PARAM_KEY);\n+\n+    DbSession session = dbClient.openSession(false);\n+\n+    try {\n+      checkProfileExists(profileKey, session);\n+      String selected = request.param(PARAM_SELECTED);\n+      String query = request.param(PARAM_QUERY);\n+      int pageSize = request.mandatoryParamAsInt(PARAM_PAGE_SIZE);\n+      int page = request.mandatoryParamAsInt(PARAM_PAGE);\n+\n+      List<ProjectQprofileAssociationDto> projects = loadProjects(profileKey, session, selected, query);\n+\n+      Collection<Long> projectIds = Collections2.transform(projects, new NonNullInputFunction<ProjectQprofileAssociationDto, Long>() {\n+        @Override\n+        protected Long doApply(ProjectQprofileAssociationDto input) {\n+          return input.getProjectId();\n+        }\n+      });\n+\n+      final Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(session, projectIds, UserSession.get().userId(), UserRole.USER);\n+      Iterable<ProjectQprofileAssociationDto> authorizedProjects = Iterables.filter(projects, new Predicate<ProjectQprofileAssociationDto>() {\n+        @Override\n+        public boolean apply(ProjectQprofileAssociationDto input) {\n+          return authorizedProjectIds.contains(input.getProjectId());\n+        }\n+      });\n+\n+      Paging paging = Paging.create(pageSize, page, authorizedProjectIds.size());\n+\n+      List<ProjectQprofileAssociationDto> pagedAuthorizedProjects = Lists.newArrayList(authorizedProjects);\n+      if (pagedAuthorizedProjects.size() <= paging.offset()) {\n+        pagedAuthorizedProjects = Lists.newArrayList();\n+      } else if (pagedAuthorizedProjects.size() > paging.pageSize()) {\n+        pagedAuthorizedProjects = pagedAuthorizedProjects.subList(paging.offset(), paging.offset() + pageSize);\n+      }\n+\n+      writeProjects(response.newJsonWriter(), pagedAuthorizedProjects, paging);\n+    } finally {\n+      session.close();\n+    }\n+  }\n+\n+  private void checkProfileExists(String profileKey, DbSession session) {\n+    try {\n+      dbClient.qualityProfileDao().getNonNullByKey(session, profileKey);\n+    } catch (IllegalArgumentException doesNotExist) {","path":"server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileProjectsAction.java","position":131,"original_position":131,"commit_id":"fc5c96c35f46609c17471abaa9e07fa5b139555e","original_commit_id":"fc5c96c35f46609c17471abaa9e07fa5b139555e","user":{"login":"jblievremont","id":1734254,"avatar_url":"https://avatars.githubusercontent.com/u/1734254?v=3","gravatar_id":"","url":"https://api.github.com/users/jblievremont","html_url":"https://github.com/jblievremont","followers_url":"https://api.github.com/users/jblievremont/followers","following_url":"https://api.github.com/users/jblievremont/following{/other_user}","gists_url":"https://api.github.com/users/jblievremont/gists{/gist_id}","starred_url":"https://api.github.com/users/jblievremont/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jblievremont/subscriptions","organizations_url":"https://api.github.com/users/jblievremont/orgs","repos_url":"https://api.github.com/users/jblievremont/repos","events_url":"https://api.github.com/users/jblievremont/events{/privacy}","received_events_url":"https://api.github.com/users/jblievremont/received_events","type":"User","site_admin":false},"body":":+1:","created_at":"2015-04-03T07:35:10Z","updated_at":"2015-04-03T07:35:10Z","html_url":"https://github.com/SonarSource/sonarqube/pull/189#discussion_r27718800","pull_request_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/189","_links":{"self":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments/27718800"},"html":{"href":"https://github.com/SonarSource/sonarqube/pull/189#discussion_r27718800"},"pull_request":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/189"}}},"pull_request":{"url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/189","id":32491905,"html_url":"https://github.com/SonarSource/sonarqube/pull/189","diff_url":"https://github.com/SonarSource/sonarqube/pull/189.diff","patch_url":"https://github.com/SonarSource/sonarqube/pull/189.patch","issue_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/189","number":189,"state":"open","locked":false,"title":"SONAR-5921 WS to list project/profile associations","user":{"login":"jblievremont","id":1734254,"avatar_url":"https://avatars.githubusercontent.com/u/1734254?v=3","gravatar_id":"","url":"https://api.github.com/users/jblievremont","html_url":"https://github.com/jblievremont","followers_url":"https://api.github.com/users/jblievremont/followers","following_url":"https://api.github.com/users/jblievremont/following{/other_user}","gists_url":"https://api.github.com/users/jblievremont/gists{/gist_id}","starred_url":"https://api.github.com/users/jblievremont/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jblievremont/subscriptions","organizations_url":"https://api.github.com/users/jblievremont/orgs","repos_url":"https://api.github.com/users/jblievremont/repos","events_url":"https://api.github.com/users/jblievremont/events{/privacy}","received_events_url":"https://api.github.com/users/jblievremont/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-04-02T08:31:04Z","updated_at":"2015-04-03T07:35:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e2715a77cd1e9faba4e2b8423d815a1c7e2085c7","assignee":{"login":"julienlancelot","id":370769,"avatar_url":"https://avatars.githubusercontent.com/u/370769?v=3","gravatar_id":"","url":"https://api.github.com/users/julienlancelot","html_url":"https://github.com/julienlancelot","followers_url":"https://api.github.com/users/julienlancelot/followers","following_url":"https://api.github.com/users/julienlancelot/following{/other_user}","gists_url":"https://api.github.com/users/julienlancelot/gists{/gist_id}","starred_url":"https://api.github.com/users/julienlancelot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/julienlancelot/subscriptions","organizations_url":"https://api.github.com/users/julienlancelot/orgs","repos_url":"https://api.github.com/users/julienlancelot/repos","events_url":"https://api.github.com/users/julienlancelot/events{/privacy}","received_events_url":"https://api.github.com/users/julienlancelot/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/189/commits","review_comments_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/189/comments","review_comment_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments{/number}","comments_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/189/comments","statuses_url":"https://api.github.com/repos/SonarSource/sonarqube/statuses/fc5c96c35f46609c17471abaa9e07fa5b139555e","head":{"label":"SonarSource:feature/qprofile_list_projects","ref":"feature/qprofile_list_projects","sha":"fc5c96c35f46609c17471abaa9e07fa5b139555e","user":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"repo":{"id":1222504,"name":"sonarqube","full_name":"SonarSource/sonarqube","owner":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SonarSource/sonarqube","description":"Put your technical debt under control","fork":false,"url":"https://api.github.com/repos/SonarSource/sonarqube","forks_url":"https://api.github.com/repos/SonarSource/sonarqube/forks","keys_url":"https://api.github.com/repos/SonarSource/sonarqube/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SonarSource/sonarqube/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SonarSource/sonarqube/teams","hooks_url":"https://api.github.com/repos/SonarSource/sonarqube/hooks","issue_events_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/events{/number}","events_url":"https://api.github.com/repos/SonarSource/sonarqube/events","assignees_url":"https://api.github.com/repos/SonarSource/sonarqube/assignees{/user}","branches_url":"https://api.github.com/repos/SonarSource/sonarqube/branches{/branch}","tags_url":"https://api.github.com/repos/SonarSource/sonarqube/tags","blobs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SonarSource/sonarqube/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/refs{/sha}","trees_url":"https://api.github.com/repos/SonarSource/sonarqube/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SonarSource/sonarqube/statuses/{sha}","languages_url":"https://api.github.com/repos/SonarSource/sonarqube/languages","stargazers_url":"https://api.github.com/repos/SonarSource/sonarqube/stargazers","contributors_url":"https://api.github.com/repos/SonarSource/sonarqube/contributors","subscribers_url":"https://api.github.com/repos/SonarSource/sonarqube/subscribers","subscription_url":"https://api.github.com/repos/SonarSource/sonarqube/subscription","commits_url":"https://api.github.com/repos/SonarSource/sonarqube/commits{/sha}","git_commits_url":"https://api.github.com/repos/SonarSource/sonarqube/git/commits{/sha}","comments_url":"https://api.github.com/repos/SonarSource/sonarqube/comments{/number}","issue_comment_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/comments{/number}","contents_url":"https://api.github.com/repos/SonarSource/sonarqube/contents/{+path}","compare_url":"https://api.github.com/repos/SonarSource/sonarqube/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SonarSource/sonarqube/merges","archive_url":"https://api.github.com/repos/SonarSource/sonarqube/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SonarSource/sonarqube/downloads","issues_url":"https://api.github.com/repos/SonarSource/sonarqube/issues{/number}","pulls_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls{/number}","milestones_url":"https://api.github.com/repos/SonarSource/sonarqube/milestones{/number}","notifications_url":"https://api.github.com/repos/SonarSource/sonarqube/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SonarSource/sonarqube/labels{/name}","releases_url":"https://api.github.com/repos/SonarSource/sonarqube/releases{/id}","created_at":"2011-01-05T11:05:17Z","updated_at":"2015-04-03T07:31:24Z","pushed_at":"2015-04-03T07:31:56Z","git_url":"git://github.com/SonarSource/sonarqube.git","ssh_url":"git@github.com:SonarSource/sonarqube.git","clone_url":"https://github.com/SonarSource/sonarqube.git","svn_url":"https://github.com/SonarSource/sonarqube","homepage":"http://www.sonarqube.org","size":635659,"stargazers_count":561,"watchers_count":561,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":309,"mirror_url":null,"open_issues_count":12,"forks":309,"open_issues":12,"watchers":561,"default_branch":"master"}},"base":{"label":"SonarSource:master","ref":"master","sha":"62aa1093cac90e198376cb6968842d34adf870a3","user":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"repo":{"id":1222504,"name":"sonarqube","full_name":"SonarSource/sonarqube","owner":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SonarSource/sonarqube","description":"Put your technical debt under control","fork":false,"url":"https://api.github.com/repos/SonarSource/sonarqube","forks_url":"https://api.github.com/repos/SonarSource/sonarqube/forks","keys_url":"https://api.github.com/repos/SonarSource/sonarqube/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SonarSource/sonarqube/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SonarSource/sonarqube/teams","hooks_url":"https://api.github.com/repos/SonarSource/sonarqube/hooks","issue_events_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/events{/number}","events_url":"https://api.github.com/repos/SonarSource/sonarqube/events","assignees_url":"https://api.github.com/repos/SonarSource/sonarqube/assignees{/user}","branches_url":"https://api.github.com/repos/SonarSource/sonarqube/branches{/branch}","tags_url":"https://api.github.com/repos/SonarSource/sonarqube/tags","blobs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SonarSource/sonarqube/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/refs{/sha}","trees_url":"https://api.github.com/repos/SonarSource/sonarqube/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SonarSource/sonarqube/statuses/{sha}","languages_url":"https://api.github.com/repos/SonarSource/sonarqube/languages","stargazers_url":"https://api.github.com/repos/SonarSource/sonarqube/stargazers","contributors_url":"https://api.github.com/repos/SonarSource/sonarqube/contributors","subscribers_url":"https://api.github.com/repos/SonarSource/sonarqube/subscribers","subscription_url":"https://api.github.com/repos/SonarSource/sonarqube/subscription","commits_url":"https://api.github.com/repos/SonarSource/sonarqube/commits{/sha}","git_commits_url":"https://api.github.com/repos/SonarSource/sonarqube/git/commits{/sha}","comments_url":"https://api.github.com/repos/SonarSource/sonarqube/comments{/number}","issue_comment_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/comments{/number}","contents_url":"https://api.github.com/repos/SonarSource/sonarqube/contents/{+path}","compare_url":"https://api.github.com/repos/SonarSource/sonarqube/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SonarSource/sonarqube/merges","archive_url":"https://api.github.com/repos/SonarSource/sonarqube/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SonarSource/sonarqube/downloads","issues_url":"https://api.github.com/repos/SonarSource/sonarqube/issues{/number}","pulls_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls{/number}","milestones_url":"https://api.github.com/repos/SonarSource/sonarqube/milestones{/number}","notifications_url":"https://api.github.com/repos/SonarSource/sonarqube/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SonarSource/sonarqube/labels{/name}","releases_url":"https://api.github.com/repos/SonarSource/sonarqube/releases{/id}","created_at":"2011-01-05T11:05:17Z","updated_at":"2015-04-03T07:31:24Z","pushed_at":"2015-04-03T07:31:56Z","git_url":"git://github.com/SonarSource/sonarqube.git","ssh_url":"git@github.com:SonarSource/sonarqube.git","clone_url":"https://github.com/SonarSource/sonarqube.git","svn_url":"https://github.com/SonarSource/sonarqube","homepage":"http://www.sonarqube.org","size":635659,"stargazers_count":561,"watchers_count":561,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":309,"mirror_url":null,"open_issues_count":12,"forks":309,"open_issues":12,"watchers":561,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/189"},"html":{"href":"https://github.com/SonarSource/sonarqube/pull/189"},"issue":{"href":"https://api.github.com/repos/SonarSource/sonarqube/issues/189"},"comments":{"href":"https://api.github.com/repos/SonarSource/sonarqube/issues/189/comments"},"review_comments":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/189/comments"},"review_comment":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/189/commits"},"statuses":{"href":"https://api.github.com/repos/SonarSource/sonarqube/statuses/fc5c96c35f46609c17471abaa9e07fa5b139555e"}}}}