Skip to content

Commit

Permalink
Merge ab09b0a into 93b4dfc
Browse files Browse the repository at this point in the history
  • Loading branch information
EBKH committed Oct 4, 2018
2 parents 93b4dfc + ab09b0a commit 9e3e865
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/tentacat/repositories/branches.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,52 @@ defmodule Tentacat.Repositories.Branches do
def find(client \\ %Client{}, owner, repo, branch) do
get("repos/#{owner}/#{repo}/branches/#{branch}", client)
end

@doc """
Update Branch Protection
Create body example:
%{
"required_status_checks" => %{
"strict" => true,
"contexts" => [
"continuous-integration/travis-ci"
]
},
"enforce_admins" => true,
"required_pull_request_reviews" => %{
"dismissal_restrictions" => %{
"users" => [
"octocat"
],
"teams" => [
"justice-league"
]
},
"dismiss_stale_reviews" => true,
"require_code_owner_reviews" => true,
"required_approving_review_count" => 2
},
"restrictions" => %{
"users" => [
"octocat"
],
"teams" => [
"justice-league"
]
}
}
## Example
Tentacat.Repositories.Branches.update_protection "elixir-lang", "elixir", "feature", body
Tentacat.Repositories.Branches.update_protection client, "elixir-lang", "elixir", "feature", body
More info at: https://developer.github.com/v3/repos/branches/#update-branch-protection
"""
@spec update_protection(Client.t(), binary, binary, binary, list | map) :: Tentacat.response()
def update_protection(client \\ %Client{}, owner, repo, branch, body) do
put("repos/#{owner}/#{repo}/branches/#{branch}/protection", client, body)
end
end
47 changes: 47 additions & 0 deletions test/fixture/vcr_cassettes/repositories/branches#find.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"request": {
"body": "\"\\\"\\\"\"",
"headers": {
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "get",
"options": [],
"request_body": "",
"url": "https://api.github.com/repos/valiot/tentacat/branches/master"
},
"response": {
"binary": false,
"body": "{\"name\":\"master\",\"commit\":{\"sha\":\"ab07d03832a8c4a052ef69e2f404a0e8ddb34e48\",\"node_id\":\"MDY6Q29tbWl0MTQ5MjAxOTE0OmFiMDdkMDM4MzJhOGM0YTA1MmVmNjllMmY0MDRhMGU4ZGRiMzRlNDg=\",\"commit\":{\"author\":{\"name\":\"EBKH\",\"email\":\"juan@ebkh.net\",\"date\":\"2018-10-04T17:46:45Z\"},\"committer\":{\"name\":\"EBKH\",\"email\":\"juan@ebkh.net\",\"date\":\"2018-10-04T17:46:45Z\"},\"message\":\"Fix data types\",\"tree\":{\"sha\":\"dbb41169ff3da8eb984887c4ab97df75440f6be3\",\"url\":\"https://api.github.com/repos/valiot/tentacat/git/trees/dbb41169ff3da8eb984887c4ab97df75440f6be3\"},\"url\":\"https://api.github.com/repos/valiot/tentacat/git/commits/ab07d03832a8c4a052ef69e2f404a0e8ddb34e48\",\"comment_count\":0,\"verification\":{\"verified\":false,\"reason\":\"unsigned\",\"signature\":null,\"payload\":null}},\"url\":\"https://api.github.com/repos/valiot/tentacat/commits/ab07d03832a8c4a052ef69e2f404a0e8ddb34e48\",\"html_url\":\"https://github.com/valiot/tentacat/commit/ab07d03832a8c4a052ef69e2f404a0e8ddb34e48\",\"comments_url\":\"https://api.github.com/repos/valiot/tentacat/commits/ab07d03832a8c4a052ef69e2f404a0e8ddb34e48/comments\",\"author\":{\"login\":\"EBKH\",\"id\":18579439,\"node_id\":\"MDQ6VXNlcjE4NTc5NDM5\",\"avatar_url\":\"https://avatars0.githubusercontent.com/u/18579439?v=4\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/EBKH\",\"html_url\":\"https://github.com/EBKH\",\"followers_url\":\"https://api.github.com/users/EBKH/followers\",\"following_url\":\"https://api.github.com/users/EBKH/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/EBKH/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/EBKH/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/EBKH/subscriptions\",\"organizations_url\":\"https://api.github.com/users/EBKH/orgs\",\"repos_url\":\"https://api.github.com/users/EBKH/repos\",\"events_url\":\"https://api.github.com/users/EBKH/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/EBKH/received_events\",\"type\":\"User\",\"site_admin\":false},\"committer\":{\"login\":\"EBKH\",\"id\":18579439,\"node_id\":\"MDQ6VXNlcjE4NTc5NDM5\",\"avatar_url\":\"https://avatars0.githubusercontent.com/u/18579439?v=4\",\"gravatar_id\":\"\",\"url\":\"https://api.github.com/users/EBKH\",\"html_url\":\"https://github.com/EBKH\",\"followers_url\":\"https://api.github.com/users/EBKH/followers\",\"following_url\":\"https://api.github.com/users/EBKH/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/EBKH/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/EBKH/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/EBKH/subscriptions\",\"organizations_url\":\"https://api.github.com/users/EBKH/orgs\",\"repos_url\":\"https://api.github.com/users/EBKH/repos\",\"events_url\":\"https://api.github.com/users/EBKH/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/EBKH/received_events\",\"type\":\"User\",\"site_admin\":false},\"parents\":[{\"sha\":\"ba6a4b6ca23860421efadd7ad135b5182a42313d\",\"url\":\"https://api.github.com/repos/valiot/tentacat/commits/ba6a4b6ca23860421efadd7ad135b5182a42313d\",\"html_url\":\"https://github.com/valiot/tentacat/commit/ba6a4b6ca23860421efadd7ad135b5182a42313d\"}]},\"_links\":{\"self\":\"https://api.github.com/repos/valiot/tentacat/branches/master\",\"html\":\"https://github.com/valiot/tentacat/tree/master\"},\"protected\":false,\"protection\":{\"enabled\":false,\"required_status_checks\":{\"enforcement_level\":\"off\",\"contexts\":[]}},\"protection_url\":\"https://api.github.com/repos/valiot/tentacat/branches/master/protection\"}",
"headers": {
"Server": "GitHub.com",
"Date": "Thu, 04 Oct 2018 22:18:43 GMT",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "3407",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4991",
"X-RateLimit-Reset": "1538692794",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": "Accept, Authorization, Cookie, X-GitHub-OTP",
"ETag": "\"f4a63a9dfc68ade81c4da491bf1d5d3b\"",
"X-OAuth-Scopes": "repo",
"X-Accepted-OAuth-Scopes": "",
"X-GitHub-Media-Type": "github.v3; format=json",
"Access-Control-Expose-Headers": "ETag, Link, Retry-After, 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": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-Runtime-rack": "0.039728",
"X-GitHub-Request-Id": "F3FF:197C:D700600:1998B3E4:5BB691C3"
},
"status_code": 200,
"type": "ok"
}
}
]
47 changes: 47 additions & 0 deletions test/fixture/vcr_cassettes/repositories/branches#list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"request": {
"body": "\"\\\"\\\"\"",
"headers": {
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "get",
"options": [],
"request_body": "",
"url": "https://api.github.com/repos/valiot/tentacat/branches"
},
"response": {
"binary": false,
"body": "[{\"name\":\"Branches-Protection\",\"commit\":{\"sha\":\"423dffd14192abc0c1d5b0f672f1dba2e8bc8a27\",\"url\":\"https://api.github.com/repos/valiot/tentacat/commits/423dffd14192abc0c1d5b0f672f1dba2e8bc8a27\"}},{\"name\":\"EBKH/Add-fixture-to-gitignore\",\"commit\":{\"sha\":\"7db56cc3a9d3b71f4ace4b9d787f31217d7548b5\",\"url\":\"https://api.github.com/repos/valiot/tentacat/commits/7db56cc3a9d3b71f4ace4b9d787f31217d7548b5\"}},{\"name\":\"EBKH/Add-team-to-repo\",\"commit\":{\"sha\":\"15908606dcf4f073f916e63d8207cbd881a0d96c\",\"url\":\"https://api.github.com/repos/valiot/tentacat/commits/15908606dcf4f073f916e63d8207cbd881a0d96c\"}},{\"name\":\"EBKH/Update-elixir-and-dependencies\",\"commit\":{\"sha\":\"e9a8a25fbcf90ff9076d6e876f80243beab810ad\",\"url\":\"https://api.github.com/repos/valiot/tentacat/commits/e9a8a25fbcf90ff9076d6e876f80243beab810ad\"}},{\"name\":\"master\",\"commit\":{\"sha\":\"ab07d03832a8c4a052ef69e2f404a0e8ddb34e48\",\"url\":\"https://api.github.com/repos/valiot/tentacat/commits/ab07d03832a8c4a052ef69e2f404a0e8ddb34e48\"}},{\"name\":\"update-travis-yml\",\"commit\":{\"sha\":\"ec8329604bb433b2420787e2eb47b48213f94bd5\",\"url\":\"https://api.github.com/repos/valiot/tentacat/commits/ec8329604bb433b2420787e2eb47b48213f94bd5\"}}]",
"headers": {
"Server": "GitHub.com",
"Date": "Thu, 04 Oct 2018 22:18:43 GMT",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "1172",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4990",
"X-RateLimit-Reset": "1538692794",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": "Accept, Authorization, Cookie, X-GitHub-OTP",
"ETag": "\"960526afdb1d62a467cf1eb06481652c\"",
"X-OAuth-Scopes": "repo",
"X-Accepted-OAuth-Scopes": "",
"X-GitHub-Media-Type": "github.v3; format=json",
"Access-Control-Expose-Headers": "ETag, Link, Retry-After, 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": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-Runtime-rack": "0.037929",
"X-GitHub-Request-Id": "F3FF:197C:D700634:1998B446:5BB691C3"
},
"status_code": 200,
"type": "ok"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"request": {
"body": "{\"enforce_admins\":null,\"required_pull_request_reviews\":null,\"required_status_checks\":null,\"restrictions\":null}",
"headers": {
"User-agent": "tentacat",
"Authorization": "token yourtokencomeshere"
},
"method": "put",
"options": [],
"request_body": "",
"url": "https://api.github.com/repos/valiot/tentacat/branches/master/protection"
},
"response": {
"binary": false,
"body": "{\"url\":\"https://api.github.com/repos/valiot/tentacat/branches/master/protection\",\"enforce_admins\":{\"url\":\"https://api.github.com/repos/valiot/tentacat/branches/master/protection/enforce_admins\",\"enabled\":false}}",
"headers": {
"Server": "GitHub.com",
"Date": "Thu, 04 Oct 2018 22:34:46 GMT",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "211",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4987",
"X-RateLimit-Reset": "1538692794",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": "Accept, Authorization, Cookie, X-GitHub-OTP",
"ETag": "\"05a66d87382bcb4a1602d16eb824bfc0\"",
"X-OAuth-Scopes": "repo",
"X-Accepted-OAuth-Scopes": "",
"X-GitHub-Media-Type": "github.v3; format=json",
"Access-Control-Expose-Headers": "ETag, Link, Retry-After, 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": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-Runtime-rack": "0.040550",
"X-GitHub-Request-Id": "FA52:197D:70DC1DC:105A98BD:5BB69585"
},
"status_code": 200,
"type": "ok"
}
}
]
41 changes: 41 additions & 0 deletions test/repositories/branches_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule Tentacat.Repositories.BranchesTest do
use ExUnit.Case, async: false
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
import Tentacat.Repositories.Branches

doctest Tentacat.Repositories.Branches

@client Tentacat.Client.new(%{access_token: "yourtokencomeshere"})

setup_all do
HTTPoison.start()
end

test "list/3" do
use_cassette "repositories/branches#list" do
assert elem(list(@client, "valiot", "tentacat"), 1)
# assert elem(list(@client, "antonydenyer", "tentatest"), 1) == []
|> Enum.count() >= 1
end
end

test "find/4" do
use_cassette "repositories/branches#find" do
assert elem(find(@client, "valiot", "tentacat", "master"), 1)["name"] == "master"
end
end

test "update_protection/5" do
body = %{
"required_status_checks" => nil,
"enforce_admins" => nil,
"required_pull_request_reviews" => nil,
"restrictions" => nil
}

use_cassette "repositories/branches#update_protection" do
assert elem(update_protection(@client, "valiot", "tentacat", "master", body), 1)["url"] ==
"https://api.github.com/repos/valiot/tentacat/branches/master/protection"
end
end
end

0 comments on commit 9e3e865

Please sign in to comment.