Skip to content
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] Refactoring #42

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
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
147 changes: 121 additions & 26 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
<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">
<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>
<version>2.30</version>
</parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>gogs-webhook</artifactId>
<version>1.0.16-SNAPSHOT</version>
<packaging>hpi</packaging>

<properties>
<jenkins.version>1.625.3</jenkins.version>
<java.level>8</java.level>
<jenkins-test-harness.version>2.13</jenkins-test-harness.version>
<java.level.test>8</java.level.test>

<!-- Jenkins -->
<jenkins.version>2.138.3</jenkins.version>
<jenkins-test-harness.version>2.38</jenkins-test-harness.version>

<!-- Security -->
<owasp.version>3.1.2</owasp.version>
<findbugs.failOnError>false</findbugs.failOnError>

<!-- Jackson -->
<jackson.version>2.9.9</jackson.version>
</properties>

<name>Jenkins Gogs plugin</name>
Expand Down Expand Up @@ -49,18 +57,28 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.2.5</version>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -77,14 +95,7 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.0-jre</version>
<!--<scope>test</scope>-->
</dependency>
<!--<dependency>-->
<!--<groupId>org.apache.httpcomponents</groupId>-->
<!--<artifactId>httpclient</artifactId>-->
<!--<version>4.5.3</version>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
Expand Down Expand Up @@ -116,9 +127,105 @@
<version>6.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>display-url-api</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<argLine>${jaCoCoArgLine}</argLine>
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
<systemPropertyVariables>
<java.awt.headless>true</java.awt.headless>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>jaCoCoArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>CLASS</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.0</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.0</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>withIntegrationTest</id>
<!--These tests can come in the way of somebody that doesn't have a docker ready environment. So it is better
Expand Down Expand Up @@ -240,18 +347,6 @@
</profile>
</profiles>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.5</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<!-- get every artifact through repo.jenkins-ci.org, which proxies all the artifacts that we need -->
<repositories>
<repository>
Expand Down
57 changes: 56 additions & 1 deletion src/main/java/org/jenkinsci/plugins/gogs/GogsCause.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,70 @@ associated documentation files (the "Software"), to deal in the Software without

package org.jenkinsci.plugins.gogs;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import hudson.model.Cause;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import static org.jenkinsci.plugins.gogs.GogsUtils.cast;

class GogsCause extends Cause {
private final String deliveryID;
private String deliveryID;
private final Map<String, String> envVars = new HashMap<>();
private final static Logger LOGGER = Logger.getLogger(GogsCause.class.getName());


public GogsCause() {
}

public GogsCause(String deliveryID) {
this.deliveryID = deliveryID;
}

public Map<String, String> getEnvVars() {
return envVars;
}

public void setDeliveryID(String deliveryID) {
this.deliveryID = deliveryID;
}

public void setGogsPayloadData(String json) {
Map<String, Object> gogsPayloadData = null;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

try {
gogsPayloadData = cast(objectMapper.readValue(json, Map.class));
} catch (Exception e) {
LOGGER.severe(e.getMessage());
}
if (gogsPayloadData != null) {
iterate(gogsPayloadData, null);
}
}

private void iterate(Map<String, Object> map, String prefix) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
StringBuilder env_name = new StringBuilder();
if (prefix != null)
env_name.append(prefix.toUpperCase()).append("_");

if (entry.getValue() instanceof Map) {
//noinspection unchecked
iterate((Map<String, Object>) entry.getValue(), env_name + entry.getKey().toUpperCase());
} else if (entry.getValue() instanceof String || entry.getValue() instanceof Long || entry.getValue() instanceof Boolean) {
env_name.append(entry.getKey().toUpperCase());
envVars.put("GOGS_" + env_name.toString(), entry.getValue().toString());
}
}
}

@Override
public String getShortDescription() {
return this.deliveryID;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.jenkinsci.plugins.gogs;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Run;
import hudson.model.TaskListener;
import jenkins.model.CoreEnvironmentContributor;

import javax.annotation.Nonnull;

@SuppressWarnings("unused")
@Extension
public class GogsEnvironmentContributor extends CoreEnvironmentContributor {
@Override
public void buildEnvironmentFor(@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull TaskListener listener) {
GogsCause gogsCause;

gogsCause = (GogsCause) r.getCause(GogsCause.class);
if (gogsCause != null) {
envs.putAll(gogsCause.getEnvVars());
}
}
}
20 changes: 5 additions & 15 deletions src/main/java/org/jenkinsci/plugins/gogs/GogsPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,20 @@
import hudson.model.EnvironmentContributingAction;
import hudson.model.InvisibleAction;

import javax.annotation.Nonnull;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

class GogsPayload extends InvisibleAction implements EnvironmentContributingAction {
private final Map<String, String> payload;
private final GogsCause gogsCause;

public GogsPayload(Map<String, String> payload) {
this.payload = payload;
}

@Nonnull
private Map<String, String> getPayload() {
return payload;
public GogsPayload(GogsCause gogsCause) {
this.gogsCause = gogsCause;
}

@Override
public void buildEnvVars(AbstractBuild<?, ?> abstractBuild, EnvVars envVars) {
LOGGER.log(Level.FINEST, "Injecting GOGS_PAYLOAD: {0}", getPayload());
for (Map.Entry<String, String> entry : payload.entrySet()) {
envVars.put("GOGS_" + entry.getKey().toUpperCase(), entry.getValue());
}

LOGGER.log(Level.FINEST, "Injecting GOGS_PAYLOAD: {0}", gogsCause.getEnvVars());
envVars.putAll(gogsCause.getEnvVars());
}

private static final Logger LOGGER = Logger.getLogger(GogsPayload.class.getName());
Expand Down
Loading