<commit_before>require 'github_api'

class Issues
  include October::Plugin
  DEFAULT_BASE = :master

  self.prefix = /^!issues? /
  register_help 'issue create title', 'create issue'
  register_help 'issue create title | body', 'create issue with body'
  register_help 'issue create title | milestone: 2', 'create issue for milestone'
  register_help 'issue create title | assign: someone', 'create assigned issue'
  register_help 'issue create title | assign: someone | body | milestone: 3', 'combined approach to create issue'
  register_help 'issue convert number head => base', 'convert issue to pull request'
  register_help 'pull head => base', 'creates a new pull request'
  register_help 'register', 'send you oauth request url'
  register_help 'register login code', 'gives you token and registers that token in db so it can do stuff as you'

  GIT = /[a-z0-9]{7}|[a-z0-9]{40}/
  match /create (.+)$/, method: :create
  match /convert (\d+) (.+?)\s*=>\s*(.+)$/, method: :convert
  match /(?:issue\s+)?#(\d+)/, method: :issue, use_prefix: false
  match /commit ([a-z0-9]{7}|[a-z0-9]{40})(?:[^a-z0-9]|$)/, method: :commit, use_prefix: false
  match /\A!pull (.+?)(?:\s*=>\s*(.+))?$/, method: :pull, use_prefix: false

  match /\A!register(?:\s+(\w+)\s+(\w+))?/, method: :register, use_prefix: false

  def create(m, text)
    issue = Retryable.do { api(m).issues.create(api.user, api.repo, IssueParser.new(text).by(m.user.nick).to_hash) }
    m.reply "created issue #{issue.number} - #{issue.html_url}"
  rescue Github::Error::UnprocessableEntity => e
    m.user.msg "Creation failed: "  + e.message
  end

  def pull(m, head, base = nil)
    base ||= DEFAULT_BASE
    pull = Retryable.do { api.pull_requests.create(api.user, api.repo, :head => head, :base => base, :title => head) }
    m.reply "Simba, there is a new pull request! #{pull.html_url}"
  rescue Github::Error::UnprocessableEntity => e
    m.user.msg "Creation failed: "  + e.message
  end

  def convert(m, number, head, base)
    pull = Retryable.do { api(m).pull_requests.create api.user, api.repo, :issue => number, :head => head, :base => base }
    m.reply "Simba, there is a new pull request! #{pull.html_url}"
  rescue Github::Error::UnprocessableEntity => e
    m.user.msg "Converting failed: "  + e.message
  end

  def issue(m, number)
    if issue = Retryable.do { api.issues.find(api.user, api.repo, number) }
      m.reply "#{issue.html_url} - #{issue.title}"
    end
  rescue Github::Error::UnprocessableEntity => e
    m.user.msg "Issue failed: "  + e.message
  end

  def commit(m, rev)
    commit = Retryable.do { api.git_data.commit api.user, api.repo, rev }
    m.reply "https://github.com/#{api.user}/#{api.repo}/commit/#{commit.sha} by #{commit.author.name}"
  rescue Github::Error::ResourceNotFound
    m.user.msg "sorry, but commit #{rev} was not found"
  end

  def comment(m, number, message)
    Retryable.do { api.issues.comments.create(api.user, api.repo, number, "body" => message)}
  rescue Github::Error::ResourceNotFound
    m.user.msg "sorry, but an error occurred while posting your comment"
  end

  # this method is used by the hudson plugin to figure out if a branch matches a pull request
  def pull_request(branch_name, m = nil)
     Retryable.do { api.pull_requests.list(api.user, api.repo)}.find { |pr| pr["head"]["ref"] == branch_name }<commit_msg>use default base for convert command<commit_after>require 'github_api'

class Issues
  include October::Plugin
  DEFAULT_BASE = :master

  self.prefix = /^!issues? /
  register_help 'issue create title', 'create issue'
  register_help 'issue create title | body', 'create issue with body'
  register_help 'issue create title | milestone: 2', 'create issue for milestone'
  register_help 'issue create title | assign: someone', 'create assigned issue'
  register_help 'issue create title | assign: someone | body | milestone: 3', 'combined approach to create issue'
  register_help 'issue convert number head => base', 'convert issue to pull request'
  register_help 'pull head => base', 'creates a new pull request'
  register_help 'register', 'send you oauth request url'
  register_help 'register login code', 'gives you token and registers that token in db so it can do stuff as you'

  GIT = /[a-z0-9]{7}|[a-z0-9]{40}/
  match /create (.+)$/, method: :create
  match /convert (\d+) (.+?)(?:\s*=>\s*(.+))?$/, method: :convert
  match /(?:issue\s+)?#(\d+)/, method: :issue, use_prefix: false
  match /commit ([a-z0-9]{7}|[a-z0-9]{40})(?:[^a-z0-9]|$)/, method: :commit, use_prefix: false
  match /\A!pull (.+?)(?:\s*=>\s*(.+))?$/, method: :pull, use_prefix: false

  match /\A!register(?:\s+(\w+)\s+(\w+))?/, method: :register, use_prefix: false

  def create(m, text)
    issue = Retryable.do { api(m).issues.create(api.user, api.repo, IssueParser.new(text).by(m.user.nick).to_hash) }
    m.reply "created issue #{issue.number} - #{issue.html_url}"
  rescue Github::Error::UnprocessableEntity => e
    m.user.msg "Creation failed: "  + e.message
  end

  def pull(m, head, base = nil)
    base ||= DEFAULT_BASE
    pull = Retryable.do { api.pull_requests.create(api.user, api.repo, :head => head, :base => base, :title => head) }
    m.reply "Simba, there is a new pull request! #{pull.html_url}"
  rescue Github::Error::UnprocessableEntity => e
    m.user.msg "Creation failed: "  + e.message
  end

  def convert(m, number, head, base = nil)
    base ||= DEFAULT_BASE
    pull = Retryable.do { api(m).pull_requests.create api.user, api.repo, :issue => number, :head => head, :base => base }
    m.reply "Simba, there is a new pull request! #{pull.html_url}"
  rescue Github::Error::UnprocessableEntity => e
    m.user.msg "Converting failed: "  + e.message
  end

  def issue(m, number)
    if issue = Retryable.do { api.issues.find(api.user, api.repo, number) }
      m.reply "#{issue.html_url} - #{issue.title}"
    end
  rescue Github::Error::UnprocessableEntity => e
    m.user.msg "Issue failed: "  + e.message
  end

  def commit(m, rev)
    commit = Retryable.do { api.git_data.commit api.user, api.repo, rev }
    m.reply "https://github.com/#{api.user}/#{api.repo}/commit/#{commit.sha} by #{commit.author.name}"
  rescue Github::Error::ResourceNotFound
    m.user.msg "sorry, but commit #{rev} was not found"
  end

  def comment(m, number, message)
    Retryable.do { api.issues.comments.create(api.user, api.repo, number, "body" => message)}
  rescue Github::Error::ResourceNotFound
    m.user.msg "sorry, but an error occurred while posting your comment"
  end

  # this method is used by the hudson plugin to figure out if a branch matches a pull request
  def pull_request(branch_name, m = nil)
     Retryable.do { api.pull_requests.list(api.user, api.repo)}.find { |pr| pr["head"]["ref"] == branch_name }
{'fraction_non_alphanumeric': 0.16077217801355734, 'fraction_numerical': 0.005305039787798408, 'mean_word_length': 3.8792235801581594, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '303404', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2282, 'n_words': 662}