Search in sources :

Example 16 with Branch

use of net.nemerosa.ontrack.model.structure.Branch in project ontrack by nemerosa.

the class BuildFilterServiceIT method saveFilter_shared_after_account_one.

/**
 * Checks that sharing a saved filter overrides the account specific one.
 */
@Test
public void saveFilter_shared_after_account_one() throws Exception {
    // Branch
    Branch branch = doCreateBranch();
    // Account for the tests
    Account account = doCreateAccount();
    Account otherAccount = doCreateAccount();
    // Creates a filter for this account
    Ack ack = asAccount(account).call(() -> buildFilterService.saveFilter(branch.getId(), false, "MyFilter", NamedBuildFilterProvider.class.getName(), objectMapper.valueToTree(NamedBuildFilterData.of("1"))));
    assertTrue("Account filter saved", ack.isSuccess());
    // Makes sure we find this filter back when logged
    Collection<BuildFilterResource<?>> filters = asAccount(account).withView(branch).call(() -> buildFilterService.getBuildFilters(branch.getId()));
    assertEquals(1, filters.size());
    BuildFilterResource<?> filter = filters.iterator().next();
    assertEquals("MyFilter", filter.getName());
    assertFalse(filter.isShared());
    // ... but it is not available for anybody else
    assertTrue("Account filter not available for everybody else", asAccount(otherAccount).withView(branch).call(() -> buildFilterService.getBuildFilters(branch.getId()).isEmpty()));
    // Now, shares a filter with the same name
    ack = asAccount(account).with(branch.projectId(), ProjectView.class).with(branch.projectId(), BranchFilterMgt.class).call(() -> buildFilterService.saveFilter(branch.getId(), // Sharing
    true, "MyFilter", NamedBuildFilterProvider.class.getName(), objectMapper.valueToTree(NamedBuildFilterData.of("1"))));
    assertTrue("Account filter shared", ack.isSuccess());
    // Makes sure we find this filter back when logged
    filters = asAccount(account).call(() -> buildFilterService.getBuildFilters(branch.getId()));
    assertEquals(1, filters.size());
    filter = filters.iterator().next();
    assertEquals("MyFilter", filter.getName());
    assertTrue(filter.isShared());
    // ... and that it is available also for not logged users
    filters = asUser().withId(10).withView(branch).call(() -> buildFilterService.getBuildFilters(branch.getId()));
    assertEquals("Account filter available for everybody else", 1, filters.size());
    filter = filters.iterator().next();
    assertEquals("MyFilter", filter.getName());
    assertTrue(filter.isShared());
}
Also used : Account(net.nemerosa.ontrack.model.security.Account) Branch(net.nemerosa.ontrack.model.structure.Branch) BuildFilterResource(net.nemerosa.ontrack.model.buildfilter.BuildFilterResource) Ack(net.nemerosa.ontrack.model.Ack) ProjectView(net.nemerosa.ontrack.model.security.ProjectView) Test(org.junit.Test)

Example 17 with Branch

use of net.nemerosa.ontrack.model.structure.Branch in project ontrack by nemerosa.

the class GQLRootQueryBranches method branchFetcher.

private DataFetcher branchFetcher() {
    return environment -> {
        Integer id = environment.getArgument("id");
        String projectName = environment.getArgument("project");
        String name = environment.getArgument("name");
        Object propertyFilterArg = environment.getArgument(GQLInputPropertyFilter.ARGUMENT_NAME);
        // Per ID
        if (id != null) {
            checkArgList(environment, "id");
            return Collections.singletonList(structureService.getBranch(ID.of(id)));
        } else // Per project name, name or property filter
        if (isNotBlank(projectName) || isNotBlank(name) || propertyFilterArg != null) {
            // Project filter
            Predicate<Project> projectFilter = p -> true;
            if (isNotBlank(projectName)) {
                projectFilter = projectFilter.and(project -> StringUtils.equals(projectName, project.getName()));
            }
            // Branch filter
            Predicate<Branch> branchFilter = b -> true;
            if (isNotBlank(name)) {
                Pattern pattern = Pattern.compile(name);
                branchFilter = branchFilter.and(b -> pattern.matcher(b.getName()).matches());
            }
            // Property filter?
            if (propertyFilterArg != null) {
                PropertyFilter filterObject = propertyFilter.convert(propertyFilterArg);
                if (filterObject != null && StringUtils.isNotBlank(filterObject.getType())) {
                    branchFilter = branchFilter.and(propertyFilter.getFilter(filterObject));
                }
            }
            // Gets the list of authorised projects
            return structureService.getProjectList().stream().filter(projectFilter).flatMap(project -> structureService.getBranchesForProject(project.getId()).stream()).filter(branchFilter).collect(Collectors.toList());
        } else // No result to return
        {
            return Collections.emptyList();
        }
    };
}
Also used : GraphQLString(graphql.Scalars.GraphQLString) StructureService(net.nemerosa.ontrack.model.structure.StructureService) Predicate(java.util.function.Predicate) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) Autowired(org.springframework.beans.factory.annotation.Autowired) GraphQLArgument.newArgument(graphql.schema.GraphQLArgument.newArgument) GraphqlUtils.stdList(net.nemerosa.ontrack.graphql.support.GraphqlUtils.stdList) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) GraphQLFieldDefinition.newFieldDefinition(graphql.schema.GraphQLFieldDefinition.newFieldDefinition) GraphqlUtils.checkArgList(net.nemerosa.ontrack.graphql.support.GraphqlUtils.checkArgList) Component(org.springframework.stereotype.Component) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) DataFetcher(graphql.schema.DataFetcher) GraphQLInt(graphql.Scalars.GraphQLInt) Pattern(java.util.regex.Pattern) Project(net.nemerosa.ontrack.model.structure.Project) Collections(java.util.Collections) ID(net.nemerosa.ontrack.model.structure.ID) Branch(net.nemerosa.ontrack.model.structure.Branch) Project(net.nemerosa.ontrack.model.structure.Project) Pattern(java.util.regex.Pattern) Branch(net.nemerosa.ontrack.model.structure.Branch) GraphQLString(graphql.Scalars.GraphQLString)

