Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Projects list #46

Open
KedXP opened this issue Apr 16, 2021 · 4 comments
Open

Projects list #46

KedXP opened this issue Apr 16, 2021 · 4 comments

Comments

@KedXP
Copy link

KedXP commented Apr 16, 2021

Hello!
I looked your code for listing projects, and, if i understand correct, you don't check, if number of projects is more than limit (default or set in const).
I looked in
godmine/main.go#L456
and
project.go#L58
but didn't find any checks, like in issues:
issue.go#L369

I hope i wrong, correct me if yes.

Have a good day!

@KedXP
Copy link
Author

KedXP commented Apr 19, 2021

And in issue.go :
Line 384 (issues = append(issues, r.Issues...))
must be before line 380 ( if r.TotalCount == uint(len(issues)) { )

@mattn
Copy link
Owner

mattn commented Apr 19, 2021

Currently, I don't have an environment to test this but could you please check this patch?

diff --git a/project.go b/project.go
index 3afab73..d48b880 100644
--- a/project.go
+++ b/project.go
@@ -17,7 +17,10 @@ type projectResult struct {
 }
 
 type projectsResult struct {
-	Projects []Project `json:"projects"`
+	Projects   []Project `json:"projects"`
+	TotalCount uint      `json:"total_count"`
+	Offset     uint      `json:"offset"`
+	Limit      uint      `json:"limit"`
 }
 
 type Project struct {
@@ -55,8 +58,9 @@ func (c *Client) Project(id int) (*Project, error) {
 	return &r.Project, nil
 }
 
-func (c *Client) Projects() ([]Project, error) {
-	res, err := c.Get(c.endpoint + "/projects.json?key=" + c.apikey + c.getPaginationClause())
+func getProject(c *Client, url string, offset int) (*projectsResult, error) {
+	res, err := c.Get(c.endpoint + url + "&offset=" + strconv.Itoa(offset))
+
 	if err != nil {
 		return nil, err
 	}
@@ -76,7 +80,49 @@ func (c *Client) Projects() ([]Project, error) {
 	if err != nil {
 		return nil, err
 	}
-	return r.Projects, nil
+
+	return &r, nil
+}
+
+func getProjects(c *Client, url string) ([]Project, error) {
+	completed := false
+	var projects []Project
+
+	for completed == false {
+		r, err := getProject(c, url, len(projects))
+
+		if err != nil {
+			return nil, err
+		}
+
+		if r.TotalCount == uint(len(projects)) {
+			completed = true
+		}
+
+		projects = append(projects, r.Projects...)
+	}
+
+	return projects, nil
+}
+
+func (c *Client) Projects() ([]Project, error) {
+	completed := false
+	var projects []Project
+
+	for completed == false {
+		r, err := getProject(c, "/projects.json?key="+c.apikey+c.getPaginationClause(), len(projects))
+
+		if err != nil {
+			return nil, err
+		}
+
+		if r.TotalCount == uint(len(projects)) {
+			completed = true
+		}
+
+		projects = append(projects, r.Projects...)
+	}
+	return projects, nil
 }
 
 func (c *Client) CreateProject(project Project) (*Project, error) {

@KedXP
Copy link
Author

KedXP commented Apr 22, 2021

  1. Cloned master to my pc
  2. Init config
  3. Hardcoded limit=3 to func projects
  4. Got only 3 projects with godmine project list
  5. applied patch
  6. hardcoded limit=3 to func getProject
  7. got all 14 projects
    It seems to work.

изображение

@KedXP
Copy link
Author

KedXP commented Apr 22, 2021

Please, check this one:

And in issue.go :
Line 384 (issues = append(issues, r.Issues...))
must be before line 380 ( if r.TotalCount == uint(len(issues)) { )

You check length of result array before appending new result. So you make one empty request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants