JiraSCMListener extends SCMListener { @Override public void onChangeLogParsed(Run<?, ?> run, SCM scm, TaskListener listener, ChangeLogSet<?> changelog) throws Exception { try { JiraSite jiraSite = JiraSite.get(run.getParent()); if (jiraSite == null) { return; } Collection<String> issueKeys = getIssueKeys(changelog, jiraSite.getIssuePattern()); if (issueKeys.isEmpty()) { return; } String jql = constructJQLQuery(issueKeys); JiraSession session = jiraSite.getSession(); if (session == null) { return; } List<Issue> issues = session.getIssuesFromJqlSearch(jql); Set<JiraIssue> issuesFromJqlSearch = issues == null ? Collections.emptySet() : issues.stream().map( input -> new JiraIssue(input) ) .collect( Collectors.toSet() ); if (issuesFromJqlSearch.isEmpty()) { return; } JiraBuildAction action = run.getAction(JiraBuildAction.class); if (action == null) { run.addAction(new JiraBuildAction(run, issuesFromJqlSearch)); } else { action.addIssues(issuesFromJqlSearch); } run.save(); } catch (Exception e ){ LOGGER.warn( "Failure executing Jira query to fetch issues. Skipping recording Jira issues.: {}", e.getMessage() ); LOGGER.debug( e.getMessage(), e); } }  @Override void onChangeLogParsed(Run<?, ?> run, SCM scm, TaskListener listener, ChangeLogSet<?> changelog);  }
@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 Collections.singletonList(entry).iterator(); } }; 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(Collections.singletonList(rawIssue)); JiraBuildAction action = new JiraBuildAction(run, new HashSet()); 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()); }
@Test public void onChangeLogParsedCreatesAction() 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 Collections.singletonList(entry).iterator(); } }; 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(Collections.singletonList(rawIssue)); when(run.getAction(JiraBuildAction.class)).thenReturn(null); ArgumentCaptor<JiraBuildAction> actionArgumentCaptor = ArgumentCaptor.forClass(JiraBuildAction.class); listener.onChangeLogParsed(run, null,null, set); verify(run).addAction(actionArgumentCaptor.capture()); JiraBuildAction action = actionArgumentCaptor.getValue(); Assert.assertFalse(action.getIssues().isEmpty()); JiraIssue issue = action.getIssue("TEST-123"); Assert.assertNotNull(issue); Assert.assertEquals("TEST-123", issue.getKey()); }
@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 Collections.singletonList(entry).iterator(); } }; 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, new HashSet()); when(run.getAction(JiraBuildAction.class)).thenReturn(action); listener.onChangeLogParsed(run, null,null, set); Assert.assertTrue(action.getIssues().isEmpty()); }
@Test public void noJiraSiteDefined() throws Exception { JiraSCMListener listener = new JiraSCMListener(); Job job = mock(Job.class); Run run = mock(Run.class); when(run.getParent()).thenReturn(job); listener.onChangeLogParsed(run, null, null, null); }