class RepositoryLister

Constants

PAGE_SIZE

Attributes

organization[R]
regexp[R]

Public Class Methods

new(params) click to toggle source
Calls superclass method GithubGraphQlClient::new
# File lib/repository_lister.rb, line 6
def initialize(params)
  @organization = params.fetch(:organization)
  @regexp = params.fetch(:regexp)
  super(params)
end

Public Instance Methods

repository_names() click to toggle source

Returns a list of repository names which match `regexp`

# File lib/repository_lister.rb, line 13
def repository_names
  list_repos
    .select { |repo| repo["name"] =~ regexp }
    .map { |repo| repo["name"] }
end

Private Instance Methods

get_repos(end_cursor = nil) click to toggle source
# File lib/repository_lister.rb, line 46
def get_repos(end_cursor = nil)
  json = run_query(
    body: repositories_query(end_cursor),
    token: github_token
  )

  JSON.parse(json).dig("data", "organization", "repositories")
end
list_repos() click to toggle source

TODO:

* figure out a way to only fetch cloud-platform-* repos
* de-duplicate the code
* filter out archived repos
* filter out disabled repos
# File lib/repository_lister.rb, line 27
def list_repos
  repos = []
  end_cursor = nil

  data = get_repos(end_cursor)
  repos = repos + data.fetch("nodes")
  next_page = data.dig("pageInfo", "hasNextPage")
  end_cursor = data.dig("pageInfo", "endCursor")

  while next_page do
    data = get_repos(end_cursor)
    repos = repos + data.fetch("nodes")
    next_page = data.dig("pageInfo", "hasNextPage")
    end_cursor = data.dig("pageInfo", "endCursor")
  end

  repos.reject { |r| r.dig("isArchived") || r.dig("isDisabled") }
end
repositories_query(end_cursor) click to toggle source

TODO: it should be possible to exclude disabled/archived repos in this query, but I don't know how to do that yet, so I'm just fetching everything and throwing away the disabled/archived repos later. We should also be able to only fetch repos whose names match the pattern we're interested in, at this stage.

# File lib/repository_lister.rb, line 60
def repositories_query(end_cursor)
  after = end_cursor.nil? ? "" : %[, after: "#{end_cursor}"]
  %[
  {
    organization(login: "#{organization}") {
      repositories(first: #{PAGE_SIZE} #{after}) {
        nodes {
          id
          name
          isLocked
          isArchived
          isDisabled
        }
        pageInfo {
          hasNextPage
          endCursor
        }
      }
    }
  }
  ]
end