From 512235002614e8c0eff1fd9961262b8eb3e828c2 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 17 Apr 2018 10:49:32 +0200 Subject: [PATCH] Add make-jenkins filter to find Makefiles with `jenkins` target Signed-off-by: Knut Ahlers --- .gitignore | 1 + filters.go | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc47dbd --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +repo-template diff --git a/filters.go b/filters.go index 2db2002..684eaf1 100644 --- a/filters.go +++ b/filters.go @@ -2,7 +2,9 @@ package main import ( "context" + "strings" + "github.com/Luzifer/go_helpers/str" "github.com/google/go-github/github" log "github.com/sirupsen/logrus" ) @@ -10,10 +12,11 @@ import ( type filterFunc func(*github.Repository) bool var filters = map[string]filterFunc{ - "archived": filterArchived, - "fork": filterFork, - "dockerfile": filterDockerfile, - "public": filterPublic, + "archived": filterArchived, + "dockerfile": filterDockerfile, + "fork": filterFork, + "make-jenkins": filterMakeJenkins, + "public": filterPublic, } func filterArchived(repo *github.Repository) bool { return repo.Archived != nil && *repo.Archived } @@ -33,5 +36,30 @@ func filterDockerfile(repo *github.Repository) bool { return true } -func filterFork(repo *github.Repository) bool { return repo.Fork != nil && *repo.Fork } +func filterFork(repo *github.Repository) bool { return repo.Fork != nil && *repo.Fork } + +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 + } + + if str.StringInSlice("jenkins:", strings.Split(*fc.Content, "\n")) { + return true + } + + return false +} + func filterPublic(repo *github.Repository) bool { return repo.Private != nil && !*repo.Private }