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

Allow package when referencing rules in github #1817

Merged
merged 3 commits into from
Jul 21, 2023

Conversation

RustedBones
Copy link
Contributor

Referencing scalafix rules does not currently allow rules to be located in a package other than fix.
Eg. in scio we've grouped rules in packages corresponding to the breaking version.

This change allows to reference a rule based on its package name (stripping the required fix package prefix)

import metaconfig.Conf
import metaconfig.ConfError
import metaconfig.Configured
import metaconfig.Configured.Ok

object GitHubUrlRule {

private val DefaultBranch = "master"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this actually be changed to main ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That sounds like a good idea to favor main over master. However, for backward compatibility with all repos and instructions out there, I would like to keep the fallback to master if we get a 404 on main. Maybe for another PR?

Copy link
Collaborator

@bjaglin bjaglin left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @RustedBones, and sorry for the delay. I am definitely in favor of merging this after a few minor adjustments 👍

import metaconfig.Conf
import metaconfig.ConfError
import metaconfig.Configured
import metaconfig.Configured.Ok

object GitHubUrlRule {

private val DefaultBranch = "master"
Copy link
Collaborator

Choose a reason for hiding this comment

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

That sounds like a good idea to favor main over master. However, for backward compatibility with all repos and instructions out there, I would like to keep the fallback to master if we get a 404 on main. Maybe for another PR?

@@ -37,6 +37,11 @@ class GitHubUrlRuleSuite extends AnyFunSuite with DiffAssertions {
"https://raw.githubusercontent.com/someorg/some-repo/master/scalafix/rules/" +
"src/main/scala/fix/RuleName.scala"
)
check(
"github:someorg/some-repo/version.RuleName",
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we add a test with several segments? like my.package.RuleName

Copy link
Collaborator

Choose a reason for hiding this comment

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

also, a test with the sha maybe? (it's not documented but I would expect the most verbose/complicated syntax to support all features)

@@ -32,17 +36,21 @@ object GitHubUrlRule {
private def guessGitHubURL(
org: String,
repo: String,
filename: String,
rule: String,
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe className (here and above) to highlight that . is used as separator and that extension is not provided?

Suggested change
rule: String,
className: String,

@@ -54,35 +62,27 @@ object GitHubUrlRule {
private val GitHubFallback =
"""github:(.*)""".r

private val alphanumerical = "[^a-zA-Z0-9]"
private val NonAlphaNumeric = "[^a-zA-Z0-9]"
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@@ -654,6 +654,7 @@ The expansion rules for `github:org/repo` are the following:
| `github:org/repo` | `scalafix/rules/src/main/scala/fix/Repo.scala` |
| `github:org/some-repo` | `scalafix/rules/src/main/scala/fix/SomeRepo.scala` |
| `github:org/repo/RuleName` | `scalafix/rules/src/main/scala/fix/RuleName.scala` |
| `github:org/repo/package.RuleName` | `scalafix/rules/src/main/scala/fix/package/RuleName.scala` |
Copy link
Collaborator

Choose a reason for hiding this comment

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

if package is explicit, shouldn't we remove the fix prefix?

Suggested change
| `github:org/repo/package.RuleName` | `scalafix/rules/src/main/scala/fix/package/RuleName.scala` |
| `github:org/repo/package.RuleName` | `scalafix/rules/src/main/scala/package/RuleName.scala` |

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks that scalafix requires the root package to be fix.
When only the rule name is provided, it searches in the fix package, this changes keep the same behavior

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks that scalafix requires the root package to be fix.

I am not aware of this constraint in scalafix-core, and none of the built-in rules is rooted in fix. Can you elaborate where you observed this limitation?

When only the rule name is provided, it searches in the fix package, this changes keep the same behavior

I agree that the existing behavior should remain, but it feels strange that the user provides a package that is only a suffix. If the example would look like this, would it make more sense?

Suggested change
| `github:org/repo/package.RuleName` | `scalafix/rules/src/main/scala/fix/package/RuleName.scala` |
| `github:org/repo/fix.package.RuleName` | `scalafix/rules/src/main/scala/fix/package/RuleName.scala` |

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, I double checked scalafix uses the rule name only, regardless of the class name/package.
I agree with the suggestion. Will update the PR

Copy link
Collaborator

@bjaglin bjaglin left a comment

Choose a reason for hiding this comment

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

Thanks!

| `github:org/some-repo` | (on `master`) `scalafix/rules/src/main/scala/fix/SomeRepo.scala` |
| `github:org/repo/RuleName` | (on `master`) `scalafix/rules/src/main/scala/fix/RuleName.scala` |
| `github:org/repo/com.example.RuleName` | (on `master`) `scalafix/rules/src/main/scala/com/example/RuleName.scala` |
| `github:org/repo/RuleName?sha=main` | (on `main`) `scalafix/rules/src/main/scala/fix/RuleName.scala` |
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@bjaglin bjaglin merged commit 3a9e668 into scalacenter:main Jul 21, 2023
@RustedBones RustedBones deleted the github-rule-package branch July 22, 2023 10:09
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.

2 participants