jenkins.scm.api.trait.SCMSourceTrait Java Examples

The following examples show how to use jenkins.scm.api.trait.SCMSourceTrait. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Deprecated
@Restricted(DoNotUse.class)
@RestrictedSince("2.2.0")
@DataBoundSetter
public void setIncludes(@NonNull String includes) {
    for (int i = 0; i < traits.size(); i++) {
        SCMSourceTrait trait = traits.get(i);
        if (trait instanceof WildcardSCMHeadFilterTrait) {
            WildcardSCMHeadFilterTrait existing = (WildcardSCMHeadFilterTrait) trait;
            if ("*".equals(includes) && "".equals(existing.getExcludes())) {
                traits.remove(i);
            } else {
                traits.set(i, new WildcardSCMHeadFilterTrait(includes, existing.getExcludes()));
            }
            return;
        }
    }
    if (!"*".equals(includes)) {
        traits.add(new WildcardSCMHeadFilterTrait(includes, ""));
    }
}
 
Example #2
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Deprecated
@Restricted(NoExternalUse.class)
@RestrictedSince("2.2.0")
@DataBoundSetter
public void setExcludes(@NonNull String excludes) {
    for (int i = 0; i < traits.size(); i++) {
        SCMSourceTrait trait = traits.get(i);
        if (trait instanceof WildcardSCMHeadFilterTrait) {
            WildcardSCMHeadFilterTrait existing = (WildcardSCMHeadFilterTrait) trait;
            if ("*".equals(existing.getIncludes()) && "".equals(excludes)) {
                traits.remove(i);
            } else {
                traits.set(i, new WildcardSCMHeadFilterTrait(existing.getIncludes(), excludes));
            }
            return;
        }
    }
    if (!"".equals(excludes)) {
        traits.add(new WildcardSCMHeadFilterTrait("*", excludes));
    }
}
 
Example #3
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
public List<NamedArrayList<? extends SCMTraitDescriptor<?>>> getTraitsDescriptorLists() {
    List<SCMTraitDescriptor<?>> all = new ArrayList<>();
    all.addAll(SCMSourceTrait._for(this, GitHubSCMSourceContext.class, null));
    all.addAll(SCMSourceTrait._for(this, null, GitHubSCMBuilder.class));
    Set<SCMTraitDescriptor<?>> dedup = new HashSet<>();
    for (Iterator<SCMTraitDescriptor<?>> iterator = all.iterator(); iterator.hasNext(); ) {
        SCMTraitDescriptor<?> d = iterator.next();
        if (dedup.contains(d)
                || d instanceof GitBrowserSCMSourceTrait.DescriptorImpl) {
            // remove any we have seen already and ban the browser configuration as it will always be github
            iterator.remove();
        } else {
            dedup.add(d);
        }
    }
    List<NamedArrayList<? extends SCMTraitDescriptor<?>>> result = new ArrayList<>();
    NamedArrayList.select(all, Messages.GitHubSCMNavigator_withinRepository(), NamedArrayList
                    .anyOf(NamedArrayList.withAnnotation(Discovery.class),
                            NamedArrayList.withAnnotation(Selection.class)),
            true, result);
    NamedArrayList.select(all, Messages.GitHubSCMNavigator_general(), null, true, result);
    return result;
}
 
Example #4
Source File: GitPipelineCreateRequestTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void createPipeline() throws UnirestException, IOException {
    User user = login("vivek", "Vivek Pandey", "[email protected]");
    Map r = new PipelineBaseTest.RequestBuilder(baseUrl)
        .status(201)
        .jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId()))
        .crumb( crumb )
        .post("/organizations/jenkins/pipelines/")
        .data(ImmutableMap.of("name", "pipeline1",
            "$class", "io.jenkins.blueocean.blueocean_git_pipeline.GitPipelineCreateRequest",
            "scmConfig", ImmutableMap.of("id", GitScm.ID, "uri", sampleRepo.toString())))
        .build(Map.class);
    assertNotNull(r);
    assertEquals("pipeline1", r.get("name"));

    MultiBranchProject mbp = (MultiBranchProject) j.getInstance().getItem("pipeline1");
    GitSCMSource source = (GitSCMSource) mbp.getSCMSources().get(0);
    List<SCMSourceTrait> traits = source.getTraits();

    Assert.assertNotNull(SCMTrait.find(traits, BranchDiscoveryTrait.class));
    Assert.assertNotNull(SCMTrait.find(traits, CleanAfterCheckoutTrait.class));
    Assert.assertNotNull(SCMTrait.find(traits, CleanBeforeCheckoutTrait.class));
    Assert.assertNotNull(SCMTrait.find(traits, LocalBranchTrait.class));
}
 
