Skip to content

Commit

Permalink
Add API to retrive all topic related data from the change
Browse files Browse the repository at this point in the history
Also adds change status to the change based events.
  • Loading branch information
Jimilian committed Feb 8, 2018
1 parent c9d1b4e commit 42a23d4
Show file tree
Hide file tree
Showing 8 changed files with 396 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ public List<JSONObject> queryFiles(String queryString) throws
return queryJava(queryString, false, true, true, false);
}

//CS IGNORE RedundantThrows FOR NEXT 18 LINES. REASON: Informative.
//CS IGNORE JavadocMethod FOR NEXT 17 LINES. REASON: It is there.

/**
* Runs the query and returns the result as a list of Java JSONObjects.
* @param queryString the query.
* @return the query result as a List of JSONObjects.
* @throws GerritQueryException if Gerrit reports an error with the query.
* @throws SshException if there is an error in the SSH Connection.
* @throws IOException for some other IO problem.
*/
public List<JSONObject> queryCurrentPatchSets(String queryString) throws
SshException, IOException, GerritQueryException {
return queryJava(queryString, false, true, false, false);
}


//CS IGNORE RedundantThrows FOR NEXT 17 LINES. REASON: Informative.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.sonymobile.tools.gerrit.gerritevents.dto;

/**
Contains constants that represent the status of changes.
*/
public enum GerritChangeStatus {
/**
* Change is open.
*/
NEW("NEW"),
/**
* Change was merged.
*/
MERGED("MERGED"),
/**
* Change was abandoned.
*/
ABANDONED("ABANDONED"),
/**
* Catch-all type if Gerrit adds a new ChangeStatus we don't know about.
*/
UNKNOWN("UNKNOWN");

private final String status;

/**
* Internal constructor for GerritChangeStatus enum.
* @param status string value returned in JSON
*/
GerritChangeStatus(String status) {
this.status = status;
}

/**
* Look up the GerritChangeStatus from a string representation.
* @param status the GerritChangeStatus returned in JSON
* @return GerritChangeStatus enum value
*/
public static GerritChangeStatus fromString(String status) {
for (GerritChangeStatus s : GerritChangeStatus.values()) {
if (s.status.equals(status)) {
return s;
}
}
return UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
*/
package com.sonymobile.tools.gerrit.gerritevents.dto.attr;

import com.sonymobile.tools.gerrit.gerritevents.GerritQueryHandler;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritChangeStatus;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritJsonDTO;

import com.sonymobile.tools.gerrit.gerritevents.dto.rest.Topic;
import com.sonymobile.tools.gerrit.gerritevents.helpers.FileHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

Expand All @@ -47,6 +51,7 @@
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.URL;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.CREATED_ON;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.LAST_UPDATED;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.STATUS;


/**
Expand All @@ -64,10 +69,14 @@ public class Change implements GerritJsonDTO {
* Branch name within project.
*/
private String branch;
/**
* Topic.
*/
private Topic topicObject;
/**
* Topic name.
*/
private String topic;
private transient String topic;
/**
* Change identifier.
*/
Expand Down Expand Up @@ -104,6 +113,31 @@ public class Change implements GerritJsonDTO {
private Date lastUpdated;

private List<Comment> comments;

/**
* The changed files in this patchset.
*/
private List<String> files;

/**
* The change status.
*/
private GerritChangeStatus status;

/**
* Converts old serialized data to newer construct.
*
* @return itself
*/
@SuppressWarnings("unused")
private Object readResolve() {
if (topic != null) {
topicObject = new Topic(topic);
topic = null;
}
return this;
}

/**
* Default constructor.
*/
Expand All @@ -123,7 +157,6 @@ public Change(JSONObject json) {
public void fromJson(JSONObject json) {
project = getString(json, PROJECT);
branch = getString(json, BRANCH);
topic = getString(json, TOPIC);
id = getString(json, ID);
number = getString(json, NUMBER);
subject = getString(json, SUBJECT);
Expand All @@ -142,7 +175,13 @@ public void fromJson(JSONObject json) {
if (json.containsKey(COMMIT_MESSAGE)) {
commitMessage = getString(json, COMMIT_MESSAGE);
}
if (json.containsKey(TOPIC)) {
topicObject = new Topic(getString(json, TOPIC));
}

url = getString(json, URL);

status = GerritChangeStatus.fromString(getString(json, STATUS));
}

/**
Expand Down Expand Up @@ -170,19 +209,54 @@ public List<Comment> getComments() {
}

/**
* Topic name.
* Change status.
* @return the change status.
*/
public GerritChangeStatus getStatus() {
return status;
}

/**
* Sets the change status.
* @param status the status.
*/
public void setStatus(GerritChangeStatus status) {
this.status = status;
}

/**
* The topic info related to this change. Can be null if there is no topic.
* @return the topic.
*/
public Topic getTopicObject() {
return topicObject;
}

/**
* Shortcut for {@code getTopicObject() != null ? getTopicObject().getName() : null}.
* @return the topic name if exists. null otherwise.
*/
public String getTopic() {
return topic;
if (topicObject == null) {
return null;
}
return topicObject.getName();
}

/**
* Topic name.
* @param topic the topic.
* Sets the topic info related to this change.
* @param topicName the topic name.
*/
public void setTopic(String topicName) {
topicObject = new Topic(topicName);
}

/**
* Sets the topic info related to this change.
* @param topicObject the topic.
*/
public void setTopic(String topic) {
this.topic = topic;
public void setTopicObject(Topic topicObject) {
this.topicObject = topicObject;
}

/**
Expand Down Expand Up @@ -387,4 +461,18 @@ public String getChangeInfo(String preText) {
s.append("Link: " + getUrl() + "\n");
return s.toString();
}

/**
* Queries gerrit for the files included in this patch set.
*
* @param gerritQueryHandler the query handler, responsible for the queries to gerrit.
* @return a list of files that are part of this patch set.
*
*/
public List<String> getFiles(GerritQueryHandler gerritQueryHandler) {
if (files == null) {
files = FileHelper.getFilesByChange(gerritQueryHandler, id);
}
return files;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@
*/
package com.sonymobile.tools.gerrit.gerritevents.dto.events;

import com.sonymobile.tools.gerrit.gerritevents.GerritQueryException;
import com.sonymobile.tools.gerrit.gerritevents.GerritQueryHandler;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.PatchSet;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.CHANGE;
Expand All @@ -45,8 +39,6 @@
* @author Tomas Westling &lt;tomas.westling@sonymobile.com&gt;
*/
public abstract class ChangeBasedEvent extends GerritTriggeredEvent {

private static final Logger logger = LoggerFactory.getLogger(ChangeBasedEvent.class);
/**
* The Gerrit change the event is related to.
*/
Expand Down Expand Up @@ -75,49 +67,21 @@ public void setChange(Change change) {
this.change = change;
}

/**
* The changed files in this patchset.
*/
private List<String> files;



/**
* Queries gerrit for the files included in this patch set.
*
* @param gerritQueryHandler the query handler, responsible for the queries to gerrit.
* @return a list of files that are part of this patch set.
*
* @deprecated use {@link Change#getFiles(GerritQueryHandler)} instead.
*/
@Deprecated
public List<String> getFiles(GerritQueryHandler gerritQueryHandler) {
if (files == null) {
files = new LinkedList<String>();
try {
List<JSONObject> jsonList = gerritQueryHandler.queryFiles("change:" + getChange().getId());
for (JSONObject json : jsonList) {
if (json.has("type") && "stats".equalsIgnoreCase(json.getString("type"))) {
continue;
}
if (json.has("currentPatchSet")) {
JSONObject currentPatchSet = json.getJSONObject("currentPatchSet");
if (currentPatchSet.has("files")) {
JSONArray changedFiles = currentPatchSet.optJSONArray("files");
for (int i = 0; i < changedFiles.size(); i++) {
JSONObject file = changedFiles.getJSONObject(i);
files.add(file.getString("file"));
}
}
}
}
} catch (IOException e) {
logger.error("IOException occured. ", e);
} catch (GerritQueryException e) {
logger.error("Bad query. ", e);
}
}
return files;
return change.getFiles(gerritQueryHandler);
}


/**
* The patchSet.
*
Expand Down
Loading

0 comments on commit 42a23d4

Please sign in to comment.