Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ysykzheng committed Aug 7, 2019
0 parents commit 519e6ff
Show file tree
Hide file tree
Showing 27 changed files with 1,648 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
target
.classpath
.idea
*.iml
.project
.settings
bin
work
*.iml
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# DaDaPush plugin for Jenkins

Based on Mattermost plugin:

https://github.com/jenkinsci/mattermost-plugin/


Includes [Jenkins Pipeline](https://github.com/jenkinsci/workflow-plugin) support as of version 2.0:

```
dadapushSend title:"test title", content:"test content"
dadapushSend title:"test title", content:"test content", failOnError:true, channelToken:"ctXXXXXX", basePath:"https://www.dadapush.com"
```

# Jenkins Instructions

1. Set up a DaDaPush server
2. Configure an incoming webhook
3. Install this plugin on your Jenkins server
4. **Add it as a Post-build action** in your Jenkins job.

# Developer instructions

Install Maven and JDK.

Run unit tests

mvn test

Run findbugs:

mvn findbugs:check

Create an HPI file to install in Jenkins (HPI file will be in `target/dadapush.hpi`).

mvn package
210 changes: 210 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.11</version>
</parent>

<artifactId>dadapush</artifactId>
<packaging>hpi</packaging>
<version>1.0.0</version>
<name>DaDaPush Notification Plugin</name>
<description>A Build status publisher that notifies channels on DaDaPush</description>
<url>https://github.com/dadapush/jenkins-dadapush-notification-plugin</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<workflow.version>1.11</workflow.version>
<hamcrest.version>1.3</hamcrest.version>
<powermock.version>1.6.2</powermock.version>
<jenkins.version>2.0</jenkins.version>
<java.level>8</java.level>
<jenkins-test-harness.version>2.13</jenkins-test-harness.version>
</properties>

<licenses>
<license>
<name>Apache 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<scm>
<connection>scm:git:git@github.com:dadapush/jenkins-dadapush-notification-plugin.git
</connection>
<developerConnection>scm:git:git@github.com:dadapush/jenkins-dadapush-notification-plugin.git
</developerConnection>
<url>https://github.com/dadapush/jenkins-dadapush-notification-plugin</url>
</scm>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
<scope>test</scope>
</dependency>
<!-- only here to prevent from being included inside hpi for hudson parent, not needed by project at all -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
<scope>provided</scope>
</dependency>
<!-- only here to prevent from being included inside hpi for hudson parent, not needed by project at all -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.4</version>
<scope>provided</scope>
</dependency>
<!--
Adding JNA to fix failing unit tests on Ubuntu 14.04
https://bugs.launchpad.net/ubuntu/+source/libjna-java/+bug/1065253
-->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>3.2.2</version>
</dependency>
<!-- for workflow support -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${workflow.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency> <!-- StepConfigTester -->
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<classifier>tests</classifier>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-reflect</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.dadapush.client</groupId>
<artifactId>dadapush-client</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>display-url-api</artifactId>
<version>0.4</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<compatibleSinceVersion>2.4.0</compatibleSinceVersion>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

<developers>
<developer>
<name>DaDaPush</name>
<email>contacts@dadapush.com</email>
<organization>www.dadapush.com</organization>
<organizationUrl>https://www.dadapush.com</organizationUrl>
</developer>
</developers>
</project>
77 changes: 77 additions & 0 deletions src/main/java/com/dadapush/integration/jenkins/ActiveNotifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.dadapush.integration.jenkins;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import java.util.logging.Logger;


@SuppressWarnings("rawtypes")
public class ActiveNotifier implements FineGrainedNotifier {

private static final Logger logger = Logger.getLogger(ActiveNotifier.class.getName());

private DaDaPushNotifier notifier;
private BuildListener listener;

public ActiveNotifier(DaDaPushNotifier notifier, BuildListener listener) {
super();
this.notifier = notifier;
this.listener = listener;
}

private DaDaPushService getDaDaPushService(AbstractBuild r) {
return notifier.newDaDaPushService(r, listener);
}

public void deleted(AbstractBuild r) {
}

public void started(AbstractBuild build) {
notifyStart(build);
}

private void notifyStart(AbstractBuild build) {
MessageBuilder messageBuilder = new MessageBuilder(notifier, build, false);
String title = messageBuilder.getTitle();
String content = messageBuilder.getContent();
getDaDaPushService(build).publish(title, content);
}

public void finalized(AbstractBuild r) {
}

public void completed(AbstractBuild r) {
AbstractProject<?, ?> project = r.getProject();
Result result = r.getResult();
AbstractBuild<?, ?> previousBuild = project.getLastBuild();
if (previousBuild != null) {
do {
previousBuild = previousBuild.getPreviousCompletedBuild();
} while (previousBuild != null && previousBuild.getResult() == Result.ABORTED);
}
Result previousResult = (previousBuild != null) ? previousBuild.getResult() : Result.SUCCESS;
if ((result == Result.ABORTED && notifier.getNotifyAborted())
|| (result == Result.FAILURE //notify only on single failed build
&& previousResult != Result.FAILURE
&& notifier.getNotifyFailure())
|| (result == Result.FAILURE //notify only on repeated failures
&& previousResult == Result.FAILURE
&& notifier.getNotifyRepeatedFailure())
|| (result == Result.NOT_BUILT && notifier.getNotifyNotBuilt())
|| (result == Result.SUCCESS
&& (previousResult == Result.FAILURE || previousResult == Result.UNSTABLE)
&& notifier.getNotifyBackToNormal())
|| (result == Result.SUCCESS && notifier.getNotifySuccess())
|| (result == Result.UNSTABLE && notifier.getNotifyUnstable())) {

MessageBuilder messageBuilder = new MessageBuilder(notifier, r, false);
String title = messageBuilder.getTitle();
String content = messageBuilder.getContent();
getDaDaPushService(r).publish(title, content);
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.dadapush.integration.jenkins;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import hudson.tasks.Publisher;
import java.util.Map;
import java.util.logging.Logger;

@Extension
@SuppressWarnings("rawtypes")
public class DaDaPushListener extends RunListener<AbstractBuild> {

private static final Logger logger = Logger.getLogger(DaDaPushListener.class.getName());

public DaDaPushListener() {
super(AbstractBuild.class);
}

@Override
public void onCompleted(AbstractBuild r, TaskListener listener) {
getNotifier(r.getProject(), listener).completed(r);
super.onCompleted(r, listener);
}

@Override
public void onStarted(AbstractBuild r, TaskListener listener) {
// getNotifier(r.getProject()).started(r);
// super.onStarted(r, listener);
}

@Override
public void onDeleted(AbstractBuild r) {
// getNotifier(r.getProject()).deleted(r);
// super.onDeleted(r);
}

@Override
public void onFinalized(AbstractBuild r) {
// getNotifier(r.getProject()).finalized(r);
// super.onFinalized(r);
}

@SuppressWarnings("unchecked")
FineGrainedNotifier getNotifier(AbstractProject project, TaskListener listener) {
Map<Descriptor<Publisher>, Publisher> map = project.getPublishersList().toMap();
for (Publisher publisher : map.values()) {
if (publisher instanceof DaDaPushNotifier) {
return new ActiveNotifier((DaDaPushNotifier) publisher, (BuildListener)listener);
}
}
return new DisabledNotifier();
}

}
Loading

0 comments on commit 519e6ff

Please sign in to comment.