Example #5
Source File: GitCloneReadSaveRequest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
public GitCloneReadSaveRequest(AbstractGitSCMSource gitSource, String branch, String commitMessage, String sourceBranch, String filePath, byte[] contents) {
    super(gitSource, branch, commitMessage, sourceBranch, filePath, contents);

    GitTool.DescriptorImpl toolDesc = Jenkins.getInstance().getDescriptorByType(GitTool.DescriptorImpl.class);
    @SuppressWarnings("deprecation")
    GitTool foundGitTool = null;
    for (SCMSourceTrait trait : gitSource.getTraits()) {
        if (trait instanceof GitToolSCMSourceTrait) {
            foundGitTool = toolDesc.getInstallation(((GitToolSCMSourceTrait) trait).getGitTool());
        }
    }
    if (foundGitTool == null) {
        foundGitTool = GitTool.getDefaultInstallation();
    }

    this.gitTool = foundGitTool;
    try {
        repositoryPath = Files.createTempDirectory("git").toFile();
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException("Unable to create working directory for repository clone");
    }
}
 
Example #6
Source File: GitHubSCMSourceTraitsTest.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Test
public void modern() throws Exception {
    GitHubSCMSource instance = load();
    assertThat(instance.getId(), is("e4d8c11a-0d24-472f-b86b-4b017c160e9a"));
    assertThat(instance.getApiUri(), is(GitHubSCMSource.GITHUB_URL));
    assertThat(instance.getRepoOwner(), is("cloudbeers"));
    assertThat(instance.getRepository(), is("stunning-adventure"));
    assertThat(instance.getCredentialsId(), is(nullValue()));
    assertThat(instance.getRepositoryUrl(), is("https://github.com/cloudbeers/stunning-adventure"));
    assertThat(instance.getTraits(), is(Collections.<SCMSourceTrait>emptyList()));
    // Legacy API
    assertThat(instance.getCheckoutCredentialsId(), is(GitHubSCMSource.DescriptorImpl.SAME));
    assertThat(instance.getIncludes(), is("*"));
    assertThat(instance.getExcludes(), is(""));
    assertThat(instance.getBuildOriginBranch(), is(false));
    assertThat(instance.getBuildOriginBranchWithPR(), is(false));
    assertThat(instance.getBuildOriginPRHead(), is(false));
    assertThat(instance.getBuildOriginPRMerge(), is(false));
    assertThat(instance.getBuildForkPRHead(), is(false));
    assertThat(instance.getBuildForkPRMerge(), is(false));
}
 
Example #7
Source File: GerritSCMSource.java    From gerrit-code-review-plugin with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public List<NamedArrayList<? extends SCMSourceTraitDescriptor>> getTraitsDescriptorLists() {
  List<NamedArrayList<? extends SCMSourceTraitDescriptor>> result = new ArrayList<>();
  List<SCMSourceTraitDescriptor> descriptors =
      SCMSourceTrait._for(this, GerritSCMSourceContext.class, GitSCMBuilder.class);
  NamedArrayList.select(
      descriptors,
      "Within Repository",
      NamedArrayList.anyOf(
          NamedArrayList.withAnnotation(Selection.class),
          NamedArrayList.withAnnotation(Discovery.class)),
      true,
      result);
  NamedArrayList.select(descriptors, "Additional", null, true, result);
  return result;
}
 
Example #8
Source File: GitHubSCMSourceTraitsTest.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Test
public void repositoryUrl() throws Exception {
    GitHubSCMSource instance = load();

    assertThat(instance.getId(), is("e4d8c11a-0d24-472f-b86b-4b017c160e9a"));
    assertThat(instance.getApiUri(), is(GitHubSCMSource.GITHUB_URL));
    assertThat(instance.getRepoOwner(), is("joseblas"));
    assertThat(instance.getRepository(), is("jx"));
    assertThat(instance.getCredentialsId(), is("abcd"));
    assertThat(instance.getRepositoryUrl(), is("https://github.com/joseblas/jx"));
    assertThat(instance.getTraits(), is(Collections.<SCMSourceTrait>emptyList()));
    // Legacy API
    assertThat(instance.getCheckoutCredentialsId(), is(GitHubSCMSource.DescriptorImpl.SAME));
    assertThat(instance.getIncludes(), is("*"));
    assertThat(instance.getExcludes(), is(""));
    assertThat(instance.getBuildOriginBranch(), is(false));
    assertThat(instance.getBuildOriginBranchWithPR(), is(false));
    assertThat(instance.getBuildOriginPRHead(), is(false));
    assertThat(instance.getBuildOriginPRMerge(), is(false));
    assertThat(instance.getBuildForkPRHead(), is(false));
    assertThat(instance.getBuildForkPRMerge(), is(false));
}
 
