Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DSL.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ job {
firstWordOfUpstreamCommit()
// - or -
mentionedInCommit()
// - or -
mentionedInCommitOrUpstreamCommits()
}
jiraOperations {
transition('Deploy to Test');
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Out of the box, you may discover issues by:
* The issue key as the first word of the upstream commit message
* Manually specifying an issue
* Looking for it being mentioned somewhere in the commit message
* Looking for it being mentioned somewhere in the commit message or any upstream build commit messages

To all of these JIRA issues, you may:

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import hudson.model.Run;
import jenkins.model.Jenkins;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -73,6 +76,43 @@ public static AbstractBuild getUpstreamBuild(AbstractBuild<?, ?> build)
return getUpstreamBuild(upstreamBuild);
}

/**
* Gets all upstream builds which caused this build.
*
* @return
*/
public static List<AbstractBuild<?, ?>> getUpstreamBuilds(AbstractBuild<?, ?> build)
{
logger.log(Level.FINE, "Find build upstream of " + build.getId());

Cause.UpstreamCause cause = getUpstreamCause(build);
if (cause == null)
{
logger.log(Level.FINE, "No upstream cause, so must be root upstream build: " + build.getId());
return Collections.<AbstractBuild<?, ?>>emptyList();
}
logger.log(Level.FINE, "Found upstream cause: " + cause.toString() + "(" + cause.getShortDescription() + ")");
AbstractProject project = (AbstractProject) Jenkins.getInstance().getItem(cause.getUpstreamProject(), build.getProject());
if (project == null)
{
logger.log(Level.WARNING, "Found an UpstreamCause (" + cause.toString()
+ "), but the upstream project (" + cause.getUpstreamProject() + ") does not appear to be valid!");
return Collections.<AbstractBuild<?, ?>>emptyList();
}
AbstractBuild upstreamBuild = project.getBuildByNumber(cause.getUpstreamBuild());
if (upstreamBuild == null)
{
logger.log(Level.WARNING, "Found an UpstreamCause (" + cause.toString()
+ "), and an upstream project (" + project.getName() + "), but the build is invalid!" + cause.getUpstreamBuild());
return Collections.<AbstractBuild<?, ?>>emptyList();
}
List<AbstractBuild<?, ?>> upstreamBuilds = new LinkedList<>();
upstreamBuilds.add(upstreamBuild);
upstreamBuilds.addAll(getUpstreamBuilds(upstreamBuild));

return upstreamBuilds;
}

