Class: GerritApi

Inherits:
Rubiclifier::BaseApi
  • Object
show all
Defined in:
lib/gerrit_api.rb

Class Method Summary collapse

Class Method Details

.account_idObject



77
78
79
# File 'lib/gerrit_api.rb', line 77

def self.
  @account_id ||= Rubiclifier::DB.get_setting("account_id")
end

.all_code_changesObject



33
34
35
36
37
38
39
40
41
# File 'lib/gerrit_api.rb', line 33

def self.all_code_changes
  wrap_with_authentication do
    get("/changes/?S=0&q=is%3Aopen%20owner%3Aself%20-is%3Awip%20-is%3Aignored%20limit%3A25&q=is%3Aopen%20owner%3Aself%20is%3Awip%20limit%3A25&q=is%3Aopen%20-owner%3Aself%20-is%3Awip%20-is%3Aignored%20reviewer%3Aself%20limit%3A25&q=is%3Aclosed%20-is%3Aignored%20%28-is%3Awip%20OR%20owner%3Aself%29%20%28owner%3Aself%20OR%20reviewer%3Aself%29%20-age%3A4w%20limit%3A10&o=DETAILED_ACCOUNTS&o=MESSAGES", {
      headers: {
        "Cookie" => "GerritAccount=#{api_token};"
      }
    })
  end.parsed_response.flat_map { |js| js.map { |j| code_change_from_json(j) } }
end

.api_token_db_keyObject



61
62
63
# File 'lib/gerrit_api.rb', line 61

def self.api_token_db_key
  "api_token"
end

.base_api_url_db_keyObject



65
66
67
# File 'lib/gerrit_api.rb', line 65

def self.base_api_url_db_key
  "base_api_url"
end

.code_change_activity_from_json(code_change, json) ⇒ Object



49
50
51
# File 'lib/gerrit_api.rb', line 49

def self.code_change_activity_from_json(code_change, json)
  CodeChangeActivity.new(json["id"].to_s, json["author"]["name"], json["author"]["_account_id"].to_s == , json["message"], Time.parse(json["date"]), code_change)
end

.code_change_from_json(json) ⇒ Object



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

def self.code_change_from_json(json)
  code_change = CodeChange.new(json["_number"].to_s, json["owner"]["name"], json["project"], json["subject"])
  code_change.code_change_activity = json["messages"].map { |m| code_change_activity_from_json(code_change, m) }
  code_change
end

.code_change_url(code_change) ⇒ Object



57
58
59
# File 'lib/gerrit_api.rb', line 57

def self.code_change_url(code_change)
  "#{base_api_url}/c/#{code_change.id}"
end

.faviconObject



53
54
55
# File 'lib/gerrit_api.rb', line 53

def self.favicon
  "#{base_api_url}/favicon.ico"
end

.invalid_credentials_errorObject



24
25
26
27
28
29
30
31
# File 'lib/gerrit_api.rb', line 24

def self.invalid_credentials_error
  Rubiclifier::Notification.new(
    "Incorrect Credentials",
    "Trying running `code_review_notifier --setup` again."
  ).send
  sleep(120)
  exit
end

.login_and_get_api_tokenObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gerrit_api.rb', line 12

def self.
  res = post("/login/", {
    body: "username=#{username}&password=#{URI.escape(password)}&rememberme=1",
    headers: {
      "Content-Type" => "application/x-www-form-urlencoded",
    },
    follow_redirects: false
  })
  set_cookie_header = res.headers["set-cookie"] || ""
  set_cookie_header.match("GerritAccount=(.*?);")&.to_a&.fetch(1)
end

.passwordObject



73
74
75
# File 'lib/gerrit_api.rb', line 73

def self.password
  @password ||= Rubiclifier::DB.get_setting("password")
end

.usernameObject



69
70
71
# File 'lib/gerrit_api.rb', line 69

def self.username
  @username ||= Rubiclifier::DB.get_setting("username")
end