Example #9
Source File: GiteaSCMSource.java    From gitea-plugin with MIT License 6 votes vote down vote up
public List<NamedArrayList<? extends SCMTraitDescriptor<?>>> getTraitsDescriptorLists() {
    List<SCMTraitDescriptor<?>> all = new ArrayList<>();
    //all.addAll(SCMSourceTrait._for(this, GitHubSCMSourceContext.class, null));
    all.addAll(SCMSourceTrait._for(this, null, GiteaSCMBuilder.class));
    Set<SCMTraitDescriptor<?>> dedup = new HashSet<>();
    for (Iterator<SCMTraitDescriptor<?>> iterator = all.iterator(); iterator.hasNext(); ) {
        SCMTraitDescriptor<?> d = iterator.next();
        if (dedup.contains(d)
                || d instanceof GitBrowserSCMSourceTrait.DescriptorImpl) {
            // remove any we have seen already and ban the browser configuration as it will always be github
            iterator.remove();
        } else {
            dedup.add(d);
        }
    }
    List<NamedArrayList<? extends SCMTraitDescriptor<?>>> result = new ArrayList<>();
    NamedArrayList.select(all, Messages.GiteaSCMSource_traitSection_withinRepo(), NamedArrayList
                    .anyOf(NamedArrayList.withAnnotation(Discovery.class),
                            NamedArrayList.withAnnotation(Selection.class)),
            true, result);
    NamedArrayList.select(all, Messages.GiteaSCMSource_traitSection_additional(), null, true, result);
    return result;
}
 
Example #10
Source File: GiteaSCMNavigator.java    From gitea-plugin with MIT License 5 votes vote down vote up
@SuppressWarnings("unused") // jelly
public List<NamedArrayList<? extends SCMTraitDescriptor<?>>> getTraitsDescriptorLists() {
    GiteaSCMSource.DescriptorImpl sourceDescriptor =
            Jenkins.get().getDescriptorByType(GiteaSCMSource.DescriptorImpl.class);
    List<SCMTraitDescriptor<?>> all = new ArrayList<>();
    all.addAll(SCMNavigatorTrait._for(this, GiteaSCMNavigatorContext.class, GiteaSCMSourceBuilder.class));
    all.addAll(SCMSourceTrait._for(sourceDescriptor, GiteaSCMSourceContext.class, null));
    all.addAll(SCMSourceTrait._for(sourceDescriptor, null, GiteaSCMBuilder.class));
    Set<SCMTraitDescriptor<?>> dedup = new HashSet<>();
    for (Iterator<SCMTraitDescriptor<?>> iterator = all.iterator(); iterator.hasNext(); ) {
        SCMTraitDescriptor<?> d = iterator.next();
        if (dedup.contains(d)
                || d instanceof GitBrowserSCMSourceTrait.DescriptorImpl) {
            // remove any we have seen already and ban the browser configuration as it will always be github
            iterator.remove();
        } else {
            dedup.add(d);
        }
    }
    List<NamedArrayList<? extends SCMTraitDescriptor<?>>> result = new ArrayList<>();
    NamedArrayList.select(all, Messages.GiteaSCMNavigator_traitSection_repositories(), new NamedArrayList.Predicate<SCMTraitDescriptor<?>>() {
                @Override
                public boolean test(SCMTraitDescriptor<?> scmTraitDescriptor) {
                    return scmTraitDescriptor instanceof SCMNavigatorTraitDescriptor;
                }
            },
            true, result);
    NamedArrayList.select(all, Messages.GiteaSCMNavigator_traitSection_withinRepo(), NamedArrayList
                    .anyOf(NamedArrayList.withAnnotation(Discovery.class),
                            NamedArrayList.withAnnotation(Selection.class)),
            true, result);
    NamedArrayList.select(all, Messages.GiteaSCMNavigator_traitSection_additional(), null, true, result);
    return result;
}
 
Example #11
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Restricted(DoNotUse.class)
@RestrictedSince("2.2.0")
@Deprecated
@CheckForNull
public String getCheckoutCredentialsId() {
    for (SCMSourceTrait trait : traits) {
        if (trait instanceof SSHCheckoutTrait) {
            return StringUtils.defaultString(
                    ((SSHCheckoutTrait) trait).getCredentialsId(),
                    GitHubSCMSource.DescriptorImpl.ANONYMOUS
            );
        }
    }
    return DescriptorImpl.SAME;
}
 
Example #12
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected boolean isCategoryEnabled(@NonNull SCMHeadCategory category) {
    for (SCMSourceTrait trait : traits) {
        if (trait.isCategoryEnabled(category)) {
            return true;
        }
    }
    return false;
}
 
Example #13
Source File: GitPipelineCreateRequest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
protected SCMSource createSource(@Nonnull MultiBranchProject project, @Nonnull BlueScmConfig scmConfig) {
    GitSCMSource gitSource = new GitSCMSource(StringUtils.defaultString(scmConfig.getUri()));
    gitSource.setCredentialsId(computeCredentialId(scmConfig));
    List<SCMSourceTrait> traits = gitSource.getTraits();
    traits.add(new BranchDiscoveryTrait());
    traits.add(new CleanBeforeCheckoutTrait());
    traits.add(new CleanAfterCheckoutTrait());
    traits.add(new LocalBranchTrait());
    return gitSource;
}
 
Example #14
Source File: GitLabSCMNavigator.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
@SuppressWarnings("unused") // jelly
public List<NamedArrayList<? extends SCMTraitDescriptor<?>>> getTraitsDescriptorLists() {
    GitLabSCMSource.DescriptorImpl sourceDescriptor =
        Jenkins.get()
            .getDescriptorByType(GitLabSCMSource.DescriptorImpl.class);
    List<SCMTraitDescriptor<?>> all = new ArrayList<>();
    all.addAll(SCMNavigatorTrait
        ._for(this, GitLabSCMNavigatorContext.class, GitLabSCMSourceBuilder.class));
    all.addAll(SCMSourceTrait._for(sourceDescriptor, GitLabSCMSourceContext.class, null));
    all.addAll(SCMSourceTrait._for(sourceDescriptor, null, GitLabSCMBuilder.class));
    Set<SCMTraitDescriptor<?>> dedup = new HashSet<>();
    for (Iterator<SCMTraitDescriptor<?>> iterator = all.iterator(); iterator.hasNext(); ) {
        SCMTraitDescriptor<?> d = iterator.next();
        if (dedup.contains(d)
            || d instanceof GitBrowserSCMSourceTrait.DescriptorImpl) {
            // remove any we have seen already and ban the browser configuration as it will always be github
            iterator.remove();
        } else {
            dedup.add(d);
        }
    }
    List<NamedArrayList<? extends SCMTraitDescriptor<?>>> result = new ArrayList<>();
    NamedArrayList
        .select(all, "Projects", new NamedArrayList.Predicate<SCMTraitDescriptor<?>>() {
                @Override
                public boolean test(SCMTraitDescriptor<?> scmTraitDescriptor) {
                    return scmTraitDescriptor instanceof SCMNavigatorTraitDescriptor;
                }
            },
            true, result);
    NamedArrayList.select(all, "Within project", NamedArrayList
            .anyOf(NamedArrayList.withAnnotation(Discovery.class),
                NamedArrayList.withAnnotation(Selection.class)),
        true, result);
    NamedArrayList.select(all, "Additional", null, true, result);
    return result;
}
 
Example #15
Source File: GiteaSCMSource.java    From gitea-plugin with MIT License 5 votes vote down vote up
public List<SCMSourceTrait> getTraitsDefaults() {
    return Arrays.asList( // TODO finalize
            new BranchDiscoveryTrait(true, false),
            new OriginPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE)),
            new ForkPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE),
                    new ForkPullRequestDiscoveryTrait.TrustContributors())
    );
}
 
Example #16
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
public List<SCMSourceTrait> getTraitsDefaults() {
    return Arrays.asList( // TODO finalize
            new BranchDiscoveryTrait(true, false),
            new OriginPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE)),
            new ForkPullRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE), new ForkPullRequestDiscoveryTrait.TrustPermission())
    );
}
 
Example #17
Source File: GitHubSCMNavigator.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * Returns the {@link SCMTraitDescriptor} instances grouped into categories.
 *
 * @return the categorized list of {@link SCMTraitDescriptor} instances.
 * @since 2.2.0
 */
@SuppressWarnings("unused") // jelly
public List<NamedArrayList<? extends SCMTraitDescriptor<?>>> getTraitsDescriptorLists() {
    GitHubSCMSource.DescriptorImpl sourceDescriptor =
            Jenkins.get().getDescriptorByType(GitHubSCMSource.DescriptorImpl.class);
    List<SCMTraitDescriptor<?>> all = new ArrayList<>();
    all.addAll(SCMNavigatorTrait._for(this, GitHubSCMNavigatorContext.class, GitHubSCMSourceBuilder.class));
    all.addAll(SCMSourceTrait._for(sourceDescriptor, GitHubSCMSourceContext.class, null));
    all.addAll(SCMSourceTrait._for(sourceDescriptor, null, GitHubSCMBuilder.class));
    Set<SCMTraitDescriptor<?>> dedup = new HashSet<>();
    for (Iterator<SCMTraitDescriptor<?>> iterator = all.iterator(); iterator.hasNext(); ) {
        SCMTraitDescriptor<?> d = iterator.next();
        if (dedup.contains(d)
                || d instanceof GitBrowserSCMSourceTrait.DescriptorImpl) {
            // remove any we have seen already and ban the browser configuration as it will always be github
            iterator.remove();
        } else {
            dedup.add(d);
        }
    }
    List<NamedArrayList<? extends SCMTraitDescriptor<?>>> result = new ArrayList<>();
    NamedArrayList.select(all, "Repositories", new NamedArrayList.Predicate<SCMTraitDescriptor<?>>() {
                @Override
                public boolean test(SCMTraitDescriptor<?> scmTraitDescriptor) {
                    return scmTraitDescriptor instanceof SCMNavigatorTraitDescriptor;
                }
            },
            true, result);
    NamedArrayList.select(all, Messages.GitHubSCMNavigator_withinRepository(), NamedArrayList.anyOf(NamedArrayList.withAnnotation(Discovery.class),NamedArrayList.withAnnotation(Selection.class)),
            true, result);
    NamedArrayList.select(all, Messages.GitHubSCMNavigator_general(), null, true, result);
    return result;
}
 
Example #18
Source File: GerritSCMSource.java    From gerrit-code-review-plugin with Apache License 2.0 5 votes vote down vote up
@Restricted(DoNotUse.class)
public String getRawRefSpecs() {
  String remoteName = null;
  RefSpecsSCMSourceTrait refSpecs = null;
  for (SCMSourceTrait trait : traits) {
    if (trait instanceof RemoteNameSCMSourceTrait) {
      remoteName = ((RemoteNameSCMSourceTrait) trait).getRemoteName();
      if (refSpecs != null) break;
    }
    if (trait instanceof RefSpecsSCMSourceTrait) {
      refSpecs = (RefSpecsSCMSourceTrait) trait;
      if (remoteName != null) break;
    }
  }
  if (remoteName == null) {
    remoteName = AbstractGitSCMSource.DEFAULT_REMOTE_NAME;
  }
  if (refSpecs == null) {
    return AbstractGitSCMSource.REF_SPEC_DEFAULT.replaceAll(
        AbstractGitSCMSource.REF_SPEC_REMOTE_NAME_PLACEHOLDER, remoteName);
  }
  StringBuilder result = new StringBuilder();
  boolean first = true;
  Pattern placeholder = Pattern.compile(AbstractGitSCMSource.REF_SPEC_REMOTE_NAME_PLACEHOLDER);
  for (String template : refSpecs.asStrings()) {
    if (first) {
      first = false;
    } else {
      result.append(' ');
    }
    result.append(placeholder.matcher(template).replaceAll(remoteName));
  }
  return result.toString();
}
 
Example #19
Source File: GerritSCMSource.java    From gerrit-code-review-plugin with Apache License 2.0 5 votes vote down vote up
@Restricted(DoNotUse.class)
@DataBoundSetter
public void setExtensions(@CheckForNull List<GitSCMExtension> extensions) {
  List<SCMSourceTrait> traits = new ArrayList<>(this.traits);
  for (Iterator<SCMSourceTrait> iterator = traits.iterator(); iterator.hasNext(); ) {
    if (iterator.next() instanceof GitSCMExtensionTrait) {
      iterator.remove();
    }
  }
  EXTENSIONS:
  for (GitSCMExtension extension : Util.fixNull(extensions)) {
    for (SCMSourceTraitDescriptor d : SCMSourceTrait.all()) {
      if (d instanceof GitSCMExtensionTraitDescriptor) {
        GitSCMExtensionTraitDescriptor descriptor = (GitSCMExtensionTraitDescriptor) d;
        if (descriptor.getExtensionClass().isInstance(extension)) {
          try {
            SCMSourceTrait trait = descriptor.convertToTrait(extension);
            if (trait != null) {
              traits.add(trait);
              continue EXTENSIONS;
            }
          } catch (UnsupportedOperationException e) {
            LOGGER.log(
                Level.WARNING,
                "Could not convert " + extension.getClass().getName() + " to a trait",
                e);
          }
        }
      }
      LOGGER.log(
          Level.FINE,
          "Could not convert {0} to a trait (likely because this option does not "
              + "make sense for a GitSCMSource)",
          extension.getClass().getName());
    }
  }
  setTraits(traits);
}
 
Example #20
Source File: GerritSCMSource.java    From gerrit-code-review-plugin with Apache License 2.0 5 votes vote down vote up
@Restricted(DoNotUse.class)
@DataBoundSetter
public void setGitTool(String gitTool) {
  List<SCMSourceTrait> traits = new ArrayList<>(this.traits);
  gitTool = Util.fixEmptyAndTrim(gitTool);
  for (Iterator<SCMSourceTrait> iterator = traits.iterator(); iterator.hasNext(); ) {
    if (iterator.next() instanceof GitToolSCMSourceTrait) {
      iterator.remove();
    }
  }
  if (gitTool != null) {
    traits.add(new GitToolSCMSourceTrait(gitTool));
  }
  setTraits(traits);
}
 
Example #21
Source File: GerritSCMSource.java    From gerrit-code-review-plugin with Apache License 2.0 5 votes vote down vote up
@Restricted(DoNotUse.class)
@DataBoundSetter
public void setBrowser(GitRepositoryBrowser browser) {
  List<SCMSourceTrait> traits = new ArrayList<>(this.traits);
  for (Iterator<SCMSourceTrait> iterator = traits.iterator(); iterator.hasNext(); ) {
    if (iterator.next() instanceof GitBrowserSCMSourceTrait) {
      iterator.remove();
    }
  }
  if (browser != null) {
    traits.add(new GitBrowserSCMSourceTrait(browser));
  }
  setTraits(traits);
}
 
Example #22
Source File: GitLabSCMSource.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
@NonNull
public List<SCMSourceTrait> getTraitsDefaults() {
    return Arrays.<SCMSourceTrait>asList( // TODO finalize
            new BranchDiscoveryTrait(true, false),
            new OriginMergeRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE)),
            new ForkMergeRequestDiscoveryTrait(EnumSet.of(ChangeRequestCheckoutStrategy.MERGE),
                    new ForkMergeRequestDiscoveryTrait.TrustPermission()));
}
 
Example #23
Source File: GitLabSCMSource.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
public List<NamedArrayList<? extends SCMTraitDescriptor<?>>> getTraitsDescriptorLists() {
    List<SCMTraitDescriptor<?>> all = new ArrayList<>();
    all.addAll(SCMSourceTrait._for(this, GitLabSCMSourceContext.class, null));
    all.addAll(SCMSourceTrait._for(this, null, GitLabSCMBuilder.class));
    Set<SCMTraitDescriptor<?>> dedup = new HashSet<>();
    for (Iterator<SCMTraitDescriptor<?>> iterator = all.iterator(); iterator.hasNext();) {
        SCMTraitDescriptor<?> d = iterator.next();
        if (dedup.contains(d) || d instanceof GitBrowserSCMSourceTrait.DescriptorImpl) {
            // remove any we have seen already and ban the browser configuration as it will
            // always be github
            iterator.remove();
        } else {
            dedup.add(d);
        }
    }
    List<NamedArrayList<? extends SCMTraitDescriptor<?>>> result = new ArrayList<>();
    NamedArrayList.select(all, "Projects", new NamedArrayList.Predicate<SCMTraitDescriptor<?>>() {
        @Override
        public boolean test(SCMTraitDescriptor<?> scmTraitDescriptor) {
            return scmTraitDescriptor instanceof SCMNavigatorTraitDescriptor;
        }
    }, true, result);
    NamedArrayList.select(all, "Within project",
            NamedArrayList.anyOf(NamedArrayList.withAnnotation(Discovery.class),
                    NamedArrayList.withAnnotation(Selection.class)),
            true, result);
    NamedArrayList.select(all, "Additional", null, true, result);
    return result;
}
 
Example #24
Source File: GitHubSCMSourceTraitsTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void given__legacyCode__when__setBuildOriginBranch__then__traitsMaintained() {
    GitHubSCMSource instance = new GitHubSCMSource("testing", "test-repo");
    instance.setTraits(Collections.emptyList());
    assertThat(instance.getTraits(), is(Collections.<SCMSourceTrait>emptyList()));
    instance.setBuildOriginBranch(true);
    assertThat(instance.getTraits(), contains(instanceOf(BranchDiscoveryTrait.class)));
    instance.setBuildOriginBranch(false);
    assertThat(instance.getTraits(), is(Collections.<SCMSourceTrait>emptyList()));

    instance.setBuildOriginBranchWithPR(true);
    assertThat(instance.getTraits(), contains(instanceOf(BranchDiscoveryTrait.class)));
    instance.setBuildOriginBranchWithPR(false);
    assertThat(instance.getTraits(), is(Collections.<SCMSourceTrait>emptyList()));

    instance.setBuildOriginBranchWithPR(true);
    instance.setBuildOriginBranch(true);
    assertThat(instance.getTraits(), contains(instanceOf(BranchDiscoveryTrait.class)));
    instance.setBuildOriginBranchWithPR(false);
    assertThat(instance.getTraits(), contains(instanceOf(BranchDiscoveryTrait.class)));
    instance.setBuildOriginBranch(false);
    assertThat(instance.getTraits(), is(Collections.<SCMSourceTrait>emptyList()));

    instance.setBuildOriginBranchWithPR(true);
    instance.setBuildOriginBranch(true);
    assertThat(instance.getTraits(), contains(instanceOf(BranchDiscoveryTrait.class)));
    instance.setBuildOriginBranch(false);
    assertThat(instance.getTraits(), contains(instanceOf(BranchDiscoveryTrait.class)));
    instance.setBuildOriginBranchWithPR(false);
    assertThat(instance.getTraits(), is(Collections.<SCMSourceTrait>emptyList()));
}
 
