Class: Lita::Handlers::GithubOrg

Inherits:
Handler
  • Object
show all
Includes:
LitaGithub::Config, LitaGithub::Filters, LitaGithub::General, LitaGithub::Octo, LitaGithub::Org
Defined in:
lib/lita/handlers/github_org.rb

Overview

GitHub Lita Handler

Constant Summary collapse

KNOWN_PERMS =
%w(pull push admin)

Instance Method Summary collapse

Methods included from LitaGithub::Filters

#func_disabled?

Methods included from LitaGithub::Org

#organization, #sort_by_name, #team?, #team_id, #team_id_by_slug

Methods included from LitaGithub::Octo

#access_token, #octo, #setup_octo

Methods included from LitaGithub::Config

#config

Methods included from LitaGithub::General

#opts_parse, #symbolize_opt_key, #to_i_if_numeric

Instance Method Details

#org_eject_user(response) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/lita/handlers/github_org.rb', line 243

def org_eject_user(response)
  return response.reply(t('method_disabled')) if func_disabled?(__method__)
  md = response.match_data
  org, username = [organization(md['org'].strip), md['username'].strip]

  begin
    user_id = octo.user(username)[:id]
  rescue Octokit::NotFound
    return response.reply(t('user_not_found', n: username))
  end

  return response.reply(t('nope')) if octo.user[:id] == user_id

  begin
    resp = octo.remove_organization_member(org, username)
  rescue StandardError => e
    return response.reply(t('boom', m: e.message))
  end

  if resp
    response.reply(t('org_eject_user.ejected', user: username, org: org))
  else
    response.reply(t('org_eject_user.failed'))
  end
end

#org_team_add(response) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/lita/handlers/github_org.rb', line 133

def org_team_add(response)
  return response.reply(t('method_disabled')) if func_disabled?(__method__)

  opts = opts_parse(response.message.body)
  vo = validate_team_add_opts(opts)
  return response.reply(vo[:msg]) unless vo[:success]

  md = response.match_data
  org, perm, name = [organization(md['org'].strip), opts[:perms].strip.downcase, opts[:name]]

  return response.reply(
    t('org_team_add.perm_not_permitted', perms: config.org_team_add_allowed_perms.join(', '))
  ) unless permission_allowed?(perm)

  begin
    resp = octo.create_team(org, name: name, permission: perm)
  rescue Octokit::NotFound
    return response.reply(t('org_not_found', org: org))
  end

  response.reply(t('org_team_add.created_team', resp.to_h))
end

#org_team_rm(response) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/lita/handlers/github_org.rb', line 156

def org_team_rm(response)
  return response.reply(t('method_disabled')) if func_disabled?(__method__)

  md = response.match_data
  org, team = [organization(md['org'].strip), md['team'].strip]

  t_id = team_id(team, org)
  t = team?(t_id)

  return response.reply(t('team_not_found', team: team)) unless t

  if octo.delete_team(t_id)
    response.reply(t('org_team_rm.pass', t.to_h))
  else
    response.reply(t('org_team_rm.fail', t.to_h))
  end
end

#org_teams_list(response) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/lita/handlers/github_org.rb', line 111

def org_teams_list(response)
  md = response.match_data
  org = md[:org].nil? ? config.default_org : organization(md[:org].strip)

  begin
    teams = octo.organization_teams(org)
  rescue Octokit::NotFound
    return response.reply(t('org_not_found', org: org))
  end

  tl = teams.length

  o = teams.shift

  reply = t('org_teams_list.header', org: org, num_teams: tl)
  reply << t('org_teams_list.team', o.to_h)

  sort_by_name(teams).each { |team| reply << t('org_teams_list.team', team.to_h) }

  response.reply(reply)
end

#org_user_add(response) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/lita/handlers/github_org.rb', line 176

def org_user_add(response)
  return response.reply(t('method_disabled')) if func_disabled?(__method__)

  md = response.match_data
  org, team, username = [organization(md['org'].strip), md['team'].strip, md['username']]

  begin
    user_id = octo.user(username)[:id]
  rescue Octokit::NotFound
    return response.reply(t('user_not_found', n: username))
  end

  return response.reply(t('nope')) if octo.user[:id] == user_id

  begin
    team_obj = octo.team(team_id(team, org))
  rescue Octokit::NotFound
    return response.reply(t('team_not_found', team: team))
  end

  begin
    resp = octo.add_team_membership(team_obj[:id], username)
  rescue StandardError => e
    return response.reply(t('boom', m: e.message))
  end

  if resp
    response.reply(t('org_user_add.added', u: username, o: org, t: team_obj[:name], s: team_obj[:slug]))
  else
    response.reply(t('org_user_add.failed', t: team_obj[:name]))
  end
end

#org_user_rm(response) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/lita/handlers/github_org.rb', line 209

def org_user_rm(response)
  return response.reply(t('method_disabled')) if func_disabled?(__method__)
  md = response.match_data
  org, team, username = [organization(md['org'].strip), md['team'].strip, md['username']]

  begin
    user_id = octo.user(username)[:id]
  rescue Octokit::NotFound
    return response.reply(t('user_not_found', n: username))
  end

  return response.reply(t('nope')) if octo.user[:id] == user_id

  begin
    team = octo.team(team_id(team, org))
  rescue Octokit::NotFound
    return response.reply(t('team_not_found', team: team))
  end

  begin
    resp = octo.remove_team_member(team[:id], username)
  rescue StandardError => e
    return response.reply(t('boom', m: e.message))
  end

  if resp
    response.reply(t('org_user_rm.removed', u: username, o: org, t: team[:name], s: team[:slug]))
  else
    response.reply(t('org_user_rm.failed', t: team[:name]))
  end
end