mattn / go-redmine

MIT License
185 stars 80 forks source link

Projects list #46

Open KedXP opened 3 years ago

KedXP commented 3 years ago

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 commented 3 years ago

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

mattn commented 3 years ago

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 commented 3 years ago
  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 commented 3 years ago

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.