mylyn-gitea/io.gitea.mylyn.core/src/io/gitea/mylyn/core/GiteaConnection.java

225 lines
7.0 KiB
Java

// Copyright (c) 2021, Fr.Terrot. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package io.gitea.mylyn.core;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import io.gitea.ApiException;
import io.gitea.api.ApiVersion;
import io.gitea.api.IssueApi;
import io.gitea.api.RepositoryApi;
import io.gitea.api.UserApi;
import io.gitea.model.Comment;
import io.gitea.model.CreateIssueCommentOption;
import io.gitea.model.CreateIssueOption;
import io.gitea.model.EditIssueCommentOption;
import io.gitea.model.EditIssueOption;
import io.gitea.model.EditLabelOption;
import io.gitea.model.Issue;
import io.gitea.model.IssueLabelsOption;
import io.gitea.model.IssueState;
import io.gitea.model.Label;
import io.gitea.model.Milestone;
import io.gitea.model.Repository;
import io.gitea.model.User;
/**
* Handle Gitea connection and provides helpers to API.
*
*/
public class GiteaConnection {
public final String owner;
public final String repo;
public final String host;
public final Repository repository;
public final GiteaAttributeMapper mapper;
private List<Milestone> milestones;
private List<User> members;
private List<Label> labels;
/** Targetted Gitea Api Version */
public static final ApiVersion apiVersion = ApiVersion.V1;
public GiteaConnection(String host, Repository project, GiteaAttributeMapper mapper) {
this.host = host;
this.repository = project;
this.mapper = mapper;
this.owner = project.getOwner().getLogin();
this.repo = project.getName();
}
public UserApi userApi() {
return new UserApi();
}
public IssueApi issueApi() {
return new IssueApi();
}
public RepositoryApi repositoryApi() {
return new RepositoryApi();
}
public void refreshMilestones() {
try {
milestones = issueGetAllMilestones();
} catch (ApiException e1) {
e1.printStackTrace();
}
}
public void refreshLabels() {
try {
labels = issueGetAllLabels();
} catch (ApiException e2) {
e2.printStackTrace();
}
}
public void refreshMembers() {
ArrayList<User> memberList = new ArrayList<User>();
try {
memberList.addAll(repoListAllCollaborators());
} catch (ApiException e1) {
e1.printStackTrace();
}
members = Collections.unmodifiableList(memberList);
}
public void update() throws IOException {
refreshMilestones();
refreshLabels();
refreshMembers();
}
public List<Repository> userCurrentListRepos() throws ApiException {
return userApi().userCurrentListRepos(null, null);
}
public List<Milestone> getMilestones() {
return Collections.unmodifiableList(milestones);
}
public List<Label> getLabels() {
return Collections.unmodifiableList(labels);
}
public List<User> getRepositoryCollaborators() {
return Collections.unmodifiableList(members);
}
public List<User> repoListAllCollaborators() throws ApiException {
return repositoryApi().repoListCollaborators(owner, repo, null, null);
}
public List<Label> issueGetAllLabels() throws ApiException {
return issueApi().issueListLabels(owner, repo, 0, 0);
}
public List<Milestone> issueGetMilestones(IssueState state, String filterByName) throws ApiException {
return issueApi().issueGetMilestonesList(owner, repo, state.toString(), filterByName, null, null);
}
public List<Milestone> issueGetAllMilestones() throws ApiException {
return issueGetMilestones(IssueState.STATE_ANY, null);
}
public List<Milestone> issueGetOpenMilestones() throws ApiException {
return issueGetMilestones(IssueState.STATE_OPENED, null);
}
/**
* Get issues list by state.
*
* @param state
* @return
* @throws ApiException
*/
public List<Issue> repoGetIssues(IssueState state, String labels, Integer page, String query, String milestones) throws ApiException {
return issueApi().issueListIssues(owner, repo, state.toString(), labels, "", query, milestones, page, 0);
}
public List<Issue> repoGetIssues(String state, String labels, Integer page, String query, String milestones) throws ApiException {
return issueApi().issueListIssues(owner, repo, state, labels, query, "", milestones, page, 0);
}
public List<Issue> repoGetAllIssues() throws ApiException {
return repoGetIssues(IssueState.STATE_ANY, "", 0, "", "");
}
public List<Issue> repoGetOpenIssues() throws ApiException {
return repoGetIssues(IssueState.STATE_OPENED, "", 0, "", "");
}
/**
* Get a specific Issue.
*
* @param issueId : issue id
* @return Issue
* @throws ApiException
*/
public Issue issueGetIssue(Long issueId) throws ApiException {
return issueApi().issueGetIssue(owner, repo, issueId);
}
/**
* Get all comments attached to an issue.
*
* @param issue Issue to retrieve comments from.
* @param since
* @return List<Comment>
* @throws ApiException
*/
public List<Comment> issueGetComments(Issue issue, String since, String before) throws ApiException {
return issueApi().issueGetComments(owner, repo, issue.getNumber(), null, null); // FIXME: support since and before
}
/**
* Get all comments attached to an issue identified by its id.
*
* @param issueId
* @param since
* @return
* @throws ApiException
*/
public List<Comment> issueGetComments(Long issueId, String since) throws ApiException {
return issueGetComments(issueGetIssue(issueId), since, null); // FIXME: support since and before
}
public List<Comment> issueGetComments(Issue issue) throws ApiException {
return issueGetComments(issue, null, null);
}
/** Add a new comment to an issue.
*
* @param issueId
* @param body
* @return
* @throws ApiException
*/
public Comment issueAddComment(Long issueId, CreateIssueCommentOption body) throws ApiException {
return issueApi().issueCreateComment(owner, repo, issueId, body);
}
public Comment issueAddComment(Issue issue, CreateIssueCommentOption body) throws ApiException {
return issueAddComment(issue.getId(), body);
}
/** Create an issue.
*
* @param body
* @return
* @throws ApiException
*/
public Issue createIssue(CreateIssueOption body) throws ApiException {
return issueApi().issueCreateIssue(owner, repo, body);
}
public Issue editIssue(Long issueId, EditIssueOption body) throws ApiException {
return issueApi().issueEditIssue(owner, repo, issueId, body);
}
public List<Label> issueReplaceLabels(Long issueId, IssueLabelsOption labels) throws ApiException {
return issueApi().issueReplaceLabels(owner, repo, issueId, labels);
}
}