jenkins.scm.api.metadata.ObjectMetadataAction Java Examples

The following examples show how to use jenkins.scm.api.metadata.ObjectMetadataAction. 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: GitLabSCMSource.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
@NonNull
@Override
protected List<Action> retrieveActions(SCMSourceEvent event, @NonNull TaskListener listener) {
    List<Action> result = new ArrayList<>();
    getGitlabProject();
    GitLabSCMSourceContext ctx = new GitLabSCMSourceContext(null, SCMHeadObserver.none()).withTraits(traits);
    String projectUrl = gitlabProject.getWebUrl();
    String name = StringUtils.isBlank(projectName) ? gitlabProject.getNameWithNamespace() : projectName;
    result.add(new ObjectMetadataAction(name, gitlabProject.getDescription(), projectUrl));
    String avatarUrl = gitlabProject.getAvatarUrl();
    if (!ctx.projectAvatarDisabled() && StringUtils.isNotBlank(avatarUrl)) {
        result.add(new GitLabAvatar(avatarUrl));
    }
    result.add(GitLabLink.toProject(projectUrl));
    return result;
}
 
Example #2
Source File: PullRequestIT.java    From office-365-connector-plugin with Apache License 2.0 6 votes vote down vote up
private void mockPullRequest() {
    Job job = run.getParent();
    SCMHead head = new SCMHeadBuilder("Pull Request");

    mockStatic(SCMHead.HeadByItem.class);
    when(SCMHead.HeadByItem.findHead(run.getParent())).thenReturn(head);

    ObjectMetadataAction objectMetadataAction = mock(ObjectMetadataAction.class);
    when(objectMetadataAction.getObjectUrl()).thenReturn("https://github.com/damianszczepanik/hook/pull/1");
    when(objectMetadataAction.getObjectDisplayName()).thenReturn("test pull request");
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(objectMetadataAction);

    ContributorMetadataAction contributorMetadataAction = mock(ContributorMetadataAction.class);
    when(contributorMetadataAction.getContributor()).thenReturn("damianszczepanik");
    when(contributorMetadataAction.getContributorDisplayName()).thenReturn("Damian Szczepanik");
    when(job.getAction(ContributorMetadataAction.class)).thenReturn(contributorMetadataAction);
}
 
Example #3
Source File: ActionableBuilderTest.java    From office-365-connector-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void pullRequestActionable_OnPureChangeRequestSCMHead_DoesNotAddFact() {

    // given
    // from @Before
    SCMHead head = new SCMHeadBuilder("Pull Request");


    Job job = mock(Job.class);
    when(run.getParent()).thenReturn(job);

    mockStatic(SCMHead.HeadByItem.class);
    when(SCMHead.HeadByItem.findHead(job)).thenReturn(head);
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(null);

    // when
    Deencapsulation.invoke(actionableBuilder, "pullRequestActionable");

    // then
    assertThat(factsBuilder.collect()).isEmpty();
}
 
Example #4
Source File: CachesTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void testPullRequestCacheLoaderWithoutScmHead() throws Exception {
    ObjectMetadataAction metadataAction = new ObjectMetadataAction("A cool PR", "A very cool change", "http://example.com/pr/1");
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(metadataAction);

    ContributorMetadataAction contributorMetadataAction = new ContributorMetadataAction("Hates Cake", "He hates cake", "[email protected]");
    when(job.getAction(ContributorMetadataAction.class)).thenReturn(contributorMetadataAction);

    PowerMockito.mockStatic(ExtensionList.class);

    ExtensionList<SCMHead.HeadByItem> extensionList = mock(ExtensionList.class);
    when(extensionList.iterator()).thenReturn(Lists.<SCMHead.HeadByItem>newArrayList().iterator());
    when(ExtensionList.lookup(SCMHead.HeadByItem.class)).thenReturn(extensionList);

    Caches.PullRequestCacheLoader loader = new Caches.PullRequestCacheLoader(jenkins);
    BranchImpl.PullRequest pr = loader.load(job.getFullName()).orNull();

    assertNull(pr);
}
 
