Class: GithubOrgReports

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ GithubOrgReports

Returns a new instance of GithubOrgReports.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/github_org_reports.rb', line 26

def initialize(args = {})
  @args = args
  @repos = []
  
  @db = @args[:db]
  raise "No ':db' was given." unless @db
  Baza::Revision.new.init_db(:db => @db, :schema => GithubOrgReports::Dbschema::SCHEMA)
  
  @ob = Baza::ModelHandler.new(
    :db => @db,
    :class_path => "#{File.dirname(__FILE__)}/../models",
    :class_pre => "",
    :module => GithubOrgReports::Models,
    :require_all => true
  )
  @ob.data[:github_org_reports] = self
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



6
7
8
# File 'lib/github_org_reports.rb', line 6

def db
  @db
end

#obObject (readonly)

Returns the value of attribute ob.



6
7
8
# File 'lib/github_org_reports.rb', line 6

def ob
  @ob
end

#reposObject (readonly)

Returns the value of attribute repos.



6
7
8
# File 'lib/github_org_reports.rb', line 6

def repos
  @repos
end

Class Method Details

.const_missing(name) ⇒ Object



8
9
10
11
12
# File 'lib/github_org_reports.rb', line 8

def self.const_missing(name)
  require "#{File.dirname(__FILE__)}/../include/github_org_reports_#{name.to_s.downcase}.rb"
  raise "Still not defined: '#{name}'." unless GithubOrgReports.const_defined?(name)
  return GithubOrgReports.const_get(name)
end

.scan_hash(str) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/github_org_reports.rb', line 49

def self.scan_hash(str)
  str.to_s.scan(/!(\{(.+?)\})!/) do |match|
    json_str = match[0]
    
    
    #Fix missing quotes in 'time' and 'orgs' to make it easier to write.
    json_str.gsub!(/time:\s*([\d+:]+)/, "\"time\": \"\\1\"")
    
    if orgs_match = json_str.match(/orgs:\s*\[(.+?)\]/)
      orgs_str = orgs_match[1]
      orgs_str.gsub!(/\s*(^|\s*,\s*)([A-z_\d]+)/, "\\1\"\\2\"")
      json_str.gsub!(orgs_match[0], "\"orgs\": [#{orgs_str}]")
    end
    
    
    #Parse the JSON and yield it.
    begin
      yield JSON.parse(json_str)
    rescue JSON::ParserError => e
      $stderr.puts e.inspect
      #$stderr.puts e.backtrace
    end
  end
end

.secs_to_time(secs) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/github_org_reports.rb', line 14

def self.secs_to_time(secs)
  return "0:00" if secs <= 0
  
  hours = (secs / 3600).floor
  secs -= hours * 3600
  
  mins = (secs / 60).floor
  secs -= mins * 60
  
  return "#{hours}:#{sprintf("%02d", mins)}"
end

Instance Method Details

#add_repo(repo) ⇒ Object



44
45
46
47
# File 'lib/github_org_reports.rb', line 44

def add_repo(repo)
  raise "Invalid class: '#{repo.class.name}'." unless repo.is_a?(GithubOrgReports::Repo)
  @repos << repo
end

#scanObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/github_org_reports.rb', line 74

def scan
  @repos.each do |repo|
    @cur_repo = repo
    
    gh_args = {
      :user => repo.user,
      :repo => repo.name
    }
    gh_args[:login] = repo.args[:login] unless repo.args[:login].to_s.strip.empty?
    gh_args[:password] = repo.args[:password] unless repo.args[:password].to_s.strip.empty?
    
    gh = ::Github.new(gh_args)
    
    commits = gh.repos.commits.all(gh_args)
    commits.each do |commit_data|
      commit = init_commit_from_data(commit_data)
    end
    
    prs = []
    gh.pull_requests.list(gh_args.merge(:state => "closed")).each do |pr|
      prs << pr
    end
    
    gh.pull_requests.list(gh_args.merge(:state => "open")).each do |pr|
      prs << pr
    end
    
    prs.each do |pr_data|
      text = pr_data.body_text
      
      name = pr_data.user.
      raise "Invalid name: '#{name}' (#{pr_data.to_hash})." if !name.is_a?(String)
      user = @ob.get_or_add(:User, {
        :name => name
      })
      
      
      #puts "PullRequest: #{pr_data.to_hash}"
      github_id = pr_data.id.to_i
      raise "Invalid github-ID: '#{github_id}'." if github_id <= 0
      
      number = pr_data.number.to_i
      raise "Invalid number: '#{number}'." if number <= 0
      
      pr = @ob.get_or_add(:PullRequest, {
        :repository_user => @cur_repo.user,
        :repository_name => @cur_repo.name,
        :github_id => github_id,
        :number => number
      })
      
      #puts "PullRequest: #{pr_data.to_hash}"
      
      pr[:user_id] = user.id
      pr[:title] = pr_data.title
      pr[:text] = pr_data.body_text
      pr[:html] = pr_data.body_html
      pr[:date] = Time.parse(pr_data.created_at)
      
      pr.scan
      
      commits = gh.pull_requests.commits(gh_args.merge(:number => pr_data.number))
      commits.each do |commit_data|
        commit = init_commit_from_data(commit_data)
        commit[:pull_request_id] = pr.id
      end
    end
  end
end

#scan_for_time_and_orgs(str) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/github_org_reports.rb', line 144

def scan_for_time_and_orgs(str)
  res = {:secs => 0, :orgs => [], :orgs_time => {}}
  
  GithubOrgReports.scan_hash(str) do |hash|
    #Parse time.
    if hash["time"] and match_time = hash["time"].to_s.match(/^(\d{1,2}):(\d{1,2})$/)
      secs = 0
      secs += match_time[1].to_i * 3600
      secs += match_time[2].to_i * 60
      
      #Parse organizations.
      if orgs = hash["orgs"]
        orgs = [orgs] if !orgs.is_a?(Array)
        orgs.each do |org_name_short|
          org_name_short_dc = org_name_short.to_s.downcase
          next if org_name_short_dc.strip.empty?
          
          raise "Invalid short-name: '#{org_name_short_dc}'." unless org_name_short_dc.match(/^[A-z\d+_]+$/)
          
          org = self.ob.get_or_add(:Organization, {:name_short => org_name_short_dc})
          
          res[:orgs] << org unless res[:orgs].include?(org)
          
          res[:orgs_time][org.id] = {:secs => 0} unless res[:orgs_time].key?(org.id)
          res[:orgs_time][org.id][:secs] += secs
        end
      else
        res[:secs] += secs
      end
    end
  end
  
  return res
end