private static Cause.UpstreamCause getUpstreamCause(Run run)
{
if (run == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
import hudson.Extension;
import javaposse.jobdsl.dsl.Context;
import javaposse.jobdsl.plugin.ContextExtensionPoint;
import org.jenkinsci.plugins.jiraext.view.FirstWordOfCommitStrategy;
import org.jenkinsci.plugins.jiraext.view.FirstWordOfUpstreamCommitStrategy;
import org.jenkinsci.plugins.jiraext.view.IssueStrategyExtension;
import org.jenkinsci.plugins.jiraext.view.MentionedInCommitStrategy;
import org.jenkinsci.plugins.jiraext.view.SingleTicketStrategy;
import org.jenkinsci.plugins.jiraext.view.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDE might have gobbled this up? We don't have a style guide but try to be consistent - please revert.


/**
* @author dalvizu
Expand Down Expand Up @@ -60,6 +56,11 @@ public void mentionedInCommit()
issueStrategy = new MentionedInCommitStrategy();
}

public void mentionedInCommitOrUpstreamCommits()
{
issueStrategy = new MentionedInCommitOrUpstreamCommitsStrategy();
}

/**
* Allows direct manipulation of the generated XML. The {@code issueStrategy} node is passed into the configure block.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@
**************************************************************************/
package org.jenkinsci.plugins.jiraext.view;

import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.scm.ChangeLogSet;
import org.jenkinsci.plugins.jiraext.domain.JiraCommit;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* An IssueStrategyExtension which assumes you want to return a list of jira commits by
* deriving them from the provided build's {@link ChangeLogSet}
* deriving them from the provided build's {@link ChangeLogSet}. Also includes upstream build ChangeLogSets
*
* @author dalvizu
*/
Expand All @@ -40,18 +45,19 @@ public abstract class AbstractParsingIssueStrategy
private static final Logger _logger = Logger.getLogger(FirstWordOfCommitStrategy.class.getName());

@Override
public final List<JiraCommit> getJiraCommits(AbstractBuild build,
public List<JiraCommit> getJiraCommits(AbstractBuild build,
BuildListener listener)
{
List<JiraCommit> result = new ArrayList<>();

try
{
_logger.log(Level.FINE, "iterateTicketsAndApply");
ChangeLogSet changeSets = build.getChangeSet();
listener.getLogger().println("ChangeLogSet class: " + changeSets.getClass());
List<Object> changeSetEntries = new LinkedList<>();

for (Object entry : changeSets)
getBuildChangeSetEntries(listener, build, changeSetEntries);

for (Object entry : changeSetEntries)
{
try
{
Expand Down Expand Up @@ -81,6 +87,14 @@ public final List<JiraCommit> getJiraCommits(AbstractBuild build,
return result;
}

private void getBuildChangeSetEntries(BuildListener listener, AbstractBuild build, List<Object> changeSetEntries) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have changeSetEntries be the return object here - there is no value in passing as mutable parameter. A return object is a self-documenting post condition.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again no style guide but try to be consistent w/ existing style -- curly brace on newline

ChangeLogSet changeSets = build.getChangeSet();
String projectName = build.getProject() == null ? "" : build.getProject().getName();
Integer buildNumber = build.getNumber();
listener.getLogger().println(String.format("ChangeLogSet from %s build %d, class: %s", projectName, buildNumber, changeSets.getClass()));
changeSetEntries.addAll(Lists.newArrayList(changeSets.iterator()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you adding all of the elements of an iterator to a list simply to use that list to only iterate over them? The only thing that has been added here is the additional logging describing build and class. Revert this new function and simply add your additional logging.

}

/**
* Parse a JIRA issue key, ie SSD-101, out of the given ChangeLogSet.Entry.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/***************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**************************************************************************/
package org.jenkinsci.plugins.jiraext.view;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import org.jenkinsci.plugins.jiraext.UpstreamBuildUtil;
import org.jenkinsci.plugins.jiraext.domain.JiraCommit;
import org.kohsuke.stapler.DataBoundConstructor;

import java.util.List;
import java.util.stream.Collectors;


/**
* Find JiraCommits by looking for word in the build's changelog. Issues must match in the pattern defined in
* global config. Also looks in Jira comments for all upstream commits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks in git commits, not jira comments

*
* @author milowg
*/
public class MentionedInCommitOrUpstreamCommitsStrategy
extends MentionedInCommitStrategy {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no style guide but try to be consistent w/ existing style -- curly brace on newline


@DataBoundConstructor
public MentionedInCommitOrUpstreamCommitsStrategy()
{
super();
}

@Override
public boolean equals(Object obj)
{
return (obj != null) && (obj instanceof MentionedInCommitOrUpstreamCommitsStrategy);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is outside the scope of just your change but when we implement equals(), hashcode() also must be implemented:

https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

}

@Override
public List<JiraCommit> getJiraCommits(AbstractBuild build, BuildListener buildListener) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no style guide but try to be consistent w/ existing style -- curly brace on newline

List<JiraCommit> jiraCommits = super.getJiraCommits(build, buildListener);

for (AbstractBuild upstreamBuild : UpstreamBuildUtil.getUpstreamBuilds(build)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no style guide but try to be consistent w/ existing style -- curly brace on newline

if (jiraCommits.addAll(
super.getJiraCommits(upstreamBuild, buildListener)
.stream()
.filter(jc -> !jiraCommits.contains(jc))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to remove duplicate entries? List is not the data structure you want -- you want to use Set during collection -- that should allow you to avoid looping and just stream the whole thing.

.collect(Collectors.toSet()))) ;
}
return jiraCommits;
}

@Extension
public static class DescriptorImpl
extends IssueStrategyExtensionDescriptor
{

@Override
public String getDisplayName()
{
return "Mentioned somewhere in commit or upstream build commit message";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/***************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**************************************************************************/
package org.jenkinsci.plugins.jiraext.view;

import hudson.model.FreeStyleProject;
import org.jenkinsci.plugins.jiraext.Config;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.Arrays;

/**
* @author dalvizu
*/
public class MentionedInCommitOrUpstreamCommitStrategyTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests on Discovery behavior are required for submission - see MentionedInCommitStrategyTest.java for inspiration

{
@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();

@Before
public void setUp()
{
Config.getGlobalConfig().setPattern("FOO-,BAR-");
}

@Test
public void testSaveConfig()
throws Exception
{
FreeStyleProject project = jenkinsRule.createFreeStyleProject();
JiraExtBuildStep builder = new JiraExtBuildStep(new MentionedInCommitOrUpstreamCommitsStrategy(),
Arrays.asList((JiraOperationExtension) new AddComment(true, "Hello World")));
project.getBuildersList().add(builder);

jenkinsRule.submit(jenkinsRule.createWebClient().getPage(project, "configure").getFormByName("config"));

JiraExtBuildStep after = project.getBuildersList().get(JiraExtBuildStep.class);
jenkinsRule.assertEqualBeans(builder, after, "issueStrategy");
}

}