Skip to content

Commit d68114a

Browse files
author
Thomas Peetz
authored
Merge pull request #8 from tpeetz/github-api
Add Github API
2 parents 6287254 + ce39c76 commit d68114a

File tree

10 files changed

+332
-9
lines changed

10 files changed

+332
-9
lines changed

pkg/server/gitserver.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package server
22

33
import (
4-
"errors"
5-
4+
"github.com/tpeetz/pull-task/pkg/types/github"
65
"github.com/tpeetz/pull-task/pkg/types/gitlab"
76
)
87

@@ -14,6 +13,15 @@ type GitServer interface {
1413
String() string
1514
}
1615

16+
// UnkownServiceTypeError defines the error condition for unkown service type.
17+
type UnkownServiceTypeError struct {
18+
Service string
19+
}
20+
21+
func (uste *UnkownServiceTypeError) Error() string {
22+
return uste.Service + " not known"
23+
}
24+
1725
// NewGitServer creates instance of GitServer from given configuration.
1826
func NewGitServer(details map[string]interface{}) (GitServer, error) {
1927
//fmt.Printf("details: %v\n", details)
@@ -23,9 +31,11 @@ func NewGitServer(details map[string]interface{}) (GitServer, error) {
2331
case "gitlab":
2432
server = &gitlab.Server{}
2533
case "github":
26-
return nil, errors.New("service type github unknown")
34+
server = &github.Server{}
2735
case "redmine":
28-
return nil, errors.New("service type redmine unknown")
36+
return nil, &UnkownServiceTypeError{"redmine"}
37+
default:
38+
return nil, &UnkownServiceTypeError{service.(string)}
2939
}
3040
server.Configure(details)
3141
return server, nil

pkg/server/gitserver_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"reflect"
55
"testing"
66

7+
"github.com/tpeetz/pull-task/pkg/types/github"
78
"github.com/tpeetz/pull-task/pkg/types/gitlab"
89
)
910

@@ -15,11 +16,14 @@ var (
1516
githubConfig = map[string]interface{}{
1617
"service": "github",
1718
}
18-
//ghServer = &GitServer{url: "https://github.com", token: "secretToken"}
19+
ghServer = &github.Server{URL: "https://api.github.com"}
1920
redmineConfig = map[string]interface{}{
2021
"service": "redmine",
2122
}
2223
//rmServer = &GitServer{url: "https://redmine.com", token: "secretToken"}
24+
unknownConfig = map[string]interface{}{
25+
"service": "unknown",
26+
}
2327
)
2428

