Skip to content

Commit

Permalink
Merge pull request #87 from jenkinsci/data_structures
Browse files Browse the repository at this point in the history
Add some data structures for future use
  • Loading branch information
olivierdagenais authored Jul 19, 2016
2 parents 444526f + c36df85 commit 85e3995
Show file tree
Hide file tree
Showing 12 changed files with 883 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/main/java/hudson/plugins/tfs/model/GitCodePushedEventArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package hudson.plugins.tfs.model;

import net.sf.ezmorph.MorpherRegistry;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.PropertyNameProcessor;
import net.sf.json.util.JSONUtils;
import net.sf.json.util.JavaIdentifierTransformer;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GitCodePushedEventArgs {
public URI collectionUri;
public URI repoUri;
public String projectId;
public String repoId;
public String commit;
public String pushedBy;
public List<WorkItem> workItems;

public static GitCodePushedEventArgs fromJsonString(final String jsonString) {
final JSONObject jsonObject = JSONObject.fromObject(jsonString);
final GitCodePushedEventArgs result;

final JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setRootClass(GitCodePushedEventArgs.class);
final HashMap<String, Class> classMap = new HashMap<String, Class>();
// TODO: the classMap is used without context, every property named here will try to use the type
classMap.put("workItems", WorkItem.class);
classMap.put("html", Link.class);
jsonConfig.setClassMap(classMap);
jsonConfig.setJavaIdentifierTransformer(new JavaIdentifierTransformer() {
@Override
public String transformToJavaIdentifier(final String str) {
return str.replace(".", "_");
}
});

final MorpherRegistry registry = JSONUtils.getMorpherRegistry();

// TODO: I don't like messing with singletons; is there a way to do this with JsonConfig?
registry.registerMorpher(URIMorpher.INSTANCE);
try {
result = (GitCodePushedEventArgs) JSONObject.toBean(jsonObject, jsonConfig);
}
finally {
registry.deregisterMorpher(URIMorpher.INSTANCE);
}

return result;
}
}
29 changes: 29 additions & 0 deletions src/main/java/hudson/plugins/tfs/model/GitStatusContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package hudson.plugins.tfs.model;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class GitStatusContext {
public String name;
public String genre;

public GitStatusContext() {
}

public GitStatusContext(final String name, final String genre) {
this.name = name;
this.genre = genre;
}

public static GitStatusContext fromJsonString(final String jsonString) {
final JSONObject jsonObject = JSONObject.fromObject(jsonString);
final GitStatusContext result;

final JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setRootClass(GitStatusContext.class);

result = (GitStatusContext) JSONObject.toBean(jsonObject, jsonConfig);

return result;
}
}
21 changes: 21 additions & 0 deletions src/main/java/hudson/plugins/tfs/model/GitStatusState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hudson.plugins.tfs.model;

public enum GitStatusState {

NotSet(0),
Pending(1),
Succeeded(2),
Failed(3),
Error(4),
;

private final int value;

GitStatusState(final int value) {
this.value = value;
}

public int getValue() {
return value;
}
}
12 changes: 12 additions & 0 deletions src/main/java/hudson/plugins/tfs/model/Link.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hudson.plugins.tfs.model;

import java.net.URI;

public class Link {
public URI href;

@Override
public String toString() {
return href.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package hudson.plugins.tfs.model;

import net.sf.ezmorph.MorpherRegistry;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;
import net.sf.json.util.JavaIdentifierTransformer;

import java.util.HashMap;

public class PullRequestMergeCommitCreatedEventArgs extends GitCodePushedEventArgs {
public int pullRequestId;
public int iterationId;

public static PullRequestMergeCommitCreatedEventArgs fromJsonString(final String jsonString) {
final JSONObject jsonObject = JSONObject.fromObject(jsonString);
final PullRequestMergeCommitCreatedEventArgs result;

final JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setRootClass(PullRequestMergeCommitCreatedEventArgs.class);
final HashMap<String, Class> classMap = new HashMap<String, Class>();
// TODO: the classMap is used without context, every property named here will try to use the type
classMap.put("workItems", WorkItem.class);
classMap.put("html", Link.class);
jsonConfig.setClassMap(classMap);
jsonConfig.setJavaIdentifierTransformer(new JavaIdentifierTransformer() {
@Override
public String transformToJavaIdentifier(final String str) {
return str.replace(".", "_");
}
});

final MorpherRegistry registry = JSONUtils.getMorpherRegistry();

// TODO: I don't like messing with singletons; is there a way to do this with JsonConfig?
registry.registerMorpher(URIMorpher.INSTANCE);
try {
result = (PullRequestMergeCommitCreatedEventArgs) JSONObject.toBean(jsonObject, jsonConfig);
}
finally {
registry.deregisterMorpher(URIMorpher.INSTANCE);
}

return result;
}

}
50 changes: 50 additions & 0 deletions src/main/java/hudson/plugins/tfs/model/URIMorpher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package hudson.plugins.tfs.model;

import net.sf.ezmorph.MorphException;
import net.sf.ezmorph.ObjectMorpher;

import java.net.URI;
import java.net.URISyntaxException;

public class URIMorpher implements ObjectMorpher {

public static final URIMorpher INSTANCE = new URIMorpher();

private URIMorpher() {

}

@Override
public Object morph(final Object value) {
if (value == null) {
return null;
}

if (URI.class.isAssignableFrom(value.getClass())) {
return value;
}

if (!supports(value.getClass())) {
throw new MorphException(value.getClass() + " is not supported");
}

final String s = value.toString();
try {
final URI result = new URI(s);
return result;
}
catch (final URISyntaxException ignored) {
throw new MorphException("'" + s + "' is not a valid URI");
}
}

@Override
public Class morphsTo() {
return URI.class;
}

@Override
public boolean supports(final Class clazz) {
return String.class.isAssignableFrom(clazz);
}
}
17 changes: 17 additions & 0 deletions src/main/java/hudson/plugins/tfs/model/VstsGitStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hudson.plugins.tfs.model;

import net.sf.json.JSONObject;

public class VstsGitStatus {

public GitStatusState state;
public String description;
public String targetUrl;
public GitStatusContext context;

public String toJson() {
final JSONObject jsonObject = JSONObject.fromObject(this);
final String result = jsonObject.toString();
return result;
}
}
12 changes: 12 additions & 0 deletions src/main/java/hudson/plugins/tfs/model/WorkItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hudson.plugins.tfs.model;

import java.net.URI;
import java.util.Map;

public class WorkItem {
public long id;
public int rev;
public Map<String, Object> fields;
public Map<String, Link> _links;
public URI url;
}
Loading

0 comments on commit 85e3995

Please sign in to comment.