Search in sources :

Example 1 with JiraSite

use of hudson.plugins.jira.JiraSite in project blueocean-plugin by jenkinsci.

the class BlueJiraIssueTest method issuesForChangeSetItem.

@Test
public void issuesForChangeSetItem() throws Exception {
    Job job = mock(Job.class);
    Run run = mock(Run.class);
    ChangeLogSet logSet = mock(ChangeLogSet.class);
    ChangeLogSet.Entry entry = mock(ChangeLogSet.Entry.class);
    when(entry.getParent()).thenReturn(logSet);
    when(logSet.getRun()).thenReturn(run);
    when(run.getParent()).thenReturn(job);
    mockStatic(JiraSite.class);
    JiraSite site = mock(JiraSite.class);
    when(site.getIssuePattern()).thenReturn(JiraSite.DEFAULT_ISSUE_PATTERN);
    when(JiraSite.get(job)).thenReturn(site);
    when(entry.getMsg()).thenReturn("TEST-123");
    // Should resolve no issues because there is no JiraJobAction
    Assert.assertTrue(BlueIssueFactory.resolve(entry).isEmpty());
    // Setup a job with a JiraJobAction
    JiraIssue jiraIssue1 = new JiraIssue("FOO-123", "A cool issue");
    JiraIssue jiraIssue2 = new JiraIssue("FOO-124", "A cool issue");
    when(site.getUrl(jiraIssue1)).thenReturn(new URL("http://jira.example.com/browse/FOO-123"));
    when(site.getUrl(jiraIssue2)).thenReturn(new URL("http://jira.example.com/browse/FOO-124"));
    JiraBuildAction action = new JiraBuildAction(run, Sets.newLinkedHashSet(ImmutableSet.of(jiraIssue1, jiraIssue2)));
    when(run.getAction(JiraBuildAction.class)).thenReturn(action);
    // Expect two issues
    when(entry.getMsg()).thenReturn("something FOO-123 vivek FOO-124 #ace");
    List<BlueIssue> issues = Lists.newLinkedList(BlueIssueFactory.resolve(entry));
    Assert.assertEquals(2, issues.size());
    BlueIssue issue1 = issues.get(0);
    Assert.assertEquals("FOO-123", issue1.getId());
    Assert.assertEquals("http://jira.example.com/browse/FOO-123", issue1.getURL());
    BlueIssue issue2 = issues.get(1);
    Assert.assertEquals("FOO-124", issue2.getId());
    Assert.assertEquals("http://jira.example.com/browse/FOO-124", issue2.getURL());
}
Also used : JiraIssue(hudson.plugins.jira.model.JiraIssue) JiraBuildAction(hudson.plugins.jira.JiraBuildAction) ChangeLogSet(hudson.scm.ChangeLogSet) Run(hudson.model.Run) Job(hudson.model.Job) JiraSite(hudson.plugins.jira.JiraSite) URL(java.net.URL) BlueIssue(io.jenkins.blueocean.rest.model.BlueIssue) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with JiraSite

use of hudson.plugins.jira.JiraSite in project blueocean-plugin by jenkinsci.

the class BlueJiraIssueTest method issuesForJobActionDoesNotHaveIssue.

@Test
public void issuesForJobActionDoesNotHaveIssue() throws Exception {
    Job job = mock(Job.class);
    mockStatic(JiraSite.class);
    JiraSite site = mock(JiraSite.class);
    when(JiraSite.get(job)).thenReturn(site);
    JiraJobAction action = new JiraJobAction(job, null);
    when(job.getAction(JiraJobAction.class)).thenReturn(action);
    // Should resolve no issues because there is no JiraJobAction
    Assert.assertTrue(BlueIssueFactory.resolve(job).isEmpty());
}
Also used : Job(hudson.model.Job) JiraJobAction(hudson.plugins.jira.JiraJobAction) JiraSite(hudson.plugins.jira.JiraSite) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with JiraSite

use of hudson.plugins.jira.JiraSite in project blueocean-plugin by jenkinsci.

the class JiraSCMListenerTest method onChangeLogParsed.

@Test
public void onChangeLogParsed() throws Exception {
    JiraSCMListener listener = new JiraSCMListener();
    Job job = mock(Job.class);
    Run run = mock(Run.class);
    ChangeLogSet logSet = mock(ChangeLogSet.class);
    final ChangeLogSet.Entry entry = mock(ChangeLogSet.Entry.class);
    when(entry.getParent()).thenReturn(logSet);
    when(logSet.getRun()).thenReturn(run);
    when(run.getParent()).thenReturn(job);
    when(entry.getMsg()).thenReturn("TEST-123");
    ChangeLogSet<ChangeLogSet.Entry> set = new ChangeLogSet<ChangeLogSet.Entry>(run, null) {

        @Override
        public boolean isEmptySet() {
            return false;
        }

        @Override
        public Iterator<Entry> iterator() {
            return ImmutableSet.of(entry).iterator();
        }
    };
    // Setup JIRA site
    mockStatic(JiraSite.class);
    JiraSite site = mock(JiraSite.class);
    JiraSession session = mock(JiraSession.class);
    when(site.getIssuePattern()).thenReturn(JiraSite.DEFAULT_ISSUE_PATTERN);
    when(site.getSession()).thenReturn(session);
    when(JiraSite.get(job)).thenReturn(site);
    Issue rawIssue = mock(Issue.class);
    when(rawIssue.getKey()).thenReturn("TEST-123");
    when(rawIssue.getSummary()).thenReturn("Foo");
    when(session.getIssuesFromJqlSearch("key in ('TEST-123')")).thenReturn(Lists.newArrayList(rawIssue));
    JiraBuildAction action = new JiraBuildAction(run, Sets.<JiraIssue>newHashSet());
    when(run.getAction(JiraBuildAction.class)).thenReturn(action);
    listener.onChangeLogParsed(run, null, null, set);
    Assert.assertFalse(action.getIssues().isEmpty());
    JiraIssue issue = action.getIssue("TEST-123");
    Assert.assertNotNull(issue);
    Assert.assertEquals("TEST-123", issue.getKey());
}
Also used : JiraBuildAction(hudson.plugins.jira.JiraBuildAction) JiraIssue(hudson.plugins.jira.model.JiraIssue) ChangeLogSet(hudson.scm.ChangeLogSet) Issue(com.atlassian.jira.rest.client.api.domain.Issue) JiraIssue(hudson.plugins.jira.model.JiraIssue) JiraSession(hudson.plugins.jira.JiraSession) Run(hudson.model.Run) Job(hudson.model.Job) JiraSite(hudson.plugins.jira.JiraSite) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with JiraSite