Example 18 with Branch

use of net.nemerosa.ontrack.model.structure.Branch in project ontrack by nemerosa.

the class ResourceHttpMessageConverterIT method branch.

@Test
public void branch() throws IOException {
    // Objects
    Project p = Project.of(new NameDescription("P", "Projet créé")).withId(ID.of(1)).withSignature(SIGNATURE);
    Branch b = Branch.of(p, new NameDescription("B", "Branch")).withId(ID.of(1)).withSignature(SIGNATURE);
    // Message
    HttpOutputMessage message = mock(HttpOutputMessage.class);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    when(message.getBody()).thenReturn(output);
    // Serialization
    converter.writeInternal(b, message);
    // Content
    String json = new String(output.toByteArray(), "UTF-8");
    // Parsing
    JsonNode node = ObjectMapperFactory.create().readTree(json);
    // Check
    TestUtils.assertJsonEquals(object().with("id", 1).with("name", "B").with("description", "Branch").with("disabled", false).with("type", "CLASSIC").with("project", object().with("id", 1).with("name", "P").with("description", "Projet créé").with("disabled", false).with("signature", SIGNATURE_OBJECT).with("_self", "urn:test:net.nemerosa.ontrack.boot.ui.ProjectController#getProject:1").with("_branches", "urn:test:net.nemerosa.ontrack.boot.ui.BranchController#getBranchListForProject:1").with("_branchStatusViews", "urn:test:net.nemerosa.ontrack.boot.ui.ProjectController#getBranchStatusViews:1").with("_buildSearch", "urn:test:net.nemerosa.ontrack.boot.ui.BuildController#buildSearchForm:1").with("_buildDiffActions", "urn:test:net.nemerosa.ontrack.boot.ui.BuildController#buildDiffActions:1").with("_properties", "urn:test:net.nemerosa.ontrack.boot.ui.PropertyController#getProperties:PROJECT,1").with("_extra", "urn:test:net.nemerosa.ontrack.boot.ui.ProjectEntityExtensionController#getInformation:PROJECT,1").with("_actions", "urn:test:net.nemerosa.ontrack.boot.ui.ProjectEntityExtensionController#getActions:PROJECT,1").with("_decorations", "urn:test:net.nemerosa.ontrack.boot.ui.DecorationsController#getDecorations:PROJECT,1").with("_events", "urn:test:net.nemerosa.ontrack.boot.ui.EventController#getEvents:PROJECT,1,0,10").with("_page", "urn:test:#:entity:PROJECT:1").end()).with("signature", SIGNATURE_OBJECT).with("_self", "urn:test:net.nemerosa.ontrack.boot.ui.BranchController#getBranch:1").with("_project", "urn:test:net.nemerosa.ontrack.boot.ui.ProjectController#getProject:1").with("_promotionLevels", "urn:test:net.nemerosa.ontrack.boot.ui.PromotionLevelController#getPromotionLevelListForBranch:1").with("_validationStamps", "urn:test:net.nemerosa.ontrack.boot.ui.ValidationStampController#getValidationStampListForBranch:1").with("_validationStampViews", "urn:test:net.nemerosa.ontrack.boot.ui.ValidationStampController#getValidationStampViewListForBranch:1").with("_allValidationStampFilters", "urn:test:net.nemerosa.ontrack.boot.ui.ValidationStampFilterController#getAllBranchValidationStampFilters:1").with("_branches", "urn:test:net.nemerosa.ontrack.boot.ui.BranchController#getBranchListForProject:1").with("_properties", "urn:test:net.nemerosa.ontrack.boot.ui.PropertyController#getProperties:BRANCH,1").with("_actions", "urn:test:net.nemerosa.ontrack.boot.ui.ProjectEntityExtensionController#getActions:BRANCH,1").with("_status", "urn:test:net.nemerosa.ontrack.boot.ui.BranchController#getBranchStatusView:1").with("_view", "urn:test:net.nemerosa.ontrack.boot.ui.BranchController#buildView:1").with("_decorations", "urn:test:net.nemerosa.ontrack.boot.ui.DecorationsController#getDecorations:BRANCH,1").with("_buildFilterResources", "urn:test:net.nemerosa.ontrack.boot.ui.BuildFilterController#buildFilters:1").with("_buildFilterForms", "urn:test:net.nemerosa.ontrack.boot.ui.BuildFilterController#buildFilterForms:1").with("_buildFilterSave", "urn:test:net.nemerosa.ontrack.boot.ui.BuildFilterController#createFilter:1,").with("_events", "urn:test:net.nemerosa.ontrack.boot.ui.EventController#getEvents:BRANCH,1,0,10").with("_page", "urn:test:#:entity:BRANCH:1").end(), node);
}
Also used : Project(net.nemerosa.ontrack.model.structure.Project) NameDescription(net.nemerosa.ontrack.model.structure.NameDescription) Branch(net.nemerosa.ontrack.model.structure.Branch) HttpOutputMessage(org.springframework.http.HttpOutputMessage) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 19 with Branch

