Skip to content

Commit

Permalink
Merge pull request #8 from tpeetz/github-api
Browse files Browse the repository at this point in the history
Add Github API
  • Loading branch information
Thomas Peetz authored Aug 12, 2020
2 parents 6287254 + ce39c76 commit d68114a
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 9 deletions.
18 changes: 14 additions & 4 deletions pkg/server/gitserver.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package server

import (
"errors"

"github.com/tpeetz/pull-task/pkg/types/github"
"github.com/tpeetz/pull-task/pkg/types/gitlab"
)

Expand All @@ -14,6 +13,15 @@ type GitServer interface {
String() string
}

// UnkownServiceTypeError defines the error condition for unkown service type.
type UnkownServiceTypeError struct {
Service string
}

func (uste *UnkownServiceTypeError) Error() string {
return uste.Service + " not known"
}

// NewGitServer creates instance of GitServer from given configuration.
func NewGitServer(details map[string]interface{}) (GitServer, error) {
//fmt.Printf("details: %v\n", details)
Expand All @@ -23,9 +31,11 @@ func NewGitServer(details map[string]interface{}) (GitServer, error) {
case "gitlab":
server = &gitlab.Server{}
case "github":
return nil, errors.New("service type github unknown")
server = &github.Server{}
case "redmine":
return nil, errors.New("service type redmine unknown")
return nil, &UnkownServiceTypeError{"redmine"}
default:
return nil, &UnkownServiceTypeError{service.(string)}
}
server.Configure(details)
return server, nil
Expand Down
12 changes: 8 additions & 4 deletions pkg/server/gitserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"testing"

"github.com/tpeetz/pull-task/pkg/types/github"
"github.com/tpeetz/pull-task/pkg/types/gitlab"
)

Expand All @@ -15,11 +16,14 @@ var (
githubConfig = map[string]interface{}{
"service": "github",
}
//ghServer = &GitServer{url: "https://github.com", token: "secretToken"}
ghServer = &github.Server{URL: "https://api.github.com"}
redmineConfig = map[string]interface{}{
"service": "redmine",
}
//rmServer = &GitServer{url: "https://redmine.com", token: "secretToken"}
unknownConfig = map[string]interface{}{
"service": "unknown",
}
)

