jenkins.scm.api.metadata.ContributorMetadataAction Java Examples

The following examples show how to use jenkins.scm.api.metadata.ContributorMetadataAction. 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: 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 #2
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 #3
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 #4
Source File: CachesTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void testPullRequestCacheLoaderWithoutObjectMetadataAction() throws Exception {

    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());
    assertNull(pr.getTitle());
    assertNull(pr.getUrl());
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
Source File: SourceActions.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 4 votes vote down vote up
private Action createAuthorMetadataAction(GitLabMergeRequest mr) {
    return new ContributorMetadataAction(mr.getAuthor().getName(), mr.getAuthor().getUsername(), mr.getAuthor().getEmail());
}