Skip to content
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

fix: validate period flag #67

Merged
merged 1 commit into from
Nov 28, 2023

Conversation

k1nho
Copy link
Contributor

@k1nho k1nho commented Nov 9, 2023

Description

This PR adds a range validation to the insights contributors command. It will check for the value of the period flag. In case the period provided is invalid, it returns a descriptive error to signal that the API only accepts (7,30,90) as period values.

Before

$ pizza insights contributors open-sauced/app -p 45
Error: error while calling 'ContributorsServiceAPI.FindAllRecentPullRequestContributors' with repository 501028599': 400 Bad Request
error while calling 'ContributorsServiceAPI.FindAllRepeatPullRequestContributors' with repository 501028599: 400 Bad Request
error while calling 'ContributorsServiceAPI.FindAllChurnPullRequestContributors' with repository 501028599': 400 Bad Request
error while calling 'ContributorsServiceAPI.NewPullRequestContributors' with repository 501028599': 400 Bad Request

Usage:
  pizza insights contributors url... [flags]
...

Now

$ pizza insights contributors open-sauced/app -p 45
Error: invalid period: 45, accepts (7,30,90)

Usage:
  pizza insights contributors url... [flags]
...

What type of PR is this? (check all applicable)

  • 🍕 Feature
  • 🐛 Bug Fix
  • 📝 Documentation Update
  • 🎨 Style
  • 🧑‍💻 Code Refactor
  • 🔥 Performance Improvements
  • ✅ Test
  • 🤖 Build
  • 🔁 CI
  • 📦 Chore (Release)
  • ⏩ Revert

Related Tickets & Documents

#66 --period flag broken

Mobile & Desktop Screenshots/Recordings

Added tests?

  • 👍 yes
  • 🙅 no, because they aren't needed
  • 🙋 no, because I need help

Added to documentation?

  • 📜 README.md
  • 📓 docs.opensauced.pizza
  • 🍕 dev.to/opensauced
  • 📕 storybook
  • 🙅 no documentation needed

[optional] Are there any post-deployment tasks we need to perform?

[optional] What gif best describes this PR or how it makes you feel?

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

PR Compliance Checks

Thank you for your Pull Request! We have run several checks on this pull request in order to make sure it's suitable for merging into this project. The results are listed in the following section.

Issue Reference

In order to be considered for merging, the pull request description must refer to a specific issue number. This is described in our Contributing Guide.
This check is looking for a phrase similar to: "Fixes #XYZ" or "Resolves #XYZ" where XYZ is the issue number that this PR is meant to address.

Copy link
Member

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

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

Looks great @k1nho! One non-blocking suggestion.

Comment on lines 4 to 7
if period == 7 || period == 30 || period == 90 {
return true
}
return false
Copy link
Member

Choose a reason for hiding this comment

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

refactor (non-blocking): No need for an explicit return of true or false unless this a preferred pattern in Go (I'm not a Gopher 😅)

Suggested change
if period == 7 || period == 30 || period == 90 {
return true
}
return false
return period == 7 || period == 30 || period == 90

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a pattern, might be a side effect from too much if err != nil 😂

Copy link
Member

Choose a reason for hiding this comment

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

return period == 7 || period == 30 || period == 90

is valid Go and it really comes down to personal preference: there will be people who say this is a prime use case for a switch in Go in order to reduce the boolean mind games someone reading your code might need to do to figure out what's going on. Like:

switch range {
case 7:
	return true
case 30:
	return true
case 90:
	return true
default:
	// invalid range
	return false
}

but others would say that's too verbose and ends up being crufty 🤷🏼

Anywho, I'm good with what's written here and makes sense to me!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there will be people who say this is a prime use case for a switch in Go in order to reduce the boolean mind games

true, there is also this way with the switch.

switch range {
case 7,30,90:
	return true
default:
	// invalid range
	return false
}

There is always the even-odd check meme way of doing it as well xD

@k1nho k1nho force-pushed the fix/validate-api-range-value branch from ba87ce9 to 4202355 Compare November 9, 2023 18:40
Copy link
Member

@jpmcb jpmcb left a comment

Choose a reason for hiding this comment

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

Looks great! just one small suggestion!! Thanks for this - and thanks for your patience: I've been heads down on abunch of stuff the last few weeks!

@@ -0,0 +1,5 @@
package api

func ValidateRange(period int32) bool {
Copy link
Member

Choose a reason for hiding this comment

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

Since this is a boolean function, can we rename this IsValidRange?

That typically reads better:

if !api.IsValidRange(opts.Period)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for the feedback!

@k1nho k1nho force-pushed the fix/validate-api-range-value branch from 4202355 to 4d8ab7f Compare November 22, 2023 17:17
@jpmcb jpmcb merged commit cc214cc into open-sauced:beta Nov 28, 2023
6 checks passed
@udaypatel1 udaypatel1 mentioned this pull request Oct 21, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants