Module: Janky::GitHub

Defined in:
lib/janky/github.rb,
lib/janky/github/api.rb,
lib/janky/github/mock.rb,
lib/janky/github/commit.rb,
lib/janky/github/payload.rb,
lib/janky/github/receiver.rb,
lib/janky/github/payload_parser.rb

Defined Under Namespace

Classes: API, Commit, Mock, Payload, PayloadParser, Receiver

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.git_hostObject (readonly)

Returns the value of attribute git_host.



23
24
25
# File 'lib/janky/github.rb', line 23

def git_host
  @git_host
end

.secretObject (readonly)

Returns the value of attribute secret.



23
24
25
# File 'lib/janky/github.rb', line 23

def secret
  @secret
end

Class Method Details

.apiObject

Default API implementation that goes over the wire (HTTP).

Returns nothing.



150
151
152
# File 'lib/janky/github.rb', line 150

def self.api
  @api ||= API.new(@api_url, @user, @password)
end

.branch_head_sha(nwo, branch) ⇒ Object

Fetch the SHA1 of the given branch HEAD.

nwo - qualified “owner/repo” name. branch - Name of the branch as a String.

Returns the SHA1 as a String or nil when the branch doesn’t exists.



69
70
71
72
73
74
# File 'lib/janky/github.rb', line 69

def self.branch_head_sha(nwo, branch)
  response = api.branch(nwo, branch)

  branch = Yajl.load(response.body)
  branch && branch["sha"]
end

.commit(nwo, sha) ⇒ Object

Fetch commit details for the given SHA1.

nwo - qualified “owner/repo” name. sha - SHA1 of the commit as a String.

Example

commit("github/janky", "35fff49dc18376845dd37e785c1ea88c6133f928")
=> { "commit" => {
       "author" => {
         "name"  => "Simon Rozet",
         "email" => "[email protected]",
       },
       "message" => "document and clean up Branch#build_for_head",
     }
   }

Returns the commit Hash.



94
95
96
97
98
99
100
101
102
103
# File 'lib/janky/github.rb', line 94

def self.commit(nwo, sha)
  response = api.commit(nwo, sha)

  if response.code != "200"
    Exception.push_http_response(response)
    raise Error, "Failed to get commit"
  end

  Yajl.load(response.body)
end

.enable_mock!Object

Turn on mock mode, meaning no request goes over the wire. Useful in testing environments.

Returns nothing.



158
159
160
# File 'lib/janky/github.rb', line 158

def self.enable_mock!
  @api = Mock.new(@user, @password)
end

.github_urlObject

URL of the GitHub website.

Retuns the URL as a String. Example: github.com



29
30
31
32
# File 'lib/janky/github.rb', line 29

def self.github_url
  api_uri = URI.parse(@api_url)
  "#{api_uri.scheme}://#{@git_host}"
end

.hook_create(nwo) ⇒ Object

Create a Post-Receive hook for the given repository. developer.github.com/v3/repos/hooks/#create-a-hook

nwo - qualified “owner/repo” name.

Returns the newly created hook URL as String when successful. Raises an Error for any other response.



112
113
114
115
116
117
118
119
120
121
# File 'lib/janky/github.rb', line 112

def self.hook_create(nwo)
  response = api.create(nwo, @secret, @hook_url)

  if response.code == "201"
    Yajl.load(response.body)["url"]
  else
    Exception.push_http_response(response)
    raise Error, "Failed to create hook"
  end
end

.hook_delete(url) ⇒ Object

Delete a post-receive hook for the given repository.

hook_url - The repository’s hook_url

Returns true or raises an exception.



136
137
138
139
140
141
142
143
144
145
# File 'lib/janky/github.rb', line 136

def self.hook_delete(url)
  response = api.delete(url)

  if response.code == "204"
    true
  else
    Exception.push_http_response(response)
    raise Error, "Failed to delete hook"
  end
end

.hook_exists?(url) ⇒ Boolean

Check existance of a hook. developer.github.com/v3/repos/hooks/#get-single-hook

url - Hook URL as a String.

Returns:

  • (Boolean)


127
128
129
# File 'lib/janky/github.rb', line 127

def self.hook_exists?(url)
  api.get(url).code == "200"
end

.receiverObject

Rack app that handles Post-Receive hook requests from GitHub.

Returns a GitHub::Receiver.



37
38
39
# File 'lib/janky/github.rb', line 37

def self.receiver
  @receiver ||= Receiver.new(@secret)
end

.repo_get(nwo) ⇒ Object

Fetch repository details. developer.github.com/v3/repos/#get

nwo - qualified “owner/repo” name.

Returns the Hash representation of the repo, nil when it doesn’t exists

or access was denied.

Raises an Error for any unexpected response.



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/janky/github.rb', line 49

def self.repo_get(nwo)
  response = api.repo_get(nwo)

  case response.code
  when "200"
    Yajl.load(response.body)
  when "403", "404"
    nil
  else
    Exception.push_http_response(response)
    raise Error, "Failed to get hook"
  end
end

.repo_make_private(nwo) ⇒ Object

Make any subsequent response for the given repository look like as if it was a private repo.

nwo - qualified “owner/repo” name.

Returns nothing.



168
169
170
# File 'lib/janky/github.rb', line 168

def self.repo_make_private(nwo)
  api.make_private(nwo)
end

.repo_make_public(nwo) ⇒ Object

Make any subsequent request to the given repository succeed. Only available in mock mode.

nwo - qualified “owner/repo” name.

Returns nothing.



178
179
180
# File 'lib/janky/github.rb', line 178

def self.repo_make_public(nwo)
  api.make_public(nwo)
end

.repo_make_unauthorized(nwo) ⇒ Object

Make any subsequent request for the given repository fail with an unauthorized response. Only available when mocked.

nwo - qualified “owner/repo” name.

Returns nothing.



188
189
190
# File 'lib/janky/github.rb', line 188

def self.repo_make_unauthorized(nwo)
  api.make_unauthorized(nwo)
end

.set_branch_head(nwo, branch, sha) ⇒ Object

Set the SHA of the named branch for the given repo. Mock only.



193
194
195
# File 'lib/janky/github.rb', line 193

def self.set_branch_head(nwo, branch, sha)
  api.set_branch_head(nwo, branch, sha)
end

.setup(user, password, secret, hook_url, api_url, git_host) ⇒ Object

Setup the GitHub API client and Post-Receive hook endpoint.

user - API user as a String. password - API password as a String. secret - Secret used to sign hook requests from GitHub. hook_url - String URL that handles Post-Receive requests. api_url - GitHub API URL as a String. Requires a trailing slash. git_host - Hostname where git repos are hosted. e.g. “github.com”

Returns nothing.



13
14
15
16
17
18
19
20
# File 'lib/janky/github.rb', line 13

def self.setup(user, password, secret, hook_url, api_url, git_host)
  @user = user
  @password = password
  @secret = secret
  @hook_url = hook_url
  @api_url = api_url
  @git_host = git_host
end