From 30b632c14fa3c4ba7fe3a30d969bbf6322d5077e Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Mon, 29 Oct 2018 22:03:51 +0000 Subject: [PATCH 1/2] [JENKINS-46790] First approach to expose the owner --- pom.xml | 22 ++-- .../OwnershipRecipientProvider.java | 113 ++++++++++++++++++ 2 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugins/ownership/recipients/OwnershipRecipientProvider.java diff --git a/pom.xml b/pom.xml index 525b4d4..e62f9e3 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ plugin 3.5 - + com.synopsys.jenkinsci ownership 0.12.2-SNAPSHOT @@ -13,14 +13,14 @@ hpi Provides explicit ownership of jobs and nodes https://wiki.jenkins.io/display/JENKINS/Ownership+Plugin - + MIT License http://www.opensource.org/licenses/mit-license.php repo - + 1.651.3 @@ -34,7 +34,7 @@ oleg_nenashev Oleg Nenashev - o.v.nenashev@gmail.com + o.v.nenashev@gmail.com maintainer @@ -47,7 +47,7 @@ https://github.com/jenkinsci/${project.artifactId}-plugin HEAD - + org.jenkins-ci.plugins @@ -91,6 +91,12 @@ 0.4 true + + org.jenkins-ci.plugins + email-ext + 2.62 + true + org.jenkins-ci.plugins @@ -117,7 +123,7 @@ workflow-cps ${workflow.version} - + org.jenkins-ci.plugins.workflow @@ -145,8 +151,8 @@ test - - + + repo.jenkins-ci.org https://repo.jenkins-ci.org/public/ diff --git a/src/main/java/org/jenkinsci/plugins/ownership/recipients/OwnershipRecipientProvider.java b/src/main/java/org/jenkinsci/plugins/ownership/recipients/OwnershipRecipientProvider.java new file mode 100644 index 0000000..99ea757 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/ownership/recipients/OwnershipRecipientProvider.java @@ -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 to, Set cc, Set 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 to, + Set cc, Set 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"; + } + + } +} From 4f04656399c3449b4b060fa94925217f23a96b2e Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Mon, 29 Oct 2018 22:20:47 +0000 Subject: [PATCH 2/2] Bump token-macro version, downgrade email-ext version to use a 1.X token macro version and use structs --- pom.xml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index e62f9e3..6e782a0 100644 --- a/pom.xml +++ b/pom.xml @@ -58,7 +58,7 @@ org.jenkins-ci.plugins token-macro - 1.6 + 1.12.1 true @@ -94,7 +94,7 @@ org.jenkins-ci.plugins email-ext - 2.62 + 2.47 true @@ -123,7 +123,11 @@ workflow-cps ${workflow.version} - + + org.jenkins-ci.plugins + structs + 1.6 + org.jenkins-ci.plugins.workflow