Skip to content

Commit

Permalink
GitHub source - Do not error on empty repository (#125)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
louisheath authored Jun 24, 2024
1 parent 104aec3 commit 4c17295
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions source/source_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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")
}

0 comments on commit 4c17295

Please sign in to comment.