Example #25
Source File: ForkPullRequestDiscoveryTrait2Test.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
private void assertRoundTrip(WorkflowMultiBranchProject p, SCMHeadAuthority<? super GitHubSCMSourceRequest, ? extends ChangeRequestSCMHead2, ? extends SCMRevision> trust, boolean configuredByUrl) throws Exception {

        GitHubSCMSource s;
        if (configuredByUrl)
            s = new GitHubSCMSource("", "", "https://github.com/nobody/nowhere", true);
        else
            s = new GitHubSCMSource("nobody", "nowhere", null, false);

        p.setSourcesList(Collections.singletonList(new BranchSource(s)));
        s.setTraits(Collections.singletonList(new ForkPullRequestDiscoveryTrait(0, trust)));
        r.configRoundtrip(p);
        List<SCMSourceTrait> traits = ((GitHubSCMSource) p.getSourcesList().get(0).getSource()).getTraits();
        assertEquals(1, traits.size());
        assertEquals(trust.getClass(), ((ForkPullRequestDiscoveryTrait) traits.get(0)).getTrust().getClass());
    }
 
Example #26
Source File: BitbucketPipelineCreateRequestTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void createdWithTraits() throws Exception {
    String credentialId = createCredential(BitbucketServerScm.ID);
    Map r = new PipelineBaseTest.RequestBuilder(baseUrl)
        .status(201)
        .jwtToken(getJwtToken(j.jenkins, authenticatedUser.getId(), authenticatedUser.getId()))
        .crumb( crumb )
        .post("/organizations/jenkins/pipelines/")
        .data(ImmutableMap.of(
            "name","pipeline1",
            "$class", "io.jenkins.blueocean.blueocean_bitbucket_pipeline.BitbucketPipelineCreateRequest",
            "scmConfig", ImmutableMap.of(
                "id", BitbucketServerScm.ID,
                "credentialId", credentialId,
                "uri", apiUrl,
                "config", ImmutableMap.of(
                    "repoOwner", "TESTP",
                    "repository", "pipeline-demo-test"
                )
            )
        ))
        .build(Map.class);
    assertNotNull(r);
    assertEquals("pipeline1", r.get("name"));

    MultiBranchProject mbp = (MultiBranchProject) j.getInstance().getItem("pipeline1");
    BitbucketSCMSource source = (BitbucketSCMSource) mbp.getSCMSources().get(0);
    List<SCMSourceTrait> traits = source.getTraits();

    Assert.assertNotNull(SCMTrait.find(traits, CleanAfterCheckoutTrait.class));
    Assert.assertNotNull(SCMTrait.find(traits, CleanBeforeCheckoutTrait.class));
    Assert.assertNotNull(SCMTrait.find(traits, LocalBranchTrait.class));

    BranchDiscoveryTrait branchDiscoveryTrait = SCMTrait.find(traits, BranchDiscoveryTrait.class);
    Assert.assertNotNull(branchDiscoveryTrait);
    Assert.assertTrue(branchDiscoveryTrait.isBuildBranch());
    Assert.assertTrue(branchDiscoveryTrait.isBuildBranchesWithPR());

    ForkPullRequestDiscoveryTrait forkPullRequestDiscoveryTrait = SCMTrait.find(traits, ForkPullRequestDiscoveryTrait.class);
    Assert.assertNotNull(forkPullRequestDiscoveryTrait);
    Assert.assertTrue(forkPullRequestDiscoveryTrait.getTrust() instanceof ForkPullRequestDiscoveryTrait.TrustTeamForks);
    Assert.assertEquals(1, forkPullRequestDiscoveryTrait.getStrategies().size());
    Assert.assertTrue(forkPullRequestDiscoveryTrait.getStrategies().contains(ChangeRequestCheckoutStrategy.MERGE));

    OriginPullRequestDiscoveryTrait originPullRequestDiscoveryTrait = SCMTrait.find(traits, OriginPullRequestDiscoveryTrait.class);
    Assert.assertNotNull(originPullRequestDiscoveryTrait);
    Assert.assertEquals(1, originPullRequestDiscoveryTrait.getStrategies().size());
    Assert.assertTrue(originPullRequestDiscoveryTrait.getStrategies().contains(ChangeRequestCheckoutStrategy.MERGE));
}
 
