-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Protected Branches API support #996
Conversation
1b86935
to
a2bc7f0
Compare
@ryangribble welcome aboard, just had a quick look at this and it looks really good! Thanks for the hard work kicking this off!
The |
/// <summary> | ||
/// Should this branch be protected or not | ||
/// </summary> | ||
public bool Enabled { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is a response object, this will currently fail our checks for public setters.
For serialization purposes, protected set
will work fine - but we also define ctors
(like in Branch
above) for scenarios where you may need to set it up in a certain way.
You can see these failures by running this from the repo root:
.\build BuildApp
.\build ConventionTests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done: TattsGroup@37df149
Integration tests are passing on my end 👍 @haacked how do you feel about the API design here? Anything stand out for feedback? |
No worries Ill fix up those protected set and parameterised ctor's shortly Regarding API design at first (and you can see on my earlier commits) I was following the octokit.rb implementation of having ProtectBranch() and UnprotectBranch() calls, however this didnt really suit the design of octokit.net which is more focused on having an Edit() method for an entity, and passing in the xxxUpdate model object with the changes... so I re-implemented as a single EditBranch() function |
@ryangribble Pro Tip™: don't forget to add your work account to your GitHub profile, so those last commits are linked to your profile. Or just amend the author info and |
@shiftkey good catch thanks, email should be fixed now |
LOL so I was just testing this against our GitHub Enterprise on premises installation, and it looks like the preview API isnt actually in GitHub Enterprise version 2.4.2 (eventhough the actual Branch Protection features are). Will need to wait for Enterprise to include the API feature before we can actually use this stuff in our tooling 😄 |
@@ -85,6 +85,12 @@ public void ResponseModelsHaveReadOnlyCollections(Type modelType) | |||
continue; | |||
} | |||
|
|||
// Let's skip collections that only have a private Setter | |||
if (property.SetMethod != null && property.SetMethod.IsPrivate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should collection properties with private setters be mutable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change makes it so collection properties with private setters are NOT added to the mutable list. Otherwise convention tests were failing saying that these properties werent obeying the Response object rules
That said, this was just a possible fix that seemed to work for me... definitely looking for feedback from main contributors and can easily remove this commit if required (and try to find another way to satisfy the convention tests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this is totally out of scope for this test, and it actually makes it incorrect. This test checks that "Response Models Have Read Only Collections". Making it skip properties with private setters also makes it allow mutable collections, which defeats the test in the first place. There's already a test that checks for non-public setters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to jump in here before this goes further off the rails, but thanks @khellang for reminding me about the intent of this test.
ICollection<T>
has the mutable Add
, Remove
and Clear
- so even if it's a read-only property, it can be manipulated arbitrarily after the object is created. We're trying to discourage that, so we use IReadOnlyList<T>
on our collections.
An example of that is CommitActivity
- https://github.com/octokit/octokit.net/blob/master/Octokit/Models/Response/CommitActivity.cs - if we could update RequiredStatusChecks.Contexts
to be a more specific interface than ICollection<T>
and then drop this change we should be able to get back to a green build...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Roger that, I'm the first to admit I haven't quite dealt with this many convention tests etc before, I was definitely hacking my way to success 😀
I'll see what I can do with the Contexts collection and ditch this incorrect convention test modification
24ff60e
to
a54a67b
Compare
RequiredStatusChecks.Context is now an IReadonlyList, implemented as per CommitActivity example Also rebased on latest master |
Unsure about the AppVeyor test failure. All unit tests are passing for me locally? |
@ryangribble that test seems to be rather flaky 😞 |
Looks like the rerun tests all passed etc Is there anything further needed from me? |
@@ -502,7 +521,8 @@ public Task<Branch> GetBranch(string owner, string repositoryName, string branch | |||
Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); | |||
Ensure.ArgumentNotNullOrEmptyString(branchName, "branchName"); | |||
|
|||
return ApiConnection.Get<Branch>(ApiUrls.RepoBranch(owner, repositoryName, branchName)); | |||
const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This const string
is duplicated in 3 methods. Could we declare it at the class level and use it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was following an example I saw in another part of the code base where the preview header was defined each time, but I can change it to a class level.
Should the unit tests use the same define or their own class level define or specify string each time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The licenses API? Yeah, there's two usages in MiscellaneousClient
which could use a bit of 💄 to tidy up at some stage.
Let's define a field on the test class which gets reused here, and then look at the other usages separately.
- Create BranchProtection models - Add Protection element to Branch model - Adjust RepositoryClient GetBranch and GetBranches calls to set the special preview accepts header to enable Protected Branches API (currently in preview) - Adjust tests to include preview accepts header - Add test for Protection element
- Create BranchUpdate model - Add ProtectBranch and UnprotectBranch calls to RepositoryClient - Add tests
Add tests for various protected/unprotected scenarios
…erve alphabetical ordering
Rework integration test examples
…ist passed into the ctor
… on IGenericList" This reverts commit eb01067.
Tidy up to be consistent with other model classes
…PI header in one place, change calls to use this
8bb5d3c
to
489cfdb
Compare
I really dont like the idea of referring to the test projects from the implementation classes (if that's what you meant) so what Ive done is created a AcceptHeaders helper class with the preview header defined, and changed the imlpementation to use that. The tests are left as is, as I believe it's good practice to have each test specify what it's success criteria are, rather than use some commonly defined thing (that could be changed later unaware of the implication to tests that were using it). Ive also got a separate branch and will follow this up with a new PR (after this one is accepted) where Ive gone and cleaned up all uses of Accepts headers (eg the licenses API as well as the standard json and html payloads etc) to use the new helper class, and remove the hardcoded strings from multiple places in implementation classes. Here's the commit in question: TattsGroup@44eb171 Also rebased on latest master |
@ryangribble that's fine, happy to merge this in after the Travis build goes green and iterate on this further... |
Add Protected Branches API support
Thanks @ryangribble! |
This Work-In-Progress PR adds support for the protected branches API, which is currently in PREVIEW
Notes
This is our first contribution to octokit.net so just let us know if anything isn't as desired etc... I've tried to comply with existing similar implementations, coding/comment styles, unit and integration tests and so on, so hopefully it isn't too far off the mark
Since the protected branches API is in preview, I wasn't sure if it's OK to be added, but we need this support for tooling we are writing and would rather contribute to upstream than use a local fork, unless we have to! I did notice a couple of other preview features in the codebase. I also saw that octokit.rb has implemented the protected branches functions, so hopefully this is OK
Implementation Details
At a high level the change involves:
To Do List
Haven't yet looked at what is required on the Octokit.Reactive projectAdded new function, not sure if anything more to do?