Skip to content
Permalink
Browse files

Adding some data about stars, watchers and forks for services that ha…

…ve a github repo associated
  • Loading branch information...
Depado committed Mar 30, 2016
1 parent 0b289f5 commit d41214094aa7450429d7f6da99537c56647f6a03
Showing with 186 additions and 58 deletions.
  1. +100 −0 models/github.go
  2. +48 −32 models/service.go
  3. +38 −26 templates/index.tmpl
@@ -59,3 +59,103 @@ type Commit struct {

// Commits represents multiple commits
type Commits []Commit

// GHRepo represents a single Github repository
type GHRepo struct {
ID int `json:"id"`
Owner GHUser `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
Private bool `json:"private"`
Fork bool `json:"fork"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
ArchiveURL string `json:"archive_url"`
AssigneesURL string `json:"assignees_url"`
BlobsURL string `json:"blobs_url"`
BranchesURL string `json:"branches_url"`
CloneURL string `json:"clone_url"`
CollaboratorsURL string `json:"collaborators_url"`
CommentsURL string `json:"comments_url"`
CommitsURL string `json:"commits_url"`
CompareURL string `json:"compare_url"`
ContentsURL string `json:"contents_url"`
ContributorsURL string `json:"contributors_url"`
DeploymentsURL string `json:"deployments_url"`
DownloadsURL string `json:"downloads_url"`
EventsURL string `json:"events_url"`
ForksURL string `json:"forks_url"`
GitCommitsURL string `json:"git_commits_url"`
GitRefsURL string `json:"git_refs_url"`
GitTagsURL string `json:"git_tags_url"`
GitURL string `json:"git_url"`
HooksURL string `json:"hooks_url"`
IssueCommentURL string `json:"issue_comment_url"`
IssueEventsURL string `json:"issue_events_url"`
IssuesURL string `json:"issues_url"`
KeysURL string `json:"keys_url"`
LabelsURL string `json:"labels_url"`
LanguagesURL string `json:"languages_url"`
MergesURL string `json:"merges_url"`
MilestonesURL string `json:"milestones_url"`
MirrorURL string `json:"mirror_url"`
NotificationsURL string `json:"notifications_url"`
PullsURL string `json:"pulls_url"`
ReleasesURL string `json:"releases_url"`
SSHURL string `json:"ssh_url"`
StargazersURL string `json:"stargazers_url"`
StatusesURL string `json:"statuses_url"`
SubscribersURL string `json:"subscribers_url"`
SubscriptionURL string `json:"subscription_url"`
SvnURL string `json:"svn_url"`
TagsURL string `json:"tags_url"`
TeamsURL string `json:"teams_url"`
TreesURL string `json:"trees_url"`
Homepage string `json:"homepage"`
Language interface{} `json:"language"`
ForksCount int `json:"forks_count"`
StargazersCount int `json:"stargazers_count"`
WatchersCount int `json:"watchers_count"`
Size int `json:"size"`
DefaultBranch string `json:"default_branch"`
OpenIssuesCount int `json:"open_issues_count"`
HasIssues bool `json:"has_issues"`
HasWiki bool `json:"has_wiki"`
HasPages bool `json:"has_pages"`
HasDownloads bool `json:"has_downloads"`
PushedAt time.Time `json:"pushed_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Permissions GHPermissions `json:"permissions"`
SubscribersCount int `json:"subscribers_count"`
Organization GHOrganization `json:"organization"`
}

// GHPermissions represents Github permissions
type GHPermissions struct {
Admin bool `json:"admin"`
Push bool `json:"push"`
Pull bool `json:"pull"`
}

// GHOrganization represents a single Github organization
type GHOrganization struct {
Login string `json:"login"`
ID int `json:"id"`
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"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
}
@@ -15,7 +15,11 @@ type Service struct {
Name string `json:"name"`
URL string `json:"url"`
ShortURL string `json:"short_url"`
Description string `json:"description"`
RepoURL string `json:"repo_url"`
RepoStars int `json:"repo_stars"`
RepoForks int `json:"repo_forks"`
RepoWatchers int `json:"repo_watchers"`
Host string `json:"host"`
BuildAPI string `json:"build_api"`
BuildURL string `json:"build_url"`
@@ -29,8 +33,8 @@ type Service struct {
Own bool `json:"own"`
}

// CheckStatus checks if the service is running
func (s *Service) CheckStatus(client *http.Client) {
// FetchStatus checks if the service is running
func (s *Service) FetchStatus(client *http.Client) {
start := time.Now()
s.Last = start.Format("2006/01/02 15:04:05")
defer func() {
@@ -51,14 +55,9 @@ func (s *Service) CheckStatus(client *http.Client) {
s.Status = resp.StatusCode
}

// CheckBuild checks the last build
func (s *Service) CheckBuild(client *http.Client) {
req, err := http.NewRequest("GET", s.BuildAPI, nil)
if err != nil {
log.Printf("[%s][ERROR] While building request for build : %v\n", s.Name, err)
return
}
resp, err := client.Do(req)
// FetchBuilds checks the last build
func (s *Service) FetchBuilds(client *http.Client) {
resp, err := http.Get(s.BuildAPI)
if err != nil {
log.Printf("[%s][ERROR] While requesting build status : %v\n", s.Name, err)
return
@@ -76,32 +75,11 @@ func (s *Service) CheckBuild(client *http.Client) {
s.LastBuilds = pall
}

// Check updates the status of the Host
func (s *Service) Check() {
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
if s.URL != "" {
go s.CheckStatus(client)
}
if s.BuildAPI != "" {
go s.CheckBuild(client)
}
if s.RepoURL != "" {
go s.FetchCommits(client)
}
}

// FetchCommits fetches the last commits associated to the repository
func (s *Service) FetchCommits(client *http.Client) {
u := strings.Split(s.RepoURL, "/")
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits", u[len(u)-2], u[len(u)-1])
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("[%s][ERROR][COMMITS] While building request : %v\n", s.Name, err)
return
}
resp, err := client.Do(req)
resp, err := http.Get(url)
if err != nil {
log.Printf("[%s][ERROR][COMMITS] While requesting : %v\n", s.Name, err)
return
@@ -115,6 +93,44 @@ func (s *Service) FetchCommits(client *http.Client) {
s.LastCommits = all
}

// FetchRepoInfos fetches the repository information
func (s *Service) FetchRepoInfos(client *http.Client) {
u := strings.Split(s.RepoURL, "/")
url := fmt.Sprintf("https://api.github.com/repos/%s/%s", u[len(u)-2], u[len(u)-1])
resp, err := http.Get(url)
if err != nil {
log.Printf("[%s][ERROR][REPO] While requesting : %v\n", s.Name, err)
return
}
defer resp.Body.Close()
var repo GHRepo
if err = json.NewDecoder(resp.Body).Decode(&repo); err != nil {
log.Printf("[%s][ERROR][REPO] Couldn't decode response : %v\n", s.Name, err)
return
}
s.RepoStars = repo.StargazersCount
s.RepoForks = repo.ForksCount
s.RepoWatchers = repo.SubscribersCount
s.Description = repo.Description
}

// Check updates the status of the Host
func (s *Service) Check() {
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
if s.URL != "" {
s.FetchStatus(client)
}
if s.BuildAPI != "" {
go s.FetchBuilds(client)
}
if s.RepoURL != "" {
go s.FetchCommits(client)
go s.FetchRepoInfos(client)
}
}

// Services represents a list of services
type Services []*Service

@@ -28,18 +28,17 @@
<img src="/static/custom/drone.svg" alt="drone"><br />
<a href="{{ .BuildURL }}" target="_blank">{{ .Name }}</a>
<div class="sub header">Last Builds</div>
</h2>
{{ $buildurl := .BuildURL }}
</h2> {{ $buildurl := .BuildURL }}
<div class="ui feed">
{{ range $i, $e := .LastBuilds }}
<div class="event">
<div class="label">
<img src="{{ if ne .AuthorAvatar "" }}{{ .AuthorAvatar }}{{ else }}/static/img/anon.jpg{{ end }}" alt="avatar">
<img src="{{ if ne .AuthorAvatar " " }}{{ .AuthorAvatar }}{{ else }}/static/img/anon.jpg{{ end }}" alt="avatar">
</div>
<div class="content">
<div class="summary">
<span>{{ .Author }} triggered</span>
<a class="user" href="{{ $buildurl }}{{ .Number }}" target="_blank">
<a class="user" href="{{ $buildurl }}/{{ .Number }}" target="_blank">
Build #{{ .Number}}
{{ if eq .Status "success" }}
<span class="right floated" style="color:#21BA45;"><i class="check icon"></i> </span>
@@ -50,19 +49,20 @@
</a>
</div>
<div class="meta">
<i class="clock icon"></i> {{ .StartedAt }}<br />
<i class="fork icon" style="margin-left: 0;"></i> {{ .Event }} on {{ .Branch }} branch<br />
<i class="refresh icon" style="margin-left: 0;"></i> {{ .Duration }}<br />
<i class="clock icon"></i> {{ .StartedAt }}
<br />
<i class="fork icon" style="margin-left: 0;"></i> {{ .Event }} on {{ .Branch }} branch
<br />
<i class="refresh icon" style="margin-left: 0;"></i> {{ .Duration }}
<br />
<a href="{{ .LinkURL }}" target="_blank" style="margin-left: 0;"><i class="chat icon"></i> {{ .Message }}</a>
</div>
</div>
</div>
{{ end }}
</div>
</div>
{{ end }}
{{ end }}
{{ range $index, $element := .all }} {{ if .RepoURL }}
{{ end }} {{ end }} {{ range $index, $element := .all }} {{ if .RepoURL }}
<div class="ui left wide sidebar vertical menu" id="sidebar-commits-{{ $index }}">
<h2 class="ui center aligned icon header">
<i class="github alternate icon"></i>
@@ -74,15 +74,17 @@
{{ range $i, $e := .LastCommits }}
<div class="event">
<div class="label">
<img src="{{ if ne .Author.AvatarURL "" }}{{ .Author.AvatarURL }}{{ else }}/static/img/anon.jpg{{ end }}" alt="avatar">
<img src="{{ if ne .Author.AvatarURL " " }}{{ .Author.AvatarURL }}{{ else }}/static/img/anon.jpg{{ end }}" alt="avatar">
</div>
<div class="content">
<div class="summary">
<a class="user" href="{{ .Author.HTMLURL }}" target="_blank">{{ .Commit.Author.Name }}</a> authored
</div>
<div class="meta">
<i class="clock icon"></i> {{ .Commit.Author.Date }}<br />
<i class="comments icon" style="margin-left: 0;"></i> {{ .Commit.CommentCount }} Comments<br />
<i class="clock icon"></i> {{ .Commit.Author.Date }}
<br />
<i class="comments icon" style="margin-left: 0;"></i> {{ .Commit.CommentCount }} Comments
<br />
<a href="{{ .HTMLURL }}" target="_blank" style="margin-left: 0;"><i class="chat icon"></i> {{ .Commit.Message }}</a>
</div>
</div>
@@ -105,22 +107,32 @@
<div class="meta">
<a href="{{ .URL }}">{{ if eq .ShortURL "" }}-{{ else }}{{ .ShortURL }}{{ end }}</a>
</div>
<div class="description" style="text-align: center;">
{{ if .RepoURL }}
<a href="{{ .RepoURL }}/stargazers" target="_blank">
<button class="ui tiny teal basic compact labeled icon button"><i class="star icon"></i>{{ .RepoStars }}</button>
</a>
<a href="{{ .RepoURL }}/network" target="_blank">
<button class="ui tiny teal basic compact labeled icon button"><i class="fork icon"></i>{{ .RepoForks }}</button>
</a>
<a href="{{ .RepoURL }}/watchers" target="_blank">
<button class="ui tiny teal basic compact labeled icon button"><i class="eye icon"></i>{{ .RepoWatchers }}</button>
</a>
{{ end }}
</div>
</div>
<div class="extra content">
<i class="setting icon"></i>{{ if .RespTime }}{{ .RespTime }}{{ else }}-{{ end }}
<span class="right floated">
{{ if .Host }}{{ .Host }}{{ else }}-{{ end}} <i class="server icon"></i>
</span>
<br />
<i class="wait icon"></i>{{ if .Last }}{{ .Last }}{{ else }}-{{ end }}
{{ if eq .URL ""}}
<span class="right floated">- <i class="help icon"></i></span>
{{ else }}
{{ if eq .Status 200 }}
<i class="wait icon"></i>{{ if .Last }}{{ .Last }}{{ else }}-{{ end }} {{ if eq .URL ""}}
<span class="right floated">- <i class="help icon"></i></span> {{ else }} {{ if eq .Status 200 }}
<span class="right floated" style="color:#21BA45;">{{ .Status }} <i class="check icon"></i></span> {{ else }}
<span class="right floated" style="color:#DB2828;">{{ .Status }} <i class="remove icon"></i></span> {{ end }}{{ end }}
<br /> {{ if .RepoURL }}
<a href="#" style="color:#6A6AFF;" id="commits-{{ $index }}" class="tooltip-up" data-content="View Recent Commits" data-variation="tiny"><i class="github alternate icon"></i> Commits</a> {{ else }}
<a href="#" style="color:#6A6AFF;" id="commits-{{ $index }}" class="tooltip-up" data-content="View Recent Commits" data-variation="tiny"><i class="github alternate icon"></i> Commits</a>{{ else }}
<i class="github alternate icon"></i> No Commits{{ end }}

<span class="right floated">
@@ -136,7 +148,7 @@
{{ else }}
No build <i class="configure icon"></i>
{{ end }}
</span>
</span>
</div>
</div>
{{ end }}
@@ -153,27 +165,27 @@
transition: 'overlay'
});
$('.tooltip-up').popup({
inline : true,
inline: true,
hoverable: true,
position : 'top left',
position: 'top left',
delay: {
show: 100,
hide: 100
}
});
$('.tooltip-up-right').popup({
inline : true,
inline: true,
hoverable: true,
position : 'top right',
position: 'top right',
delay: {
show: 100,
hide: 100
}
});
$('.tooltip-down').popup({
inline : true,
inline: true,
hoverable: true,
position : 'bottom left',
position: 'bottom left',
delay: {
show: 100,
hide: 100

0 comments on commit d412140

Please sign in to comment.
You can’t perform that action at this time.