-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from tpeetz/redmine-api
Add Redmine API
- Loading branch information
Showing
12 changed files
with
258 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package redmine | ||
|
||
import "fmt" | ||
|
||
// CustomField represents a custom field of issue in Redmine. | ||
type CustomField struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
} | ||
|
||
func (customField *CustomField) String() string { | ||
return fmt.Sprintf("CustomField: %d=%s", customField.ID, customField.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package redmine | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Issue represents issue in Redmine. | ||
type Issue struct { | ||
ID int `json:"id"` | ||
Project Project `json:"project"` | ||
Tracker Tracker `json:"tracker"` | ||
Status Status `json:"status"` | ||
Priority Priority `json:"priority"` | ||
Author User `json:"author"` | ||
Assigned User `json:"assigned_to"` | ||
Parent ParentIssue `json:"parent"` | ||
Subject string `json:"subject"` | ||
Description string `json:"description,omitempty"` | ||
StartDate string `json:"start_date,omitempty"` | ||
DueDate string `json:"due_date,omitempty"` | ||
DoneRatio int `json:"done_ratio"` | ||
Estimated json.Number `json:"estimated_hours"` | ||
Created string `json:"created_on,omitempty"` | ||
Updated string `json:"updated_on,omitempty"` | ||
CustomFields []CustomField `json:"custom_fields,omitempty"` | ||
} | ||
|
||
func (issue Issue) String() string { | ||
return fmt.Sprintf("Issue %d: %s\n %s\n Start : %s\n Due : %s\n %s\n", issue.ID, issue.Subject, issue.Project, issue.StartDate, issue.DueDate, issue.Status) | ||
} | ||
|
||
// Short returns ID and subject of issue. | ||
func (issue Issue) Short() string { | ||
return fmt.Sprintf("Issue %d: %s", issue.ID, issue.Subject) | ||
} | ||
|
||
// SingleIssue represents a JSON answer of a single issue in Redmine. | ||
type SingleIssue struct { | ||
Issue Issue `json:"issue"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package redmine | ||
|
||
// IssueList represents the list of issue in Redmine. | ||
type IssueList struct { | ||
Issues []Issue `json:"issues"` | ||
TotalCount int `json:"total_count"` | ||
Offset int `json:"offset"` | ||
Limit int `json:"limit"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package redmine | ||
|
||
import "fmt" | ||
|
||
// ParentIssue holds the ID of the parent issue of the current one. | ||
type ParentIssue struct { | ||
ID int `json:"id"` | ||
} | ||
|
||
func (parent *ParentIssue) String() string { | ||
return fmt.Sprintf("Parent Issue: %d", parent.ID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package redmine | ||
|
||
import "fmt" | ||
|
||
// Priority represents priority of issue in Redmine. | ||
type Priority struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func (priority Priority) String() string { | ||
return fmt.Sprintf("Priority: %s", priority.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package redmine | ||
|
||
import "fmt" | ||
|
||
// Project represents project in Redmine. | ||
type Project struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func (project Project) String() string { | ||
return fmt.Sprintf("Project: %s", project.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package redmine | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// Server represents a instance on an Redmine server. | ||
type Server struct { | ||
URL string | ||
Token string | ||
Limit int | ||
} | ||
|
||
func (server *Server) String() string { | ||
return fmt.Sprintf("Redmine server: %v", server.URL) | ||
} | ||
|
||
// Configure reads the configuration details from map and sets the server instance. | ||
func (server *Server) Configure(details map[string]interface{}) error { | ||
url, ok := details["domain"] | ||
if ok { | ||
serverURL, correctType := url.(string) | ||
if correctType { | ||
server.URL = serverURL | ||
} | ||
} else { | ||
server.URL = "https://redmine.example.com" | ||
} | ||
token, ok := details["token"] | ||
if ok { | ||
serverToken, correctType := token.(string) | ||
if correctType { | ||
server.Token = serverToken | ||
} | ||
} | ||
limit, ok := details["limit"] | ||
if ok { | ||
serverLimit, correctType := limit.(int) | ||
if correctType { | ||
server.Limit = serverLimit | ||
} | ||
} else { | ||
server.Limit = 120 | ||
} | ||
return nil | ||
} | ||
|
||
// LoadIssues gets issues from Redmine server. | ||
func (server *Server) LoadIssues() error { | ||
fmt.Printf("Redmine load issues from %s\n", server.URL) | ||
issuesURL := fmt.Sprintf("%s/issues.json?limit=%d", server.URL, server.Limit) | ||
request, err := http.NewRequest("GET", issuesURL, nil) | ||
if err != nil { | ||
fmt.Printf("creation of request failed: %v\n", err) | ||
return err | ||
} | ||
request.Header.Set("Content-Type", "application/json") | ||
request.Header.Set("X-Redmine-API-Key", server.Token) | ||
client := &http.Client{} | ||
response, err := client.Do(request) | ||
if err != nil { | ||
fmt.Printf("The HTTP request failed with error %s\n", err) | ||
return err | ||
} | ||
defer response.Body.Close() | ||
var issueList IssueList | ||
if err := json.NewDecoder(response.Body).Decode(&issueList); err != nil { | ||
fmt.Printf("Response could not parsed as JSON - %v\n", err) | ||
return err | ||
} | ||
fmt.Printf("Issue List:\n%v\n", issueList) | ||
return nil | ||
} | ||
|
||
// LoadProjects gets projects from Redmine server. | ||
func (server *Server) LoadProjects() error { | ||
fmt.Printf("Gitlab load projects from %s\n", server.URL) | ||
projectsURL := fmt.Sprintf("%s/%s/projects?per_page=%d", server.URL, "api/v4", 120) | ||
request, err := http.NewRequest("GET", projectsURL, nil) | ||
if err != nil { | ||
fmt.Printf("creation of request failed: %v\n", err) | ||
return err | ||
} | ||
request.Header.Set("Content-Type", "application/json") | ||
request.Header.Set("PRIVATE-TOKEN", server.Token) | ||
client := &http.Client{} | ||
response, err := client.Do(request) | ||
if err != nil { | ||
fmt.Printf("The HTTP request failed with error %s\n", err) | ||
return err | ||
} | ||
defer response.Body.Close() | ||
var projectList []Project | ||
if err := json.NewDecoder(response.Body).Decode(&projectList); err != nil { | ||
fmt.Printf("Response could not parsed as JSON - %v\n", err) | ||
return err | ||
} | ||
fmt.Printf("Project List:\n%v\n", projectList) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package redmine | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Status represents the issue status in Redmine. | ||
type Status struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func (status Status) String() string { | ||
return fmt.Sprintf("Status : %s", status.Name) | ||
} | ||
|
||
// Convert removes whitespace from status name. | ||
func (status Status) Convert() string { | ||
return strings.Replace(status.Name, " ", "", 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package redmine | ||
|
||
import "fmt" | ||
|
||
// Tracker represents a issue type in Redmine. | ||
type Tracker struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func (tracker *Tracker) String() string { | ||
return fmt.Sprintf("Tracker %s", tracker.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package redmine | ||
|
||
import "fmt" | ||
|
||
// User represents an user in Redmine. | ||
type User struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func (user *User) String() string { | ||
return fmt.Sprintf("User: %d=%s", user.ID, user.Name) | ||
} |