2529
func TestNewGitServer(t *testing.T) {
@@ -33,9 +37,9 @@ func TestNewGitServer(t *testing.T) {
3337
wantErr bool
3438
}{
3539
{"gitlab", args{gitlabConfig}, glServer, false},
36-
{"github", args{githubConfig}, nil, true},
37-
{"redmine", args{githubConfig}, nil, true},
38-
{"unknown", args{githubConfig}, nil, true},
40+
{"github", args{githubConfig}, ghServer, false},
41+
{"redmine", args{redmineConfig}, nil, true},
42+
{"unknown", args{unknownConfig}, nil, true},
3943
}
4044
for _, tt := range tests {
4145
t.Run(tt.name, func(t *testing.T) {

pkg/types/github/issue.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package github
2+
3+
import "fmt"
4+
5+
// Issue represents issue in Github.
6+
type Issue struct {
7+
ID int `json:"id"`
8+
NodeID string `json:"node_id"`
9+
Title string `json:"title"`
10+
Number int `json:"number"`
11+
User User `json:"user"`
12+
Labels []Label `json:"labels"`
13+
URL string `json:"url"`
14+
RepositoryURL string `json:"repository_url"`
15+
LabelsURL string `json:"labels_url"`
16+
CommentsURL string `json:"comments_url"`
17+
EventsURL string `json:"events_url"`
18+
HTMLURL string `json:"html_url"`
19+
State string `json:"state"`
20+
Locked bool `json:"locked"`
21+
Assignee User `json:"assignee"`
22+
Assignees []User `json:"assignees"`
23+
Milestone Milestone `json:"milestone,omitempty"`
24+
Comments int `json:"comments"`
25+
Created string `json:"created_at,omitempty"`
26+
Updated string `json:"updated_at,omitempty"`
27+
Closed string `json:"closed_at,omitempty"`
28+
AuthorAssociation string `json:"author_association"`
29+
ActiveLockReason string `json:"active_lock_reason,omitempty"`
30+
Repository Repository `json:"repository"`
31+
Body string `json:"body"`
32+
PerformedViaGithubApp string `json:"performed_via_github_app,omitempty"`
33+
}
34+
35+
func (issue Issue) String() string {
36+
//return fmt.Sprintf("Issue %d: %s\n Project: %d\n Start : %s\n Due : %s\n %s\n %v\n %s\n", r.ID, r.Title, r.ProjectID, r.Created, r.DueDate, r.State, r.Labels, r.WebURL)
37+
return fmt.Sprintf("Issue %d: %s\n", issue.ID, issue.Title)
38+
}
39+
40+
// Short returns issue details in short.
41+
func (issue Issue) Short() string {
42+
return fmt.Sprintf("Issue %d: %s", issue.ID, issue.Title)
43+
}

pkg/types/github/label.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package github
2+
3+
import "fmt"
4+
5+
// Label represents issue label.
6+
type Label struct {
7+
ID int `json:"id"`
8+
NodeID string `json:"node_id"`
9+
URL string `json:"url"`
10+
Name string `json:"name"`
11+
Color string `json:"color"`
12+
DEfault bool `json:"default"`
13+
Description string `json:"description"`
14+
}
15+
16+
func (label Label) String() string {
17+
return fmt.Sprintf("License %s\n", label.Name)
18+
}

pkg/types/github/license.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package github
2+
3+
import "fmt"
4+
5+
// License holds information about repository license.
6+
type License struct {
7+
Key string `json:"key"`
8+
Name string `json:"name"`
9+
SpdxID string `json:"spdx_id"`
10+
URL string `json:"url"`
11+
NodeID string `json:"node_id"`
12+
}
13+
14+
func (license License) String() string {
15+
return fmt.Sprintf("License %s\n", license.Name)
16+
}

pkg/types/github/milestone.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package github
2+
3+
import "fmt"
4+
5+
// Milestone represents milestone of a Github project.
6+
type Milestone struct {
7+
ID int `json:"id"`
8+
NodeID string `json:"node_id"`
9+
Number int `json:"number"`
10+
Title string `json:"title"`
11+
Description string `json:"description"`
12+
Creator User `json:"creator"`
13+
URL string `json:"url"`
14+
HTMLURL string `json:"html_url"`
15+
LabelsURL string `json:"labels_url"`
16+
OpenIssues int `json:"open_issues"`
17+
ClosedIssues int `json:"closed_issues"`
18+
State string `json:"state"`
19+
Created string `json:"created_at,omitempty"`
20+
Updated string `json:"updated_at,omitempty"`
21+
Due string `json:"due_on,omitempty"`
22+
Closed string `json:"closed_at,omitempty"`
23+
}
24+
25+
func (milestone Milestone) String() string {
26+
return fmt.Sprintf("Issue %d: %s\n", milestone.ID, milestone.Title)
27+
}

pkg/types/github/repository.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package github
2+
3+
import "fmt"
4+
5+
// Repository holds repository details.
6+
type Repository struct {
7+
ID int `json:"id"`
8+
NodeID string `json:"node_id"`
9+
Name string `json:"name"`
10+
Fullname string `json:"full_name"`
11+
Private bool `json:"private"`
12+
Owner User `json:"owner"`
13+
Description string `json:"description"`
14+
Fork bool `json:"fork"`
15+
URL string `json:"url"`
16+
HTMLURL string `json:"html_url"`
17+
ForksURL string `json:"forks_url"`
18+
KeysURL string `json:"keys_url"`
19+
CollaboratorsURL string `json:"collaborators_url"`
20+
TeamsURL string `json:"teams_url"`
21+
HooksURL string `json:"hooks_url"`
22+
IssueEventsURL string `json:"issue_events_url"`
23+
EventsURL string `json:"events_url"`
24+
AssigneesURL string `json:"assignees_url"`
25+
BranchesURL string `json:"branches_url"`
26+
TagsURL string `json:"tags_url"`
27+
BlobsURL string `json:"blobs_url"`
28+
GitTagsURL string `json:"git_tags_url"`
29+
GitRefsURL string `json:"git_refs_url"`
30+
TreesURL string `json:"trees_url"`
31+
StatusesURL string `json:"statuses_url"`
32+
LanguagesURL string `json:"languages_url"`
33+
StargazersURL string `json:"stargazers_url"`
34+
ContributorsURL string `json:"contributors_url"`
35+
SubscribersURL string `json:"subscribers_url"`
36+
SubscriptionURL string `json:"subscription_url"`
37+
CommitsURL string `json:"commits_url"`
38+
GitCommitsURL string `json:"git_commits_url"`
39+
CommentsURL string `json:"comments_url"`
40+
IssueCommentsURL string `json:"issue_comment_url"`
41+
ContentsURL string `json:"contents_url"`
42+
CompareURL string `json:"compare_url"`
43+
MergesURL string `json:"merges_url"`
44+
ArchiveURL string `json:"archive_url"`
45+
DownloadsURL string `json:"downloads_url"`
46+
IssuesURL string `json:"issues_url"`
47+
PullsURL string `json:"pulls_url"`
48+
MilestonesURL string `json:"milestones_url"`
49+
NotificationsURL string `json:"notifications_url"`
50+
LabelsURL string `json:"labels_url"`
51+
ReleasesURL string `json:"releases_url"`
52+
DeploymentsURL string `json:"deployments_url"`
53+
Created string `json:"created_at,omitempty"`
54+
Updated string `json:"updated_at,omitempty"`
55+
Pushed string `json:"pushed_at,omitempty"`
56+
GitURL string `json:"git_url"`
57+
SSHURL string `json:"ssh_url"`
58+
CloneURL string `json:"clone_url"`
59+
SVNURL string `json:"svn_url"`
60+
Homepage string `json:"homepage"`
61+
Size int `json:"size"`
62+
StargazersCount int `json:"stargazers_count"`
63+
WatchersCount int `json:"watchers_count"`
64+
Language string `json:"language"`
65+
HasIssues bool `json:"has_issues"`
66+
HasProjects bool `json:"has_projects"`
67+
HasDownloads bool `json:"has_downloads"`
68+
HasWiki bool `json:"has_wiki"`
69+
HasPages bool `json:"has_pages"`
70+
ForksCount int `json:"forks_count"`
71+
MirrorURL string `json:"mirror_url,omitempty"`
72+
Archived bool `json:"archived"`
73+
Disabled bool `json:"disabled"`
74+
OpenIssuesCount int `json:"open_issues_count"`
75+
License License `json:"license,omitempty"`
76+
Forks int `json:"forks"`
77+
OpenIssues int `json:"open_issues"`
78+
Watchers int `json:"watchers"`
79+
DefaultBranch string `json:"default_branch"`
80+
}
81+
82+
func (repository Repository) String() string {
83+
return fmt.Sprintf("License %s\n", repository.Name)
84+
}

pkg/types/github/server.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package github
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
// Server represents an instance of a Github server.
10+
type Server struct {
11+
URL string
12+
Token string
13+
}
14+
15+
func (server *Server) String() string {
16+
return fmt.Sprintf("Github server: %v", server.URL)
17+
}
18+
19+
// Configure reads the configuration details from map and sets the server instance.
20+
func (server *Server) Configure(details map[string]interface{}) error {
21+
url, ok := details["domain"]
22+
if ok {
23+
serverURL, correctType := url.(string)
24+
if correctType {
25+
server.URL = serverURL
26+
}
27+
} else {
28+
server.URL = "https://api.github.com"
29+
}
30+
token, ok := details["token"]
31+
if ok {
32+
serverToken, correctType := token.(string)
33+
if correctType {
34+
server.Token = serverToken
35+
}
36+
}
37+
return nil
38+
}
39+
40+
// LoadIssues gets issues from Gitlab server.
41+
func (server *Server) LoadIssues() error {
42+
fmt.Printf("Github load issues from %s\n", server.URL)
43+
issuesURL := fmt.Sprintf("%s/issues", server.URL)
44+
request, err := http.NewRequest("GET", issuesURL, nil)
45+
if err != nil {
46+
fmt.Printf("creation of request failed: %v\n", err)
47+
return err
48+
}
49+
request.Header.Set("Content-Type", "application/json")
50+
request.Header.Set("Authorization", fmt.Sprintf("token %s", server.Token))
51+
client := &http.Client{}
52+
response, err := client.Do(request)
53+
if err != nil {
54+
fmt.Printf("The HTTP request failed with error %s\n", err)
55+
return err
56+
}
57+
defer response.Body.Close()
58+
var issueList []Issue
59+
if err := json.NewDecoder(response.Body).Decode(&issueList); err != nil {
60+
fmt.Printf("Response could not parsed as JSON - %v\n", err)
61+
return err
62+
}
63+
fmt.Printf("Issue List:\n%v\n", issueList)
64+
return nil
65+
}
66+
67+
// LoadProjects gets projects from Gitlab server.
68+
func (server *Server) LoadProjects() error {
69+
fmt.Printf("Gitlab load projects from %s\n", server.URL)
70+
projectsURL := fmt.Sprintf("%s/%s/projects?per_page=%d", server.URL, "api/v4", 120)
71+
request, err := http.NewRequest("GET", projectsURL, nil)
72+
if err != nil {
73+
fmt.Printf("creation of request failed: %v\n", err)
74+
return err
75+
}
76+
request.Header.Set("Content-Type", "application/json")
77+
request.Header.Set("PRIVATE-TOKEN", server.Token)
78+
client := &http.Client{}
79+
response, err := client.Do(request)
80+
if err != nil {
81+
fmt.Printf("The HTTP request failed with error %s\n", err)
82+
return err
83+
}
84+
defer response.Body.Close()
85+
var projectList []interface{}
86+
if err := json.NewDecoder(response.Body).Decode(&projectList); err != nil {
87+
fmt.Printf("Response could not parsed as JSON - %v\n", err)
88+
return err
89+
}
90+
fmt.Printf("Project List:\n%v\n", projectList)
91+
return nil
92+
}

pkg/types/github/user.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package github
2+
3+
import "fmt"
4+
5+
// User represents an user in Github.
6+
type User struct {
7+
ID int `json:"id"`
8+
NodeID string `json:"node_id"`
9+
Login string `json:"login"`
10+
Type string `json:"type"`
11+
SiteAdmin bool `json:"site_admin"`
12+
AvatarURL string `json:"avatar_url"`
13+
GravatarID string `json:"gravatar_id"`
14+
URL string `json:"url"`
15+
HTMLURL string `json:"html_url"`
16+
FollowersURL string `json:"followers_url"`
17+
FollowingURL string `json:"following_url"`
18+
GistsURL string `json:"gists_url"`
19+
StarredURL string `json:"starred_url"`
20+
SubscriptionsURL string `json:"subscriptions_url"`
21+
OrganizationsURL string `json:"organizations_url"`
22+
ReposURL string `json:"repos_url"`
23+
EventsURL string `json:"events_url"`
24+
ReceivedEventsURL string `json:"received_events_url"`
25+
}
26+
27+
func (user User) String() string {
28+
return fmt.Sprintf("User %s\n", user.Login)
29+
}

0 commit comments

Comments
 (0)