Example #5
Source File: CachesTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void testPullRequestCacheLoader() throws Exception {
    ObjectMetadataAction metadataAction = new ObjectMetadataAction("A cool PR", "A very cool change", "http://example.com/pr/1");
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(metadataAction);

    ContributorMetadataAction contributorMetadataAction = new ContributorMetadataAction("Hates Cake", "He hates cake", "[email protected]");
    when(job.getAction(ContributorMetadataAction.class)).thenReturn(contributorMetadataAction);

    PowerMockito.mockStatic(ExtensionList.class);

    ExtensionList<SCMHead.HeadByItem> extensionList = mock(ExtensionList.class);
    when(extensionList.iterator()).thenReturn(Lists.<SCMHead.HeadByItem>newArrayList(new HeadByItemForTest()).iterator());
    when(ExtensionList.lookup(SCMHead.HeadByItem.class)).thenReturn(extensionList);

    Caches.PullRequestCacheLoader loader = new Caches.PullRequestCacheLoader(jenkins);
    BranchImpl.PullRequest pr = loader.load(job.getFullName()).orNull();

    assertNotNull(pr);
    assertEquals("Hates Cake", pr.getAuthor());
    assertEquals("1", pr.getId());
    assertEquals("A cool PR", pr.getTitle());
    assertEquals("http://example.com/pr/1", pr.getUrl());
}
 
Example #6
Source File: BranchMetadataTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void testBranchInfo() {
    assertNull(branch.getBranch());

    ObjectMetadataAction oma = new ObjectMetadataAction(
        "My Branch",
        "A feature branch",
        "https://path/to/branch"
    );
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(oma);
    when(job.getAction(PrimaryInstanceMetadataAction.class)).thenReturn(new PrimaryInstanceMetadataAction());

    Caches.BRANCH_METADATA.invalidateAll();

    assertEquals("https://path/to/branch", branch.getBranch().getUrl());
    assertTrue(branch.getBranch().isPrimary());
}
 
Example #7
Source File: BranchMetadataTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void testGetURL() {
    assertNull(branch.getBranch());

    ObjectMetadataAction oma = new ObjectMetadataAction(
        "My Branch",
        "A feature branch",
        "https://path/to/branch"
    );
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(oma);

    Caches.BRANCH_METADATA.invalidateAll();

    assertNotNull(branch.getBranch());
    assertFalse(branch.getBranch().isPrimary());
    assertEquals("https://path/to/branch", branch.getBranch().getUrl());
}
 
Example #8
Source File: Caches.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Override
public Optional<PullRequest> load(String key) throws Exception {
    Jenkins jenkins = Objects.firstNonNull(this.jenkins, Jenkins.getInstance());
    Job job = jenkins.getItemByFullName(key, Job.class);
    if (job == null) {
        return Optional.absent();
    }
    // TODO probably want to be using SCMHeadCategory instances to categorize them instead of hard-coding for PRs
    SCMHead head = SCMHead.HeadByItem.findHead(job);
    if (head instanceof ChangeRequestSCMHead) {
        ChangeRequestSCMHead cr = (ChangeRequestSCMHead) head;
        ObjectMetadataAction om = job.getAction(ObjectMetadataAction.class);
        ContributorMetadataAction cm = job.getAction(ContributorMetadataAction.class);
        return Optional.of(new PullRequest(
                cr.getId(),
                om != null ? om.getObjectUrl() : null,
                om != null ? om.getObjectDisplayName() : null,
                cm != null ? cm.getContributor() : null
            )
        );
    }
    return Optional.absent();
}
 
