Skip to content

Commit

Permalink
Merge pull request edgurgel#85 from danielfarrell/org-webhooks
Browse files Browse the repository at this point in the history
Add support for organization webhooks
  • Loading branch information
duksis committed May 26, 2016
2 parents f3d4b51 + 2ab4129 commit ce2782a
Show file tree
Hide file tree
Showing 9 changed files with 431 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Tentacool + Cat = Tentacat
* Organizations
* Members
* Teams
* Webhooks
* Teams
* Members
* Users
Expand Down
98 changes: 98 additions & 0 deletions lib/tentacat/organizations/hooks.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
defmodule Tentacat.Organizations.Hooks do
import Tentacat
alias Tentacat.Client

@doc """
List hooks of an organization. The response will differ if the authenticated user is also owner of the organization
## Example
Tentacat.Organizations.Hooks.list "github"
Tentacat.Organizations.Hooks.list "github", client
More info at: http://developer.github.com/v3/orgs/hooks/#list-hooks
"""
@spec list(binary, Client.t) :: Tentacat.response
def list(organization, client \\ %Client{}) do
get "orgs/#{organization}/hooks", client
end

@doc """
Get a webhook for an organization by id.
## Example
Tentacat.Organizations.Hooks.find("github", "1234567", client)
More info at: http://developer.github.com/v3/orgs/hooks/#get-single-hook
"""
@spec find(binary, binary | integer, Client.t) :: Tentacat.response
def find(organization, hook_id, client) do
get "orgs/#{organization}/hooks/#{hook_id}", client
end

@doc """
Create webhook for an organization.
## Example
Tentacat.Organizations.Hooks.create("github", hook_body, client)
More info at: http://developer.github.com/v3/orgs/hooks/#create-a-hook
"""
@spec create(binary, list, Client.t) :: Tentacat.response
def create(organization, body, client) do
post "orgs/#{organization}/hooks", client, body
end

@doc """
hook_body = %{
"name" => "web",
"active" => true,
"events" => [ "push", "pull_request" ],
"config" => {
"url" => "http://example.com/webhook",
"content_type" => "json"
}
}
## Example
Tentacat.Organizations.Hooks.update("github", "1234567", hook_body, client)
More info at: http://developer.github.com/v3/orgs/hooks/#edit-a-hook
"""
@spec update(binary, binary | integer, list, Client.t) :: Tentacat.response
def update(organization, hook_id, body, client) do
patch "orgs/#{organization}/hooks/#{hook_id}", client, body
end

@doc """
This will trigger a ping event to be sent to the hook.
## Example
Tentacat.Organizations.Hooks.ping("github", "1234567", client)
More info at: http://developer.github.com/v3/orgs/hooks/#ping-a-hook
"""
@spec ping(binary, binary | integer, Client.t) :: Tentacat.response
def ping(organization, hook_id, client) do
post "orgs/#{organization}/hooks/#{hook_id}/pings", client, ""
end

@doc """
Delete an organization's webhook by it's Id.
## Example
Tentacat.Organizations.Hooks.remove("github", "1234567", client)
More info at: http://developer.github.com/v3/orgs/hooks/#delete-a-hook
"""
@spec remove(binary, binary | integer, Client.t) :: Tentacat.response
def remove(organization, hook_id, client) do
delete "orgs/#{organization}/hooks/#{hook_id}", client
end

end
47 changes: 47 additions & 0 deletions test/fixture/vcr_cassettes/organizations/hooks#create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"request": {
"body": "{\"active\":true,\"config\":{\"content_type\":\"json\",\"url\":\"http://example.com/webhook\"},\"events\":[\"push\",\"pull_request\"],\"name\":\"web\"}",
"headers": {
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "post",
"options": [],
"request_body": "",
"url": "https://api.github.com/orgs/tentatest/hooks"
},
"response": {
"body": "{\"url\":\"https://api.github.com/orgs/tentatest/hooks/6736758\",\"test_url\":\"https://api.github.com/orgs/tentatest/hooks/6736758/test\",\"ping_url\":\"https://api.github.com/orgs/tentatest/hooks/6736758/pings\",\"id\":6736758,\"name\":\"web\",\"active\":true,\"events\":[\"push\",\"pull_request\"],\"config\":{\"content_type\":\"json\",\"url\":\"http://example.com/webhook\"},\"last_response\":{\"code\":null,\"status\":\"unused\",\"message\":null},\"updated_at\":\"2015-12-19T07:25:49Z\",\"created_at\":\"2015-12-19T07:25:49Z\"}",
"headers": {
"Server": "GitHub.com",
"Date": "Sat, 19 Dec 2015 07:25:49 GMT",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "541",
"Status": "201 Created",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4986",
"X-RateLimit-Reset": "1450510991",
"Cache-Control": "private, max-age=60, s-maxage=60",
"ETag": "\"71ae880155e3d5dbd6f503ae50fb77c4\"",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user",
"X-Accepted-OAuth-Scopes": "admin:repo_hook, public_repo, repo, write:repo_hook",
"Location": "https://api.github.com/orgs/tentatest/hooks/6736758",
"Vary": "Accept, Authorization, Cookie, X-GitHub-OTP",
"X-GitHub-Media-Type": "github.v3; format=json",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Expose-Headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval",
"Access-Control-Allow-Origin": "*",
"Content-Security-Policy": "default-src 'none'",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "deny",
"X-XSS-Protection": "1; mode=block",
"X-Served-By": "cee4c0729c8e9147e7abcb45b9d69689",
"X-GitHub-Request-Id": "3E552BA7:134F5:C7071D7:5675067D"
},
"status_code": 201,
"type": "ok"
}
}
]
42 changes: 42 additions & 0 deletions test/fixture/vcr_cassettes/organizations/hooks#find.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"request": {
"body": "\"\"",
"headers": {
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "get",
"options": [],
"request_body": "",
"url": "https://api.github.com/orgs/tentatest/hooks/1234"
},
"response": {
"body": "{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3\"}",
"headers": {
"Server": "GitHub.com",
"Date": "Sat, 19 Dec 2015 07:25:49 GMT",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "77",
"Status": "404 Not Found",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4985",
"X-RateLimit-Reset": "1450510991",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user",
"X-Accepted-OAuth-Scopes": "admin:repo_hook, public_repo, read:repo_hook, repo, write:repo_hook",
"X-GitHub-Media-Type": "github.v3; format=json",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Expose-Headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval",
"Access-Control-Allow-Origin": "*",
"Content-Security-Policy": "default-src 'none'",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "deny",
"X-XSS-Protection": "1; mode=block",
"X-GitHub-Request-Id": "3E552BA7:134F5:C707217:5675067D"
},
"status_code": 404,
"type": "ok"
}
}
]
46 changes: 46 additions & 0 deletions test/fixture/vcr_cassettes/organizations/hooks#list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"request": {
"body": "\"\"",
"headers": {
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "get",
"options": [],
"request_body": "",
"url": "https://api.github.com/orgs/tentatest/hooks"
},
"response": {
"body": "[]",
"headers": {
"Server": "GitHub.com",
"Date": "Sat, 19 Dec 2015 07:25:50 GMT",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "2",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4984",
"X-RateLimit-Reset": "1450510991",
"Cache-Control": "private, max-age=60, s-maxage=60",
"ETag": "\"411634914830e1b11d359df5aeb17ed4\"",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user",
"X-Accepted-OAuth-Scopes": "admin:repo_hook, public_repo, read:repo_hook, repo, write:repo_hook",
"Vary": "Accept, Authorization, Cookie, X-GitHub-OTP",
"X-GitHub-Media-Type": "github.v3; format=json",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Expose-Headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval",
"Access-Control-Allow-Origin": "*",
"Content-Security-Policy": "default-src 'none'",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "deny",
"X-XSS-Protection": "1; mode=block",
"X-Served-By": "bd82876e9bf04990f289ba22f246ee9b",
"X-GitHub-Request-Id": "3E552BA7:134F5:C70724F:5675067D"
},
"status_code": 200,
"type": "ok"
}
}
]
42 changes: 42 additions & 0 deletions test/fixture/vcr_cassettes/organizations/hooks#ping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"request": {
"body": "\"\"",
"headers": {
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "post",
"options": [],
"request_body": "",
"url": "https://api.github.com/orgs/tentatest/hooks/6736758/pings"
},
"response": {
"body": "",
"headers": {
"Server": "GitHub.com",
"Date": "Sat, 19 Dec 2015 07:27:27 GMT",
"Status": "204 No Content",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4981",
"X-RateLimit-Reset": "1450510991",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user",
"X-Accepted-OAuth-Scopes": "admin:repo_hook, public_repo, repo, write:repo_hook",
"X-GitHub-Media-Type": "github.v3; format=json",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Expose-Headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval",
"Access-Control-Allow-Origin": "*",
"Content-Security-Policy": "default-src 'none'",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "deny",
"X-XSS-Protection": "1; mode=block",
"Vary": "Accept-Encoding",
"X-Served-By": "318e55760cf7cdb40e61175a4d36cd32",
"X-GitHub-Request-Id": "3E552BA7:14B60:C64918A:567506DE"
},
"status_code": 204,
"type": "ok"
}
}
]
42 changes: 42 additions & 0 deletions test/fixture/vcr_cassettes/organizations/hooks#remove.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"request": {
"body": "\"\"",
"headers": {
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "delete",
"options": [],
"request_body": "",
"url": "https://api.github.com/orgs/tentatest/hooks/6736758"
},
"response": {
"body": "",
"headers": {
"Server": "GitHub.com",
"Date": "Sun, 20 Dec 2015 09:21:25 GMT",
"Status": "204 No Content",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4999",
"X-RateLimit-Reset": "1450606885",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user",
"X-Accepted-OAuth-Scopes": "admin:repo_hook, public_repo, repo",
"X-GitHub-Media-Type": "github.v3; format=json",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Expose-Headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval",
"Access-Control-Allow-Origin": "*",
"Content-Security-Policy": "default-src 'none'",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "deny",
"X-XSS-Protection": "1; mode=block",
"Vary": "Accept-Encoding",
"X-Served-By": "474556b853193c38f1b14328ce2d1b7d",
"X-GitHub-Request-Id": "3E552BA7:A399:E83D655:56767314"
},
"status_code": 204,
"type": "ok"
}
}
]
46 changes: 46 additions & 0 deletions test/fixture/vcr_cassettes/organizations/hooks#update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"request": {
"body": "{\"active\":false,\"add_events\":[\"issue\"]}",
"headers": {
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "patch",
"options": [],
"request_body": "",
"url": "https://api.github.com/orgs/tentatest/hooks/6736758"
},
"response": {
"body": "{\"url\":\"https://api.github.com/orgs/tentatest/hooks/6736758\",\"test_url\":\"https://api.github.com/orgs/tentatest/hooks/6736758/test\",\"ping_url\":\"https://api.github.com/orgs/tentatest/hooks/6736758/pings\",\"id\":6736758,\"name\":\"web\",\"active\":false,\"events\":[\"push\",\"pull_request\"],\"config\":{\"content_type\":\"json\",\"url\":\"http://example.com/webhook\"},\"last_response\":{\"code\":422,\"status\":\"misconfigured\",\"message\":\"Invalid HTTP Response: 404\"},\"updated_at\":\"2015-12-19T07:31:18Z\",\"created_at\":\"2015-12-19T07:25:49Z\"}",
"headers": {
"Server": "GitHub.com",
"Date": "Sat, 19 Dec 2015 07:31:18 GMT",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "572",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4980",
"X-RateLimit-Reset": "1450510991",
"Cache-Control": "private, max-age=60, s-maxage=60",
"ETag": "\"bb46ae16382d70fc71b2fd76996e21d5\"",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user",
"X-Accepted-OAuth-Scopes": "admin:repo_hook, public_repo, repo, write:repo_hook",
"Vary": "Accept, Authorization, Cookie, X-GitHub-OTP",
"X-GitHub-Media-Type": "github.v3; format=json",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Expose-Headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval",
"Access-Control-Allow-Origin": "*",
"Content-Security-Policy": "default-src 'none'",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "deny",
"X-XSS-Protection": "1; mode=block",
"X-Served-By": "dc1ce2bfb41810a06c705e83b388572d",
"X-GitHub-Request-Id": "3E552BA7:14B62:11E833F2:567507C5"
},
"status_code": 200,
"type": "ok"
}
}
]
Loading

0 comments on commit ce2782a

Please sign in to comment.