3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.almintegration.ws.github;
22 import java.util.List;
23 import java.util.Optional;
24 import org.sonar.alm.client.github.GithubApplicationClient;
25 import org.sonar.alm.client.github.GithubApplicationClient.Organization;
26 import org.sonar.alm.client.github.GithubApplicationClientImpl;
27 import org.sonar.alm.client.github.security.AccessToken;
28 import org.sonar.alm.client.github.security.UserAccessToken;
29 import org.sonar.api.server.ws.Request;
30 import org.sonar.api.server.ws.Response;
31 import org.sonar.api.server.ws.WebService;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.alm.pat.AlmPatDto;
35 import org.sonar.db.alm.setting.AlmSettingDto;
36 import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
37 import org.sonar.server.exceptions.BadRequestException;
38 import org.sonar.server.exceptions.NotFoundException;
39 import org.sonar.server.user.UserSession;
40 import org.sonarqube.ws.AlmIntegrations;
41 import org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse;
42 import org.sonarqube.ws.Common;
44 import static java.util.Objects.requireNonNull;
45 import static org.sonar.api.server.ws.WebService.Param.PAGE;
46 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
47 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
48 import static org.sonar.server.ws.WsUtils.writeProtobuf;
50 public class ListGithubOrganizationsAction implements AlmIntegrationsWsAction {
52 public static final String PARAM_ALM_SETTING = "almSetting";
53 public static final String PARAM_TOKEN = "token";
55 private final DbClient dbClient;
56 private final UserSession userSession;
57 private final GithubApplicationClient githubApplicationClient;
59 public ListGithubOrganizationsAction(DbClient dbClient, UserSession userSession, GithubApplicationClientImpl githubApplicationClient) {
60 this.dbClient = dbClient;
61 this.userSession = userSession;
62 this.githubApplicationClient = githubApplicationClient;
66 public void define(WebService.NewController context) {
67 WebService.NewAction action = context.createAction("list_github_organizations")
68 .setDescription("List GitHub organizations<br/>" +
69 "Requires the 'Create Projects' permission")
74 action.createParam(PARAM_ALM_SETTING)
76 .setMaximumLength(200)
77 .setDescription("ALM setting key");
79 action.createParam(PARAM_TOKEN)
80 .setMaximumLength(200)
81 .setDescription("Github authorization code");
83 action.createParam(PAGE)
84 .setDescription("Index of the page to display")
86 action.createParam(PAGE_SIZE)
87 .setDescription("Size for the paging to apply")
88 .setDefaultValue(100);
92 public void handle(Request request, Response response) {
93 ListGithubOrganizationsWsResponse getResponse = doHandle(request);
94 writeProtobuf(getResponse, request, response);
97 private ListGithubOrganizationsWsResponse doHandle(Request request) {
98 try (DbSession dbSession = dbClient.openSession(false)) {
99 userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
101 String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
102 AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey)
103 .orElseThrow(() -> new NotFoundException(String.format("GitHub ALM Setting '%s' not found", almSettingKey)));
105 String userUuid = requireNonNull(userSession.getUuid(), "User UUID is not null");
106 String url = requireNonNull(almSettingDto.getUrl(), String.format("No URL set for GitHub ALM '%s'", almSettingKey));
108 AccessToken accessToken;
109 if (request.hasParam(PARAM_TOKEN)) {
110 String code = request.mandatoryParam(PARAM_TOKEN);
111 String clientId = requireNonNull(almSettingDto.getClientId(), String.format("No clientId set for GitHub ALM '%s'", almSettingKey));
112 String clientSecret = requireNonNull(almSettingDto.getClientSecret(), String.format("No clientSecret set for GitHub ALM '%s'", almSettingKey));
115 accessToken = githubApplicationClient.createUserAccessToken(url, clientId, clientSecret, code);
116 } catch (IllegalArgumentException e) {
117 // it could also be that the code has expired!
118 throw BadRequestException.create("Unable to authenticate with GitHub. "
119 + "Check the GitHub App client ID and client secret configured in the Global Settings and try again.");
121 Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
122 if (almPatDto.isPresent()) {
123 AlmPatDto almPat = almPatDto.get();
124 almPat.setPersonalAccessToken(accessToken.getValue());
125 dbClient.almPatDao().update(dbSession, almPat);
127 AlmPatDto almPat = new AlmPatDto()
128 .setPersonalAccessToken(accessToken.getValue())
129 .setAlmSettingUuid(almSettingDto.getUuid())
130 .setUserUuid(userUuid);
131 dbClient.almPatDao().insert(dbSession, almPat);
135 accessToken = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto)
136 .map(AlmPatDto::getPersonalAccessToken)
137 .map(UserAccessToken::new)
138 .orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
141 int page = request.hasParam(PAGE) ? request.mandatoryParamAsInt(PAGE) : 1;
142 int pageSize = request.hasParam(PAGE_SIZE) ? request.mandatoryParamAsInt(PAGE_SIZE) : 100;
143 GithubApplicationClient.Organizations githubOrganizations = githubApplicationClient.listOrganizations(url, accessToken, page, pageSize);
145 ListGithubOrganizationsWsResponse.Builder response = ListGithubOrganizationsWsResponse.newBuilder()
146 .setPaging(Common.Paging.newBuilder()
148 .setPageSize(pageSize)
149 .setTotal(githubOrganizations.getTotal())
152 List<Organization> organizations = githubOrganizations.getOrganizations();
153 if (organizations != null) {
155 .forEach(githubOrganization -> response.addOrganizations(AlmIntegrations.GithubOrganization.newBuilder()
156 .setKey(githubOrganization.getLogin())
157 .setName(githubOrganization.getLogin())
161 return response.build();