Example #9
Source File: GiteaSCMSource.java    From gitea-plugin with MIT License 6 votes vote down vote up
@NonNull
@Override
protected List<Action> retrieveActions(SCMSourceEvent event, @NonNull TaskListener listener)
        throws IOException, InterruptedException {
    if (giteaRepository == null) {
        try (GiteaConnection c = gitea().open()) {
            listener.getLogger().format("Looking up repository %s/%s%n", repoOwner, repository);
            giteaRepository = c.fetchRepository(repoOwner, repository);
        }
    }
    List<Action> result = new ArrayList<>();
    result.add(new ObjectMetadataAction(null, giteaRepository.getDescription(), giteaRepository.getWebsite()));
    result.add(new GiteaLink("icon-gitea-repo", UriTemplate.buildFromTemplate(serverUrl)
            .path(UriTemplateBuilder.var("owner"))
            .path(UriTemplateBuilder.var("repository"))
            .build()
            .set("owner", repoOwner)
            .set("repository", repository)
            .expand()
    ));
    return result;
}
 
Example #10
Source File: ActionableBuilderTest.java    From office-365-connector-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void pullRequestActionable_OnContributorMetadataAction_AddsFact() {

    // given
    // from @Before
    SCMHead head = new SCMHeadBuilder("Pull Request");

    Job job = mock(Job.class);
    when(run.getParent()).thenReturn(job);

    mockStatic(SCMHead.HeadByItem.class);
    when(SCMHead.HeadByItem.findHead(job)).thenReturn(head);

    ObjectMetadataAction objectMetadataAction = mock(ObjectMetadataAction.class);
    when(objectMetadataAction.getObjectUrl()).thenReturn("https://github.com/organization/repository/pull/1");
    when(objectMetadataAction.getObjectDisplayName()).thenReturn("test pull request");
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(objectMetadataAction);

    // when
    Deencapsulation.invoke(actionableBuilder, "pullRequestActionable");

    // then
    assertThat(factsBuilder.collect()).hasSize(1);

    List<PotentialAction> potentialActions = Deencapsulation.getField(actionableBuilder, "potentialActions");
    assertThat(potentialActions).hasSize(1);
}
 
Example #11
Source File: GitHubSCMSource.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Nonnull
@Override
protected List<Action> retrieveActions(@Nonnull SCMHead head,
                                       @CheckForNull SCMHeadEvent event,
                                       @Nonnull TaskListener listener)
        throws IOException, InterruptedException {
    List<Action> actions = new ArrayList<>();

    GHRepository remoteRepo = getRemoteRepo();

    boolean primary = false;
    GitHubLinkAction link = null;
    String desc = null;

    if (head instanceof GitHubBranchSCMHead) {
        // mark default branch item as primary
        primary = remoteRepo.getDefaultBranch().equals(head.getName());
        link = new GitHubBranchAction(remoteRepo, head.getName());
        desc = null;
    } else if (head instanceof GitHubTagSCMHead) {
        link = new GitHubTagAction(remoteRepo, head.getName());
        desc = null;
    } else if (head instanceof GitHubPRSCMHead) {
        GitHubPRSCMHead prHead = (GitHubPRSCMHead) head;
        link = new GitHubPRAction(remoteRepo, prHead.getPrNumber());
        desc = remoteRepo.getPullRequest(prHead.getPrNumber()).getTitle();
    }

    if (nonNull(link)) {
        actions.add(link);
    }

    actions.add(new ObjectMetadataAction(null, desc, isNull(link) ? null : link.getUrlName()));
    if (primary) {
        actions.add(new PrimaryInstanceMetadataAction());
    }

    return actions;
}
 
Example #12
Source File: GitHubSCMNavigatorTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void fetchActions() throws Exception {
    assertThat(navigator.fetchActions(Mockito.mock(SCMNavigatorOwner.class), null, null), Matchers.containsInAnyOrder(
            Matchers.is(
                    new ObjectMetadataAction("CloudBeers, Inc.", null, "https://github.com/cloudbeers")
            ),
            Matchers.is(new GitHubOrgMetadataAction("https://avatars.githubusercontent.com/u/4181899?v=3")),
            Matchers.is(new GitHubLink("icon-github-logo", "https://github.com/cloudbeers"))));
}
 
