Class: LgPodPlugin::GithubAPI

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

Class Method Summary collapse

Class Method Details

.get_gitlab_repository_tree(git, sha) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lg_pod_plugin/git/github_api.rb', line 58

def self.get_gitlab_repository_tree(git, sha)
  base_url = LUtils.get_gitlab_base_url git
  if base_url.include?("https://github.com/")
    repo_name = base_url.split("https://github.com/", 0).last
  elsif base_url.include?("[email protected]:")
    repo_name = base_url.split("[email protected]:", 0).last
  else
    repo_name = nil
  end
  return [] unless repo_name
  begin
    uri = URI("https://api.github.com/repos/#{repo_name}/git/trees/#{sha}")
    res = Net::HTTP.get_response(uri)
    if res.body
      json = JSON.parse(res.body)
    else
      json = Hash.new
    end
    return [] unless json && json.is_a?(Hash)
    trees = json["tree"]
    if trees && trees.is_a?(Array)
      return trees
    else
      return []
    end
  rescue
    return []
  end
end

.get_podspec_file_content(git, sha, filename) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lg_pod_plugin/git/github_api.rb', line 90

def self.get_podspec_file_content(git, sha, filename)
  base_url = LUtils.get_gitlab_base_url git
  if base_url.include?("https://github.com/")
    repo_name = base_url.split("https://github.com/", 0).last
  elsif base_url.include?("[email protected]:")
    repo_name = base_url.split("[email protected]:", 0).last
  else
    repo_name = nil
  end
  return nil unless repo_name
  trees = self.get_gitlab_repository_tree git, sha
  return nil if trees.empty?
  request_url = nil
  trees.each do |dict|
    type = dict["type"]
    next if type == "tree"
    path = dict["path"]
    next unless path.include?(".podspec")
    if path == filename
      request_url = dict["url"]
      break
    end
  end
  begin
    uri = URI(request_url)
    res = Net::HTTP.get_response(uri)
    if res.body
      json = JSON.parse(res.body)
    else
      json = nil
    end
    return nil unless json && json.is_a?(Hash)
    content = json["content"]
    return nil unless content && LUtils.is_a_string?(content)
    encoding = json["encoding"] ||= "base64"
    if encoding == "base64"
      content = LUtils.base64_decode(content)
      return content
    else
      return nil
    end
  rescue
    return nil
  end
end

.request_github_refs_heads(git, branch) ⇒ Object

获取 gitlab最新 commit_id



9
10
11
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
47
48
49
50
51
52
53
54
55
# File 'lib/lg_pod_plugin/git/github_api.rb', line 9

def self.request_github_refs_heads(git, branch)
  base_url = LUtils.get_gitlab_base_url git
  if base_url.include?("https://github.com/")
    repo_name = base_url.split("https://github.com/", 0).last
  elsif base_url.include?("[email protected]:")
    repo_name = base_url.split("[email protected]:", 0).last
  else
    repo_name = nil
  end
  return [nil, nil] unless repo_name
  request_url = "https://api.github.com/repos/" + repo_name
  if branch
    request_url += ("/branches")
  else
    request_url += ("/commits/" + "HEAD")
  end
  LgPodPlugin.log_blue "http reqeust: #{request_url}"
  begin
    uri = URI(request_url)
    res = Net::HTTP.get_response(uri)
    case res
    when Net::HTTPSuccess, Net::HTTPRedirection
      json = JSON.parse(res.body)
      if branch
        return [nil, nil] unless json.is_a?(Array)
        json.each do |element|
          name = element["name"]
          if name == branch
            commit = element["commit"]
            sha = commit["sha"]
            return [sha, commit]
          end
        end
        return [nil, branch]
      else
        return [nil, nil] unless json.is_a?(Hash)
        sha = json["sha"]
        return [sha, branch]
      end
    else
      return [nil, nil]
    end
  rescue
    return [nil, nil]
  end

end