#2, #7: changed login method to token authorization, got rid of project search limitations by implementing manual project name input

pull/11/head
Roman Pedchenko 5 years ago
parent f94722bcc7
commit d7d60548a9
No known key found for this signature in database
GPG Key ID: C4C5918838CFD10A

@ -9,6 +9,8 @@
]]></description>
<change-notes><![CDATA[
Changed login method to token authorization.<br>
Got rid of project search limitations by implementing manual project name input.<br>
Fixed "HTTP: Unauthorized" exception.<br>
]]>
</change-notes>
@ -24,5 +26,4 @@
<actions>
</actions>
</idea-plugin>

@ -3,11 +3,9 @@
*/
package biz.elfuego.idea.issues.gitea;
import biz.elfuego.idea.issues.gitea.model.GiteaProject;
import biz.elfuego.idea.issues.gitea.model.GiteaTask;
import biz.elfuego.idea.issues.gitea.util.Consts;
import biz.elfuego.idea.issues.gitea.util.Consts.CommentFields;
import biz.elfuego.idea.issues.gitea.util.Consts.ProjectFilter;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@ -20,12 +18,9 @@ import com.intellij.tasks.CustomTaskState;
import com.intellij.tasks.Task;
import com.intellij.tasks.impl.BaseRepositoryImpl;
import com.intellij.tasks.impl.SimpleComment;
import com.intellij.util.xmlb.annotations.AbstractCollection;
import com.intellij.util.xmlb.annotations.Tag;
import com.intellij.util.xmlb.annotations.Transient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
@ -51,9 +46,9 @@ class GiteaRepository extends BaseRepositoryImpl {
private String userId = null;
private String userLogin = null;
private ProjectFilter projectFilter = ProjectFilter.GENERAL;
private List<GiteaProject> projects = new ArrayList<>();
private GiteaProject selectedProject = null;
private String repoName = null;
private String projName = null;
private String token = null;
@SuppressWarnings("UnusedDeclaration")
public GiteaRepository() {
@ -72,9 +67,9 @@ class GiteaRepository extends BaseRepositoryImpl {
super(other);
userId = other.userId;
userLogin = other.userLogin;
projectFilter = other.projectFilter;
projects = other.projects;
selectedProject = other.selectedProject;
repoName = other.repoName;
projName = other.projName;
token = other.token;
}
@Override
@ -84,9 +79,9 @@ class GiteaRepository extends BaseRepositoryImpl {
GiteaRepository other = (GiteaRepository) o;
return equal(userId, other.userId) &&
equal(userLogin, other.userLogin) &&
equal(projectFilter, other.projectFilter) &&
equal(projects, other.projects) &&
equal(selectedProject, other.selectedProject);
equal(repoName, other.repoName) &&
equal(projName, other.projName) &&
equal(token, other.token);
}
private boolean equal(Object o1, Object o2) {
@ -172,7 +167,7 @@ class GiteaRepository extends BaseRepositoryImpl {
"application/json",
"UTF-8"
);
HttpMethod patchTask = getPatchMethod(getApiUrl() + Consts.EndPoint.REPOS + selectedProject.getName()
HttpMethod patchTask = getPatchMethod(getApiUrl() + Consts.EndPoint.REPOS + getProject()
+ Consts.EndPoint.ISSUES + "/" + giteaTask.getId(), data);
executeMethod(patchTask);
}
@ -210,10 +205,13 @@ class GiteaRepository extends BaseRepositoryImpl {
if (result && StringUtil.isEmpty(this.getUrl())) {
result = false;
}
if (result && StringUtil.isEmpty(this.getUsername())) {
if (result && StringUtil.isEmpty(this.getRepoName())) {
result = false;
}
if (result && StringUtil.isEmpty(this.getPassword())) {
if (result && StringUtil.isEmpty(this.getProjName())) {
result = false;
}
if (result && StringUtil.isEmpty(this.getToken())) {
result = false;
}
return result;
@ -226,14 +224,19 @@ class GiteaRepository extends BaseRepositoryImpl {
result += "Server";
errors++;
}
if (StringUtil.isEmpty(getUsername())) {
if (StringUtil.isEmpty(getRepoName())) {
result += !StringUtils.isEmpty(result) ? " & " : "";
result += "Repo name";
errors++;
}
if (StringUtil.isEmpty(getProjName())) {
result += !StringUtils.isEmpty(result) ? " & " : "";
result += "Username";
result += "Project name";
errors++;
}
if (StringUtil.isEmpty(getPassword())) {
if (StringUtil.isEmpty(getToken())) {
result += !StringUtils.isEmpty(result) ? " & " : "";
result += "Password";
result += "Token";
errors++;
}
if (!result.isEmpty()) {
@ -242,20 +245,18 @@ class GiteaRepository extends BaseRepositoryImpl {
}
private Task[] getIssues() throws Exception {
if (ifNoSelectedProj())
return new Task[]{};
if (!ensureUserId())
return new Task[]{};
List<GiteaTaskImpl> result = new ArrayList<>();
final String url = getApiUrl() + Consts.EndPoint.REPOS + selectedProject.getName() + Consts.EndPoint.ISSUES;
final String url = getApiUrl() + Consts.EndPoint.REPOS + getProject() + Consts.EndPoint.ISSUES;
final JsonElement response = executeMethod(new GetMethod(url));
if (response == null)
return new Task[]{};
JsonArray tasks = getArray(response);
for (int i = 0; i < tasks.size(); i++) {
JsonObject current = tasks.get(i).getAsJsonObject();
GiteaTask raw = new GiteaTask(selectedProject, current);
GiteaTask raw = new GiteaTask(getProject(), current);
if (!raw.isValid()) {
continue;
}
@ -267,18 +268,16 @@ class GiteaRepository extends BaseRepositoryImpl {
return result.toArray(primArray);
}
private boolean ifNoSelectedProj() {
return selectedProject == null || selectedProject.getId().equals("-1");
private String getProject() {
return String.format("%s/%s", repoName, projName);
}
Comment[] getComments(GiteaTaskImpl task) throws Exception {
if (ifNoSelectedProj())
return new Comment[]{};
if (!ensureUserId())
return new Comment[]{};
List<SimpleComment> result = new ArrayList<>();
final String url = getApiUrl() + Consts.EndPoint.REPOS + selectedProject.getName() + Consts.EndPoint.ISSUES
final String url = getApiUrl() + Consts.EndPoint.REPOS + getProject() + Consts.EndPoint.ISSUES
+ "/" + task.getId() + Consts.EndPoint.COMMENTS;
final JsonElement response = executeMethod(new GetMethod(url));
if (response == null)
@ -299,9 +298,8 @@ class GiteaRepository extends BaseRepositoryImpl {
}
private JsonElement executeMethod(@NotNull HttpMethod method) throws Exception {
method.addRequestHeader("Authorization", "token " + token);
method.addRequestHeader("Content-type", "application/json");
List authPrefs = Collections.singletonList(AuthPolicy.BASIC);
method.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
getHttpClient().executeMethod(method);
if (method.getStatusCode() != HttpStatus.SC_OK && method.getStatusCode() != HttpStatus.SC_CREATED) {
@ -335,56 +333,31 @@ class GiteaRepository extends BaseRepositoryImpl {
return true;
}
@Transient
List<GiteaProject> getProjectList(ProjectFilter projectFilter) throws Exception {
if (!ensureUserId())
return Collections.emptyList();
final String query;
if (projectFilter == null)
projectFilter = ProjectFilter.GENERAL;
switch (projectFilter) {
case CONTRUBUTOR:
query = Consts.EndPoint.REPOS_SEARCH_UID + userId;
break;
case OWNER:
query = Consts.EndPoint.REPOS_SEARCH_UID_EX + userId;
break;
default:
query = Consts.EndPoint.REPOS_SEARCH;
break;
}
JsonElement response = executeMethod(new GetMethod(getApiUrl() + query));
if (response == null)
return Collections.emptyList();
JsonArray reply = getOkData(response);
List<GiteaProject> result = new ArrayList<>();
for (int i = 0; i < reply.size(); i++) {
JsonObject current = getObject(reply.get(i));
GiteaProject project = new GiteaProject().setId(getString(current, "id", ""))
.setName(getString(current, "full_name", ""));
if (!project.isValid()) {
continue;
}
result.add(project);
}
projects = result;
return projects;
@SuppressWarnings("WeakerAccess")
public String getRepoName() {
return repoName;
}
public ProjectFilter getProjectFilter() {
return projectFilter;
public void setRepoName(String repoName) {
this.repoName = repoName;
}
public void setProjectFilter(ProjectFilter projectFilter) {
this.projectFilter = projectFilter;
@SuppressWarnings("WeakerAccess")
public String getProjName() {
return projName;
}
public GiteaProject getSelectedProject() {
return selectedProject;
public void setProjName(String projName) {
this.projName = projName;
}
public void setSelectedProject(GiteaProject selectedProject) {
this.selectedProject = selectedProject;
@SuppressWarnings("WeakerAccess")
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
@SuppressWarnings("UnusedDeclaration")
@ -406,15 +379,4 @@ class GiteaRepository extends BaseRepositoryImpl {
public void setUserLogin(String userLogin) {
this.userLogin = userLogin;
}
@SuppressWarnings("UnusedDeclaration")
@AbstractCollection(surroundWithTag = false, elementTag = "GiteaProject", elementTypes = GiteaProject.class)
public List<GiteaProject> getProjects() {
return projects;
}
@SuppressWarnings("UnusedDeclaration")
public void setProjects(List<GiteaProject> projects) {
this.projects = projects;
}
}

@ -3,112 +3,87 @@
*/
package biz.elfuego.idea.issues.gitea;
import biz.elfuego.idea.issues.gitea.model.GiteaProject;
import biz.elfuego.idea.issues.gitea.util.Consts.ProjectFilter;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.tasks.config.BaseRepositoryEditor;
import com.intellij.tasks.impl.TaskUiUtil;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.Consumer;
import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jdesktop.swingx.HorizontalLayout;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.List;
/**
* @author Roman Pedchenko <elfuego@elfuego.biz>
* @date 2018.06.30
*/
public class GiteaRepositoryEditor extends BaseRepositoryEditor<GiteaRepository> {
private JBLabel projectLabel;
private ComboBox projectBox;
private JBLabel repoLabel;
private JTextField repoName;
private JBLabel filterLabel;
private ComboBox<ProjectFilter> filterBox;
private JTextField projName;
private JBLabel tokenLabel;
private JPasswordField token;
GiteaRepositoryEditor(GiteaRepository repository, Project project, Consumer<GiteaRepository> consumer) {
super(project, repository, consumer);
filterBox.setSelectedItem(myRepository.getProjectFilter());
projectBox.setSelectedItem(myRepository.getSelectedProject());
installListener(filterBox);
installListener(projectBox);
UIUtil.invokeLaterIfNeeded(this::initialize);
}
private void initialize() {
if (myRepository.isConfigured()) {
new FetchProjectsTask().queue();
}
repoName.setText(repository.getRepoName());
projName.setText(repository.getProjName());
token.setText(repository.getToken());
installListener(repoName);
installListener(projName);
installListener(token);
myUserNameText.setVisible(false);
myPasswordText.setVisible(false);
myUserNameText.setEnabled(false);
myPasswordText.setEnabled(false);
myUsernameLabel.setVisible(false);
myPasswordLabel.setVisible(false);
}
@Nullable
@Override
protected JComponent createCustomPanel() {
filterBox = new ComboBox<>(ProjectFilter.values(), 300);
filterBox.addActionListener(e -> new FetchProjectsTask().queue());
filterLabel = new JBLabel("Project filter:", SwingConstants.RIGHT);
filterLabel.setLabelFor(filterBox);
projectBox = new ComboBox(300);
projectBox.setRenderer(new TaskUiUtil.SimpleComboBoxRenderer("Set URL, username, and password"));
projectLabel = new JBLabel("Project:", SwingConstants.RIGHT);
projectLabel.setLabelFor(projectBox);
JPanel panel = new JPanel();
panel.setLayout(new HorizontalLayout());
repoName = new JTextField(20);
repoLabel = new JBLabel("Repository:", SwingConstants.RIGHT);
repoLabel.setLabelFor(panel);
panel.add(repoName);
projName = new JTextField(30);
JBLabel projLabel = new JBLabel(" / Project: ", SwingConstants.RIGHT);
projLabel.setLabelFor(projName);
panel.add(projLabel);
panel.add(projName);
token = new JPasswordField();
tokenLabel = new JBLabel("Token:", SwingConstants.RIGHT);
tokenLabel.setLabelFor(token);
return new FormBuilder().setAlignLabelOnRight(true)
.addLabeledComponent(filterLabel, filterBox)
.addLabeledComponent(projectLabel, projectBox)
.addLabeledComponent(repoLabel, panel)
.addLabeledComponent(tokenLabel, token)
.getPanel();
}
@Override
public void setAnchor(@Nullable JComponent anchor) {
super.setAnchor(anchor);
filterLabel.setAnchor(anchor);
projectLabel.setAnchor(anchor);
}
@Override
protected void afterTestConnection(boolean connectionSuccessful) {
if (connectionSuccessful) {
new FetchProjectsTask().queue();
}
repoLabel.setAnchor(anchor);
tokenLabel.setAnchor(anchor);
}
@Override
public void apply() {
super.apply();
myRepository.setProjectFilter((ProjectFilter) filterBox.getSelectedItem());
myRepository.setSelectedProject((GiteaProject) projectBox.getSelectedItem());
myRepository.setRepoName(repoName.getText());
myRepository.setProjName(projName.getText());
//noinspection deprecation
myRepository.setToken(token.getText());
myTestButton.setEnabled(myRepository.isConfigured());
}
private class FetchProjectsTask extends TaskUiUtil.ComboBoxUpdater<GiteaProject> {
private FetchProjectsTask() {
super(GiteaRepositoryEditor.this.myProject, "Downloading Gitea projects...", projectBox);
}
@Override
public GiteaProject getExtraItem() {
return GiteaProject.UNSPECIFIED_PROJECT;
}
@Nullable
@Override
public GiteaProject getSelectedItem() {
return myRepository.getSelectedProject();
}
@NotNull
@Override
protected List<GiteaProject> fetch(@NotNull ProgressIndicator indicator) throws Exception {
return myRepository.getProjectList((ProjectFilter) filterBox.getSelectedItem());
}
}
}

@ -3,7 +3,6 @@
*/
package biz.elfuego.idea.issues.gitea;
import biz.elfuego.idea.issues.gitea.model.GiteaProject;
import biz.elfuego.idea.issues.gitea.model.GiteaTask;
import biz.elfuego.idea.issues.gitea.util.Consts;
import com.intellij.openapi.util.IconLoader;
@ -22,7 +21,7 @@ import java.util.Date;
* @date 2018.06.30
*/
public class GiteaTaskImpl extends Task implements Comparable<GiteaTaskImpl> {
private final GiteaProject project;
private final String project;
private final GiteaRepository repository;
private Comment[] comments;
final GiteaTask task;
@ -107,7 +106,7 @@ public class GiteaTaskImpl extends Task implements Comparable<GiteaTaskImpl> {
@Nullable
@Override
public String getIssueUrl() {
return repository.getUrl() + "/" + project.getName() + Consts.EndPoint.ISSUES + "/" + task.getId();
return repository.getUrl() + "/" + project + Consts.EndPoint.ISSUES + "/" + task.getId();
}
@Override

@ -1,71 +0,0 @@
/*
* Copyright © 2018 by elfuego.biz
*/
package biz.elfuego.idea.issues.gitea.model;
import biz.elfuego.idea.issues.gitea.util.Consts;
/**
* @author Roman Pedchenko <elfuego@elfuego.biz>
* @date 2018.06.30
*/
public class GiteaProject {
private String id;
private String name;
public String getId() {
return id;
}
public GiteaProject setId(String mProjectId) {
this.id = mProjectId;
return this;
}
public String getName() {
return name;
}
public GiteaProject setName(String mProjectTitle) {
this.name = mProjectTitle;
return this;
}
public boolean isValid() {
return !(id.equals("") || name.equals(""));
}
@Override
public String toString() {
return name != null ? name : super.toString();
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
GiteaProject that = (GiteaProject) o;
return (id != null ? id.equals(that.id) : that.id == null) && (name != null ? name.equals(that.name) : that.name == null);
}
public static final GiteaProject UNSPECIFIED_PROJECT = new GiteaProject() {
@Override
public String getName() {
return "-- Select A Project (Required) --";
}
@Override
public String getId() {
return Consts.UNSPEC_PROJ_ID;
}
@Override
public String toString() {
return getName();
}
};
}

@ -17,7 +17,7 @@ import static biz.elfuego.idea.issues.gitea.util.Utils.getString;
* @date 2018.06.30
*/
public class GiteaTask {
private GiteaProject project;
private String project;
private String id;
private String title;
private String description;
@ -26,16 +26,17 @@ public class GiteaTask {
private String state;
private String assignee;
public GiteaTask(GiteaProject project, JsonObject json) {
public GiteaTask(String project, JsonObject json) {
this.project = project;
this.fromJson(json);
}
public GiteaProject getProject() {
public String getProject() {
return project;
}
public void setProject(GiteaProject project) {
@SuppressWarnings("unused")
public void setProject(String project) {
this.project = project;
}
@ -88,11 +89,11 @@ public class GiteaTask {
return state;
}
@SuppressWarnings("WeakerAccess")
public void setState(String state) {
this.state = state;
}
@SuppressWarnings("unused")
public String getAssignee() {
return assignee;
}

Loading…
Cancel
Save