Example #13
Source File: GitHubSCMSourceTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void fetchActions() throws Exception {
    assertThat(source.fetchActions(null, null), Matchers.containsInAnyOrder(
            Matchers.is(
                    new ObjectMetadataAction(null, "You only live once", "http://yolo.example.com")
            ),
            Matchers.is(
                    new GitHubDefaultBranch("cloudbeers", "yolo", "master")
            ),
            instanceOf(GitHubRepoMetadataAction.class),
            Matchers.is(new GitHubLink("icon-github-repo", "https://github.com/cloudbeers/yolo"))));
}
 
Example #14
Source File: GitHubSCMNavigator.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@NonNull
@Override
public List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner,
                                    @CheckForNull SCMNavigatorEvent event,
                                    @NonNull TaskListener listener) throws IOException, InterruptedException {
    // TODO when we have support for trusted events, use the details from event if event was from trusted source
    listener.getLogger().printf("Looking up details of %s...%n", getRepoOwner());
    List<Action> result = new ArrayList<>();
    StandardCredentials credentials = Connector.lookupScanCredentials((Item)owner, getApiUri(), credentialsId);
    GitHub hub = Connector.connect(getApiUri(), credentials);
    try {
        Connector.checkApiRateLimit(listener, hub);
        GHUser u = hub.getUser(getRepoOwner());
        String objectUrl = u.getHtmlUrl() == null ? null : u.getHtmlUrl().toExternalForm();
        result.add(new ObjectMetadataAction(
                Util.fixEmpty(u.getName()),
                null,
                objectUrl)
        );
        result.add(new GitHubOrgMetadataAction(u));
        result.add(new GitHubLink("icon-github-logo", u.getHtmlUrl()));
        if (objectUrl == null) {
            listener.getLogger().println("Organization URL: unspecified");
        } else {
            listener.getLogger().printf("Organization URL: %s%n",
                    HyperlinkNote.encodeTo(objectUrl, StringUtils.defaultIfBlank(u.getName(), objectUrl)));
        }
        return result;
    } finally {
        Connector.release(hub);
    }
}
 
Example #15
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@NonNull
@Override
protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event,
                                       @NonNull TaskListener listener) throws IOException {
    // TODO when we have support for trusted events, use the details from event if event was from trusted source
    List<Action> result = new ArrayList<>();
    result.add(new GitHubRepoMetadataAction());
    String repository = this.repository;

    StandardCredentials credentials = Connector.lookupScanCredentials((Item) getOwner(), apiUri, credentialsId);
    GitHub hub = Connector.connect(apiUri, credentials);
    try {
        Connector.checkConnectionValidity(apiUri, listener, credentials, hub);
        try {
            ghRepository = hub.getRepository(getRepoOwner() + '/' + repository);
            resolvedRepositoryUrl = ghRepository.getHtmlUrl();
        } catch (FileNotFoundException e) {
            throw new AbortException(
                    String.format("Invalid scan credentials when using %s to connect to %s/%s on %s",
                            credentials == null ? "anonymous access" : CredentialsNameProvider.name(credentials), repoOwner, repository, apiUri));
        }
        result.add(new ObjectMetadataAction(null, ghRepository.getDescription(), Util.fixEmpty(ghRepository.getHomepage())));
        result.add(new GitHubLink("icon-github-repo", ghRepository.getHtmlUrl()));
        if (StringUtils.isNotBlank(ghRepository.getDefaultBranch())) {
            result.add(new GitHubDefaultBranch(getRepoOwner(), repository, ghRepository.getDefaultBranch()));
        }
        return result;
    } finally {
        Connector.release(hub);
    }
}
 
