Skip to content

Commit

Permalink
improved prometheus metrics (#134)
Browse files Browse the repository at this point in the history
* improved prometheus metrics

* change RepoTime to gauge

* fix gitea, only set metrics if ran
  • Loading branch information
cooperspencer authored Feb 10, 2023
1 parent 7bffb8d commit f61574b
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 71 deletions.
6 changes: 4 additions & 2 deletions bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
)

// Get TODO.
func Get(conf *types.Conf) []types.Repo {
func Get(conf *types.Conf) ([]types.Repo, bool) {
ran := false
repos := []types.Repo{}
for _, repo := range conf.Source.BitBucket {
ran = true
client := bitbucket.NewBasicAuth(repo.Username, repo.Password)
if repo.User == "" {
repo.User = repo.Username
Expand Down Expand Up @@ -88,5 +90,5 @@ func Get(conf *types.Conf) []types.Repo {
}
}

return repos
return repos, ran
}
57 changes: 33 additions & 24 deletions gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func getRepoVisibility(visibility string) bool {
}

// Backup TODO.
func Backup(r types.Repo, d types.GenRepo, dry bool) {
func Backup(r types.Repo, d types.GenRepo, dry bool) bool {
orgvisibilty := getOrgVisibility(d.Visibility.Organizations)
repovisibility := getRepoVisibility(d.Visibility.Repositories)
if d.URL == "" {
Expand All @@ -45,15 +45,17 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {

giteaclient, err := gitea.NewClient(d.URL, gitea.SetToken(d.GetToken()))
if err != nil {
log.Fatal().Str("stage", "gitea").Str("url", d.URL).Msg(err.Error())
log.Error().Str("stage", "gitea").Str("url", d.URL).Msg(err.Error())
return false
}

user, _, err := giteaclient.GetMyUserInfo()
if err != nil {
log.Fatal().
log.Error().
Str("stage", "gitea").
Str("url", d.URL).
Msg(err.Error())
return false
}

if d.User == "" && d.CreateOrg {
Expand All @@ -69,25 +71,27 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {
Visibility: orgvisibilty,
})
if err != nil {
log.Fatal().
log.Error().
Str("stage", "gitea").
Str("url", d.URL).
Msg(err.Error())
return false
}
user.ID = org.ID
user.UserName = org.UserName
} else {
log.Fatal().
log.Error().
Str("stage", "gitea").
Str("url", d.URL).
Msg(err.Error())
return false
}
}

}

if dry {
return
return true
}

repo, _, err := giteaclient.GetRepo(user.UserName, r.Name)
Expand Down Expand Up @@ -121,15 +125,15 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {
Str("stage", "gitea").
Str("url", d.URL).
Err(err)
return
return false
}

log.Info().
Str("stage", "gitea").
Str("url", d.URL).
Msgf("mirrored %s to %s", types.Blue(r.Name), d.URL)

return
return true
}
if repo.Mirror {
log.Info().
Expand All @@ -143,20 +147,24 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {
Str("stage", "gitea").
Str("url", d.URL).
Err(err)
return
return false
}

log.Info().
Str("stage", "gitea").
Str("url", d.URL).
Msgf("successfully synced %s.", types.Blue(r.Name))
}

return true
}

// Get TODO.
func Get(conf *types.Conf) []types.Repo {
func Get(conf *types.Conf) ([]types.Repo, bool) {
ran := false
repos := []types.Repo{}
for _, repo := range conf.Source.Gitea {
ran = true
if repo.URL == "" {
repo.URL = "https://gitea.com"
}
Expand Down Expand Up @@ -293,21 +301,22 @@ func Get(conf *types.Conf) []types.Repo {
}
orgopt := gitea.ListOptions{Page: 1, PageSize: 50}
orgs := []*gitea.Organization{}
for {
o, _, err := client.ListUserOrgs(repo.User, gitea.ListOrgsOptions{ListOptions: orgopt})
if err != nil {
log.Fatal().
Str("stage", "gitea").
Str("url", repo.URL).
Msg(err.Error())
}
if len(o) == 0 {
break
if token != "" {
for {
o, _, err := client.ListUserOrgs(repo.User, gitea.ListOrgsOptions{ListOptions: orgopt})
if err != nil {
log.Fatal().
Str("stage", "gitea").
Str("url", repo.URL).
Msg(err.Error())
}
if len(o) == 0 {
break
}
orgs = append(orgs, o...)
orgopt.Page++
}
orgs = append(orgs, o...)
orgopt.Page++
}

orgrepos := []*gitea.Repository{}
for _, org := range orgs {
orgopt.Page = 1
Expand Down Expand Up @@ -392,7 +401,7 @@ func Get(conf *types.Conf) []types.Repo {
}
}

return repos
return repos, ran
}

func getOrgRepos(client *gitea.Client, org *gitea.Organization,
Expand Down
6 changes: 4 additions & 2 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ func addWiki(r github.Repository, repo types.GenRepo, token string) types.Repo {
}

// Get TODO.
func Get(conf *types.Conf) []types.Repo {
func Get(conf *types.Conf) ([]types.Repo, bool) {
ran := false
repos := []types.Repo{}
for _, repo := range conf.Source.Github {
ran = true
if repo.User == "" {
log.Info().
Str("stage", "github").
Expand Down Expand Up @@ -194,5 +196,5 @@ func Get(conf *types.Conf) []types.Repo {
}
}

return repos
return repos, ran
}
21 changes: 14 additions & 7 deletions gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// Backup TODO.
func Backup(r types.Repo, d types.GenRepo, dry bool) {
func Backup(r types.Repo, d types.GenRepo, dry bool) bool {
var gitlabclient *gitlab.Client
token := d.GetToken()
var err error
Expand All @@ -23,10 +23,11 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {
}

if err != nil {
log.Fatal().
log.Error().
Str("stage", "gitlab").
Str("url", d.URL).
Msg(err.Error())
return false
}

log.Info().
Expand All @@ -43,7 +44,8 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {

projects, _, err := gitlabclient.Projects.ListProjects(&opt)
if err != nil {
log.Fatal().Str("stage", "gitlab").Str("url", d.URL).Msg(err.Error())
log.Error().Str("stage", "gitlab").Str("url", d.URL).Msg(err.Error())
return false
}

found := false
Expand All @@ -54,7 +56,7 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {
}

if dry || found {
return
return true
}

if r.Token != "" {
Expand All @@ -72,17 +74,22 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {

_, _, err = gitlabclient.Projects.CreateProject(opts)
if err != nil {
log.Fatal().
log.Error().
Str("stage", "gitlab").
Str("url", d.URL).
Msg(err.Error())
return false
}

return true
}

// Get TODO.
func Get(conf *types.Conf) []types.Repo {
func Get(conf *types.Conf) ([]types.Repo, bool) {
ran := false
repos := []types.Repo{}
for _, repo := range conf.Source.Gitlab {
ran = true
if repo.URL == "" {
repo.URL = "https://gitlab.com"
}
Expand Down Expand Up @@ -360,7 +367,7 @@ func Get(conf *types.Conf) []types.Repo {
}
}

return repos
return repos, ran
}

func activeWiki(r *gitlab.Project, client *gitlab.Client, repo types.GenRepo) bool {
Expand Down
19 changes: 13 additions & 6 deletions gogs/gogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func getRepoVisibility(visibility string) bool {
}

// Backup TODO.
func Backup(r types.Repo, d types.GenRepo, dry bool) {
func Backup(r types.Repo, d types.GenRepo, dry bool) bool {
repovisibility := getRepoVisibility(d.Visibility.Repositories)
log.Info().
Str("stage", "gogs").
Expand All @@ -29,10 +29,11 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {

user, err := gogsclient.GetSelfInfo()
if err != nil {
log.Fatal().
log.Error().
Str("stage", "gogs").
Str("url", d.URL).
Msg(err.Error())
return false
}

if d.User == "" && d.CreateOrg {
Expand Down Expand Up @@ -64,7 +65,7 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {
}

if dry {
return
return true
}

repo, err := gogsclient.GetRepo(user.UserName, r.Name)
Expand Down Expand Up @@ -96,9 +97,10 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {
Str("stage", "gogs").
Str("url", d.URL).
Err(err)
return false
}

return
return true
}

if repo.Mirror {
Expand All @@ -113,19 +115,24 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) {
Str("stage", "gogs").
Str("url", d.URL).
Err(err)
return false
}

log.Info().
Str("stage", "gogs").
Str("url", d.URL).
Msgf("successfully synced %s.", types.Blue(r.Name))
}

return true
}

// Get TODO.
func Get(conf *types.Conf) []types.Repo {
func Get(conf *types.Conf) ([]types.Repo, bool) {
ran := false
repos := []types.Repo{}
for _, repo := range conf.Source.Gogs {
ran = true
if repo.User == "" {
log.Info().
Str("stage", "gogs").
Expand Down Expand Up @@ -331,5 +338,5 @@ func Get(conf *types.Conf) []types.Repo {
}
}

return repos
return repos, ran
}
15 changes: 11 additions & 4 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

// Locally TODO.
func Locally(repo types.Repo, l types.Local, dry bool) {
func Locally(repo types.Repo, l types.Local, dry bool) bool {
date := time.Now()

if l.Structured {
Expand All @@ -42,21 +42,23 @@ func Locally(repo types.Repo, l types.Local, dry bool) {
stat, err := os.Stat(l.Path)
if os.IsNotExist(err) && !dry {
if err := os.MkdirAll(l.Path, 0o777); err != nil {
log.Fatal().
log.Error().
Str("stage", "locally").
Str("path", l.Path).
Msg(err.Error())
return false
}

stat, _ = os.Stat(l.Path)
}

if stat != nil && stat.IsDir() {
if err := os.Chdir(l.Path); err != nil {
log.Fatal().
log.Error().
Str("stage", "locally").
Str("path", l.Path).
Msg(err.Error())
return false
}
}

Expand All @@ -73,7 +75,11 @@ func Locally(repo types.Repo, l types.Local, dry bool) {

auth, err = ssh.NewPublicKeysFromFile("git", repo.Origin.SSHKey, "")
if err != nil {
panic(err)
log.Error().
Str("stage", "locally").
Str("path", l.Path).
Msg(err.Error())
return false
}
case repo.Token != "":
auth = &http.BasicAuth{
Expand Down Expand Up @@ -262,6 +268,7 @@ func Locally(repo types.Repo, l types.Local, dry bool) {

x = 5
}
return true
}

func updateRepository(repoPath string, auth transport.AuthMethod, dry bool, bare bool) error {
Expand Down
Loading

0 comments on commit f61574b

Please sign in to comment.