Class: Github::PullRequest

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/github/pull_request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actorObject

Returns the value of attribute actor.



6
7
8
# File 'app/models/github/pull_request.rb', line 6

def actor
  @actor
end

Class Method Details

._fetch_issues_for!(repo) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'app/models/github/pull_request.rb', line 69

def _fetch_issues_for!(repo)
  if repo.end_with? "/*"
    Houston.github.org_issues(repo[0...-2], filter: "all", state: "open")
  else
    Houston.github.issues(repo, filter: "all", state: "open")
  end
rescue Octokit::NotFound
  []
end

._repo_name_from_url(url) ⇒ Object



65
66
67
# File 'app/models/github/pull_request.rb', line 65

def _repo_name_from_url(url)
  url[/\Agit@github\.com:(.*)\.git\Z/, 1] || url[/\Agit:\/\/github.com\/(.*)\.git\Z/, 1]
end

.close!(github_pr, options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'app/models/github/pull_request.rb', line 109

def close!(github_pr, options={})
  pr = find_by(
    repo: github_pr["base"]["repo"]["name"],
    number: github_pr["number"])
  return unless pr

  pr.actor = options[:as]
  pr.destroy
end

.fetch!(projects = Project.unretired) ⇒ Object

Makes X + Y requests to GitHub where X is the number of projects in Houston on GitHub and Y is the number of pull requests for those projects

We could group repos by their owner and fetch org_issues but that will only work for organizations, not personal accounts.

This method can chomp through your rate limit rather quickly. Also, on my computer it took 19 seconds to fetch 39 pull requests from 52 repos.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/github/pull_request.rb', line 35

def fetch!(projects = Project.unretired)
  repos = projects
    .where("extended_attributes->'git_location' LIKE '%github.com%'")
    .pluck("extended_attributes->'git_location'")
    .map { |url| _repo_name_from_url(url) }
    .compact

  Houston.benchmark "Fetching pull requests" do
    requests = 0
    issues = repos.flat_map do |repo|
      _fetch_issues_for!(repo).tap do |results|
        requests += 1 + (results.length / 30)
      end
    end

    pulls = issues
      .select { |issue| !issue.pull_request.nil? }
      .map { |issue|
        requests += 1
        repo = issue.pull_request.url[/https:\/\/api.github.com\/repos\/(.*)\/pulls\/\d+/, 1]
        Houston.github.pull_request(repo, issue.number)
          .to_h
          .merge(labels: issue.labels)
          .with_indifferent_access }

    Rails.logger.info "[pulls] #{requests} requests; #{Houston.github.last_response.headers["x-ratelimit-remaining"]} remaining"
    pulls
  end
end

.labeled(*labels) ⇒ Object



139
140
141
# File 'app/models/github/pull_request.rb', line 139

def labeled(*labels)
  where(["exists (select 1 from jsonb_array_elements(pull_requests.json_labels) as \"label\" where \"label\"->>'name' IN (?))", labels])
end

.sync!(projects = Project.unretired) ⇒ Object



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
# File 'app/models/github/pull_request.rb', line 79

def sync!(projects = Project.unretired)
  expected_pulls = fetch!(projects)
  Houston.benchmark "Syncing pull requests" do
    existing_pulls = all.to_a

    # Delete unexpected pulls
    existing_pulls.each do |existing_pr|
      unless expected_pulls.detect { |expected_pr|
        expected_pr["base"]["repo"]["name"] == existing_pr.repo &&
        expected_pr["number"] == existing_pr.number }
        existing_pr.destroy
      end
    end

    # Create or Update existing pulls
    expected_pulls.map do |expected_pr|
      existing_pr = existing_pulls.detect { |existing_pr|
        expected_pr["base"]["repo"]["name"] == existing_pr.repo &&
        expected_pr["number"] == existing_pr.number }

      existing_pr ||= Github::PullRequest.new
      existing_pr.merge_attributes(expected_pr)
      unless existing_pr.save
        Rails.logger.warn "\e[31m[pulls] Invalid PR: #{existing_pr.errors.full_messages.join("; ")}\e[0m"
      end
      existing_pr
    end
  end
end

.upsert(github_pr) ⇒ Object



132
133
134
135
136
137
# File 'app/models/github/pull_request.rb', line 132

def upsert(github_pr)
  Github::PullRequest.find_or_initialize_by(
    repo: github_pr["base"]["repo"]["name"],
    number: github_pr["number"])
    .merge_attributes(github_pr)
end

.upsert!(github_pr, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/github/pull_request.rb', line 119

def upsert!(github_pr, options={})
  retry_count ||= 0
  upsert(github_pr).tap do |pr|
    if pr.valid?
      pr.actor = options[:as]
      pr.save
    end
  end
rescue ActiveRecord::RecordNotUnique
  retry unless (retry_count += 1) > 1
  raise
end

Instance Method Details

#add_label!(label, options = {}) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'app/models/github/pull_request.rb', line 154

def add_label!(label, options={})
  label = label.to_h.stringify_keys.pick("name", "color")

  transaction do
    pr = self.class.lock.find id
    new_labels = pr.json_labels.reject { |l| l["name"] == label["name"] } + [label]
    pr.update_attributes! json_labels: new_labels, actor: options[:as]
  end
end

#labelsObject



150
151
152
# File 'app/models/github/pull_request.rb', line 150

def labels
  json_labels
end

#labels=(value) ⇒ Object



146
147
148
# File 'app/models/github/pull_request.rb', line 146

def labels=(value)
  self.json_labels = value.map { |label| label.to_h.stringify_keys.pick("name", "color") }
end

#merge_attributes(pr) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/models/github/pull_request.rb', line 176

def merge_attributes(pr)
  self.repo = pr["base"]["repo"]["name"] unless repo
  self.number = pr["number"] unless number
  self.username = pr["user"]["login"] unless username
  self.avatar_url = pr["user"]["avatar_url"] unless avatar_url
  self.url = pr["html_url"] unless url
  self.base_sha = pr["base"]["sha"] unless base_sha
  self.base_ref = pr["base"]["ref"] unless base_ref

  self.created_at = pr["created_at"]
  self.title = pr["title"]
  self.body = pr["body"]
  self.head_sha = pr["head"]["sha"]
  self.head_ref = pr["head"]["ref"]
  self.labels = pr["labels"] if pr.key?("labels")

  self
end

#remove_label!(label, options = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'app/models/github/pull_request.rb', line 164

def remove_label!(label, options={})
  label = label.to_h.stringify_keys.pick("name", "color")

  transaction do
    pr = self.class.lock.find id
    new_labels = pr.json_labels.reject { |l| l["name"] == label["name"] }
    pr.update_attributes! json_labels: new_labels, actor: options[:as]
  end
end