use of hudson.plugins.jira.JiraSite in project blueocean-plugin by jenkinsci.

the class JiraSCMListenerTest method changeSetHasNoJiraIssue.

@Test
public void changeSetHasNoJiraIssue() throws Exception {
    JiraSCMListener listener = new JiraSCMListener();
    Job job = mock(Job.class);
    Run run = mock(Run.class);
    ChangeLogSet logSet = mock(ChangeLogSet.class);
    final ChangeLogSet.Entry entry = mock(ChangeLogSet.Entry.class);
    when(entry.getParent()).thenReturn(logSet);
    when(logSet.getRun()).thenReturn(run);
    when(run.getParent()).thenReturn(job);
    when(entry.getMsg()).thenReturn("No jira ticket here");
    ChangeLogSet<ChangeLogSet.Entry> set = new ChangeLogSet<ChangeLogSet.Entry>(run, null) {

        @Override
        public boolean isEmptySet() {
            return false;
        }

        @Override
        public Iterator<Entry> iterator() {
            return ImmutableSet.of(entry).iterator();
        }
    };
    // Setup JIRA site
    mockStatic(JiraSite.class);
    JiraSite site = mock(JiraSite.class);
    when(site.getIssuePattern()).thenReturn(JiraSite.DEFAULT_ISSUE_PATTERN);
    when(JiraSite.get(job)).thenReturn(site);
    JiraBuildAction action = new JiraBuildAction(run, Sets.<JiraIssue>newHashSet());
    when(run.getAction(JiraBuildAction.class)).thenReturn(action);
    listener.onChangeLogParsed(run, null, null, set);
    Assert.assertTrue(action.getIssues().isEmpty());
}
Also used : JiraBuildAction(hudson.plugins.jira.JiraBuildAction) ChangeLogSet(hudson.scm.ChangeLogSet) Run(hudson.model.Run) Job(hudson.model.Job) JiraSite(hudson.plugins.jira.JiraSite) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with JiraSite

use of hudson.plugins.jira.JiraSite in project blueocean-plugin by jenkinsci.

the class JiraSCMListener method onChangeLogParsed.

@Override
public void onChangeLogParsed(Run<?, ?> run, SCM scm, TaskListener listener, ChangeLogSet<?> changelog) throws Exception {
    JiraSite jiraSite = JiraSite.get(run.getParent());
    if (jiraSite == null) {
        return;
    }
    List<String> issueKeys = Lists.newArrayList();
    for (ChangeLogSet.Entry entry : changelog) {
        issueKeys.addAll(BlueJiraIssue.findIssueKeys(entry.getMsg(), jiraSite.getIssuePattern()));
    }
    if (issueKeys.isEmpty()) {
        return;
    }
    String jql = constructJQLQuery(issueKeys);
    JiraSession session = jiraSite.getSession();
    if (session == null) {
        return;
    }
    // Query for JIRA issues
    Set<JiraIssue> issuesFromJqlSearch = Sets.newHashSet(Iterables.transform(session.getIssuesFromJqlSearch(jql), new Function<Issue, JiraIssue>() {

        @Override
        public JiraIssue apply(Issue input) {
            return new JiraIssue(input);
        }
    }));
    // If there are no JIRA issues, do not update the actions
    if (issuesFromJqlSearch.isEmpty()) {
        return;
    }
    // Create or update the JiraBuildAction
    JiraBuildAction action = run.getAction(JiraBuildAction.class);
    if (action == null) {
        run.addAction(new JiraBuildAction(run, issuesFromJqlSearch));
    } else {
        action.addIssues(issuesFromJqlSearch);
    }
    run.save();
}
Also used : JiraIssue(hudson.plugins.jira.model.JiraIssue) Function(com.google.common.base.Function) JiraBuildAction(hudson.plugins.jira.JiraBuildAction) ChangeLogSet(hudson.scm.ChangeLogSet) Issue(com.atlassian.jira.rest.client.api.domain.Issue) JiraIssue(hudson.plugins.jira.model.JiraIssue) JiraSession(hudson.plugins.jira.JiraSession) JiraSite(hudson.plugins.jira.JiraSite)

Aggregations

JiraSite (hudson.plugins.jira.JiraSite)8 Job (hudson.model.Job)7 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 JiraBuildAction (hudson.plugins.jira.JiraBuildAction)5 JiraIssue (hudson.plugins.jira.model.JiraIssue)5 ChangeLogSet (hudson.scm.ChangeLogSet)5 Run (hudson.model.Run)4 Issue (com.atlassian.jira.rest.client.api.domain.Issue)3 JiraSession (hudson.plugins.jira.JiraSession)3 JiraJobAction (hudson.plugins.jira.JiraJobAction)2 BlueIssue (io.jenkins.blueocean.rest.model.BlueIssue)2 URL (java.net.URL)2 Function (com.google.common.base.Function)1