Adds ensure_current_branch_using_HEAD git helper#520
Adds ensure_current_branch_using_HEAD git helper#520
Conversation
| # the implementation is different, so it'd be a breaking change for all clients. | ||
| # 3. This method relies on the `current_git_branch` helper which internally | ||
| # uses the `git_branch_name_using_HEAD` fastlane helper. | ||
| def self.ensure_current_branch_using_HEAD(branch) |
There was a problem hiding this comment.
| 🚫 | Use snake_case for method names. |
There was a problem hiding this comment.
Since the name of the method is up for discussion, I'll wait until it's resolved to address this lint.
There was a problem hiding this comment.
While I get that this method relies on current_branch_name_using_HEAD and that's probably what the name you picked come from, I think this is a bit over-verbose (and would make it named like this mostly only as a counterpoint to the fastlane one that uses env vars, while we should already in our own implementations in release-toolkit that we don't want to rely on ENV vars, so imho the precision in the naming is likely unnecessary)
As such, I'd suggest just ensure_current_branch! — the ! in the name being a common Ruby convention to indicate that the method will crash on purpose / interrupt the code and exit if the precondition is not met.
There was a problem hiding this comment.
Also, given branch is taken as a regex, I'd make this explicit in the parameter name. Finally, I'd also suggest to use keyword parameters rather than just positional parameters, to enforce the call site having to provide the parameter name and make it even more explicit that this is a RegExp pattern.
| def self.ensure_current_branch_using_HEAD(branch) | |
| def self.ensure_current_branch!(matches:) |
or
| def self.ensure_current_branch_using_HEAD(branch) | |
| def self.ensure_current_branch!(pattern:) |
There was a problem hiding this comment.
Actually, I like the ensure_on_branch! method name, so I wonder if we shouldn't instead keep that method, update its implementation, and just fix all the call sites.
After all, (1) this method being a helper, it's expected to only be used internally by the release-toolkit, not externally (as things that are meant to be used externally should be made into actions), so changing its behavior/implementation should only affect the release-toolkit itself, not its clients and (2) the only usages I see of ensure_on_branch! in the release-toolkit are 4 occurrences, which all look like ensure_on_branch!('release'), and which would be easy to change to ensure_on_branch!(pattern: '^release/') to match the new behavior of using current_branch.match?(/#{pattern}/).
That way, no deprecation needed, still having a nice name for that helper, and making it more robust.
There was a problem hiding this comment.
Even if that's the expectation - it's very likely that it's used in the clients so I don't think we should do that. This work is something that popped up as something extra and I want to finish it as quickly as I can to focus on my project.
There was a problem hiding this comment.
Then another option would be to make the existing ensure_on_branch! keep the same (legacy) implementation (with include? if the parameter is a String (for backwards compatibility, until we stop the client projects from using the helper directly, which they shouldn't do), and use the new implementation if the parameter is a RegExp.
def self.ensure_on_branch!(pattern)
current_branch_name = Action.sh('git', 'symbolic-ref', '-q', 'HEAD')
is_right_branch = pattern.is_a?(RegExp) ? current_branch_name.match?(pattern) : current_branch_name.include?(pattern)
UI.user_error!("This command works only on #{branch_name} branch") unless is_right_branch
endd5fe2ec to
0b0669c
Compare
AliSoftware
left a comment
There was a problem hiding this comment.
If you plan to use this as part of your Fastfile to check expected branches, you're gonna have to also create a Fastlane::Action to wrap the call to that helper; because by design the Helpers are only supposed to be used internally by the release-toolkit, not as a public API by the clients and your Fastfile, which instead should only call actions.
NB: Currently we made an exception for this with the Fastlane::Wpmreleasetoolkit::Versioning classes which are used directly in our Fastfiles instead of being exposed as actions. This is a (potentially temporary) exception that we are assuming, and shouldn't be the norm. If any other Helper class is currently being used directly in any of our Fastfile, this should be considered a mistake and should be addressed to replace it with actions.
| # the implementation is different, so it'd be a breaking change for all clients. | ||
| # 3. This method relies on the `current_git_branch` helper which internally | ||
| # uses the `git_branch_name_using_HEAD` fastlane helper. | ||
| def self.ensure_current_branch_using_HEAD(branch) |
There was a problem hiding this comment.
Also, given branch is taken as a regex, I'd make this explicit in the parameter name. Finally, I'd also suggest to use keyword parameters rather than just positional parameters, to enforce the call site having to provide the parameter name and make it even more explicit that this is a RegExp pattern.
| def self.ensure_current_branch_using_HEAD(branch) | |
| def self.ensure_current_branch!(matches:) |
or
| def self.ensure_current_branch_using_HEAD(branch) | |
| def self.ensure_current_branch!(pattern:) |
| # the implementation is different, so it'd be a breaking change for all clients. | ||
| # 3. This method relies on the `current_git_branch` helper which internally | ||
| # uses the `git_branch_name_using_HEAD` fastlane helper. | ||
| def self.ensure_current_branch_using_HEAD(branch) |
There was a problem hiding this comment.
Actually, I like the ensure_on_branch! method name, so I wonder if we shouldn't instead keep that method, update its implementation, and just fix all the call sites.
After all, (1) this method being a helper, it's expected to only be used internally by the release-toolkit, not externally (as things that are meant to be used externally should be made into actions), so changing its behavior/implementation should only affect the release-toolkit itself, not its clients and (2) the only usages I see of ensure_on_branch! in the release-toolkit are 4 occurrences, which all look like ensure_on_branch!('release'), and which would be easy to change to ensure_on_branch!(pattern: '^release/') to match the new behavior of using current_branch.match?(/#{pattern}/).
That way, no deprecation needed, still having a nice name for that helper, and making it more robust.
TIL. With this in mind, I think it's best to drop the helper all together and introduce a fastlane action basically copy pasting the original one from Since that's going to be almost a full rewrite, I am going to close this PR and open a new PR for it. |
What does it do?
As part of the versioning changes @spencertransier worked on, we have started using
ensure_git_branchfastlane action. Unfortunately this action relies ongit_branchaction which uses environment variables to modify the result. This is not what we want in Buildkite because it'll ensure which branch the CI build is started from and ignore any branch switches.We had to introduce our own helper
git_current_branchin #463 to replacegit_branch. Similarly, this PR addsensure_current_branch_using_HEADthat we should use instead ofensure_git_branchso the release actions work as expected in Buildkite.ensure_on_branch!helper method. Since this is not an action, I am not entirely sure how it should be deprecated. I added a comment and started printing a warning, but please let me know if there is an official way to do this.Checklist before requesting a review
bundle exec rubocopto test for code style violations and recommendationsspecs/*_spec.rb) if applicablebundle exec rspecto run the whole test suite and ensure all your tests passCHANGELOG.mdfile to describe your changes under the appropriate existing###subsection of the existing## Trunksection.MIGRATION.mdfile to describe how the changes will affect the migration from the previous major version and what the clients will need to change and consider.