Skip to content

Commit

Permalink
Fix injectTest issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderv32 committed May 13, 2018
1 parent 66256e6 commit 3610b30
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 85 deletions.
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<java.level.test>8</java.level.test>

<!-- Jenkins -->
<jenkins.version>2.60.1</jenkins.version>
<jenkins-test-harness.version>2.36</jenkins-test-harness.version>
<jenkins.version>2.60.3</jenkins.version>
<jenkins-test-harness.version>2.38</jenkins-test-harness.version>

<!-- Security -->
<owasp.version>3.1.2</owasp.version>
Expand Down Expand Up @@ -120,6 +120,12 @@
<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>

<build>
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/jenkinsci/plugins/gogs/GogsCause.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ associated documentation files (the "Software"), to deal in the Software without

package org.jenkinsci.plugins.gogs;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import hudson.model.Cause;
Expand All @@ -32,6 +31,8 @@ associated documentation files (the "Software"), to deal in the Software without
import java.util.Map;
import java.util.logging.Logger;

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

class GogsCause extends Cause {
private String deliveryID;
private final Map<String, String> envVars = new HashMap<>();
Expand All @@ -54,23 +55,20 @@ public void setDeliveryID(String deliveryID) {
}

public void setGogsPayloadData(String json) {
Map<String, Object> map = null;
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 gogsPayloadData = objectMapper.readValue(json, GogsPayloadData.class);
map = objectMapper.convertValue(gogsPayloadData, new TypeReference<Map<String, Object>>() {
});
gogsPayloadData = cast(objectMapper.readValue(json, Map.class));
} catch (Exception e) {
LOGGER.severe(e.getMessage());
}
if (map != null) {
iterate(map, null);
if (gogsPayloadData != null) {
iterate(gogsPayloadData, null);
}

}

private void iterate(Map<String, Object> map, String prefix) {
Expand Down
71 changes: 0 additions & 71 deletions src/main/java/org/jenkinsci/plugins/gogs/GogsPayloadData.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ associated documentation files (the "Software"), to deal in the Software without
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

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

@SuppressWarnings("ALL")
Expand Down Expand Up @@ -68,7 +69,7 @@ public boolean getGogsUsePayload() {
return gogsUsePayload;
}

public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) {
public JobProperty<?> newInstance(@Nonnull StaplerRequest req, @Nonnull JSONObject formData) {
GogsProjectProperty tpp = req.bindJSON(
GogsProjectProperty.class,
formData.getJSONObject(GOGS_PROJECT_BLOCK_NAME)
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/jenkinsci/plugins/gogs/GogsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import jenkins.model.Jenkins;
import org.apache.commons.codec.binary.Hex;

import javax.annotation.Nonnull;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -55,12 +54,23 @@ static Map<String, String> splitQuery(String qs) {
* @return a String with the encoded sha256 hmac
* @throws Exception Something went wrong getting the sha256 hmac
*/
static @Nonnull
String encode(String data, String key) throws Exception {
static String encode(String data, String key) throws Exception {
final Charset asciiCs = Charset.forName("UTF-8");
final Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
final SecretKeySpec secret_key = new javax.crypto.spec.SecretKeySpec(asciiCs.encode(key).array(), "HmacSHA256");
sha256_HMAC.init(secret_key);
return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
}

/**
* Cast object
*
* @param obj object to cast
* @param <T> cast to type
* @return Casted object
*/
@SuppressWarnings("unchecked")
static public <T> T cast(Object obj) {
return (T) obj;
}
}

0 comments on commit 3610b30

Please sign in to comment.