Example #16
Source File: GitLabSCMNavigator.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
@NonNull
@Override
protected List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner,
    SCMNavigatorEvent event,
    @NonNull TaskListener listener) throws IOException, InterruptedException {
    getGitlabOwner();
    String fullName = gitlabOwner.getFullName();
    String webUrl = gitlabOwner.getWebUrl();
    String avatarUrl = gitlabOwner.getAvatarUrl();
    String description = null;
    if (gitlabOwner instanceof GitLabGroup) {
        description = ((GitLabGroup) gitlabOwner).getDescription();
    }
    List<Action> result = new ArrayList<>();
    result.add(new ObjectMetadataAction(
        Util.fixEmpty(fullName),
        description,
        webUrl)
    );
    if (StringUtils.isNotBlank(avatarUrl)) {
        result.add(new GitLabAvatar(avatarUrl));
    }
    result.add(GitLabLink.toGroup(webUrl));
    if (StringUtils.isBlank(webUrl)) {
        listener.getLogger().println("Web URL unspecified");
    } else {
        listener.getLogger().printf("%s URL: %s%n", gitlabOwner.getWord(),
            HyperlinkNote
                .encodeTo(webUrl, StringUtils.defaultIfBlank(fullName, webUrl)));
    }
    return result;
}
 
Example #17
Source File: ActionableBuilderTest.java    From office-365-connector-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void pullRequestActionable_OnEmptyContributorDisplayName_AdddFact() {

    // given
    // from @Before
    SCMHead head = new SCMHeadBuilder("Pull Request");

    Job job = mock(Job.class);
    when(run.getParent()).thenReturn(job);

    mockStatic(SCMHead.HeadByItem.class);
    when(SCMHead.HeadByItem.findHead(job)).thenReturn(head);
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(null);

    ContributorMetadataAction contributorMetadataAction = mock(ContributorMetadataAction.class);
    when(contributorMetadataAction.getContributor()).thenReturn("damianszczepanik");
    when(contributorMetadataAction.getContributorDisplayName()).thenReturn(null);
    when(job.getAction(ContributorMetadataAction.class)).thenReturn(contributorMetadataAction);

    // when
    Deencapsulation.invoke(actionableBuilder, "pullRequestActionable");

    // then
    String pronoun = StringUtils.defaultIfBlank(
            head.getPronoun(),
            Messages.Office365ConnectorWebhookNotifier_ChangeRequestPronoun());
    FactAssertion.assertThat(factsBuilder)
            .hasName(Messages.Office365ConnectorWebhookNotifier_AuthorHeader(pronoun))
            .hasValue("damianszczepanik");
}
 
Example #18
Source File: ActionableBuilderTest.java    From office-365-connector-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void pullRequestActionable_OnEmptyContributor_AdddFact() {

    // given
    // from @Before
    SCMHead head = new SCMHeadBuilder("Pull Request");

    Job job = mock(Job.class);
    when(run.getParent()).thenReturn(job);

    mockStatic(SCMHead.HeadByItem.class);
    when(SCMHead.HeadByItem.findHead(job)).thenReturn(head);
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(null);

    ContributorMetadataAction contributorMetadataAction = mock(ContributorMetadataAction.class);
    when(contributorMetadataAction.getContributor()).thenReturn(null);
    when(contributorMetadataAction.getContributorDisplayName()).thenReturn("Damian Szczepanik");
    when(job.getAction(ContributorMetadataAction.class)).thenReturn(contributorMetadataAction);

    // when
    Deencapsulation.invoke(actionableBuilder, "pullRequestActionable");

    // then
    String pronoun = StringUtils.defaultIfBlank(
            head.getPronoun(),
            Messages.Office365ConnectorWebhookNotifier_ChangeRequestPronoun());
    FactAssertion.assertThat(factsBuilder)
            .hasName(Messages.Office365ConnectorWebhookNotifier_AuthorHeader(pronoun))
            .hasValue("Damian Szczepanik");
}
 
Example #19
Source File: ActionableBuilderTest.java    From office-365-connector-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void pullRequestActionable_OnObjectMetadataAction_DoesNotAddFact() {

    // given
    // from @Before
    SCMHead head = new SCMHeadBuilder("Pull Request");

    Job job = mock(Job.class);
    when(run.getParent()).thenReturn(job);

    mockStatic(SCMHead.HeadByItem.class);
    when(SCMHead.HeadByItem.findHead(job)).thenReturn(head);
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(null);

    ContributorMetadataAction contributorMetadataAction = mock(ContributorMetadataAction.class);
    when(contributorMetadataAction.getContributor()).thenReturn("damianszczepanik");
    when(contributorMetadataAction.getContributorDisplayName()).thenReturn("Damian Szczepanik");
    when(job.getAction(ContributorMetadataAction.class)).thenReturn(contributorMetadataAction);

    // when
    Deencapsulation.invoke(actionableBuilder, "pullRequestActionable");

    // then
    assertThat(factsBuilder.collect()).hasSize(1);

    List<PotentialAction> potentialActions = Deencapsulation.getField(actionableBuilder, "potentialActions");
    assertThat(potentialActions).isEmpty();
}
 
