#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> ]]></description>
<change-notes><![CDATA[ <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> Fixed "HTTP: Unauthorized" exception.<br>
]]> ]]>
</change-notes> </change-notes>
@ -24,5 +26,4 @@
<actions> <actions>
</actions> </actions>
</idea-plugin> </idea-plugin>

@ -3,11 +3,9 @@
*/ */
package biz.elfuego.idea.issues.gitea; 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.model.GiteaTask;
import biz.elfuego.idea.issues.gitea.util.Consts; 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.CommentFields;
import biz.elfuego.idea.issues.gitea.util.Consts.ProjectFilter;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -20,12 +18,9 @@ import com.intellij.tasks.CustomTaskState;
import com.intellij.tasks.Task; import com.intellij.tasks.Task;
import com.intellij.tasks.impl.BaseRepositoryImpl; import com.intellij.tasks.impl.BaseRepositoryImpl;
import com.intellij.tasks.impl.SimpleComment; 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.Tag;
import com.intellij.util.xmlb.annotations.Transient;
import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus; 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.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity;
@ -51,9 +46,9 @@ class GiteaRepository extends BaseRepositoryImpl {
private String userId = null; private String userId = null;
private String userLogin = null; private String userLogin = null;
private ProjectFilter projectFilter = ProjectFilter.GENERAL; private String repoName = null;
private List<GiteaProject> projects = new ArrayList<>(); private String projName = null;
private GiteaProject selectedProject = null; private String token = null;
@SuppressWarnings("UnusedDeclaration") @SuppressWarnings("UnusedDeclaration")
public GiteaRepository() { public GiteaRepository() {
@ -72,9 +67,9 @@ class GiteaRepository extends BaseRepositoryImpl {
super(other); super(other);
userId = other.userId; userId = other.userId;
userLogin = other.userLogin; userLogin = other.userLogin;
projectFilter = other.projectFilter; repoName = other.repoName;
projects = other.projects; projName = other.projName;
selectedProject = other.selectedProject; token = other.token;
} }
@Override @Override
@ -84,9 +79,9 @@ class GiteaRepository extends BaseRepositoryImpl {
GiteaRepository other = (GiteaRepository) o; GiteaRepository other = (GiteaRepository) o;
return equal(userId, other.userId) && return equal(userId, other.userId) &&
equal(userLogin, other.userLogin) && equal(userLogin, other.userLogin) &&
equal(projectFilter, other.projectFilter) && equal(repoName, other.repoName) &&
equal(projects, other.projects) && equal(projName, other.projName) &&
equal(selectedProject, other.selectedProject); equal(token, other.token);
} }
private boolean equal(Object o1, Object o2) { private boolean equal(Object o1, Object o2) {
@ -172,7 +167,7 @@ class GiteaRepository extends BaseRepositoryImpl {
"application/json", "application/json",
"UTF-8" "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); + Consts.EndPoint.ISSUES + "/" + giteaTask.getId(), data);
executeMethod(patchTask); executeMethod(patchTask);
} }
@ -210,10 +205,13 @@ class GiteaRepository extends BaseRepositoryImpl {
if (result && StringUtil.isEmpty(this.getUrl())) { if (result && StringUtil.isEmpty(this.getUrl())) {
result = false; result = false;
} }
if (result && StringUtil.isEmpty(this.getUsername())) { if (result && StringUtil.isEmpty(this.getRepoName())) {
result = false; result = false;
} }
if (result && StringUtil.isEmpty(this.getPassword())) { if (result && StringUtil.isEmpty(this.getProjName())) {
result = false;
}
if (result && StringUtil.isEmpty(this.getToken())) {
result = false; result = false;
} }
return result; return result;
@ -226,14 +224,19 @@ class GiteaRepository extends BaseRepositoryImpl {
result += "Server"; result += "Server";
errors++; 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 += !StringUtils.isEmpty(result) ? " & " : "";
result += "Username"; result += "Project name";
errors++; errors++;
} }
if (StringUtil.isEmpty(getPassword())) { if (StringUtil.isEmpty(getToken())) {
result += !StringUtils.isEmpty(result) ? " & " : ""; result += !StringUtils.isEmpty(result) ? " & " : "";
result += "Password"; result += "Token";
errors++; errors++;
} }
if (!result.isEmpty()) { if (!result.isEmpty()) {
@ -242,20 +245,18 @@ class GiteaRepository extends BaseRepositoryImpl {
} }
private Task[] getIssues() throws Exception { private Task[] getIssues() throws Exception {
if (ifNoSelectedProj())
return new Task[]{};
if (!ensureUserId()) if (!ensureUserId())
return new Task[]{}; return new Task[]{};
List<GiteaTaskImpl> result = new ArrayList<>(); 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)); final JsonElement response = executeMethod(new GetMethod(url));
if (response == null) if (response == null)
return new Task[]{}; return new Task[]{};
JsonArray tasks = getArray(response); JsonArray tasks = getArray(response);
for (int i = 0; i < tasks.size(); i++) { for (int i = 0; i < tasks.size(); i++) {
JsonObject current = tasks.get(i).getAsJsonObject(); JsonObject current = tasks.get(i).getAsJsonObject();
GiteaTask raw = new GiteaTask(selectedProject, current); GiteaTask raw = new GiteaTask(getProject(), current);
if (!raw.isValid()) { if (!raw.isValid()) {
continue; continue;
} }
@ -267,18 +268,16 @@ class GiteaRepository extends BaseRepositoryImpl {
return result.toArray(primArray); return result.toArray(primArray);
} }
private boolean ifNoSelectedProj() { private String getProject() {
return selectedProject == null || selectedProject.getId().equals("-1"); return String.format("%s/%s", repoName, projName);
} }
Comment[] getComments(GiteaTaskImpl task) throws Exception { Comment[] getComments(GiteaTaskImpl task) throws Exception {
if (ifNoSelectedProj())
return new Comment[]{};
if (!ensureUserId()) if (!ensureUserId())
return new Comment[]{}; return new Comment[]{};
List<SimpleComment> result = new ArrayList<>(); 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; + "/" + task.getId() + Consts.EndPoint.COMMENTS;
final JsonElement response = executeMethod(new GetMethod(url)); final JsonElement response = executeMethod(new GetMethod(url));
if (response == null) if (response == null)
@ -299,9 +298,8 @@ class GiteaRepository extends BaseRepositoryImpl {
} }
private JsonElement executeMethod(@NotNull HttpMethod method) throws Exception { private JsonElement executeMethod(@NotNull HttpMethod method) throws Exception {
method.addRequestHeader("Authorization", "token " + token);
method.addRequestHeader("Content-type", "application/json"); method.addRequestHeader("Content-type", "application/json");
List authPrefs = Collections.singletonList(AuthPolicy.BASIC);
method.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
getHttpClient().executeMethod(method); getHttpClient().executeMethod(method);
if (method.getStatusCode() != HttpStatus.SC_OK && method.getStatusCode() != HttpStatus.SC_CREATED) { if (method.getStatusCode() != HttpStatus.SC_OK && method.getStatusCode() != HttpStatus.SC_CREATED) {
@ -335,56 +333,31 @@ class GiteaRepository extends BaseRepositoryImpl {
return true; return true;
} }
@Transient @SuppressWarnings("WeakerAccess")
List<GiteaProject> getProjectList(ProjectFilter projectFilter) throws Exception { public String getRepoName() {
if (!ensureUserId()) return repoName;
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;
} }
public ProjectFilter getProjectFilter() { public void setRepoName(String repoName) {
return projectFilter; this.repoName = repoName;
} }
public void setProjectFilter(ProjectFilter projectFilter) { @SuppressWarnings("WeakerAccess")
this.projectFilter = projectFilter; public String getProjName() {
return projName;
} }
public GiteaProject getSelectedProject() { public void setProjName(String projName) {
return selectedProject; this.projName = projName;
} }
public void setSelectedProject(GiteaProject selectedProject) { @SuppressWarnings("WeakerAccess")
this.selectedProject = selectedProject; public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
} }
@SuppressWarnings("UnusedDeclaration") @SuppressWarnings("UnusedDeclaration")
@ -406,15 +379,4 @@ class GiteaRepository extends BaseRepositoryImpl {
public void setUserLogin(String userLogin) { public void setUserLogin(String userLogin) {
this.userLogin = 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; 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.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.tasks.config.BaseRepositoryEditor; import com.intellij.tasks.config.BaseRepositoryEditor;
import com.intellij.tasks.impl.TaskUiUtil;
import com.intellij.ui.components.JBLabel; import com.intellij.ui.components.JBLabel;
import com.intellij.util.Consumer; import com.intellij.util.Consumer;
import com.intellij.util.ui.FormBuilder; import com.intellij.util.ui.FormBuilder;
import com.intellij.util.ui.UIUtil; import org.jdesktop.swingx.HorizontalLayout;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import javax.swing.*; import javax.swing.*;
import java.util.List;
/** /**
* @author Roman Pedchenko <elfuego@elfuego.biz> * @author Roman Pedchenko <elfuego@elfuego.biz>
* @date 2018.06.30 * @date 2018.06.30
*/ */
public class GiteaRepositoryEditor extends BaseRepositoryEditor<GiteaRepository> { public class GiteaRepositoryEditor extends BaseRepositoryEditor<GiteaRepository> {
private JBLabel projectLabel; private JBLabel repoLabel;
private ComboBox projectBox; private JTextField repoName;
private JBLabel filterLabel; private JTextField projName;
private ComboBox<ProjectFilter> filterBox;
private JBLabel tokenLabel;
private JPasswordField token;
GiteaRepositoryEditor(GiteaRepository repository, Project project, Consumer<GiteaRepository> consumer) { GiteaRepositoryEditor(GiteaRepository repository, Project project, Consumer<GiteaRepository> consumer) {
super(project, repository, consumer); super(project, repository, consumer);
filterBox.setSelectedItem(myRepository.getProjectFilter()); repoName.setText(repository.getRepoName());
projectBox.setSelectedItem(myRepository.getSelectedProject()); projName.setText(repository.getProjName());
installListener(filterBox); token.setText(repository.getToken());
installListener(projectBox); installListener(repoName);
installListener(projName);
UIUtil.invokeLaterIfNeeded(this::initialize); installListener(token);
}
myUserNameText.setVisible(false);
private void initialize() { myPasswordText.setVisible(false);
if (myRepository.isConfigured()) { myUserNameText.setEnabled(false);
new FetchProjectsTask().queue(); myPasswordText.setEnabled(false);
} myUsernameLabel.setVisible(false);
myPasswordLabel.setVisible(false);
} }
@Nullable @Nullable
@Override @Override
protected JComponent createCustomPanel() { protected JComponent createCustomPanel() {
filterBox = new ComboBox<>(ProjectFilter.values(), 300); JPanel panel = new JPanel();
filterBox.addActionListener(e -> new FetchProjectsTask().queue()); panel.setLayout(new HorizontalLayout());
filterLabel = new JBLabel("Project filter:", SwingConstants.RIGHT); repoName = new JTextField(20);
filterLabel.setLabelFor(filterBox); repoLabel = new JBLabel("Repository:", SwingConstants.RIGHT);
repoLabel.setLabelFor(panel);
projectBox = new ComboBox(300); panel.add(repoName);
projectBox.setRenderer(new TaskUiUtil.SimpleComboBoxRenderer("Set URL, username, and password"));
projectLabel = new JBLabel("Project:", SwingConstants.RIGHT); projName = new JTextField(30);
projectLabel.setLabelFor(projectBox); 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) return new FormBuilder().setAlignLabelOnRight(true)
.addLabeledComponent(filterLabel, filterBox) .addLabeledComponent(repoLabel, panel)
.addLabeledComponent(projectLabel, projectBox) .addLabeledComponent(tokenLabel, token)
.getPanel(); .getPanel();
} }
@Override @Override
public void setAnchor(@Nullable JComponent anchor) { public void setAnchor(@Nullable JComponent anchor) {
super.setAnchor(anchor); super.setAnchor(anchor);
filterLabel.setAnchor(anchor); repoLabel.setAnchor(anchor);
projectLabel.setAnchor(anchor); tokenLabel.setAnchor(anchor);
}
@Override
protected void afterTestConnection(boolean connectionSuccessful) {
if (connectionSuccessful) {
new FetchProjectsTask().queue();
}
} }
@Override @Override
public void apply() { public void apply() {
super.apply(); super.apply();
myRepository.setProjectFilter((ProjectFilter) filterBox.getSelectedItem()); myRepository.setRepoName(repoName.getText());
myRepository.setSelectedProject((GiteaProject) projectBox.getSelectedItem()); myRepository.setProjName(projName.getText());
//noinspection deprecation
myRepository.setToken(token.getText());
myTestButton.setEnabled(myRepository.isConfigured()); 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; 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.model.GiteaTask;
import biz.elfuego.idea.issues.gitea.util.Consts; import biz.elfuego.idea.issues.gitea.util.Consts;
import com.intellij.openapi.util.IconLoader; import com.intellij.openapi.util.IconLoader;
@ -22,7 +21,7 @@ import java.util.Date;
* @date 2018.06.30 * @date 2018.06.30
*/ */
public class GiteaTaskImpl extends Task implements Comparable<GiteaTaskImpl> { public class GiteaTaskImpl extends Task implements Comparable<GiteaTaskImpl> {
private final GiteaProject project; private final String project;
private final GiteaRepository repository; private final GiteaRepository repository;
private Comment[] comments; private Comment[] comments;
final GiteaTask task; final GiteaTask task;
@ -107,7 +106,7 @@ public class GiteaTaskImpl extends Task implements Comparable<GiteaTaskImpl> {
@Nullable @Nullable
@Override @Override
public String getIssueUrl() { public String getIssueUrl() {
return repository.getUrl() + "/" + project.getName() + Consts.EndPoint.ISSUES + "/" + task.getId(); return repository.getUrl() + "/" + project + Consts.EndPoint.ISSUES + "/" + task.getId();
} }
@Override @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 * @date 2018.06.30
*/ */
public class GiteaTask { public class GiteaTask {
private GiteaProject project; private String project;
private String id; private String id;
private String title; private String title;
private String description; private String description;
@ -26,16 +26,17 @@ public class GiteaTask {
private String state; private String state;
private String assignee; private String assignee;
public GiteaTask(GiteaProject project, JsonObject json) { public GiteaTask(String project, JsonObject json) {
this.project = project; this.project = project;
this.fromJson(json); this.fromJson(json);
} }
public GiteaProject getProject() { public String getProject() {
return project; return project;
} }
public void setProject(GiteaProject project) { @SuppressWarnings("unused")
public void setProject(String project) {
this.project = project; this.project = project;
} }
@ -88,11 +89,11 @@ public class GiteaTask {
return state; return state;
} }
@SuppressWarnings("WeakerAccess")
public void setState(String state) { public void setState(String state) {
this.state = state; this.state = state;
} }
@SuppressWarnings("unused")
public String getAssignee() { public String getAssignee() {
return assignee; return assignee;
} }

Loading…
Cancel
Save