Search in sources :

Example 1 with ProtectedTag

use of org.gitlab4j.api.models.ProtectedTag in project legend-sdlc by finos.

the class GitLabProjectApi method checkUserReleasePermission.

private boolean checkUserReleasePermission(GitLabProjectId projectId, ProjectAuthorizationAction action, AccessLevel accessLevel) {
    try {
        List<ProtectedTag> protectedTags = withRetries(() -> getGitLabApi(projectId.getGitLabMode()).getTagsApi().getProtectedTags(projectId.getGitLabId()));
        if (protectedTags == null || protectedTags.isEmpty()) {
            // By default user can perform a release if the user has developer access or above https://docs.gitlab.com/ee/user/permissions.html#release-permissions-with-protected-tags
            return defaultReleaseAction(accessLevel);
        }
        protectedTags = protectedTags.stream().filter(a -> a.getName().startsWith("release") || a.getName().startsWith("version")).collect(Collectors.toList());
        for (ProtectedTag tag : protectedTags) {
            if (tag.getCreateAccessLevels().isEmpty()) {
                return defaultReleaseAction(accessLevel);
            }
            // with th release protected tag the user must have the min access_level
            List<ProtectedTag.CreateAccessLevel> matchedTags = tag.getCreateAccessLevels().stream().filter(a -> a.getAccess_level().value >= accessLevel.value).collect(Collectors.toList());
            // if the  machedTags are empty or null user access does not match any of the protected tags
            if (matchedTags.isEmpty()) {
                return defaultReleaseAction(accessLevel);
            }
            // User does not meet all criteria not authorized for the action
            if (matchedTags.size() != tag.getCreateAccessLevels().size()) {
                return false;
            }
        }
    } catch (Exception e) {
        throw buildException(e, () -> "Failed to get protected tags for " + projectId.getGitLabId());
    }
    return false;
}
Also used : ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) ProjectStructureConfiguration(org.finos.legend.sdlc.server.project.config.ProjectStructureConfiguration) Branch(org.gitlab4j.api.models.Branch) GitLabConfiguration(org.finos.legend.sdlc.server.gitlab.GitLabConfiguration) ProjectAuthorizationAction(org.finos.legend.sdlc.domain.model.project.accessRole.ProjectAuthorizationAction) LoggerFactory(org.slf4j.LoggerFactory) AccessLevel(org.gitlab4j.api.models.AccessLevel) GitLabApiTools(org.finos.legend.sdlc.server.gitlab.tools.GitLabApiTools) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) MergeRequest(org.gitlab4j.api.models.MergeRequest) BackgroundTaskProcessor(org.finos.legend.sdlc.server.tools.BackgroundTaskProcessor) EnumSet(java.util.EnumSet) Visibility(org.gitlab4j.api.models.Visibility) RepositoryApi(org.gitlab4j.api.RepositoryApi) Pager(org.gitlab4j.api.Pager) Set(java.util.Set) Iterate(org.eclipse.collections.impl.utility.Iterate) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ProjectConfigurationUpdateBuilder(org.finos.legend.sdlc.server.project.ProjectConfigurationUpdateBuilder) List(java.util.List) Stream(java.util.stream.Stream) ProjectStructurePlatformExtensions(org.finos.legend.sdlc.server.project.ProjectStructurePlatformExtensions) Pattern(java.util.regex.Pattern) GitLabApi(org.gitlab4j.api.GitLabApi) ProjectApi(org.finos.legend.sdlc.server.domain.api.project.ProjectApi) Permissions(org.gitlab4j.api.models.Permissions) Lists(org.eclipse.collections.api.factory.Lists) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) ProjectStructureExtensionProvider(org.finos.legend.sdlc.server.project.extension.ProjectStructureExtensionProvider) ProjectCreationConfiguration(org.finos.legend.sdlc.server.project.config.ProjectCreationConfiguration) AccessRole(org.finos.legend.sdlc.domain.model.project.accessRole.AccessRole) Inject(javax.inject.Inject) StreamSupport(java.util.stream.StreamSupport) PagerTools(org.finos.legend.sdlc.server.gitlab.tools.PagerTools) Status(javax.ws.rs.core.Response.Status) Sets(org.eclipse.collections.api.factory.Sets) Logger(org.slf4j.Logger) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode) ProtectedTag(org.gitlab4j.api.models.ProtectedTag) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) ListIterate(org.eclipse.collections.impl.utility.ListIterate) WorkspaceType(org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType) ProjectStructureVersion(org.finos.legend.sdlc.domain.model.project.configuration.ProjectStructureVersion) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Project(org.finos.legend.sdlc.domain.model.project.Project) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) Comparator(java.util.Comparator) ProjectAccess(org.gitlab4j.api.models.ProjectAccess) Collections(java.util.Collections) ProtectedTag(org.gitlab4j.api.models.ProtectedTag) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException)

Example 2 with ProtectedTag

use of org.gitlab4j.api.models.ProtectedTag in project choerodon-starters by open-hand.

the class TagsApi method protectTag.

/**
 * Protects a single repository tag or several project repository tags using a wildcard protected tag.
 *
 * <pre><code>GitLab Endpoint: GET /projects/:id/protected_tags</code></pre>
 *
 * @param projectIdOrPath   id, path of the project, or a Project instance holding the project ID or path
 * @param name              the name of the tag or wildcard
 * @param createAccessLevel the access level allowed to create
 * @return a ProtectedTag instance
 * @throws GitLabApiException if any exception occurs
 */
public ProtectedTag protectTag(Object projectIdOrPath, String name, AccessLevel createAccessLevel) throws GitLabApiException {
    Form formData = new GitLabApiForm().withParam("name", name, true).withParam("create_access_level", createAccessLevel);
    Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "protected_tags");
    return (response.readEntity(ProtectedTag.class));
}
Also used : Response(javax.ws.rs.core.Response) Form(javax.ws.rs.core.Form) ProtectedTag(org.gitlab4j.api.models.ProtectedTag)

Aggregations

ProtectedTag (org.gitlab4j.api.models.ProtectedTag)2 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 EnumSet (java.util.EnumSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 Inject (javax.inject.Inject)1 Form (javax.ws.rs.core.Form)1 Response (javax.ws.rs.core.Response)1 Status (javax.ws.rs.core.Response.Status)1 Lists (org.eclipse.collections.api.factory.Lists)1 Sets (org.eclipse.collections.api.factory.Sets)1 Iterate (org.eclipse.collections.impl.utility.Iterate)1 ListIterate (org.eclipse.collections.impl.utility.ListIterate)1 Project (org.finos.legend.sdlc.domain.model.project.Project)1