forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreferences.rb
More file actions
141 lines (123 loc) · 4.53 KB
/
Copy pathreferences.rb
File metadata and controls
141 lines (123 loc) · 4.53 KB
1
2
3
4
5
6
7
8
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
56
57
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
87
88
89
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
135
136
137
138
139
140
141
# encoding: utf-8
module Github
class GitData::References < API
VALID_REF_PARAM_NAMES = %w[ ref sha force ].freeze
VALID_REF_PARAM_VALUES = {
'ref' => %r{^refs\/\w+(\/\w+)*} # test fully qualified reference
}
# Creates new GitData::References API
def initialize(options = {})
super(options)
end
# Get all references
#
# This will return an array of all the references on the system,
# including things like notes and stashes if they exist on the server.
# Anything in the namespace, not just <tt>heads</tt> and <tt>tags</tt>,
# though that would be the most common.
#
# = Examples
# github = Github.new
# github.git_data.references.list 'user-name', 'repo-name'
#
# github.git_data.references.list 'user-name', 'repo-name', ref:'tags'
#
def list(user_name, repo_name, params={})
set :user => user_name, :repo => repo_name
assert_presence_of user, repo
normalize! params
response = if params['ref']
ref = params.delete('ref')
validate_reference ref
get_request("/repos/#{user}/#{repo}/git/refs/#{ref}", params)
else
get_request("/repos/#{user}/#{repo}/git/refs", params)
end
return response unless block_given?
response.each { |el| yield el }
end
alias :all :list
# Get a reference
#
# The ref in the URL must be formatted as <tt>heads/branch</tt>,
# not just branch. For example, the call to get the data for a
# branch named <tt>sc/featureA</tt> would be formatted as
# <tt>heads/sc/featureA</tt>
#
# = Examples
# github = Github.new
# github.git_data.references.get 'user-name', 'repo-name', 'heads/branch'
#
def get(user_name, repo_name, ref, params={})
set :user => user_name, :repo => repo_name
assert_presence_of user, repo, ref
validate_reference ref
normalize! params
get_request("/repos/#{user}/#{repo}/git/refs/#{ref}", params)
end
alias :find :get
# Create a reference
#
# = Inputs
# * <tt>:ref</tt> - String of the name of the fully qualified reference (ie: refs/heads/master). If it doesn’t start with ‘refs’ and have at least two slashes, it will be rejected.
# * <tt>:sha</tt> - String of the SHA1 value to set this reference to
#
# = Examples
# github = Github.new
# github.git_data.references.create 'user-name', 'repo-name',
# "ref" => "refs/heads/master",
# "sha" => "827efc6d56897b048c772eb4087f854f46256132"
#
def create(user_name, repo_name, params={})
set :user => user_name, :repo => repo_name
normalize! params
filter! VALID_REF_PARAM_NAMES, params
assert_presence_of user, repo, params['ref']
validate_reference params['ref']
assert_required_keys(%w[ ref sha ], params)
post_request("/repos/#{user}/#{repo}/git/refs", params)
end
# Update a reference
#
# = Inputs
# * <tt>:sha</tt> - String of the SHA1 value to set this reference to
# * <tt>:force</tt> - Boolean indicating whether to force the update or to make sure the update is a fast-forward update. The default is <tt>false</tt>, so leaving this out or setting it to false will make sure you’re not overwriting work.
#
# = Examples
# github = Github.new
# github.git_data.references.update 'user-name', 'repo-name', 'heads/master',
# "sha" => "827efc6d56897b048c772eb4087f854f46256132",
# "force" => true
#
def update(user_name, repo_name, ref, params={})
set :user => user_name, :repo => repo_name
assert_presence_of user, repo, ref
validate_reference ref
normalize! params
filter! VALID_REF_PARAM_NAMES, params
assert_required_keys(%w[ sha ], params)
patch_request("/repos/#{user}/#{repo}/git/refs/#{ref}", params)
end
# Delete a reference
#
# = Examples
# github = Github.new
# github.git_data.references.delete 'user-name', 'repo-name',
# "ref" => "refs/heads/master",
#
def delete(user_name, repo_name, ref, params={})
set :user => user_name, :repo => repo_name
assert_presence_of user, repo, ref
normalize! params
delete_request("/repos/#{user}/#{repo}/git/refs/#{ref}", params)
end
alias :remove :delete
private
def validate_reference ref
refs = ref.index('ref') ? ref : "refs/#{ref}"
unless VALID_REF_PARAM_VALUES['ref'] =~ refs
raise ArgumentError, "Provided 'reference' is invalid"
end
end
end # GitData::References
end # Github