Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

Add support for scope validation #65

Conversation

Garbee
Copy link
Collaborator

@Garbee Garbee commented Jan 24, 2017

This adds support for validating allowed scopes. To do this we provide a scopes option object. It can contain one of four possible keys:

  • required - Boolean to determine if a scope is required
  • multiple - Boolean to allow for multiple scopes or a singular scope only
  • allowed - Array of possible values or '*' for anything.
  • validate - Boolean to turn on validation

This does currently use an ES6 arrow function. So, that may increase the required node version. I'm not sure what the minimum supported version is. If the arrow function is a problem then I can refactor this out.

Copy link
Collaborator

@spirosikmd spirosikmd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for the PR, nice feature! Few comments from my side!

"name": "Jonathan Garbee",
"avatar_url": "https://avatars.githubusercontent.com/u/868301?v=3",
"profile": "http://jonathan.garbee.me",
"contributions": []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add your type of contribution by using yarn add-contributor, in this case it would be code, test, doc?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gah, I did. It failed and I didn't catch that looking at it. At least I think I selected something... I'll get that fixed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try again and if for some reason it doesn't work, do let us know. Maybe there is another issue? Thanks!

@@ -34,6 +34,12 @@ You can specify options in `.vcmrc`
```js
{
"types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "chore", "revert"], // default
"scopes": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would maybe have this a singular. types is plural as the actual value is an array. But in this case I see this more as config for scope property of the commit message. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM.

var allowedScopes = config.scopes.allowed || '*';
var scopeRequired = config.scopes.required || false;
var scopes = scope ? scope.split(',') : [];
var validateScope = (item) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not ready yet I believe (correct me if I am wrong @kentcdodds) to drop support for 0.10.x. You could refactor this to es5 function?

Another idea, as this function is getting too big, would be to split all these validations. We can start by splitting the scope if (validateScopes) {...} validation and move it to lib/validation.js which could export validation functions? This should make the code easier to test in isolation as well. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactoring this is pretty easy. I just need to convert it to a standard for loop instead of using forEach on the array. Was just trying to keep it as concise as possible.

+1 to finding a way to split things up. However, I'd rather we discuss that in its own issue and PR to implement (which I'm happy working on.) That way this PR is as concise as possible to the addition being added.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we've ever supported 0.10.x. But I think we can drop 0.12.x too since it's no longer officially supported by NodeJS anyway.

As far as concise-ness, I'm not at all interested in making things concise. Let's focus on making things readable/maintainable for future contributors.

Copy link
Collaborator

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer the scope validation to at least be contained in a function on its own just to make things a little more organized.

Thank you so much for your contribution! This is great! <3

@Garbee
Copy link
Collaborator Author

Garbee commented Jan 25, 2017

Ok, I'll get it into its own function and address the other things tomorrow.

Thank you.

@Garbee
Copy link
Collaborator Author

Garbee commented Jan 25, 2017

So, dumb me forgot for a moment JS functions just go up the scope chain when you're calling a variable and it doesn't exist in the current scope. So the fat arrow wasn't even necessary, yay.

Just moved the check into a function, that takes the current valid state and the scope to check as parameters. At the end, returning the valid state for the main process to continue as normal.

The add-contributor method also works just fine. I just didn't press spacebar to select something, I assumed enter would do that if I only was on one with the cursor.

Copy link
Collaborator

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this is failing is due to code coverage not 100%. You should be able to run the tests locally and open files in coverage in a browser to see what's missing. Thanks for working on this!

@@ -21,6 +21,45 @@ var error = function() {

exports.config = config;

var validateScope = function(isValid, scope) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We're not doing linting (yet), but I prefer FunctionDeclarations over FunctionExpressions (so this would be: function validateScope(isValid, scope) { ... })

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And actually, because this is a utility method, I prefer that it go at the bottom of the file. Read more about my preference here

var allowedScopes = config.scope.allowed || '*';
var scopeRequired = config.scope.required || false;
var scopes = scope ? scope.split(',') : [];
var validateScope = function(item) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a function declaration for this one as well, also put at the bottom?

…rty now . Validation moved into its own function. Fat arrow removed

since it wasn't necessary.
@Garbee
Copy link
Collaborator Author

Garbee commented Jan 26, 2017

So, the declarations are changed and the function is moved to the bottom with this update. However, the inner function to validate a single scope is still contained within the function instead of being on its own. Because, it is relying on the variables within the scope it is defined so it can be as simple as possible.

If it were to get moved out, we'd need to pass in the allowedScopes and isValid, returning isValid and then assigning the primary scopes validity state to that as the loop happens. That would complicate the code much more for people trying to maintain in later then what the current setup provides.

I'm looking into adding more test coverage now, but that is the update on the code style. If anyone has an idea on a pattern to let the inner function get moved out without overcomplicating utilizing it then I'm all ears.

Provides full code coverage to the new functionality.
@codecov-io
Copy link

Current coverage is 100% (diff: 100%)

Merging #65 into master will not change coverage

@@           master   #65   diff @@
===================================
  Files           3     3          
  Lines          94   121    +27   
  Methods         0     0          
  Messages        0     0          
  Branches        0     0          
===================================
+ Hits           94   121    +27   
  Misses          0     0          
  Partials        0     0          

Powered by Codecov. Last update 04facc4...3149921

Copy link
Collaborator

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me! 👍 I'll leave it to one of the other collaborators to review and merge.

Thank you so much for this contribution!

Copy link
Collaborator

@spirosikmd spirosikmd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thank you very much for this feature 🎉

@spirosikmd spirosikmd merged commit 815bc77 into conventional-changelog-archived-repos:master Jan 26, 2017
@spirosikmd
Copy link
Collaborator

@kentcdodds should we create a new release for this?

@kentcdodds
Copy link
Collaborator

That happens automatically ;-) Check the releases page on this repo, it's probably already done.

Learn more about how that works here :)

@kentcdodds
Copy link
Collaborator

Whoops! Looks like my GitHub token and NPM token are busted... hold please.

@kentcdodds
Copy link
Collaborator

Weird, it appears travis doesn't have a build for the latest version of master... 🤔

@Garbee Garbee deleted the garbee/filter-scopes branch January 26, 2017 16:45
@Garbee
Copy link
Collaborator Author

Garbee commented Jan 26, 2017

Thank you all! If anyone reports a bug with the scope checking (or wants it modified somehow) just ping me. I don't mind hopping back in to work on it.

@kentcdodds
Copy link
Collaborator

Hey @Garbee, thanks so much for your help! I've added you as a collaborator on the project. Please make sure that you review the CONTRIBUTING.md file (specifically the bit about the commit messages and the git hooks) and familiarize yourself with the code of conduct (we're using the contributor covenant). You might also want to watch the repo to be notified when someone files an issue/PR. Please continue to make PRs as you feel the need (you can make your branches directly on the repo rather than your fork if you want). Thanks! And welcome to the team :)

@spirosikmd
Copy link
Collaborator

@kentcdodds thank you very much for the link on writing open source!

@kentcdodds
Copy link
Collaborator

Alrighty, this has now been released as the latest version. Sorry for the trouble earlier! Thanks again!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants