From 89a4b6aafa2cedaaa0197479a72c11fe9a31c9af Mon Sep 17 00:00:00 2001
From: Knut Ahlers <knut@ahlers.me>
Date: Sun, 11 Apr 2021 13:50:32 +0200
Subject: [PATCH] Add generic has-file filter

Signed-off-by: Knut Ahlers <knut@ahlers.me>
---
 filters.go | 16 ++++++++++++++++
 main.go    |  1 +
 2 files changed, 17 insertions(+)

diff --git a/filters.go b/filters.go
index d8a2a12..e14cc96 100644
--- a/filters.go
+++ b/filters.go
@@ -16,6 +16,7 @@ var filters = map[string]filterFunc{
 	"archived":     filterArchived,
 	"dockerfile":   filterDockerfile,
 	"fork":         filterFork,
+	"has-file":     filterHasFile,
 	"make-jenkins": filterMakeJenkins,
 	"public":       filterPublic,
 }
@@ -39,6 +40,21 @@ func filterDockerfile(repo *github.Repository) bool {
 
 func filterFork(repo *github.Repository) bool { return repo.Fork != nil && *repo.Fork }
 
+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
+}
+
 func filterMakeJenkins(repo *github.Repository) bool {
 	ctx := context.Background()
 	fc, _, resp, err := client.Repositories.GetContents(ctx, *repo.Owner.Login, *repo.Name, "Makefile", nil)
diff --git a/main.go b/main.go
index d66d990..50b5417 100644
--- a/main.go
+++ b/main.go
@@ -23,6 +23,7 @@ var (
 	cfg = struct {
 		Blacklist      []string `flag:"blacklist,b" default:"" description:"Repos to ignore even when matched through filters"`
 		ExpandMatches  bool     `flag:"expand-matches" default:"false" description:"Replace matched repos with their full version"`
+		FilterHasFile  string   `flag:"filter-has-file" default:"" description:"Input for 'has-file' filter: Repo needs to contain this file"`
 		Filters        []string `flag:"filter,f" default:"" description:"Filters to match the repos against"`
 		GithubToken    string   `flag:"token" default:"" env:"GITHUB_TOKEN" description:"Token to access Github API"`
 		LogLevel       string   `flag:"log-level" default:"info" description:"Log level for output (debug, info, warn, error, fatal)"`