2018-02-07 09:02:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-04-17 09:31:24 +00:00
|
|
|
"bufio"
|
2018-02-07 09:02:13 +00:00
|
|
|
"context"
|
2018-04-17 09:48:23 +00:00
|
|
|
"encoding/base64"
|
2018-04-17 08:49:32 +00:00
|
|
|
"strings"
|
2018-02-07 09:02:13 +00:00
|
|
|
|
2021-04-11 11:44:58 +00:00
|
|
|
"github.com/google/go-github/v34/github"
|
2018-02-07 09:02:13 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type filterFunc func(*github.Repository) bool
|
|
|
|
|
|
|
|
var filters = map[string]filterFunc{
|
2018-04-17 08:49:32 +00:00
|
|
|
"archived": filterArchived,
|
|
|
|
"dockerfile": filterDockerfile,
|
|
|
|
"fork": filterFork,
|
2021-04-11 11:50:32 +00:00
|
|
|
"has-file": filterHasFile,
|
2018-04-17 08:49:32 +00:00
|
|
|
"make-jenkins": filterMakeJenkins,
|
|
|
|
"public": filterPublic,
|
2018-02-07 09:02:13 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 14:26:47 +00:00
|
|
|
func filterArchived(repo *github.Repository) bool { return repo.Archived != nil && *repo.Archived }
|
|
|
|
|
2018-02-07 09:02:13 +00:00
|
|
|
func filterDockerfile(repo *github.Repository) bool {
|
|
|
|
ctx := context.Background()
|
|
|
|
_, _, resp, err := client.Repositories.GetContents(ctx, *repo.Owner.Login, *repo.Name, "Dockerfile", nil)
|
|
|
|
if err != nil {
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithError(err).Error("Error while looking for Dockerfile")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:49:32 +00:00
|
|
|
func filterFork(repo *github.Repository) bool { return repo.Fork != nil && *repo.Fork }
|
|
|
|
|
2021-04-11 11:50:32 +00:00
|
|
|
func filterHasFile(repo *github.Repository) bool {
|
|
|
|
ctx := context.Background()
|
|
|
|
_, _, resp, err := client.Repositories.GetContents(ctx, *repo.Owner.Login, *repo.Name, cfg.FilterHasFile, nil)
|
|
|
|
if err != nil {
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithError(err).Error("Error while looking for file")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:49:32 +00:00
|
|
|
func filterMakeJenkins(repo *github.Repository) bool {
|
|
|
|
ctx := context.Background()
|
|
|
|
fc, _, resp, err := client.Repositories.GetContents(ctx, *repo.Owner.Login, *repo.Name, "Makefile", nil)
|
|
|
|
if err != nil {
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithError(err).Error("Error while looking for Dockerfile")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if fc.Content == nil {
|
|
|
|
log.Error("File content had no content")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-17 09:48:23 +00:00
|
|
|
scanner := bufio.NewScanner(base64.NewDecoder(base64.StdEncoding, strings.NewReader(*fc.Content)))
|
2018-04-17 09:31:24 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
if strings.HasPrefix(scanner.Text(), "jenkins:") {
|
|
|
|
return true
|
|
|
|
}
|
2018-04-17 08:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-07 09:02:13 +00:00
|
|
|
func filterPublic(repo *github.Repository) bool { return repo.Private != nil && !*repo.Private }
|