Example #20
Source File: Caches.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public Optional<Branch> load(String key) throws Exception {
    Jenkins jenkins = Objects.firstNonNull(this.jenkins, Jenkins.getInstance());
    Job job = jenkins.getItemByFullName(key, Job.class);
    if (job == null) {
        return Optional.absent();
    }
    ObjectMetadataAction om = job.getAction(ObjectMetadataAction.class);
    PrimaryInstanceMetadataAction pima = job.getAction(PrimaryInstanceMetadataAction.class);
    String url = om != null && om.getObjectUrl() != null ? om.getObjectUrl() : null;
    if (StringUtils.isEmpty(url)) {
        /*
         * Borrowed from https://github.com/jenkinsci/branch-api-plugin/blob/c4d394415cf25b6890855a08360119313f1330d2/src/main/java/jenkins/branch/BranchNameContributor.java#L63
         * for those that don't implement object metadata action
         */
        ItemGroup parent = job.getParent();
        if (parent instanceof MultiBranchProject) {
            BranchProjectFactory projectFactory = ((MultiBranchProject) parent).getProjectFactory();
            if (projectFactory.isProject(job)) {
                SCMHead head = projectFactory.getBranch(job).getHead();
                url = head.getName();
            }
        }
    }
    if (StringUtils.isEmpty(url) && pima == null) {
        return Optional.absent();
    }
    return Optional.of(new Branch(url, pima != null, BlueIssueFactory.resolve(job)));
}
 
Example #21
Source File: ActionableBuilder.java    From office-365-connector-plugin with Apache License 2.0 5 votes vote down vote up
private void pullRequestActionable() {
    Job job = run.getParent();
    SCMHead head = SCMHead.HeadByItem.findHead(job);
    if (head instanceof ChangeRequestSCMHead) {
        String pronoun = StringUtils.defaultIfBlank(
                head.getPronoun(),
                Messages.Office365ConnectorWebhookNotifier_ChangeRequestPronoun()
        );
        String viewHeader = Messages.Office365ConnectorWebhookNotifier_ViewHeader(pronoun);
        String titleHeader = Messages.Office365ConnectorWebhookNotifier_TitleHeader(pronoun);
        String authorHeader = Messages.Office365ConnectorWebhookNotifier_AuthorHeader(pronoun);

        ObjectMetadataAction oma = job.getAction(ObjectMetadataAction.class);
        if (oma != null) {
            String urlString = oma.getObjectUrl();
            PotentialAction viewPRPotentialAction = new PotentialAction(viewHeader, urlString);
            potentialActions.add(viewPRPotentialAction);
            factsBuilder.addFact(titleHeader, oma.getObjectDisplayName());
        }
        ContributorMetadataAction cma = job.getAction(ContributorMetadataAction.class);
        if (cma != null) {
            String contributor = cma.getContributor();
            String contributorDisplayName = cma.getContributorDisplayName();

            if (StringUtils.isNotBlank(contributor) && StringUtils.isNotBlank(contributorDisplayName)) {
                factsBuilder.addFact(authorHeader, String.format("%s (%s)", contributor, contributorDisplayName));
            } else {
                factsBuilder.addFact(authorHeader, StringUtils.defaultIfBlank(contributor, contributorDisplayName));
            }
        }
    }
}
 
