lib/lita/handlers/github_repo.rb in lita-github-0.0.9 vs lib/lita/handlers/github_repo.rb in lita-github-0.0.10

- old
+ new

@@ -12,10 +12,11 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +require 'uri' require 'lita-github/r' require 'lita-github/config' require 'lita-github/octo' require 'lita-github/org' require 'lita-github/repo' @@ -82,10 +83,18 @@ 'gh repo team add 42 PagerDuty/lita-test' => 'add a team using ID to your repo', 'gh repo team rm everyone PagerDuty/lita-test' => 'remove a team using slug to your repo', 'gh repo team rm 42 PagerDuty/lita-test' => 'remove a team using ID to your repo' } ) + + route( + /#{LitaGithub::R::A_REG}repo\s+update\s+?(?<field>description|homepage)\s+?#{LitaGithub::R::REPO_REGEX}\s+?(?<content>.*)$/, + :repo_update_router, command: true, confirmation: true, + help: { + 'gh repo description PagerDuty/lita-github' => 'get the repo description' + } + ) # rubocop:enable Metrics/LineLength def repo_create(response) return response.reply(t('method_disabled')) if func_disabled?(__method__) @@ -148,14 +157,19 @@ response.reply(reply) end def repo_team_router(response) - action = response.match_data['action'] + action = response.match_data['action'].strip response.reply(send("repo_team_#{action}".to_sym, response)) end + def repo_update_router(response) + field = response.match_data['field'].strip + response.reply(send("repo_update_#{field}".to_sym, response)) + end + private def repo_team_add(response) return t('method_disabled') if func_disabled?(__method__) md = response.match_data @@ -186,9 +200,50 @@ unless repo_has_team?(full_name, team[:id]) return t('repo_team_rm.exists', repo: full_name, team: team[:name]) end remove_team_from_repo(full_name, team) + end + + def repo_update_description(response) + return t('method_disabled') if func_disabled?(__method__) + md = response.match_data + org, repo = repo_match(md) + full_name = rpo(org, repo) + + return t('not_found', org: org, repo: repo) unless repo?(full_name) + + content = md['content'].strip + + begin + resp = octo.edit_repository(full_name, description: content) + rescue StandardError + return t('repo_update_description.boom', repo: full_name) + end + + t('repo_update_description.updated', repo: full_name, desc: resp[:description]) + end + + def repo_update_homepage(response) + return t('method_disabled') if func_disabled?(__method__) + md = response.match_data + org, repo = repo_match(md) + full_name = rpo(org, repo) + + return t('not_found', org: org, repo: repo) unless repo?(full_name) + + regexp = URI::DEFAULT_PARSER.regexp[:ABS_URI] + content = md['content'].strip + + return t('repo_update_homepage.invalid_url', url: content) unless regexp.match(content) + + begin + resp = octo.edit_repository(full_name, homepage: content) + rescue StandardError + return t('repo_update_homepage.boom', repo: full_name) + end + + t('repo_update_homepage.updated', repo: full_name, url: resp[:homepage]) end def command_opts(cmd) o = {} cmd.scan(LitaGithub::R::OPT_REGEX).flatten.compact.each do |opt|