Skip to content
Merged
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
103 changes: 103 additions & 0 deletions src/main/java/org/gitlab/api/GitlabAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3912,4 +3912,107 @@ public GitlabRunner getRunnerDetail(int id) throws IOException {
String tailUrl = String.format("%s/%d", GitlabRunner.URL, id);
return retrieve().to(tailUrl, GitlabRunner.class);
}

/**
* Get events for a project.
*
* @param action If not null, include only events of a particular action type
* @param targetType If not null, include only events of a particular target type
* @param before If not null, include only events created before a particular date.
* @param after If not null, include only events created before a
* particular date.
* @param sort If null, uses the server's default, which is "desc"
*/
public List<GitlabEvent> getEvents(GitlabProject project,
GitlabEvent.ActionType action,
GitlabEvent.TargetType targetType,
GitlabDate before,
GitlabDate after,
SortOrder sortOrder)
throws IOException {
return getEvents(project, action, targetType, before,
after, sortOrder, new Pagination());
}

/**
* Get events for a project.
*
* @param action If not null, include only events of a particular action type
* @param targetType If not null, include only events of a particular target type
* @param before If not null, include only events created before a particular date.
* @param after If not null, include only events created before a
* particular date.
* @param sort If null, uses the server's default, which is "desc"
*/
public List<GitlabEvent> getEvents(GitlabProject project,
GitlabEvent.ActionType action,
GitlabEvent.TargetType targetType,
GitlabDate before,
GitlabDate after,
SortOrder sortOrder,
Pagination pagination)
throws IOException {
return getProjectEvents(project.getId(), action, targetType, before,
after, sortOrder, pagination);
}

/**
* Get events for a project.
*
* @param action If not null, include only events of a particular action type
* @param targetType If not null, include only events of a particular target type
* @param before If not null, include only events created before a particular date.
* @param after If not null, include only events created before a
* particular date.
* @param sort If null, uses the server's default, which is "desc"
*/
public List<GitlabEvent> getProjectEvents(Serializable projectId,
GitlabEvent.ActionType action,
GitlabEvent.TargetType targetType,
GitlabDate before,
GitlabDate after,
SortOrder sort)
throws IOException {
return getProjectEvents(projectId, action, targetType, before,
after, sort, new Pagination());
}

/**
* Get events for a project.
*
* @param action If not null, include only events of a particular action type
* @param targetType If not null, include only events of a particular target type
* @param before If not null, include only events created before a particular date.
* @param after If not null, include only events created before a
* particular date.
* @param sort If null, uses the server's default, which is "desc"
*/
public List<GitlabEvent> getProjectEvents(Serializable projectId,
GitlabEvent.ActionType action,
GitlabEvent.TargetType targetType,
GitlabDate before,
GitlabDate after,
SortOrder sort,
Pagination pagination)
throws IOException {

final Query query = new Query();
query.appendIf("action", action);
query.appendIf("target_type", targetType);
query.appendIf("before", before);
query.appendIf("after", after);
query.appendIf("sort", sort);

if (pagination != null) {
query.mergeWith(pagination.asQuery());
}

StringBuilder tailUrl = new StringBuilder(GitlabProject.URL)
.append("/")
.append(sanitizeProjectId(projectId))
.append(GitlabEvent.URL)
.append(query.toString());

return Arrays.asList(retrieve().method(GET).to(tailUrl.toString(), GitlabEvent[].class));
}
}
34 changes: 1 addition & 33 deletions src/main/java/org/gitlab/api/http/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,7 @@ public Query append(final String name, final String value) throws UnsupportedEnc
* @return this
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final String value) throws UnsupportedEncodingException {
if (value != null) {
append(name, value);
}
return this;
}