use of net.nemerosa.ontrack.model.structure.Branch in project ontrack by nemerosa.

the class ResourceHttpMessageConverterIT method branch_enable_granted_for_automation.

@Test
public void branch_enable_granted_for_automation() throws Exception {
    // Objects
    Project p = Project.of(new NameDescription("P", "Projet créé")).withId(ID.of(1)).withSignature(SIGNATURE);
    Branch b = Branch.of(p, new NameDescription("B", "Branch")).withId(ID.of(1)).withDisabled(true).withSignature(SIGNATURE);
    // Message
    HttpOutputMessage message = mock(HttpOutputMessage.class);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    when(message.getBody()).thenReturn(output);
    // Serialization
    asGlobalRole("AUTOMATION").execute(() -> {
        try {
            converter.writeInternal(b, message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    // Content
    String json = new String(output.toByteArray(), "UTF-8");
    // Parsing
    JsonNode node = ObjectMapperFactory.create().readTree(json);
    // Enable link
    assertEquals("urn:test:net.nemerosa.ontrack.boot.ui.BranchController#enableBranch:1", node.path("_enable").asText());
}
Also used : Project(net.nemerosa.ontrack.model.structure.Project) NameDescription(net.nemerosa.ontrack.model.structure.NameDescription) Branch(net.nemerosa.ontrack.model.structure.Branch) HttpOutputMessage(org.springframework.http.HttpOutputMessage) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 20 with Branch

use of net.nemerosa.ontrack.model.structure.Branch in project ontrack by nemerosa.

the class ResourcesTest method resource_collection_with_filtering.

@Test
public void resource_collection_with_filtering() throws JsonProcessingException {
    Project project = Project.of(new NameDescription("PRJ", "Project"));
    List<Branch> branches = Arrays.asList(Branch.of(project, new NameDescription("B1", "Branch 1")).withSignature(SIGNATURE), Branch.of(project, new NameDescription("B2", "Branch 2")).withSignature(SIGNATURE));
    Resources<Branch> resourceCollection = Resources.of(branches, URI.create("urn:branch"));
    assertResourceJson(mapper, object().with("_self", "urn:branch").with("resources", array().with(object().with("id", 0).with("name", "B1").with("description", "Branch 1").with("disabled", false).with("type", "CLASSIC").with("signature", SIGNATURE_OBJECT).end()).with(object().with("id", 0).with("name", "B2").with("description", "Branch 2").with("disabled", false).with("type", "CLASSIC").with("signature", SIGNATURE_OBJECT).end()).end()).end(), resourceCollection, Resources.class);
}
Also used : Project(net.nemerosa.ontrack.model.structure.Project) NameDescription(net.nemerosa.ontrack.model.structure.NameDescription) Branch(net.nemerosa.ontrack.model.structure.Branch) Test(org.junit.Test)

Aggregations

Branch (net.nemerosa.ontrack.model.structure.Branch)33 Test (org.junit.Test)29 IntNode (com.fasterxml.jackson.databind.node.IntNode)12 Account (net.nemerosa.ontrack.model.security.Account)7 Ack (net.nemerosa.ontrack.model.Ack)6 Project (net.nemerosa.ontrack.model.structure.Project)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 BuildFilterResource (net.nemerosa.ontrack.model.buildfilter.BuildFilterResource)4 NameDescription (net.nemerosa.ontrack.model.structure.NameDescription)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HttpOutputMessage (org.springframework.http.HttpOutputMessage)3 IOException (java.io.IOException)2 Collectors (java.util.stream.Collectors)2 BranchFilterMgt (net.nemerosa.ontrack.model.security.BranchFilterMgt)2 ProjectView (net.nemerosa.ontrack.model.security.ProjectView)2 ID (net.nemerosa.ontrack.model.structure.ID)2 StructureService (net.nemerosa.ontrack.model.structure.StructureService)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 GraphQLInt (graphql.Scalars.GraphQLInt)1 GraphQLString (graphql.Scalars.GraphQLString)1