Class: MultiRepo::Service::Github
- Inherits:
-
Object
- Object
- MultiRepo::Service::Github
- Defined in:
- lib/multi_repo/service/github.rb
Instance Attribute Summary collapse
-
#dry_run ⇒ Object
readonly
Returns the value of attribute dry_run.
Class Method Summary collapse
- .api_endpoint ⇒ Object
- .api_endpoint=(endpoint) ⇒ Object
- .api_token ⇒ Object
- .api_token=(token) ⇒ Object
- .client ⇒ Object
- .disabled_workflows(repo_name) ⇒ Object
- .find_milestone_by_title(repo_name, title) ⇒ Object
- .find_team_by_name(org, team) ⇒ Object
- .org_member_names(org) ⇒ Object
- .org_repo_names(org, include_forks: false, include_archived: false) ⇒ Object
- .parse_milestone_date(date) ⇒ Object
- .team_ids_by_name(org) ⇒ Object
- .team_member_names(org, team) ⇒ Object
- .team_members(org, team) ⇒ Object
- .team_names(org) ⇒ Object
- .valid_milestone_date?(date) ⇒ Boolean
Instance Method Summary collapse
- #add_team_membership(org, team, user) ⇒ Object
- #close_milestone(repo_name, milestone_number) ⇒ Object
- #create_label(repo_name, label, color) ⇒ Object
- #create_milestone(repo_name, title, due_on) ⇒ Object
- #create_or_update_repository_secret(repo_name, key, value) ⇒ Object
- #delete_label!(repo_name, label) ⇒ Object
- #edit_repository(repo_name, settings) ⇒ Object
- #enable_workflow(repo_name, workflow_number) ⇒ Object
-
#initialize(dry_run: false) ⇒ Github
constructor
A new instance of Github.
- #protect_branch(repo_name, branch, settings) ⇒ Object
- #remove_collaborator(repo_name, user) ⇒ Object
- #remove_team_membership(org, team, user) ⇒ Object
- #update_label(repo_name, label, color: nil, name: nil) ⇒ Object
- #update_milestone(repo_name, milestone_number, due_on) ⇒ Object
Constructor Details
#initialize(dry_run: false) ⇒ Github
Returns a new instance of Github.
88 89 90 91 92 |
# File 'lib/multi_repo/service/github.rb', line 88 def initialize(dry_run: false) require "octokit" @dry_run = dry_run end |
Instance Attribute Details
#dry_run ⇒ Object (readonly)
Returns the value of attribute dry_run.
86 87 88 |
# File 'lib/multi_repo/service/github.rb', line 86 def dry_run @dry_run end |
Class Method Details
.api_endpoint ⇒ Object
13 14 15 |
# File 'lib/multi_repo/service/github.rb', line 13 def self.api_endpoint @api_endpoint ||= ENV["GITHUB_API_ENDPOINT"] end |
.api_endpoint=(endpoint) ⇒ Object
17 18 19 |
# File 'lib/multi_repo/service/github.rb', line 17 def self.api_endpoint=(endpoint) @api_endpoint = endpoint end |
.api_token ⇒ Object
5 6 7 |
# File 'lib/multi_repo/service/github.rb', line 5 def self.api_token @api_token ||= ENV["GITHUB_API_TOKEN"] end |
.api_token=(token) ⇒ Object
9 10 11 |
# File 'lib/multi_repo/service/github.rb', line 9 def self.api_token=(token) @api_token = token end |
.client ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/multi_repo/service/github.rb', line 21 def self.client @client ||= begin raise "Missing GitHub API Token" if api_token.nil? params = { :api_endpoint => api_endpoint, :access_token => api_token, :auto_paginate => true }.compact require 'octokit' Octokit::Client.new(params) end end |
.disabled_workflows(repo_name) ⇒ Object
82 83 84 |
# File 'lib/multi_repo/service/github.rb', line 82 def self.disabled_workflows(repo_name) client.workflows(repo_name)[:workflows].select { |w| w.state == "disabled_inactivity" } end |
.find_milestone_by_title(repo_name, title) ⇒ Object
52 53 54 |
# File 'lib/multi_repo/service/github.rb', line 52 def self.find_milestone_by_title(repo_name, title) client.list_milestones(repo_name, :state => :all).detect { |m| m.title.casecmp?(title) } end |
.find_team_by_name(org, team) ⇒ Object
60 61 62 |
# File 'lib/multi_repo/service/github.rb', line 60 def self.find_team_by_name(org, team) client.org_teams(org).detect { |t| t.slug == team } end |
.org_member_names(org) ⇒ Object
56 57 58 |
# File 'lib/multi_repo/service/github.rb', line 56 def self.org_member_names(org) client.org_members(org).map(&:login).sort_by(&:downcase) end |
.org_repo_names(org, include_forks: false, include_archived: false) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/multi_repo/service/github.rb', line 36 def self.org_repo_names(org, include_forks: false, include_archived: false) repos = client.list_repositories(org, :type => "sources") repos.reject!(&:fork?) unless include_forks repos.reject!(&:archived?) unless include_archived repos.map(&:full_name).sort end |
.parse_milestone_date(date) ⇒ Object
47 48 49 50 |
# File 'lib/multi_repo/service/github.rb', line 47 def self.parse_milestone_date(date) require "active_support/core_ext/time" ActiveSupport::TimeZone.new('Pacific Time (US & Canada)').parse(date) # LOL GitHub, TimeZones are hard end |
.team_ids_by_name(org) ⇒ Object
73 74 75 76 |
# File 'lib/multi_repo/service/github.rb', line 73 def self.team_ids_by_name(org) @team_ids_by_name ||= {} @team_ids_by_name[org] ||= client.org_teams(org).map { |t| [t.slug, t.id] }.sort.to_h end |
.team_member_names(org, team) ⇒ Object
69 70 71 |
# File 'lib/multi_repo/service/github.rb', line 69 def self.team_member_names(org, team) team_members(org, team).map(&:login).sort_by(&:downcase) end |
.team_members(org, team) ⇒ Object
64 65 66 67 |
# File 'lib/multi_repo/service/github.rb', line 64 def self.team_members(org, team) team_id = find_team_by_name(org, team)&.id team_id ? client.team_members(team_id) : [] end |
.team_names(org) ⇒ Object
78 79 80 |
# File 'lib/multi_repo/service/github.rb', line 78 def self.team_names(org) team_ids_by_name(org).keys end |
.valid_milestone_date?(date) ⇒ Boolean
43 44 45 |
# File 'lib/multi_repo/service/github.rb', line 43 def self.valid_milestone_date?(date) !!parse_milestone_date(date) end |
Instance Method Details
#add_team_membership(org, team, user) ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/multi_repo/service/github.rb', line 173 def add_team_membership(org, team, user) team_id = team_ids_by_name(org)[team] if dry_run puts "** dry-run: github.add_team_membership(#{team_id.inspect}, #{user.inspect})".light_black else client.add_team_membership(team_id, user) end end |
#close_milestone(repo_name, milestone_number) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/multi_repo/service/github.rb', line 157 def close_milestone(repo_name, milestone_number) if dry_run puts "** dry-run: github.update_milestone(#{repo_name.inspect}, #{milestone_number}, :state => 'closed')".light_black else client.update_milestone(repo_name, milestone_number, :state => "closed") end end |
#create_label(repo_name, label, color) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/multi_repo/service/github.rb', line 114 def create_label(repo_name, label, color) if dry_run puts "** dry-run: github.add_label(#{repo_name.inspect}, #{label.inspect}, #{color.inspect})".light_black else client.add_label(repo_name, label, color) end end |
#create_milestone(repo_name, title, due_on) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/multi_repo/service/github.rb', line 141 def create_milestone(repo_name, title, due_on) if dry_run puts "** dry-run: github.create_milestone(#{repo_name.inspect}, #{title.inspect}, :due_on => #{due_on.strftime("%Y-%m-%d").inspect})".light_black else client.create_milestone(repo_name, title, :due_on => due_on) end end |
#create_or_update_repository_secret(repo_name, key, value) ⇒ Object
211 212 213 214 215 216 217 218 219 |
# File 'lib/multi_repo/service/github.rb', line 211 def create_or_update_repository_secret(repo_name, key, value) payload = encode_secret(repo_name, value) if dry_run puts "** dry-run: github.create_or_update_secret(#{repo_name.inspect}, #{key.inspect}, #{payload.inspect})".light_black else client.create_or_update_secret(repo_name, key, payload) end end |
#delete_label!(repo_name, label) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/multi_repo/service/github.rb', line 133 def delete_label!(repo_name, label) if dry_run puts "** dry-run: github.delete_label!(#{repo_name.inspect}, #{label.inspect})".light_black else client.delete_label!(repo_name, label) end end |
#edit_repository(repo_name, settings) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/multi_repo/service/github.rb', line 106 def edit_repository(repo_name, settings) if dry_run puts "** dry-run: github.edit_repository(#{repo_name.inspect}, #{settings.inspect[1..-2]})".light_black else client.edit_repository(repo_name, settings) end end |
#enable_workflow(repo_name, workflow_number) ⇒ Object
201 202 203 204 205 206 207 208 209 |
# File 'lib/multi_repo/service/github.rb', line 201 def enable_workflow(repo_name, workflow_number) command = "repos/#{repo_name}/actions/workflows/#{workflow_number}/enable" if dry_run puts "** dry-run: github.put(#{command.inspect})".light_black else client.put(command) end end |
#protect_branch(repo_name, branch, settings) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/multi_repo/service/github.rb', line 165 def protect_branch(repo_name, branch, settings) if dry_run puts "** dry-run: github.protect_branch(#{repo_name.inspect}, #{branch.inspect}, #{settings.inspect[1..-2]})".light_black else client.protect_branch(repo_name, branch, settings) end end |
#remove_collaborator(repo_name, user) ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/multi_repo/service/github.rb', line 193 def remove_collaborator(repo_name, user) if dry_run puts "** dry-run: github.remove_collaborator(#{repo_name.inspect}, #{user.inspect})".light_black else client.remove_collaborator(repo_name, user) end end |
#remove_team_membership(org, team, user) ⇒ Object
183 184 185 186 187 188 189 190 191 |
# File 'lib/multi_repo/service/github.rb', line 183 def remove_team_membership(org, team, user) team_id = team_ids_by_name(org)[team] if dry_run puts "** dry-run: github.remove_team_membership(#{team_id.inspect}, #{user.inspect})".light_black else client.remove_team_membership(team_id, user) end end |
#update_label(repo_name, label, color: nil, name: nil) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/multi_repo/service/github.rb', line 122 def update_label(repo_name, label, color: nil, name: nil) settings = {:color => color, :name => name}.compact raise ArgumentError, "one of color or name must be passed" if settings.empty? if dry_run puts "** dry-run: github.update_label(#{repo_name.inspect}, #{label.inspect}, #{settings.inspect[1..-2]})".light_black else client.update_label(repo_name, label, settings) end end |
#update_milestone(repo_name, milestone_number, due_on) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/multi_repo/service/github.rb', line 149 def update_milestone(repo_name, milestone_number, due_on) if dry_run puts "** dry-run: github.update_milestone(#{repo_name.inspect}, #{milestone_number}, :due_on => #{due_on.strftime("%Y-%m-%d").inspect})".light_black else client.update_milestone(repo_name, milestone_number, :due_on => due_on) end end |