-
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 #8 from tpeetz/github-api
Add Github API
- Loading branch information
Showing
10 changed files
with
332 additions
and
9 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,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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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 | ||
} |
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,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) | ||
} |
Oops, something went wrong.