Example #22
Source File: GiteaSCMNavigator.java    From gitea-plugin with MIT License 5 votes vote down vote up
@NonNull
@Override
protected List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner, SCMNavigatorEvent event,
                                       @NonNull TaskListener listener) throws IOException, InterruptedException {
    if (this.giteaOwner == null) {
        try (GiteaConnection c = gitea(owner).open()) {
            this.giteaOwner = c.fetchUser(repoOwner);
            if (StringUtils.isBlank(giteaOwner.getEmail())) {
                this.giteaOwner = c.fetchOrganization(repoOwner);
            }
        }
    }
    List<Action> result = new ArrayList<>();
    String objectUrl = UriTemplate.buildFromTemplate(serverUrl)
            .path("owner")
            .build()
            .set("owner", repoOwner)
            .expand();
    result.add(new ObjectMetadataAction(
            Util.fixEmpty(giteaOwner.getFullName()),
            null,
            objectUrl)
    );
    if (StringUtils.isNotBlank(giteaOwner.getAvatarUrl())) {
        result.add(new GiteaAvatar(giteaOwner.getAvatarUrl()));
    }
    result.add(new GiteaLink("icon-gitea-org", objectUrl));
    if (giteaOwner instanceof GiteaOrganization) {
        String website = ((GiteaOrganization) giteaOwner).getWebsite();
        if (StringUtils.isBlank(website)) {
            listener.getLogger().println("Organization website: unspecified");
        } else {
            listener.getLogger().printf("Organization website: %s%n",
                    HyperlinkNote.encodeTo(website, StringUtils.defaultIfBlank(giteaOwner.getFullName(), website)));
        }
    }
    return result;
}
 
Example #23
Source File: CachesTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void testBranchCacheLoaderWithNoPrimaryInstanceMetadataAction() throws Exception {
    ObjectMetadataAction metadataAction = new ObjectMetadataAction("A cool branch", "A very cool change", "http://example.com/branches/cool-branch");
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(metadataAction);
    when(job.getFullName()).thenReturn("cool-branch");

    Caches.BranchCacheLoader loader = new Caches.BranchCacheLoader(jenkins);
    BranchImpl.Branch branch = loader.load(job.getFullName()).orNull();

    assertNotNull(branch);
    assertFalse(branch.isPrimary());
    assertEquals("http://example.com/branches/cool-branch", branch.getUrl());
}
 
Example #24
Source File: CachesTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void testBranchCacheLoader() throws Exception {
    ObjectMetadataAction metadataAction = new ObjectMetadataAction("A cool branch", "A very cool change", "http://example.com/branches/cool-branch");
    PrimaryInstanceMetadataAction instanceMetadataAction = new PrimaryInstanceMetadataAction();
    when(job.getAction(ObjectMetadataAction.class)).thenReturn(metadataAction);
    when(job.getAction(PrimaryInstanceMetadataAction.class)).thenReturn(instanceMetadataAction);

    Caches.BranchCacheLoader loader = new Caches.BranchCacheLoader(jenkins);
    BranchImpl.Branch branch = loader.load(job.getFullName()).orNull();

    assertNotNull(branch);
    assertTrue(branch.isPrimary());
    assertEquals("http://example.com/branches/cool-branch", branch.getUrl());
}
 
Example #25
Source File: SourceActions.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Nonnull
List<Action> retrieve(@CheckForNull SCMSourceEvent event, @Nonnull TaskListener listener) throws IOException {
    GitLabProject project = source.getProject();
    return asList(
            new ObjectMetadataAction(project.getNameWithNamespace(), project.getDescription(), project.getWebUrl()),
            new GitLabProjectAvatarMetadataAction(project.getId(), source.getSourceSettings().getConnectionName()),
            GitLabLinkAction.toProject(project));
}
 
