forked from synopsys-arc-oss/ownership-plugin
-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] [JENKINS-46790] Expose a RecipientProvider for ownership #76
Open
v1v
wants to merge
2
commits into
jenkinsci:master
Choose a base branch
from
v1v:JENKINS-46790
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,22 +5,22 @@ | |
<artifactId>plugin</artifactId> | ||
<version>3.5</version> | ||
</parent> | ||
|
||
<groupId>com.synopsys.jenkinsci</groupId> | ||
<artifactId>ownership</artifactId> | ||
<version>0.12.2-SNAPSHOT</version> | ||
<name>Job and Node ownership plugin</name> | ||
<packaging>hpi</packaging> | ||
<description>Provides explicit ownership of jobs and nodes</description> | ||
<url>https://wiki.jenkins.io/display/JENKINS/Ownership+Plugin</url> | ||
|
||
<licenses> | ||
<license> | ||
<name>MIT License</name> | ||
<url>http://www.opensource.org/licenses/mit-license.php</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
</licenses> | ||
|
||
<properties> | ||
<jenkins.version>1.651.3</jenkins.version> | ||
|
@@ -34,7 +34,7 @@ | |
<developer> | ||
<id>oleg_nenashev</id> | ||
<name>Oleg Nenashev</name> | ||
<email>[email protected]</email> | ||
<email>[email protected]</email> | ||
<roles> | ||
<role>maintainer</role> | ||
</roles> | ||
|
@@ -47,7 +47,7 @@ | |
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url> | ||
<tag>HEAD</tag> | ||
</scm> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
|
@@ -58,7 +58,7 @@ | |
<dependency> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>token-macro</artifactId> | ||
<version>1.6</version> | ||
<version>1.12.1</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
|
@@ -91,6 +91,12 @@ | |
<version>0.4</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>email-ext</artifactId> | ||
<version>2.47</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<!--Plugins decoupled from the core. Would be great to make these deps optional.--> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
|
@@ -117,7 +123,11 @@ | |
<artifactId>workflow-cps</artifactId> | ||
<version>${workflow.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.jenkins-ci.plugins</groupId> | ||
<artifactId>structs</artifactId> | ||
<version>1.6</version> | ||
</dependency> | ||
<!-- Test framework --> | ||
<dependency> | ||
<groupId>org.jenkins-ci.plugins.workflow</groupId> | ||
|
@@ -145,8 +155,8 @@ | |
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<repositories> | ||
|
||
<repositories> | ||
<repository> | ||
<id>repo.jenkins-ci.org</id> | ||
<url>https://repo.jenkins-ci.org/public/</url> | ||
|
113 changes: 113 additions & 0 deletions
113
src/main/java/org/jenkinsci/plugins/ownership/recipients/OwnershipRecipientProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2018 Victor Martinez | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.jenkinsci.plugins.ownership.recipients; | ||
|
||
import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerJobProperty; | ||
import hudson.EnvVars; | ||
import hudson.Extension; | ||
import hudson.model.Cause; | ||
import hudson.model.Job; | ||
import hudson.model.Run; | ||
import hudson.plugins.emailext.EmailRecipientUtils; | ||
import hudson.plugins.emailext.ExtendedEmailPublisherContext; | ||
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor; | ||
import hudson.plugins.emailext.plugins.RecipientProvider; | ||
import hudson.plugins.emailext.plugins.RecipientProviderDescriptor; | ||
import hudson.plugins.emailext.plugins.recipients.RecipientProviderUtilities; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.Symbol; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
import javax.mail.MessagingException; | ||
import javax.mail.internet.InternetAddress; | ||
import java.io.PrintStream; | ||
import java.util.Set; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* | ||
* @author Victor Martinez | ||
*/ | ||
@Extension(optional = true) | ||
public class OwnershipRecipientProvider extends RecipientProvider { | ||
|
||
@DataBoundConstructor | ||
public OwnershipRecipientProvider() { | ||
|
||
} | ||
|
||
@Override | ||
public void addRecipients(final ExtendedEmailPublisherContext context, EnvVars env, Set<InternetAddress> to, Set<InternetAddress> cc, Set<InternetAddress> bcc) { | ||
final class Debug implements RecipientProviderUtilities.IDebug { | ||
private final ExtendedEmailPublisherDescriptor descriptor | ||
= Jenkins.getActiveInstance().getDescriptorByType(ExtendedEmailPublisherDescriptor.class); | ||
|
||
private final PrintStream logger = context.getListener().getLogger(); | ||
|
||
public void send(final String format, final Object... args) { | ||
descriptor.debug(logger, format, args); | ||
} | ||
} | ||
final Debug debug = new Debug(); | ||
// looking for Upstream build. | ||
Run<?, ?> cur = context.getRun(); | ||
Cause.UpstreamCause upc = cur.getCause(Cause.UpstreamCause.class); | ||
Job<?, ?> p = null; | ||
while (upc != null) { | ||
// UpstreamCause.getUpStreamProject() returns the full name, so use getItemByFullName | ||
p = (Job<?, ?>) Jenkins.getActiveInstance().getItemByFullName(upc.getUpstreamProject()); | ||
if (p == null) { | ||
context.getListener().getLogger().print("There is a break in the project linkage, could not retrieve upstream project information"); | ||
break; | ||
} | ||
} | ||
try { | ||
addJobOwner(p, to, cc, bcc, env, context, debug); | ||
} catch (MessagingException e) { | ||
Logger.getLogger(OwnershipRecipientProvider.class.getName()).log(Level.SEVERE, null, e); | ||
} | ||
} | ||
|
||
private static void addJobOwner(Job<?,?> job, Set<InternetAddress> to, | ||
Set<InternetAddress> cc, Set<InternetAddress> bcc, EnvVars env, final ExtendedEmailPublisherContext context, RecipientProviderUtilities.IDebug debug) throws MessagingException { | ||
final String owner = job.getProperty(JobOwnerJobProperty.class).getOwnership().getPrimaryOwnerEmail(); | ||
if (owner != null) { | ||
EmailRecipientUtils.addAddressesFromRecipientList(to, cc, bcc, EmailRecipientUtils.getRecipientList(context, owner), env, context.getListener()); | ||
} | ||
} | ||
|
||
//return User.get(id, false, (Map)null); | ||
|
||
@Extension | ||
@Symbol("owners") | ||
public static final class DescriptorImpl extends RecipientProviderDescriptor { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Owners"; | ||
} | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you put it on both class and descriptor?