/**
* Conditionally append a parameter to the query
* if the value of the parameter is not null
*
* @param name Parameter name
* @param value Parameter value
* @return this
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final Integer value) throws UnsupportedEncodingException {
if (value != null) {
append(name, value.toString());
}
return this;
}

/**
* Conditionally append a parameter to the query
* if the value of the parameter is not null
*
* @param name Parameter name
* @param value Parameter value
* @return this
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final Boolean value) throws UnsupportedEncodingException {
public <T> Query appendIf(final String name, final T value) throws UnsupportedEncodingException {
if (value != null) {
append(name, value.toString());
}
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/org/gitlab/api/models/GitlabDate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.gitlab.api.models;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Date;
import java.util.List;

/**
* A date, with no time or timezone. This is, given the lack of
* timezone, an object whose meaning is somewhat ill-defined, but we
* must use the API that the hexarchs^W^W Gitlab gives us. We're
* going to make this immutable, because that's less error-prone. And
* we won't provide a constructor from Date, because Date is an
* instant in time rather than a calendar period.
*/
public final class GitlabDate {
private int year;
private int month;
private int day;

/**
* @param month and day are 1-based.
*/
public GitlabDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}

public int getYear() {
return year;
}

public int getMonth() {
return month;
}
public int getDay() {
return day;
}

public String toString() {
// Gitlab requires this specific format
return String.format("%04d-%02d-%02d", year, month, day);
}

public int hashCode() {
return this.year * 31 * 12 + this.month * 31 + this.day;
}

public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
GitlabDate that = (GitlabDate) o;
return this.year == that.year && this.month == that.month && this.day == that.day;
}
}
185 changes: 185 additions & 0 deletions src/main/java/org/gitlab/api/models/GitlabEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package org.gitlab.api.models;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Date;
import java.util.List;

public class GitlabEvent {

public final static String URL = "/events";

private String title;

@JsonProperty("project_id")
private String projectId;

@JsonProperty("action_name")
private String actionName;

// nullable
@JsonProperty("target_id")
private Integer targetId;

// nullable
@JsonProperty("target_iid")
private Integer targetIid;

@JsonProperty("target_type")
private TargetType targetType;

// It's not clear if this is nullable
@JsonProperty("author_id")
private Integer authorId;

// nullable
@JsonProperty("target_title")
private String targetTitle;

@JsonProperty("created_at")
private Date createdAt;

// see above re "author"
private GitlabUser author;

// see above re "author"
@JsonProperty("author_username")
private String authorUsername;

@JsonProperty("push_data")
private GitlabPushData pushData;

public String getTitle() {
return title;
}

public String getProjectId() {
return projectId;
}

/**
* It would be reasonable to expect that this matches up with
* ActionType, below, but it doesn't. The action type "pushed" is
* spelled "pushed to" in action_name (but it's spelled "pushed"
* in GitlabPushData).
*/
public String getActionName() {
return actionName;
}

public Integer getTargetId() {
return targetId;
}

public Integer getTargetIid() {
return targetIid;
}

public TargetType getTargetType() {
return targetType;
}

/** See() {@link #getAuthor()} for note */
public Integer getAuthorId() {
return authorId;
}

public String getTargetTitle() {
return targetTitle;
}

public Date getCreatedAt() {
return createdAt;
}

/**
* For many events, this seem to have nothing to do with any
* "author", but is in fact the user who performed the action.
* e.g. a push's "author" is not necessarily the author or
* committer of any commit, but the user doing the pushing.
*/
public GitlabUser getAuthor() {
return author;
}

/** See {@link #getAuthor()} for note */
public String getAuthorUsername() {
return authorUsername;
}

public GitlabPushData getPushData() {
return pushData;
}

public void setTitle(String title) {
this.title = title;
}

public void setProjectId(String projectId) {
this.projectId = projectId;
}

public void setActionName(String actionName) {
this.actionName = actionName;
}

public void setTargetId(Integer targetId) {
this.targetId = targetId;
}

public void setTargetIid(Integer targetIid) {
this.targetIid = targetIid;
}

public void setTargetType(TargetType targetType) {
this.targetType = targetType;
}

public void setAuthorId(Integer authorId) {
this.authorId = authorId;
}

public void setTargetTitle(String targetTitle) {
this.targetTitle = targetTitle;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public void setAuthor(GitlabUser author) {
this.author = author;
}

public void setAuthorUsername(String authorUsername) {
this.authorUsername = authorUsername;
}

public void setPushData(GitlabPushData pushData) {
this.pushData = pushData;
}

public enum ActionType {
created,
updated,
closed,
reopened,
pushed,
commented,
merged,
joined,
left,
destroyed,
expired
}

public enum TargetType {
issue,
milestone,
merge_request,
note,
project,
snippet,
user
}
}
Loading