Example #26
Source File: AbstractGerritSCMSource.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@NonNull
@Override
protected List<Action> retrieveActions(
    @NonNull SCMHead head, @CheckForNull SCMHeadEvent event, @NonNull TaskListener listener)
    throws IOException, InterruptedException {
  final List<Action> actions =
      doRetrieve(
          head,
          (GitClient client,
              GerritSCMSourceContext context,
              String remoteName,
              Changes.QueryRequest changeQuery) -> {
            SCMSourceOwner owner = getOwner();
            if (owner instanceof Actionable && head instanceof ChangeSCMHead) {
              final Actionable actionableOwner = (Actionable) owner;
              final ChangeSCMHead change = (ChangeSCMHead) head;
              String gerritBaseUrl = getGerritBaseUrl();

              return actionableOwner
                  .getActions(GitRemoteHeadRefAction.class)
                  .stream()
                  .filter(action -> action.getRemote().equals(getRemote()))
                  .map(
                      action ->
                          new ObjectMetadataAction(
                              change.getName(),
                              change.getId(),
                              String.format("%s%d", gerritBaseUrl, change.getChangeNumber())))
                  .collect(Collectors.toList());
            } else {
              return Collections.emptyList();
            }
          },
          new GerritSCMSourceContext(null, SCMHeadObserver.none()).withTraits(getTraits()),
          listener,
          false);

  final ImmutableList.Builder<Action> resultBuilder = new ImmutableList.Builder<>();
  resultBuilder.addAll(super.retrieveActions(head, event, listener));
  resultBuilder.addAll(actions);
  return resultBuilder.build();
}
 
Example #27
Source File: GiteaSCMSource.java    From gitea-plugin with MIT License 4 votes vote down vote up
@NonNull
@Override
protected List<Action> retrieveActions(@NonNull SCMHead head, SCMHeadEvent event, @NonNull TaskListener listener)
        throws IOException, InterruptedException {
    if (giteaRepository == null) {
        try (GiteaConnection c = gitea().open()) {
            listener.getLogger().format("Looking up repository %s/%s%n", repoOwner, repository);
            giteaRepository = c.fetchRepository(repoOwner, repository);
        }
    }
    List<Action> result = new ArrayList<>();
    if (head instanceof BranchSCMHead) {
        String branchUrl = UriTemplate.buildFromTemplate(serverUrl)
                .path(UriTemplateBuilder.var("owner"))
                .path(UriTemplateBuilder.var("repository"))
                .literal("/src/branch")
                .path(UriTemplateBuilder.var("branch"))
                .build()
                .set("owner", repoOwner)
                .set("repository", repository)
                .set("branch", head.getName())
                .expand();
        result.add(new ObjectMetadataAction(
                null,
                null,
                branchUrl
        ));
        result.add(new GiteaLink("icon-gitea-branch", branchUrl));
        if (head.getName().equals(giteaRepository.getDefaultBranch())) {
            result.add(new PrimaryInstanceMetadataAction());
        }
    } else if (head instanceof TagSCMHead) {
        String tagUrl = UriTemplate.buildFromTemplate(serverUrl)
                .path(UriTemplateBuilder.var("owner"))
                .path(UriTemplateBuilder.var("repository"))
                .literal("/src/tag")
                .path(UriTemplateBuilder.var("tag"))
                .build()
                .set("owner", repoOwner)
                .set("repository", repository)
                .set("tag", head.getName())
                .expand();
        result.add(new ObjectMetadataAction(
                null,
                null,
                tagUrl
        ));
        result.add(new GiteaLink("icon-gitea-branch", tagUrl));
        if (head.getName().equals(giteaRepository.getDefaultBranch())) {
            result.add(new PrimaryInstanceMetadataAction());
        }
    } else if (head instanceof PullRequestSCMHead) {
        String pullUrl = UriTemplate.buildFromTemplate(serverUrl)
                .path(UriTemplateBuilder.var("owner"))
                .path(UriTemplateBuilder.var("repository"))
                .literal("/pulls")
                .path(UriTemplateBuilder.var("id"))
                .build()
                .set("owner", repoOwner)
                .set("repository", repository)
                .set("id", ((PullRequestSCMHead) head).getId())
                .expand();
        result.add(new ObjectMetadataAction(
                null,
                null,
                pullUrl
        ));
        result.add(new GiteaLink("icon-gitea-branch", pullUrl));
    }
    return result;
}