Class: LgPodPlugin::GitLabAPI

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

Class Method Summary collapse

Class Method Details

.refresh_gitlab_access_token(host, refresh_token) ⇒ Object

刷新gitlab_token



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
# File 'lib/lg_pod_plugin/gitlab_api.rb', line 80

def self.refresh_gitlab_access_token(host, refresh_token)
  begin
    hash_map = Hash.new
    hash_map["scope"] = "api"
    hash_map["grant_type"] = "refresh_token"
    hash_map["refresh_token"] = refresh_token
    uri = URI("#{host}/oauth/token")
    res = Net::HTTP.post_form(uri, hash_map)
    json = JSON.parse(res.body) if res.body
    return nil unless json.is_a?(Hash)
    error = json["error"]
    if error != nil
      error_description = json["error_description"]
      raise error_description
    end
    access_token = json["access_token"]
    refresh_token = json["refresh_token"]
    expires_in = json["expires_in"] ||= 7200
    created_at = json["created_at"] ||= Time.now.to_i
    user_id = LUserAuthInfo.get_user_id(host)
    user_model = LSqliteDb.shared.(user_id)
    user_model.expires_in = (created_at + expires_in)
    user_model.access_token = access_token
    user_model.refresh_token = refresh_token
    LSqliteDb.shared.(user_model)
    return user_model
  rescue => exception
    LgPodPlugin.log_yellow "刷新 `access_token` 失败, error => #{exception.to_s}"
    return nil
  end
end

.request_gitlab_access_token(host, username, password) ⇒ Object

获取 GitLab access_token



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lg_pod_plugin/gitlab_api.rb', line 12

def self.request_gitlab_access_token(host, username, password)
  begin
    uri = URI("#{host}/oauth/token")
    hash_map = { "grant_type" => "password", "username" => username, "password" => password }
    LgPodPlugin.log_green "开始请求 access_token, url => #{uri.to_s} "
    req = Net::HTTP::Post.new(uri)
    req.set_form_data(hash_map)
    res = Net::HTTP.start(uri.hostname, uri.port) do |http|
      http.open_timeout = 15
      http.read_timeout = 15
      http.request(req)
    end
    case res
    when Net::HTTPSuccess, Net::HTTPRedirection
      json = JSON.parse(res.body)
    else
      LgPodPlugin.log_red "获取 `access_token` 失败, 请检查用户名和密码是否有误!"
      return
    end
    if (json["error"] != nil)
      raise json["error_description"]
    end
    access_token = json["access_token"]
    refresh_token = json["refresh_token"]
    expires_in = json["expires_in"] ||= 7200
    created_at = json["created_at"] ||= Time.now.to_i
    user_id = LUserAuthInfo.get_user_id(host)
    user_model = LUserAuthInfo.new(user_id, username, password, host, access_token, refresh_token, (created_at + expires_in))
    LSqliteDb.shared.(user_model)
    # GitLabAPI.get_user_projects(access_token, host, 1)
    LgPodPlugin.log_green "请求成功: `access_token` => #{access_token}, expires_in => #{expires_in}"
  rescue => exception
    LgPodPlugin.log_red "获取 `access_token` 失败, error => #{exception.to_s}"
  end
end

.request_project_info(host, project_name, access_token, git = nil) ⇒ Object

通过名称搜索项目信息



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/lg_pod_plugin/gitlab_api.rb', line 113

def self.request_project_info(host,project_name, access_token, git = nil )
  begin
    hash_map = Hash.new
    hash_map["search"] = project_name
    hash_map["access_token"] = access_token
    uri = URI("#{host}/api/v4/projects")
    uri.query = URI.encode_www_form(hash_map)
    res = Net::HTTP.get_response(uri)
    array = JSON.parse(res.body) if res.body
    return nil unless array.is_a?(Array)
    array.each do |json|
      path = json["path"] ||= ""
      path_with_namespace = json["path_with_namespace"] ||= ""
      name_with_namespace = (json["name_with_namespace"] ||= "").gsub(/[ ]/, '')
      next unless (name == project_name || path == project_name)
      next unless git.include?(name_with_namespace) || git.include?(path_with_namespace)
      id = json["id"]
      name = json["name"] ||= ""
      web_url = json["web_url"]
      description = json["description"]
      ssh_url_to_repo = json["ssh_url_to_repo"]
      http_url_to_repo = json["http_url_to_repo"]
      project = ProjectModel.new(id, name, description, path, ssh_url_to_repo, http_url_to_repo, web_url, name_with_namespace, path_with_namespace)
      LSqliteDb.shared.insert_project(project)
      return project
    end
  rescue
    return nil
  end
end