Skip to content

Instantly share code, notes, and snippets.

@jystewart
Last active September 4, 2015 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jystewart/cc8e349fe7f10f824e7f to your computer and use it in GitHub Desktop.
Save jystewart/cc8e349fe7f10f824e7f to your computer and use it in GitHub Desktop.
Generate YAML file of teams' github repositories suitable for generating pages with github pages
# Generate YAML file of teams' github repositories suitable for generating pages
# with jekyll/github pages and upload it to a github repository.
#
# Requires the 'octokit' gem
#
# Usage
# export TOKEN=my-github-personal-token
# export ORG=my-org
# ruby team-repo-list.rb
# export TOKEN=
# export ORG=
require 'octokit'
require 'yaml'
auth_token = ENV['TOKEN']
organisation = ENV['ORG']
repo = "jystewart/quicktest"
output_path = "mytest.yaml"
if ENV['DEBUG'] && ENV['DEBUG'] == 1
stack = Faraday::RackBuilder.new do |builder|
builder.response :logger
builder.use Octokit::Response::RaiseError
builder.adapter Faraday.default_adapter
end
Octokit.middleware = stack
end
client = Octokit::Client.new(access_token: auth_token)
def get_repos_for_teams(client, organisation)
teams = client.get "orgs/#{organisation}/teams"
output = {}
teams.each do |t|
repos = t.rels[:repositories].get.data
output[t.name] = repos.collect do |r|
{ name: r.name, link: r.html_url, description: r.description }
end
end
output
end
def find_or_create_file(client, repo, path)
client.content repo, path: path
rescue Octokit::NotFound
client.create_content repo, path, "Create empty #{path} file", ""
end
def update_file_with_repo_yaml(client, repo, path, repo_data)
file = find_or_create_file(client, repo, path)
client.update_contents repo, path, "Update list of repos in #{path}",
file.sha, YAML.dump(repo_data)
end
repo_data = get_repos_for_teams(client, organisation)
update_file_with_repo_yaml(client, repo, output_path, repo_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment