Skip to content

Commit

Permalink
Merge pull request #895 from jeetchoudhary/protection
Browse files Browse the repository at this point in the history
GHBranch getProtection method to return correct number of reviewers #890
  • Loading branch information
bitwiseman authored Jul 17, 2020
2 parents d68c66c + cd92b51 commit 727932a
Show file tree
Hide file tree
Showing 24 changed files with 338 additions and 159 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/kohsuke/github/GHBranch.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ public URL getProtectionUrl() {
* the io exception
*/
public GHBranchProtection getProtection() throws IOException {
return root.createRequest().setRawUrlPath(protection_url).fetch(GHBranchProtection.class).wrap(this);
return root.createRequest()
.withPreview(Previews.LUKE_CAGE)
.setRawUrlPath(protection_url)
.fetch(GHBranchProtection.class)
.wrap(this);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/kohsuke/github/GHBranchProtection.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

/**
* The type GHBranchProtection.
*
* @see <a href="https://docs.github.com/en/rest/reference/repos#get-branch-protection">GitHub Branch Protection</a>
*/
@SuppressFBWarnings(
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/kohsuke/github/GHBranchProtectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.kohsuke.github.GHBranchProtection.RequiredReviews;
import org.kohsuke.github.GHBranchProtection.RequiredStatusChecks;

import static org.hamcrest.CoreMatchers.*;

public class GHBranchProtectionTest extends AbstractGitHubWireMockTest {
private static final String BRANCH = "master";
private static final String BRANCH_REF = "heads/" + BRANCH;
Expand All @@ -32,6 +34,14 @@ public void testEnableBranchProtections() throws Exception {
.includeAdmins()
.enable();

verifyBranchProtection(protection);

// Get goes through a different code path. Make sure it also gets the correct data.
protection = branch.getProtection();
verifyBranchProtection(protection);
}

private void verifyBranchProtection(GHBranchProtection protection) {
RequiredStatusChecks statusChecks = protection.getRequiredStatusChecks();
assertNotNull(statusChecks);
assertTrue(statusChecks.isRequiresBranchUpToDate());
Expand Down Expand Up @@ -66,7 +76,20 @@ public void testDisableProtectionOnly() throws Exception {
public void testEnableRequireReviewsOnly() throws Exception {
GHBranchProtection protection = branch.enableProtection().requireReviews().enable();

RequiredReviews requiredReviews = protection.getRequiredReviews();
assertNotNull(protection.getRequiredReviews());
assertFalse(requiredReviews.isDismissStaleReviews());
assertFalse(requiredReviews.isRequireCodeOwnerReviews());
assertThat(protection.getRequiredReviews().getRequiredReviewers(), equalTo(1));

// Get goes through a different code path. Make sure it also gets the correct data.
protection = branch.getProtection();
requiredReviews = protection.getRequiredReviews();

assertNotNull(protection.getRequiredReviews());
assertFalse(requiredReviews.isDismissStaleReviews());
assertFalse(requiredReviews.isRequireCodeOwnerReviews());
assertThat(protection.getRequiredReviews().getRequiredReviewers(), equalTo(1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": 222596043,
"node_id": "MDEwOlJlcG9zaXRvcnkyMjI1OTYwNDM=",
"id": 280488362,
"node_id": "MDEwOlJlcG9zaXRvcnkyODA0ODgzNjI=",
"name": "temp-testEnableBranchProtections",
"full_name": "hub4j-test-org/temp-testEnableBranchProtections",
"private": false,
Expand Down Expand Up @@ -64,9 +64,9 @@
"labels_url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/labels{/name}",
"releases_url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/releases{/id}",
"deployments_url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/deployments",
"created_at": "2019-11-19T03:01:26Z",
"updated_at": "2019-11-19T03:01:31Z",
"pushed_at": "2019-11-19T03:01:27Z",
"created_at": "2020-07-17T17:40:22Z",
"updated_at": "2020-07-17T17:40:27Z",
"pushed_at": "2020-07-17T17:40:24Z",
"git_url": "git://github.com/hub4j-test-org/temp-testEnableBranchProtections.git",
"ssh_url": "[email protected]:hub4j-test-org/temp-testEnableBranchProtections.git",
"clone_url": "https://github.com/hub4j-test-org/temp-testEnableBranchProtections.git",
Expand Down Expand Up @@ -96,9 +96,11 @@
"push": true,
"pull": true
},
"temp_clone_token": "",
"allow_squash_merge": true,
"allow_merge_commit": true,
"allow_rebase_merge": true,
"delete_branch_on_merge": false,
"organization": {
"login": "hub4j-test-org",
"id": 7544739,
Expand All @@ -120,5 +122,5 @@
"site_admin": false
},
"network_count": 0,
"subscribers_count": 4
"subscribers_count": 8
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
{
"name": "master",
"commit": {
"sha": "298ffff9c7928b06e9b74d01ae8a34ab98e9b4c2",
"node_id": "MDY6Q29tbWl0MjIyNTk2MDQzOjI5OGZmZmY5Yzc5MjhiMDZlOWI3NGQwMWFlOGEzNGFiOThlOWI0YzI=",
"sha": "7b85665b2b0b99ac61e7c23a35b0479daa9014d7",
"node_id": "MDY6Q29tbWl0MjgwNDg4MzYyOjdiODU2NjViMmIwYjk5YWM2MWU3YzIzYTM1YjA0NzlkYWE5MDE0ZDc=",
"commit": {
"author": {
"name": "Liam Newman",
"email": "[email protected]",
"date": "2019-11-19T03:01:27Z"
"date": "2020-07-17T17:40:24Z"
},
"committer": {
"name": "GitHub",
"email": "[email protected]",
"date": "2019-11-19T03:01:27Z"
"date": "2020-07-17T17:40:24Z"
},
"message": "Initial commit",
"tree": {
"sha": "3858cfba073ecd82947ddca0dc3613584db75e4c",
"url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/git/trees/3858cfba073ecd82947ddca0dc3613584db75e4c"
},
"url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/git/commits/298ffff9c7928b06e9b74d01ae8a34ab98e9b4c2",
"url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/git/commits/7b85665b2b0b99ac61e7c23a35b0479daa9014d7",
"comment_count": 0,
"verification": {
"verified": true,
"reason": "valid",
"signature": "-----BEGIN PGP SIGNATURE-----\n\nwsBcBAABCAAQBQJd01sHCRBK7hj4Ov3rIwAAdHIIABgp0lFAcHc2m3RCcuCUqfMl\nzfgjjYNYsqyWgwWeB0c44naL8nqhF/a80KlY4+1T2gVR8GOSZNyC8ewDH/6V+X3q\n5MzjTNJBk2ks5oA4bz2fgdsE7fm8kKDZQRqL9+BN4JF9MNps4M2+nR5Rg4sBm2+A\nSZHcdfzZ6ll0ptcTGl7403Xe9irj/V6iymycUapbK4+RHBotrN6kbBGZtjILempU\nMbYrIuqteQF3B2p9FSXFCJ8SpAL/Cq/GkpGvEbYIPdQ2IFQ6ImiRSf6eNTvSXxtG\nLJygBJsaRCH81FEhBfUnbP+stB9x4dloj/yhPLd6BXv1VCuzT2h9roYtaemjb7c=\n=ycC3\n-----END PGP SIGNATURE-----\n",
"payload": "tree 3858cfba073ecd82947ddca0dc3613584db75e4c\nauthor Liam Newman <[email protected]> 1574132487 -0800\ncommitter GitHub <[email protected]> 1574132487 -0800\n\nInitial commit"
"signature": "-----BEGIN PGP SIGNATURE-----\n\nwsBcBAABCAAQBQJfEeKICRBK7hj4Ov3rIwAAdHIIAIhWRCiGPP64NglqLTELU0qx\nMtPpqwEx7yPwksfvU2XD6WY3NKCHqAlBgIrn6VSsmLx8IF3r0i+hLO4RXOyq+leJ\njmbq697BZNG8OmkB8asAV5r/wow9zL0dvXglpgPdozxPFqTS/aTJWvbwLHZ3UoA7\nCnHnHWVqQMiwBLD/5Jn2pjJ5Oa3C0FfdaocjfZsHZZD7YmfX4rnI8rZW0VpvfzKD\nSs9s5VpsxW/EIQPMAnduntTngdO3Op6dbqbF6PoweS3jDKpACbXI1OZV40LrmvQ6\nBQCSqc0Hy8j04wAswLSI1RdU87ST0fkvB3s61XFB0BO/bxiSSI0U1owMKFkJLIU=\n=+M0n\n-----END PGP SIGNATURE-----\n",
"payload": "tree 3858cfba073ecd82947ddca0dc3613584db75e4c\nauthor Liam Newman <[email protected]> 1595007624 -0700\ncommitter GitHub <[email protected]> 1595007624 -0700\n\nInitial commit"
}
},
"url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/commits/298ffff9c7928b06e9b74d01ae8a34ab98e9b4c2",
"html_url": "https://github.com/hub4j-test-org/temp-testEnableBranchProtections/commit/298ffff9c7928b06e9b74d01ae8a34ab98e9b4c2",
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/commits/298ffff9c7928b06e9b74d01ae8a34ab98e9b4c2/comments",
"url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/commits/7b85665b2b0b99ac61e7c23a35b0479daa9014d7",
"html_url": "https://github.com/hub4j-test-org/temp-testEnableBranchProtections/commit/7b85665b2b0b99ac61e7c23a35b0479daa9014d7",
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/commits/7b85665b2b0b99ac61e7c23a35b0479daa9014d7/comments",
"author": {
"login": "bitwiseman",
"id": 1958953,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@
"enforce_admins": {
"url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/master/protection/enforce_admins",
"enabled": true
},
"required_linear_history": {
"enabled": false
},
"allow_force_pushes": {
"enabled": false
},
"allow_deletions": {
"enabled": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/master/protection",
"required_status_checks": {
"url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/master/protection/required_status_checks",
"strict": true,
"contexts": [
"test-status-check"
],
"contexts_url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/master/protection/required_status_checks/contexts"
},
"required_pull_request_reviews": {
"url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/master/protection/required_pull_request_reviews",
"dismiss_stale_reviews": true,
"require_code_owner_reviews": true,
"required_approving_review_count": 2
},
"enforce_admins": {
"url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/master/protection/enforce_admins",
"enabled": true
},
"required_linear_history": {
"enabled": false
},
"allow_force_pushes": {
"enabled": false
},
"allow_deletions": {
"enabled": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@
"location": "Seattle, WA, USA",
"email": "[email protected]",
"hireable": null,
"bio": "https://twitter.com/bitwiseman",
"public_repos": 176,
"bio": null,
"twitter_username": "bitwiseman",
"public_repos": 196,
"public_gists": 7,
"followers": 140,
"followers": 165,
"following": 9,
"created_at": "2012-07-11T20:38:33Z",
"updated_at": "2019-09-24T19:32:29Z",
"private_gists": 7,
"total_private_repos": 9,
"updated_at": "2020-07-15T22:26:44Z",
"private_gists": 19,
"total_private_repos": 14,
"owned_private_repos": 0,
"disk_usage": 33697,
"disk_usage": 33700,
"collaborators": 0,
"two_factor_authentication": true,
"plan": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "02ee4913-4f10-4403-9f86-c911719d8b88",
"id": "4efe7367-0313-4dff-ac98-6800c1b71d96",
"name": "repos_hub4j-test-org_temp-testenablebranchprotections",
"request": {
"url": "/repos/hub4j-test-org/temp-testEnableBranchProtections",
Expand All @@ -12,37 +12,35 @@
},
"response": {
"status": 200,
"bodyFileName": "repos_hub4j-test-org_temp-02ee4913.json",
"bodyFileName": "repos_hub4j-test-org_temp-testenablebranchprotections-2.json",
"headers": {
"Date": "Tue, 19 Nov 2019 03:01:32 GMT",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Date": "Fri, 17 Jul 2020 17:40:28 GMT",
"Content-Type": "application/json; charset=utf-8",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4937",
"X-RateLimit-Reset": "1574135997",
"X-RateLimit-Remaining": "4967",
"X-RateLimit-Reset": "1595008101",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding"
"Accept-Encoding, Accept, X-Requested-With"
],
"ETag": "W/\"85ef7d73fa0d8de7e964cdc69d1648a0\"",
"Last-Modified": "Tue, 19 Nov 2019 03:01:31 GMT",
"ETag": "W/\"6013dab6b9fb4f1cf8b48240299ad51f\"",
"Last-Modified": "Fri, 17 Jul 2020 17:40:27 GMT",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-Accepted-OAuth-Scopes": "repo",
"X-GitHub-Media-Type": "unknown, github.v3",
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"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-GitHub-Request-Id": "C495:6994:5EFFF:6F6AF:5DD35B05"
"X-GitHub-Request-Id": "CEEF:2B4C:15D0835:2472826:5F11E285"
}
},
"uuid": "02ee4913-4f10-4403-9f86-c911719d8b88",
"uuid": "4efe7367-0313-4dff-ac98-6800c1b71d96",
"persistent": true,
"insertionIndex": 2
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "040c1d69-d6af-4a94-b583-7d18957bad10",
"id": "74041767-9edf-4710-baa8-823cb96977dc",
"name": "repos_hub4j-test-org_temp-testenablebranchprotections_branches_master",
"request": {
"url": "/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/master",
Expand All @@ -12,36 +12,34 @@
},
"response": {
"status": 200,
"bodyFileName": "repos_hub4j-test-org_temp-040c1d69.json",
"bodyFileName": "repos_hub4j-test-org_temp-testenablebranchprotections_branches_master-3.json",
"headers": {
"Date": "Tue, 19 Nov 2019 03:01:32 GMT",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Date": "Fri, 17 Jul 2020 17:40:28 GMT",
"Content-Type": "application/json; charset=utf-8",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4936",
"X-RateLimit-Reset": "1574135997",
"X-RateLimit-Remaining": "4966",
"X-RateLimit-Reset": "1595008101",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding"
"Accept-Encoding, Accept, X-Requested-With"
],
"ETag": "W/\"b4b2b52ee5173543cc0d41dfff61781c\"",
"ETag": "W/\"87757a577671d3f33dc288cf1ed01337\"",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-Accepted-OAuth-Scopes": "",
"X-GitHub-Media-Type": "unknown, github.v3",
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"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-GitHub-Request-Id": "C495:6994:5F01F:6FB32:5DD35B0C"
"X-GitHub-Request-Id": "CEEF:2B4C:15D0850:2472E41:5F11E28C"
}
},
"uuid": "040c1d69-d6af-4a94-b583-7d18957bad10",
"uuid": "74041767-9edf-4710-baa8-823cb96977dc",
"persistent": true,
"insertionIndex": 3
}
Loading

0 comments on commit 727932a

Please sign in to comment.