From 4c17295f8c0886f2a666abae01aabffebdd5302d Mon Sep 17 00:00:00 2001 From: Louis Heath Date: Mon, 24 Jun 2024 09:40:34 +0100 Subject: [PATCH] GitHub source - Do not error on empty repository (#125) The Catalog Importer's GitHub "Source" is able to read from all repositories of an organisation, searching each repo's tree for files matching specified file path patterns. If a repository contains no matching files, no error is thrown. If a repository is empty, it makes sense for the same optimistic behaviour to be followed. Though GitHub's `Response` type embeds the HTTP Response, we can't rely on the 409 alone (The [API docs](https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28) describe 409 broadly as "conflict"), so I've opted to string match Issue #68 --- source/source_github.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/source/source_github.go b/source/source_github.go index 80550d8..6db0de0 100644 --- a/source/source_github.go +++ b/source/source_github.go @@ -139,6 +139,11 @@ func (s SourceGitHub) Load(ctx context.Context, logger kitlog.Logger) ([]*Source "owner", target.Owner, "repo", target.Repo, "ref", target.Ref) tree, _, err := client.Git.GetTree(ctx, target.Owner, target.Repo, target.Ref, true) if err != nil { + if repositoryEmpty(err) { + logger.Log("msg", "GitHub repository is empty, skipping", + "owner", target.Owner, "repo", target.Repo, "ref", target.Ref) + return nil + } return errors.Wrap(err, fmt.Sprintf("getting tree for '%s/%s' at ref %s", target.Owner, target.Repo, target.Ref)) } @@ -222,3 +227,10 @@ func (s SourceGitHub) Load(ctx context.Context, logger kitlog.Logger) ([]*Source return entries, nil } + +func repositoryEmpty(err error) bool { + if err == nil { + return false + } + return strings.Contains(err.Error(), "409 Git Repository is empty") +}