Skip to content

Commit

Permalink
Adding a way to specify a github oauth token
Browse files Browse the repository at this point in the history
  • Loading branch information
Depado committed Apr 1, 2016
1 parent e9586ef commit e8817f0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 77 deletions.
75 changes: 18 additions & 57 deletions conf/conf.go
@@ -1,23 +1,20 @@
package conf

import (
"fmt"
"io/ioutil"
"strings"
"time"

"github.com/Depado/gomonit/models"

"gopkg.in/yaml.v2"
)

// Configuration is the type representing the configuration of the service
type Configuration struct {
Listen string
Debug bool
CIURL string
UpdateInterval time.Duration
Services []service
Listen string
Debug bool
CIURL string
UpdateInterval time.Duration
GithubOAuthToken string
Services []service
}

type service struct {
Expand All @@ -32,11 +29,12 @@ type service struct {
}

type unparsed struct {
Listen string `yaml:"listen"`
Debug bool `yaml:"debug"`
CIURL string `yaml:"ci_url"`
UpdateInterval string `yaml:"update_interval"`
Services []service
Listen string `yaml:"listen"`
Debug bool `yaml:"debug"`
CIURL string `yaml:"ci_url"`
UpdateInterval string `yaml:"update_interval"`
GithubOAuthToken string `yaml:"github_oauth_token"`
Services []service
}

// C is the main configuration that is exported
Expand All @@ -58,49 +56,12 @@ func Load(fp string) error {
return err
}
C = Configuration{
Listen: u.Listen,
Debug: u.Debug,
CIURL: u.CIURL,
UpdateInterval: d,
Services: u.Services,
}
return nil
}

// Parse parses the configuration and returns the appropriate Services
func (c Configuration) Parse() error {
models.All = make(models.Services, len(c.Services))
c.CIURL = strings.TrimSuffix(c.CIURL, "/")
for i, s := range c.Services {
if s.CIType != "" && s.CIType != "drone" {
return fmt.Errorf("Unable to use %s as CI, currently only drone is supported.", s.CIType)
}
if s.RepoType != "" && s.RepoType != "github" {
return fmt.Errorf("Unable to use %s as repository, currently only github is supported.", s.RepoType)
}
repoURL := ""
if s.Repo != "" {
repoURL = fmt.Sprintf("https://github.com/%s", s.Repo)
}
buildAPI := ""
buildURL := ""
if s.CIType != "" {
buildAPI = fmt.Sprintf("%s/api/repos/%s/builds", c.CIURL, s.Repo)
buildURL = fmt.Sprintf("%s/%s", c.CIURL, s.Repo)
}
short := strings.TrimPrefix(s.URL, "http://")
short = strings.TrimPrefix(short, "https://")
models.All[i] = &models.Service{
Name: s.Name,
URL: s.URL,
ShortURL: short,
Host: s.Host,
BuildAPI: buildAPI,
BuildURL: buildURL,
RepoURL: repoURL,
Icon: "/static/custom/" + s.Icon,
Own: s.Own,
}
Listen: u.Listen,
Debug: u.Debug,
CIURL: u.CIURL,
UpdateInterval: d,
Services: u.Services,
GithubOAuthToken: u.GithubOAuthToken,
}
return nil
}
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -19,7 +19,7 @@ func main() {
if err = conf.Load("conf.yml"); err != nil {
log.Fatal(err)
}
if err = conf.C.Parse(); err != nil {
if err = models.ParseConf(); err != nil {
log.Fatal(err)
}
// Starting monitoring of services
Expand Down
46 changes: 46 additions & 0 deletions models/parser.go
@@ -0,0 +1,46 @@
package models

import (
"fmt"
"strings"

"github.com/Depado/gomonit/conf"
)

// ParseConf parses the configuration and returns the appropriate Services
func ParseConf() error {
All = make(Services, len(conf.C.Services))
conf.C.CIURL = strings.TrimSuffix(conf.C.CIURL, "/")
for i, s := range conf.C.Services {
if s.CIType != "" && s.CIType != "drone" {
return fmt.Errorf("Unable to use %s as CI, currently only drone is supported.", s.CIType)
}
if s.RepoType != "" && s.RepoType != "github" {
return fmt.Errorf("Unable to use %s as repository, currently only github is supported.", s.RepoType)
}
repoURL := ""
if s.Repo != "" {
repoURL = fmt.Sprintf("https://github.com/%s", s.Repo)
}
buildAPI := ""
buildURL := ""
if s.CIType != "" {
buildAPI = fmt.Sprintf("%s/api/repos/%s/builds", conf.C.CIURL, s.Repo)
buildURL = fmt.Sprintf("%s/%s", conf.C.CIURL, s.Repo)
}
short := strings.TrimPrefix(s.URL, "http://")
short = strings.TrimPrefix(short, "https://")
All[i] = &Service{
Name: s.Name,
URL: s.URL,
ShortURL: short,
Host: s.Host,
BuildAPI: buildAPI,
BuildURL: buildURL,
RepoURL: repoURL,
Icon: "/static/custom/" + s.Icon,
Own: s.Own,
}
}
return nil
}
52 changes: 35 additions & 17 deletions models/service.go
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
"strings"
"time"

"github.com/Depado/gomonit/conf"
)

// All represents all the services
Expand Down Expand Up @@ -37,7 +39,10 @@ type Service struct {
}

// FetchStatus checks if the service is running
func (s *Service) FetchStatus(client *http.Client) {
func (s *Service) FetchStatus() {
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
start := time.Now()
s.Last = start.Format("2006/01/02 15:04:05")
defer func() {
Expand All @@ -59,7 +64,7 @@ func (s *Service) FetchStatus(client *http.Client) {
}

// FetchBuilds checks the last build
func (s *Service) FetchBuilds(client *http.Client) {
func (s *Service) FetchBuilds() {
resp, err := http.Get(s.BuildAPI)
if err != nil {
log.Printf("[%s][ERROR] While requesting build status : %v\n", s.Name, err)
Expand All @@ -79,35 +84,51 @@ func (s *Service) FetchBuilds(client *http.Client) {
}

// FetchCommits fetches the last commits associated to the repository
func (s *Service) FetchCommits(client *http.Client) {
func (s *Service) FetchCommits() {
u := strings.Split(s.RepoURL, "/")
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits", u[len(u)-2], u[len(u)-1])
resp, err := http.Get(url)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("[%s][ERROR][COMMITS] Couldn't create request : %v\n", s.Name, err)
}
if conf.C.GithubOAuthToken != "" {
req.Header.Add("Authorization", "token "+conf.C.GithubOAuthToken)
}
res, err := client.Do(req)
if err != nil {
log.Printf("[%s][ERROR][COMMITS] While requesting : %v\n", s.Name, err)
return
}
defer resp.Body.Close()
defer res.Body.Close()
var all Commits
if err = json.NewDecoder(resp.Body).Decode(&all); err != nil {
if err = json.NewDecoder(res.Body).Decode(&all); err != nil {
log.Printf("[%s][ERROR][COMMITS] Couldn't decode response : %v\n", s.Name, err)
return
}
s.LastCommits = all
}

// FetchRepoInfos fetches the repository information
func (s *Service) FetchRepoInfos(client *http.Client) {
func (s *Service) FetchRepoInfos() {
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)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("[%s][ERROR][REPO] Couldn't create request : %v\n", s.Name, err)
}
if conf.C.GithubOAuthToken != "" {
req.Header.Add("Authorization", "token "+conf.C.GithubOAuthToken)
}
res, err := client.Do(req)
if err != nil {
log.Printf("[%s][ERROR][REPO] While requesting : %v\n", s.Name, err)
return
}
defer resp.Body.Close()
defer res.Body.Close()
var repo GHRepo
if err = json.NewDecoder(resp.Body).Decode(&repo); err != nil {
if err = json.NewDecoder(res.Body).Decode(&repo); err != nil {
log.Printf("[%s][ERROR][REPO] Couldn't decode response : %v\n", s.Name, err)
return
}
Expand All @@ -119,18 +140,15 @@ func (s *Service) FetchRepoInfos(client *http.Client) {

// 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)
s.FetchStatus()
}
if s.BuildAPI != "" {
go s.FetchBuilds(client)
go s.FetchBuilds()
}
if s.RepoURL != "" {
go s.FetchCommits(client)
go s.FetchRepoInfos(client)
go s.FetchCommits()
go s.FetchRepoInfos()
}
}

Expand Down
8 changes: 6 additions & 2 deletions templates/index.tmpl
Expand Up @@ -146,9 +146,9 @@
</span>

</div>
{{ if .RepoURL }}
<br />
<div class="extra content" 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>
Expand All @@ -158,8 +158,12 @@
<a href="{{ .RepoURL }}/watchers" target="_blank">
<button class="ui tiny teal basic compact labeled icon button"><i class="eye icon"></i>{{ .RepoWatchers }}</button>
</a>
</div>
{{ else }}
<button class="ui tiny disabled teal basic compact labeled icon button"><i class="star icon"></i>-</button>
<button class="ui tiny disabled teal basic compact labeled icon button"><i class="fork icon"></i>-</button>
<button class="ui tiny disabled teal basic compact labeled icon button"><i class="eye icon"></i>-</button>
{{ end }}
</div>
</div>
{{ end }}
</div>
Expand Down

0 comments on commit e8817f0

Please sign in to comment.