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.config.internal.Encryption;
30 import org.sonar.api.config.internal.Settings;
31 import org.sonar.api.server.ws.Request;
32 import org.sonar.api.server.ws.Response;
33 import org.sonar.api.server.ws.WebService;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.alm.pat.AlmPatDto;
37 import org.sonar.db.alm.setting.AlmSettingDto;
38 import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
39 import org.sonar.server.exceptions.BadRequestException;
40 import org.sonar.server.exceptions.NotFoundException;
41 import org.sonar.server.user.UserSession;
42 import org.sonarqube.ws.AlmIntegrations;
43 import org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse;
44 import org.sonarqube.ws.Common;
46 import static java.util.Objects.requireNonNull;
47 import static org.sonar.api.server.ws.WebService.Param.PAGE;
48 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
49 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
50 import static org.sonar.server.ws.WsUtils.writeProtobuf;
52 public class ListGithubOrganizationsAction implements AlmIntegrationsWsAction {
54 public static final String PARAM_ALM_SETTING = "almSetting";
55 public static final String PARAM_TOKEN = "token";
57 private final DbClient dbClient;
58 private final Encryption encryption;
59 private final UserSession userSession;
60 private final GithubApplicationClient githubApplicationClient;
62 public ListGithubOrganizationsAction(DbClient dbClient, Settings settings, UserSession userSession,
63 GithubApplicationClientImpl githubApplicationClient) {
64 this.dbClient = dbClient;
65 this.encryption = settings.getEncryption();
66 this.userSession = userSession;
67 this.githubApplicationClient = githubApplicationClient;
71 public void define(WebService.NewController context) {
72 WebService.NewAction action = context.createAction("list_github_organizations")
73 .setDescription("List GitHub organizations<br/>" +
74 "Requires the 'Create Projects' permission")
79 action.createParam(PARAM_ALM_SETTING)
81 .setMaximumLength(200)
82 .setDescription("ALM setting key");
84 action.createParam(PARAM_TOKEN)
85 .setMaximumLength(200)
86 .setDescription("Github authorization code");
88 action.createParam(PAGE)
89 .setDescription("Index of the page to display")
91 action.createParam(PAGE_SIZE)
92 .setDescription("Size for the paging to apply")
93 .setDefaultValue(100);
97 public void handle(Request request, Response response) {
98 ListGithubOrganizationsWsResponse getResponse = doHandle(request);
99 writeProtobuf(getResponse, request, response);
102 private ListGithubOrganizationsWsResponse doHandle(Request request) {
103 try (DbSession dbSession = dbClient.openSession(false)) {
104 userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
106 String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
107 AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey)
108 .orElseThrow(() -> new NotFoundException(String.format("GitHub ALM Setting '%s' not found", almSettingKey)));
110 String userUuid = requireNonNull(userSession.getUuid(), "User UUID is not null");
111 String url = requireNonNull(almSettingDto.getUrl(), String.format("No URL set for GitHub ALM '%s'", almSettingKey));
113 AccessToken accessToken;
114 if (request.hasParam(PARAM_TOKEN)) {
115 String code = request.mandatoryParam(PARAM_TOKEN);
116 String clientId = requireNonNull(almSettingDto.getClientId(), String.format("No clientId set for GitHub ALM '%s'", almSettingKey));
117 String clientSecret = requireNonNull(almSettingDto.getDecryptedClientSecret(encryption), String.format("No clientSecret set for GitHub ALM '%s'",
121 accessToken = githubApplicationClient.createUserAccessToken(url, clientId, clientSecret, code);
122 } catch (IllegalArgumentException e) {
123 // it could also be that the code has expired!
124 throw BadRequestException.create("Unable to authenticate with GitHub. "
125 + "Check the GitHub App client ID and client secret configured in the Global Settings and try again.");
127 Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
128 if (almPatDto.isPresent()) {
129 AlmPatDto almPat = almPatDto.get();
130 almPat.setPersonalAccessToken(accessToken.getValue());
131 dbClient.almPatDao().update(dbSession, almPat, userSession.getLogin(), almSettingDto.getKey());
133 AlmPatDto almPat = new AlmPatDto()
134 .setPersonalAccessToken(accessToken.getValue())
135 .setAlmSettingUuid(almSettingDto.getUuid())
136 .setUserUuid(userUuid);
137 dbClient.almPatDao().insert(dbSession, almPat, userSession.getLogin(), almSettingDto.getKey());
141 accessToken = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto)
142 .map(AlmPatDto::getPersonalAccessToken)
143 .map(UserAccessToken::new)
144 .orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
147 int page = request.hasParam(PAGE) ? request.mandatoryParamAsInt(PAGE) : 1;
148 int pageSize = request.hasParam(PAGE_SIZE) ? request.mandatoryParamAsInt(PAGE_SIZE) : 100;
149 GithubApplicationClient.Organizations githubOrganizations = githubApplicationClient.listOrganizations(url, accessToken, page, pageSize);
151 ListGithubOrganizationsWsResponse.Builder response = ListGithubOrganizationsWsResponse.newBuilder()
152 .setPaging(Common.Paging.newBuilder()
154 .setPageSize(pageSize)
155 .setTotal(githubOrganizations.getTotal())
158 List<Organization> organizations = githubOrganizations.getOrganizations();
159 if (organizations != null) {
161 .forEach(githubOrganization -> response.addOrganizations(AlmIntegrations.GithubOrganization.newBuilder()
162 .setKey(githubOrganization.getLogin())
163 .setName(githubOrganization.getLogin())
167 return response.build();