Example #27
Source File: GitHubSCMSourceTraitsTest.java    From github-branch-source-plugin with MIT License 4 votes vote down vote up
@Test
public void given__instance__when__setTraits_empty__then__traitsEmpty() {
    GitHubSCMSource instance = new GitHubSCMSource("testing", "test-repo");
    instance.setTraits(Collections.emptyList());
    assertThat(instance.getTraits(), is(Collections.<SCMSourceTrait>emptyList()));
}
 
Example #28
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 4 votes vote down vote up
/**
 * Sets the behaviours that are applied to this {@link GitHubSCMSource}.
 * @param traits the behaviours that are to be applied.
 */
@DataBoundSetter
public void setTraits(@CheckForNull List<SCMSourceTrait> traits) {
    this.traits = new ArrayList<>(Util.fixNull(traits));
}
 
Example #29
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 * @since 2.2.0
 */
@Override
public List<SCMSourceTrait> getTraits() {
    return traits;
}
 
Example #30
Source File: GithubPipelineCreateRequestTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void createPipeline() throws UnirestException, IOException {
    PowerMockito.mockStatic(GitHubWebHook.class);
    GitHubWebHook gitHubWebHookMock = Mockito.spy(GitHubWebHook.class);
    PowerMockito.when(GitHubWebHook.get()).thenReturn(gitHubWebHookMock);
    PowerMockito.when(GitHubWebHook.getJenkinsInstance()).thenReturn(this.j.jenkins);
    String credentialId = createGithubCredential(user);
    Map r = new PipelineBaseTest.RequestBuilder(baseUrl)
            .status(201)
            .jwtToken(getJwtToken(j.jenkins, user.getId(), user.getId()))
            .crumb( this.crumb )
            .post("/organizations/jenkins/pipelines/")
            .data(ImmutableMap.of("name", "pipeline1", "$class", "io.jenkins.blueocean.blueocean_github_pipeline.GithubPipelineCreateRequest",
                    "scmConfig", ImmutableMap.of("id", GithubScm.ID, "uri", githubApiUrl, "credentialId", credentialId,
                            "config", ImmutableMap.of("repoOwner", "cloudbeers", "repository", "PR-demo"))))
            .build(Map.class);
    assertNotNull(r);
    assertEquals("pipeline1", r.get("name"));

    MultiBranchProject mbp = (MultiBranchProject) j.getInstance().getItem("pipeline1");
    GitHubSCMSource source = (GitHubSCMSource) mbp.getSCMSources().get(0);
    List<SCMSourceTrait> traits = source.getTraits();

    Assert.assertNotNull(SCMTrait.find(traits, CleanAfterCheckoutTrait.class));
    Assert.assertNotNull(SCMTrait.find(traits, CleanBeforeCheckoutTrait.class));
    Assert.assertNotNull(SCMTrait.find(traits, LocalBranchTrait.class));

    BranchDiscoveryTrait branchDiscoveryTrait = SCMTrait.find(traits, BranchDiscoveryTrait.class);
    Assert.assertNotNull(branchDiscoveryTrait);
    Assert.assertTrue(branchDiscoveryTrait.isBuildBranch());
    Assert.assertTrue(branchDiscoveryTrait.isBuildBranchesWithPR());

    ForkPullRequestDiscoveryTrait forkPullRequestDiscoveryTrait = SCMTrait.find(traits, ForkPullRequestDiscoveryTrait.class);
    Assert.assertNotNull(forkPullRequestDiscoveryTrait);
    Assert.assertTrue(forkPullRequestDiscoveryTrait.getTrust() instanceof ForkPullRequestDiscoveryTrait.TrustPermission);
    Assert.assertEquals(1, forkPullRequestDiscoveryTrait.getStrategies().size());
    Assert.assertTrue(forkPullRequestDiscoveryTrait.getStrategies().contains(ChangeRequestCheckoutStrategy.MERGE));

    OriginPullRequestDiscoveryTrait originPullRequestDiscoveryTrait = SCMTrait.find(traits, OriginPullRequestDiscoveryTrait.class);
    Assert.assertNotNull(originPullRequestDiscoveryTrait);
    Assert.assertEquals(1, originPullRequestDiscoveryTrait.getStrategies().size());
    Assert.assertTrue(originPullRequestDiscoveryTrait.getStrategies().contains(ChangeRequestCheckoutStrategy.MERGE));
    Mockito.verify(gitHubWebHookMock, Mockito.times(1)).registerHookFor(mbp);
}