func TestNewGitServer(t *testing.T) {
Expand All @@ -33,9 +37,9 @@ func TestNewGitServer(t *testing.T) {
wantErr bool
}{
{"gitlab", args{gitlabConfig}, glServer, false},
{"github", args{githubConfig}, nil, true},
{"redmine", args{githubConfig}, nil, true},
{"unknown", args{githubConfig}, nil, true},
{"github", args{githubConfig}, ghServer, false},
{"redmine", args{redmineConfig}, nil, true},
{"unknown", args{unknownConfig}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
43 changes: 43 additions & 0 deletions pkg/types/github/issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package github

import "fmt"

// Issue represents issue in Github.
type Issue struct {
ID int `json:"id"`
NodeID string `json:"node_id"`
Title string `json:"title"`
Number int `json:"number"`
User User `json:"user"`
Labels []Label `json:"labels"`
URL string `json:"url"`
RepositoryURL string `json:"repository_url"`
LabelsURL string `json:"labels_url"`
CommentsURL string `json:"comments_url"`
EventsURL string `json:"events_url"`
HTMLURL string `json:"html_url"`
State string `json:"state"`
Locked bool `json:"locked"`
Assignee User `json:"assignee"`
Assignees []User `json:"assignees"`
Milestone Milestone `json:"milestone,omitempty"`
Comments int `json:"comments"`
Created string `json:"created_at,omitempty"`
Updated string `json:"updated_at,omitempty"`
Closed string `json:"closed_at,omitempty"`
AuthorAssociation string `json:"author_association"`
ActiveLockReason string `json:"active_lock_reason,omitempty"`
Repository Repository `json:"repository"`
Body string `json:"body"`
PerformedViaGithubApp string `json:"performed_via_github_app,omitempty"`
}

func (issue Issue) String() string {
//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)
return fmt.Sprintf("Issue %d: %s\n", issue.ID, issue.Title)
}

// Short returns issue details in short.
func (issue Issue) Short() string {
return fmt.Sprintf("Issue %d: %s", issue.ID, issue.Title)
}
18 changes: 18 additions & 0 deletions pkg/types/github/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package github

import "fmt"

// Label represents issue label.
type Label struct {
ID int `json:"id"`
NodeID string `json:"node_id"`
URL string `json:"url"`
Name string `json:"name"`
Color string `json:"color"`
DEfault bool `json:"default"`
Description string `json:"description"`
}

func (label Label) String() string {
return fmt.Sprintf("License %s\n", label.Name)
}
16 changes: 16 additions & 0 deletions pkg/types/github/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package github

import "fmt"

// License holds information about repository license.
type License struct {
Key string `json:"key"`
Name string `json:"name"`
SpdxID string `json:"spdx_id"`
URL string `json:"url"`
NodeID string `json:"node_id"`
}

func (license License) String() string {
return fmt.Sprintf("License %s\n", license.Name)
}
27 changes: 27 additions & 0 deletions pkg/types/github/milestone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package github

import "fmt"

// Milestone represents milestone of a Github project.
type Milestone struct {
ID int `json:"id"`
NodeID string `json:"node_id"`
Number int `json:"number"`
Title string `json:"title"`
Description string `json:"description"`
Creator User `json:"creator"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
LabelsURL string `json:"labels_url"`
OpenIssues int `json:"open_issues"`
ClosedIssues int `json:"closed_issues"`
State string `json:"state"`
Created string `json:"created_at,omitempty"`
Updated string `json:"updated_at,omitempty"`
Due string `json:"due_on,omitempty"`
Closed string `json:"closed_at,omitempty"`
}

func (milestone Milestone) String() string {
return fmt.Sprintf("Issue %d: %s\n", milestone.ID, milestone.Title)
}
84 changes: 84 additions & 0 deletions pkg/types/github/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package github

import "fmt"

// Repository holds repository details.
type Repository struct {
ID int `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
Fullname string `json:"full_name"`
Private bool `json:"private"`
Owner User `json:"owner"`
Description string `json:"description"`
Fork bool `json:"fork"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
ForksURL string `json:"forks_url"`
KeysURL string `json:"keys_url"`
CollaboratorsURL string `json:"collaborators_url"`
TeamsURL string `json:"teams_url"`
HooksURL string `json:"hooks_url"`
IssueEventsURL string `json:"issue_events_url"`
EventsURL string `json:"events_url"`
AssigneesURL string `json:"assignees_url"`
BranchesURL string `json:"branches_url"`
TagsURL string `json:"tags_url"`
BlobsURL string `json:"blobs_url"`
GitTagsURL string `json:"git_tags_url"`
GitRefsURL string `json:"git_refs_url"`
TreesURL string `json:"trees_url"`
StatusesURL string `json:"statuses_url"`
LanguagesURL string `json:"languages_url"`
StargazersURL string `json:"stargazers_url"`
ContributorsURL string `json:"contributors_url"`
SubscribersURL string `json:"subscribers_url"`
SubscriptionURL string `json:"subscription_url"`
CommitsURL string `json:"commits_url"`
GitCommitsURL string `json:"git_commits_url"`
CommentsURL string `json:"comments_url"`
IssueCommentsURL string `json:"issue_comment_url"`
ContentsURL string `json:"contents_url"`
CompareURL string `json:"compare_url"`
MergesURL string `json:"merges_url"`
ArchiveURL string `json:"archive_url"`
DownloadsURL string `json:"downloads_url"`
IssuesURL string `json:"issues_url"`
PullsURL string `json:"pulls_url"`
MilestonesURL string `json:"milestones_url"`
NotificationsURL string `json:"notifications_url"`
LabelsURL string `json:"labels_url"`
ReleasesURL string `json:"releases_url"`
DeploymentsURL string `json:"deployments_url"`
Created string `json:"created_at,omitempty"`
Updated string `json:"updated_at,omitempty"`
Pushed string `json:"pushed_at,omitempty"`
GitURL string `json:"git_url"`
SSHURL string `json:"ssh_url"`
CloneURL string `json:"clone_url"`
SVNURL string `json:"svn_url"`
Homepage string `json:"homepage"`
Size int `json:"size"`
StargazersCount int `json:"stargazers_count"`
WatchersCount int `json:"watchers_count"`
Language string `json:"language"`
HasIssues bool `json:"has_issues"`
HasProjects bool `json:"has_projects"`
HasDownloads bool `json:"has_downloads"`
HasWiki bool `json:"has_wiki"`
HasPages bool `json:"has_pages"`
ForksCount int `json:"forks_count"`
MirrorURL string `json:"mirror_url,omitempty"`
Archived bool `json:"archived"`
Disabled bool `json:"disabled"`
OpenIssuesCount int `json:"open_issues_count"`
License License `json:"license,omitempty"`
Forks int `json:"forks"`
OpenIssues int `json:"open_issues"`
Watchers int `json:"watchers"`
DefaultBranch string `json:"default_branch"`
}

func (repository Repository) String() string {
return fmt.Sprintf("License %s\n", repository.Name)
}
92 changes: 92 additions & 0 deletions pkg/types/github/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package github

import (
"encoding/json"
"fmt"
"net/http"
)

// Server represents an instance of a Github server.
type Server struct {
URL string
Token string
}

func (server *Server) String() string {
return fmt.Sprintf("Github 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://api.github.com"
}
token, ok := details["token"]
if ok {
serverToken, correctType := token.(string)
if correctType {
server.Token = serverToken
}
}
return nil
}

// LoadIssues gets issues from Gitlab server.
func (server *Server) LoadIssues() error {
fmt.Printf("Github load issues from %s\n", server.URL)
issuesURL := fmt.Sprintf("%s/issues", server.URL)
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("Authorization", fmt.Sprintf("token %s", 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 []Issue
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 Gitlab 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 []interface{}
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
}
29 changes: 29 additions & 0 deletions pkg/types/github/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package github

import "fmt"

// User represents an user in Github.
type User struct {
ID int `json:"id"`
NodeID string `json:"node_id"`
Login string `json:"login"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
}

func (user User) String() string {
return fmt.Sprintf("User %s\n", user.Login)
}
Loading

0 comments on commit d68114a

Please sign in to comment.