Skip to content

Commit c49bd6c

Browse files
committed
Fix injectTest issue
1 parent 66256e6 commit c49bd6c

File tree

5 files changed

+48
-85
lines changed

5 files changed

+48
-85
lines changed

pom.xml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
<java.level.test>8</java.level.test>
1717

1818
<!-- Jenkins -->
19-
<jenkins.version>2.60.1</jenkins.version>
20-
<jenkins-test-harness.version>2.36</jenkins-test-harness.version>
19+
<jenkins.version>2.60.3</jenkins.version>
20+
<jenkins-test-harness.version>2.38</jenkins-test-harness.version>
2121

2222
<!-- Security -->
2323
<owasp.version>3.1.2</owasp.version>
@@ -120,6 +120,12 @@
120120
<version>6.3</version>
121121
<scope>test</scope>
122122
</dependency>
123+
<dependency>
124+
<groupId>org.jenkins-ci.plugins</groupId>
125+
<artifactId>display-url-api</artifactId>
126+
<version>2.2.0</version>
127+
<scope>test</scope>
128+
</dependency>
123129
</dependencies>
124130

125131
<build>
@@ -169,6 +175,25 @@
169175
<goals>
170176
<goal>check</goal>
171177
</goals>
178+
<configuration>
179+
<rules>
180+
<rule>
181+
<element>CLASS</element>
182+
<limits>
183+
<limit>
184+
<counter>LINE</counter>
185+
<value>COVEREDRATIO</value>
186+
<minimum>0.0</minimum>
187+
</limit>
188+
<limit>
189+
<counter>BRANCH</counter>
190+
<value>COVEREDRATIO</value>
191+
<minimum>0.0</minimum>
192+
</limit>
193+
</limits>
194+
</rule>
195+
</rules>
196+
</configuration>
172197
</execution>
173198
</executions>
174199
</plugin>

src/main/java/org/jenkinsci/plugins/gogs/GogsCause.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ associated documentation files (the "Software"), to deal in the Software without
2323

2424
package org.jenkinsci.plugins.gogs;
2525

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

34+
import static org.jenkinsci.plugins.gogs.GogsUtils.cast;
35+
3536
class GogsCause extends Cause {
3637
private String deliveryID;
3738
private final Map<String, String> envVars = new HashMap<>();
@@ -54,23 +55,20 @@ public void setDeliveryID(String deliveryID) {
5455
}
5556

5657
public void setGogsPayloadData(String json) {
57-
Map<String, Object> map = null;
58+
Map<String, Object> gogsPayloadData = null;
5859

5960
ObjectMapper objectMapper = new ObjectMapper();
6061
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
6162
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
6263

6364
try {
64-
GogsPayloadData gogsPayloadData = objectMapper.readValue(json, GogsPayloadData.class);
65-
map = objectMapper.convertValue(gogsPayloadData, new TypeReference<Map<String, Object>>() {
66-
});
65+
gogsPayloadData = cast(objectMapper.readValue(json, Map.class));
6766
} catch (Exception e) {
6867
LOGGER.severe(e.getMessage());
6968
}
70-
if (map != null) {
71-
iterate(map, null);
69+
if (gogsPayloadData != null) {
70+
iterate(gogsPayloadData, null);
7271
}
73-
7472
}
7573

7674
private void iterate(Map<String, Object> map, String prefix) {

src/main/java/org/jenkinsci/plugins/gogs/GogsPayloadData.java

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/main/java/org/jenkinsci/plugins/gogs/GogsProjectProperty.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ associated documentation files (the "Software"), to deal in the Software without
3131
import org.kohsuke.stapler.DataBoundConstructor;
3232
import org.kohsuke.stapler.StaplerRequest;
3333

34+
import javax.annotation.Nonnull;
3435
import java.util.logging.Logger;
3536

3637
@SuppressWarnings("ALL")
@@ -68,7 +69,7 @@ public boolean getGogsUsePayload() {
6869
return gogsUsePayload;
6970
}
7071

71-
public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) {
72+
public JobProperty<?> newInstance(@Nonnull StaplerRequest req, @Nonnull JSONObject formData) {
7273
GogsProjectProperty tpp = req.bindJSON(
7374
GogsProjectProperty.class,
7475
formData.getJSONObject(GOGS_PROJECT_BLOCK_NAME)

src/main/java/org/jenkinsci/plugins/gogs/GogsUtils.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import jenkins.model.Jenkins;
55
import org.apache.commons.codec.binary.Hex;
66

7-
import javax.annotation.Nonnull;
87
import javax.crypto.Mac;
98
import javax.crypto.spec.SecretKeySpec;
109
import java.nio.charset.Charset;
@@ -55,12 +54,23 @@ static Map<String, String> splitQuery(String qs) {
5554
* @return a String with the encoded sha256 hmac
5655
* @throws Exception Something went wrong getting the sha256 hmac
5756
*/
58-
static @Nonnull
59-
String encode(String data, String key) throws Exception {
57+
static String encode(String data, String key) throws Exception {
6058
final Charset asciiCs = Charset.forName("UTF-8");
6159
final Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
6260
final SecretKeySpec secret_key = new javax.crypto.spec.SecretKeySpec(asciiCs.encode(key).array(), "HmacSHA256");
6361
sha256_HMAC.init(secret_key);
6462
return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
6563
}
64+
65+
/**
66+
* Cast object
67+
*
68+
* @param obj object to cast
69+
* @param <T> cast to type
70+
* @return Casted object
71+
*/
72+
@SuppressWarnings("unchecked")
73+
static public <T> T cast(Object obj) {
74+
return (T) obj;
75+
}
6676
}

0 commit comments

Comments
 (0)