Module: Cid::Helpers::Github

Extended by:
Memoist
Included in:
Publish
Defined in:
lib/cid/helpers/github.rb

Constant Summary collapse

GITHUB_REPO_REGEX =
/github.com[:\/]([^\/]*)\/([^\.]*)/

Instance Method Summary collapse

Instance Method Details

#add_blob_to_tree(sha, filename) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/cid/helpers/github.rb', line 67

def add_blob_to_tree(sha, filename)
  tree = tree default_branch
  new_tree = github.git_data.trees.create user, repo, "base_tree" => tree['sha'], "tree" => [
    "path" => filename,
    "mode" => "100644",
    "type" => "blob",
    "sha" => sha
  ]
  new_tree['sha']
end

#blob_content(sha) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/cid/helpers/github.rb', line 51

def blob_content(sha)
  blob = github.git_data.blobs.get user, repo, sha
  if blob['encoding'] == 'base64'
    Base64.decode64(blob['content'])
  else
    blob['content']
  end
end

#blob_shas(branch, path) ⇒ Object



45
46
47
48
# File 'lib/cid/helpers/github.rb', line 45

def blob_shas(branch, path)
  tree = tree branch
  Hash[tree['tree'].select{|x| x['path'] =~ /^#{path}$/ && x['type'] == 'blob'}.map{|x| [x.path, x.sha]}]
end

#commit(sha) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/cid/helpers/github.rb', line 78

def commit(sha)
  parent = latest_commit(default_branch)
  commit = github.git_data.commits.create user, repo, "message" => "Updated datapackage.json [ci skip]",
            "parents" => [parent],
            "tree" => sha
  commit['sha']
end

#create_blob(content) ⇒ Object



62
63
64
65
# File 'lib/cid/helpers/github.rb', line 62

def create_blob(content)
  blob = github.git_data.blobs.create user, repo, "content" => content, "encoding" => "utf-8"
  blob['sha']
end

#default_branchObject



28
29
30
31
# File 'lib/cid/helpers/github.rb', line 28

def default_branch
  repository = github.repos.get user, repo
  repository.default_branch
end

#githubObject



11
12
13
# File 'lib/cid/helpers/github.rb', line 11

def github
  Github.new oauth_token: @oauth_token
end

#latest_commit(branch_name) ⇒ Object



34
35
36
37
# File 'lib/cid/helpers/github.rb', line 34

def latest_commit(branch_name)
  branch_data = github.repos.branch user, repo, branch_name
  branch_data['commit']['sha']
end

#push(sha) ⇒ Object



86
87
88
89
# File 'lib/cid/helpers/github.rb', line 86

def push(sha)
  branch = github.git_data.references.update user, repo, "heads/#{default_branch}", "sha" => sha
  branch['ref']
end

#repoObject



22
23
24
25
# File 'lib/cid/helpers/github.rb', line 22

def repo
  match = @git_url.match GITHUB_REPO_REGEX
  match ? match[2] : nil
end

#tree(branch) ⇒ Object



40
41
42
# File 'lib/cid/helpers/github.rb', line 40

def tree(branch)
  github.git_data.trees.get user, repo, branch
end

#userObject



16
17
18
19
# File 'lib/cid/helpers/github.rb', line 16

def user
  match = @git_url.match GITHUB_REPO_REGEX
  match ? match[1] : nil
end