-
Notifications
You must be signed in to change notification settings - Fork 434
Add recipe to update SCM tag based on Git origin #5647
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
base: main
Are you sure you want to change the base?
Conversation
rewrite-maven/src/main/java/org/openrewrite/maven/search/FindScm.java
Outdated
Show resolved
Hide resolved
rewrite-maven/src/test/java/org/openrewrite/maven/UpdateScmFromGitOriginTest.java
Outdated
Show resolved
Hide resolved
rewrite-maven/src/test/java/org/openrewrite/maven/search/FindScmTest.java
Outdated
Show resolved
Hide resolved
rewrite-maven/src/test/java/org/openrewrite/maven/utilities/ScmValuesTest.java
Outdated
Show resolved
Hide resolved
*/ | ||
package org.openrewrite.maven.utilities; | ||
|
||
public class ScmValues { |
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.
Would be used by future AddScm
recipe.
Also, once higher java is supported, could be replaced by Record
Hmm, I'm not sure why these tests have failed. I don't think my changes impact them. If someone with permissions could help re-trigger the build, that’d be appreciated. |
Thanks for triggering the build, @timtebeek. |
if (origin.startsWith("git@")) { | ||
// SSH origin | ||
String hostAndPath = cleanOrigin.substring("git@".length()).replaceFirst(":", "/"); | ||
url = "https://" + hostAndPath; | ||
connection = "scm:git:https://" + hostAndPath + ".git"; | ||
developerConnection = "scm:git:" + origin; | ||
} else if (origin.startsWith("http://") || origin.startsWith("https://")) { | ||
// HTTPS origin | ||
url = cleanOrigin; | ||
connection = "scm:git:" + origin; | ||
String sshPath = cleanOrigin | ||
.replaceFirst("^https?://", "") // github.com/user/repo | ||
.replaceFirst("/", ":"); // github.com:user/repo | ||
developerConnection = "scm:git:git@" + sshPath + ".git"; | ||
} else { | ||
url = cleanOrigin; | ||
connection = "scm:git:" + origin; | ||
developerConnection = "scm:git:" + origin; | ||
} |
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 wonder a bit about the logic here specifically for developerConnection
, as we already have GitRemote to go in the other direction that seems to differ for some Service
s. Would it make sense to harmonize these here?
rewrite/rewrite-core/src/main/java/org/openrewrite/GitRemote.java
Lines 100 to 103 in 9a001ee
servers.add(new RemoteServer(Service.GitHub, "github.com", URI.create("https://github.com"), URI.create("ssh://git@github.com"))); | |
servers.add(new RemoteServer(Service.GitLab, "gitlab.com", URI.create("https://gitlab.com"), URI.create("ssh://git@gitlab.com"))); | |
servers.add(new RemoteServer(Service.BitbucketCloud, "bitbucket.org", URI.create("https://bitbucket.org"), URI.create("ssh://git@bitbucket.org"))); | |
servers.add(new RemoteServer(Service.AzureDevOps, "dev.azure.com", URI.create("https://dev.azure.com"), URI.create("ssh://git@ssh.dev.azure.com"))); |
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.
Also I feel like maybe we can keep ScmValues
as private class of UpdateScmfromGitOrigin
for now, as we're usually a bit hesitant to add public API surface before we've seen a few more use cases.
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.
Thanks for reviewing and making the changes @timtebeek !
I have just looked closely at GitRemote
class and to be honest, I'm not sure how we could harmonize it.
Tbh, more I look at the values, more confused I get..
AFAIK, in SCM we need:
<url>
which usually is http
or https
, without .git
at the end of the path. So e.g.: <url>https://new-server.example.com/username/repo</url>
We can't use git.getOrigin()
directly (because it may contain value like: git@new-server.example.com:username/repo.git
) so we would need to do some String operations.
Looking at git.getGitRemote()
:
{GitRemote@5224}
service = {GitRemote$Service@5225} "Unknown"
url = "git@new-server.example.com:username/repo.git"
origin = "new-server.example.com"
path = "username/repo"
organization = "username"
repositoryName = "repo"
we could build url
from origin
+ path
+ .git
. We would also need to derive the protocol from url
.
So it's kind of the same as we do in ScmValues
, isn't it?
I also wonder if for origin
that has ssh://
as protocol, shall we still use https
or keep ssh://
in url
?
<connection>
starts with scm:git:
and then we could put value of <url>
+ .git
. We can't use url
directly.
<developerConnection>
starts with scm:git:
. Then I'm not sure if we should always go with e.g. git@github.com:username/repo.git
even if git.origin
contains https
as protocol. Because in fact 'write-access' could also go via https afaik.
For ssh://
I would assume it's: scm:git:ssh://git@github.com/username/repo.git
In any case, I'm not sure if GitRemote
can help here. For reference, for https
protocol we get:
result = {GitRemote@5222}
service = {GitRemote$Service@5223} "Unknown"
url = "https://new-server.example.com/username/repo.git"
origin = "new-server.example.com"
path = "username/repo"
organization = "username"
repositoryName = "repo"
--
Did I miss anything? Are these SCM values Service
-specific in any way? Could we easily derive them from GitRemote
?
Another idea would be to extend GitRemote
with the logic to derive the values, but not sure if scm-specific logic really belongs there.
Regarding making ScmValues
class private - happy to do so if we decide to keep it.
Having that said, I wonder how we’ll reuse this logic when creating a new Recipe to add the tag (#5561). (But maybe that’s a tomorrow's problem :) )
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.
Great to see already! Shaping up nicely. I made some small changes already, and a suggestion to reduce the public API we start with; I think once addressed we could add this fairly soon after. Thanks again!
What's changed?
UpdateScmFromGitOrigin
that updates the<scm>
section inpom.xml
based on the Git origin URL.ScmValues
for URL generation logic.FindScm
used as a precondition to check if<scm>
tag is present.What's your motivation?
scm
configuration to match theorigin
of the repository #5560Anything in particular you'd like reviewers to focus on?
As it's my first recipe, I would say:
Visitor
+ use ofPreconditions.check(new FindScm(), ...)
git
ScmValues
logic for various Git origin formatsChecklist