Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-10713 Badges can only be used on projects, long living branches…
… and applications
- Loading branch information
1 parent
aff59ce
commit 0f0c3b7
Showing
26 changed files
with
664 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
server/sonar-server/src/main/java/org/sonar/server/badge/ws/ProjectBadgesSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,96 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2018 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program 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. | |||
* | |||
* This program 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.badge.ws; | |||
|
|||
import java.util.Optional; | |||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.component.BranchDto; | |||
import org.sonar.db.component.ComponentDto; | |||
import org.sonar.server.component.ComponentFinder; | |||
import org.sonar.server.exceptions.NotFoundException; | |||
import org.sonar.server.user.UserSession; | |||
|
|||
import static org.sonar.api.resources.Qualifiers.APP; | |||
import static org.sonar.api.resources.Qualifiers.PROJECT; | |||
import static org.sonar.api.web.UserRole.USER; | |||
import static org.sonar.db.component.BranchType.LONG; | |||
import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001; | |||
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; | |||
|
|||
public class ProjectBadgesSupport { | |||
|
|||
private static final String PARAM_PROJECT = "project"; | |||
private static final String PARAM_BRANCH = "branch"; | |||
|
|||
private final UserSession userSession; | |||
private final DbClient dbClient; | |||
private final ComponentFinder componentFinder; | |||
|
|||
public ProjectBadgesSupport(UserSession userSession, DbClient dbClient, ComponentFinder componentFinder) { | |||
this.userSession = userSession; | |||
this.dbClient = dbClient; | |||
this.componentFinder = componentFinder; | |||
} | |||
|
|||
void addProjectAndBranchParams(WebService.NewAction action) { | |||
action.createParam(PARAM_PROJECT) | |||
.setDescription("Project or application key") | |||
.setRequired(true) | |||
.setExampleValue(KEY_PROJECT_EXAMPLE_001); | |||
action | |||
.createParam(PARAM_BRANCH) | |||
.setDescription("Long living branch key") | |||
.setExampleValue(KEY_BRANCH_EXAMPLE_001); | |||
} | |||
|
|||
ComponentDto getComponent(DbSession dbSession, Request request) { | |||
try { | |||
String projectKey = request.mandatoryParam(PARAM_PROJECT); | |||
String branchName = request.param(PARAM_BRANCH); | |||
ComponentDto project = componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, projectKey, branchName, null); | |||
checkComponentType(dbSession, project); | |||
userSession.checkComponentPermission(USER, project); | |||
return project; | |||
} catch (NotFoundException e) { | |||
throw new NotFoundException("Project has not been found"); | |||
} | |||
} | |||
|
|||
private void checkComponentType(DbSession dbSession, ComponentDto project) { | |||
Optional<BranchDto> branch = dbClient.branchDao().selectByUuid(dbSession, project.uuid()); | |||
if (project.isPrivate()) { | |||
throw generateInvalidProjectExcpetion(); | |||
} | |||
if (branch.isPresent() && !branch.get().getBranchType().equals(LONG)) { | |||
throw generateInvalidProjectExcpetion(); | |||
} | |||
if (!project.qualifier().equals(PROJECT) && !project.qualifier().equals(APP)) { | |||
throw generateInvalidProjectExcpetion(); | |||
} | |||
} | |||
|
|||
private static ProjectBadgesException generateInvalidProjectExcpetion() { | |||
return new ProjectBadgesException("Project is invalid"); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...r/sonar-server/src/main/resources/org/sonar/server/badge/ws/templates/error.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions
1
...ver/src/main/resources/org/sonar/server/badge/ws/templates/sonarcloud/badge.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions
1
...esources/org/sonar/server/badge/ws/templates/sonarcloud/quality_gate_failed.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.