Class: Versionbot::Bot

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/versionbot.rb

Constant Summary collapse

@@access_token =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.access_token(token) ⇒ Object



25
26
27
# File 'lib/versionbot.rb', line 25

def self.access_token(token)
  @@access_token = token
end

Instance Method Details

#fetch_data(conn, url) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/versionbot.rb', line 59

def fetch_data(conn, url) 
  resp = conn.get(url)
  status = resp.status
  if status == 200
    resp.body
  else
    nil
  end
end

#fetch_version(group) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/versionbot.rb', line 29

def fetch_version(group)
  unless (@@access_token.is_a? String) && @@access_token.size > 0 
    @@access_token = ENV["VERSIONBOT_TOKEN"]
  end

  conn = Faraday.new("http://code.yiye.ai") do |f|
    f.request :json
    f.response :json
    f.headers = {
      "Content-Type" => "application/json",
      "PRIVATE-TOKEN" => @@access_token
    }
  end
  projects = fetch_data(conn, "/api/v4/groups/#{group}/projects?archived=false")
  if projects.nil?
    return {error_msg: "group not exists or server is broken", data: {}}
  end

  max_ver = "0.0.0"
  ver_map = {}
  projects.each do |prj|
    latest_tag = fetch_data(conn, "/api/v4/projects/#{prj["id"]}/repository/tags?per_page=1")
    unless latest_tag.nil? || latest_tag.size == 0
      max_ver = max_version(max_ver, latest_tag[0]["name"])
      ver_map[prj["path_with_namespace"]] = latest_tag[0]["name"]
    end
  end
  {error_msg: nil, data: {max_version: max_ver, version_map: ver_map}}
end

#max_version(cur, income) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/versionbot.rb', line 69

def max_version(cur, income) 
  if cur.start_with? 'v'
    cur = cur[1..-1]
  end
  if income.start_with? 'v'
    income = income[1..-1]
  end
  cur_major, cur_minor, cur_bugfix = cur.split('.')
  income_major, income_minor, income_bugfix = income.split('.')
  #
  cur_num = cur_major.to_i * 10000 + cur_minor.to_i * 10 + cur_bugfix.to_i
  income_num = income_major.to_i * 10000 + income_minor.to_i * 10 + income_bugfix.to_i

  if cur_num > income_num
    return cur
  else
    return income 
  end
end