Class: Github

Inherits:
Object
  • Object
show all
Defined in:
lib/newsman/github.rb

Overview

This class represents a useful abstraction over Github API.

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Github

Returns a new instance of Github.



27
28
29
# File 'lib/newsman/github.rb', line 27

def initialize(token)
  @client = Octokit::Client.new(github_token: token)
end

Instance Method Details

#issue_details(issue) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/newsman/github.rb', line 67

def issue_details(issue)
  title = issue.title.to_s
  repository = issue.repository_url.split('/').last
  number = issue.number.to_s
  puts "Found issue in #{repository}:[##{number}] #{title}"
  [title, repository, number]
end

#issues(username, repositories) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/newsman/github.rb', line 41

def issues(username, repositories)
  one_year_ago = Date.today.prev_year.strftime('%Y-%m-%d')
  query = "is:issue is:open author:#{username}"\
    " author:0pdd created:>=#{one_year_ago} #{repositories}"
  puts "Searching issues using the following query: '#{query}'"
  @client.search_issues(query).items.map do |issue|
    parse_issue(issue)
  end.select(&:important?)
end

#parse_issue(issue) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/newsman/github.rb', line 58

def parse_issue(issue)
  title, repository, number = issue_details(issue)
  if issue.user. == '0pdd'
    PddIssue.new(title, issue.body.to_s, repository, number, url: issue.html_url, labels: issue.labels.map(&:name))
  else
    Issue.new(title, issue.body.to_s, repository, number, url: issue.html_url, labels: issue.labels.map(&:name))
  end
end

#parse_pr(pull_request) ⇒ Object



51
52
53
54
55
56
# File 'lib/newsman/github.rb', line 51

def parse_pr(pull_request)
  title = pull_request.title.to_s
  repository = pull_request.repository_url.split('/').last
  puts "Found PR in #{repository}: #{title}"
  PullRequest.new(repository, title, pull_request.body.to_s, url: pull_request.html_url)
end

#pull_requests(username, repositories) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/newsman/github.rb', line 31

def pull_requests(username, repositories)
  query = "is:pr author:#{username} created:>=#{date_one_week_ago(Date.today)} #{repositories}"
  puts "Searching pull requests for #{username}."
  puts 'Newsman uses the following request to GitHub to gather the'\
    " required information about user activity: '#{query}'"
  @client.search_issues(query).items.map do |pull_request|
    parse_pr(pull_request)
  end
end