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

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



66
67
68
69
70
71
72
73
74
# File 'app/models/github/pull_request.rb', line 66

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!Object



25
26
27
28
29
30
31
32
33
34
# File 'app/models/github/pull_request.rb', line 25

def fetch!
  Houston.benchmark "Fetching pull requests" do
    Houston.github.org_issues(Houston.config.github[:organization], filter: "all", state: "open")
      .select { |issue| !issue.pull_request.nil? }
      .map { |issue| Houston.github.pull_request(issue.repository.full_name, issue.number)
        .to_h
        .merge(labels: issue.labels.map(&:name))
        .with_indifferent_access }
  end
end

.sync!Object



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

def sync!
  expected_pulls = fetch!
  expected_pulls.select! { |pr| pr["base"]["repo"]["name"] == pr["head"]["repo"]["name"] }
  # select only ones where head and base are the same repo
  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)
      existing_pr.save if existing_pr.valid?
      existing_pr
    end
  end
end

.upsert(github_pr) ⇒ Object



89
90
91
92
93
94
# File 'app/models/github/pull_request.rb', line 89

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



76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/github/pull_request.rb', line 76

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



107
108
109
110
111
112
# File 'app/models/github/pull_request.rb', line 107

def add_label!(label, options={})
  transaction do
    pr = self.class.lock.find id
    pr.update_attributes! labels: pr.labels + [label], actor: options[:as]
  end
end

#labelsObject



99
100
101
# File 'app/models/github/pull_request.rb', line 99

def labels
  super.split(/\n/)
end

#labels=(value) ⇒ Object



103
104
105
# File 'app/models/github/pull_request.rb', line 103

def labels=(value)
  super Array(value).uniq.join("\n")
end

#merge_attributes(pr) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/github/pull_request.rb', line 123

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

  self.title = pr["title"]
  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



114
115
116
117
118
119
# File 'app/models/github/pull_request.rb', line 114

def remove_label!(label, options={})
  transaction do
    pr = self.class.lock.find id
    pr.update_attributes! labels: pr.labels - [label], actor: options[:as]
  end
end