-
Notifications
You must be signed in to change notification settings - Fork 349
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
feat: Determine SCM and parse clone url in GitProvenance for more accurate path/origin/organiation/groups #4367
Closed
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f21f282
feat: Determine SCM in GitProvenance for more accurate path estimation
pstreef 9774c15
Apply suggestions from code review
pstreef 973bed5
rename gitlab scm
pstreef 6e937cf
Comments
pstreef e30b193
fix tests
pstreef 4fd0365
fix boolean
pstreef 9fd2b07
fix git provenance tests
pstreef b331cef
Rename to `ScmUrlComponents` to `CloneUrl` and move logic into that
pstreef 3d5d541
Add full cloneUrl
pstreef cbb7a1a
fully parse on construction
pstreef 453566b
Update rewrite-core/src/test/java/org/openrewrite/scm/GitLabScmTest.java
pstreef 4c2387c
Update rewrite-core/src/main/java/org/openrewrite/scm/AzureDevopsClon…
pstreef 4fef5be
Merge branch 'main' into feat/add-scm-url-component-detection-logic
pstreef aacea53
Apply suggestions from code review
timtebeek c2e9e9c
Add missing newlines at the end of files
timtebeek 5fe218a
Add missing newline at the end of GitLabScmTest
timtebeek 4fefa63
refactor: Azure devops refactoring (#4378)
bryceatmoderne ce49346
new line?
pstreef 5dec1dd
Lazy initialization
pstreef 8d07e18
Lombok compiler issue
pstreef 43d7957
Update rewrite-core/src/main/java/org/openrewrite/internal/Lazy.java
pstreef a13a68a
Replace Lazy with `@NonFinal` field
pstreef f408a35
fix message, use getters
pstreef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
rewrite-core/src/main/java/org/openrewrite/scm/AzureDevOpsScm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.openrewrite.scm; | ||
pstreef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import org.openrewrite.internal.lang.Nullable; | ||
|
||
public class AzureDevOpsScm implements Scm { | ||
|
||
@Override | ||
public String getOrigin() { | ||
return "dev.azure.com"; | ||
} | ||
|
||
@Override | ||
public String cleanHostAndPath(String url) { | ||
UrlComponents uri = UrlComponents.parse(url); | ||
String host = uri.getHost(); | ||
String path = uri.getPath(); | ||
if (uri.isSsh() && host.startsWith("ssh.")) { | ||
host = host.substring(4); | ||
path = path.replaceFirst("v3/", ""); | ||
} else { | ||
path = path.replaceFirst("_git/", ""); | ||
} | ||
String hostAndPath = host + "/" + path | ||
.replaceFirst("\\.git$", ""); | ||
return hostAndPath.replaceFirst("/$", ""); | ||
} | ||
|
||
@Override | ||
public String determineRepositoryName(String path) { | ||
return path.substring(path.lastIndexOf("/") + 1); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String determineProject(String path) { | ||
return path.substring(path.indexOf("/") + 1, path.lastIndexOf("/")); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
rewrite-core/src/main/java/org/openrewrite/scm/BitbucketServerScm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.openrewrite.scm; | ||
pstreef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BitbucketServerScm implements Scm { | ||
private final String origin; | ||
|
||
public BitbucketServerScm(String origin) { | ||
if (origin.startsWith("ssh://") || origin.startsWith("http://") || origin.startsWith("https://")) { | ||
origin = cleanHostAndPath(origin); | ||
} | ||
this.origin = origin; | ||
} | ||
|
||
@Override | ||
public String cleanHostAndPath(String url) { | ||
UrlComponents uri = UrlComponents.parse(url); | ||
String hostAndPath = uri.getHost() + uri.maybePort() + "/" + uri.getPath() | ||
.replaceFirst("^/", "") | ||
.replaceFirst("scm/", "") | ||
.replaceFirst("\\.git$", ""); | ||
return hostAndPath.replaceFirst("/$", ""); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
rewrite-core/src/main/java/org/openrewrite/scm/GroupPathScm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.openrewrite.scm; | ||
pstreef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import lombok.Getter; | ||
import org.openrewrite.internal.lang.Nullable; | ||
|
||
/** | ||
* Can be used for GitLab | ||
*/ | ||
@Getter | ||
public class GroupPathScm implements Scm { | ||
private final String origin; | ||
|
||
public GroupPathScm(String origin) { | ||
if (origin.startsWith("ssh://") || origin.startsWith("http://") || origin.startsWith("https://")) { | ||
origin = cleanHostAndPath(origin); | ||
} | ||
this.origin = origin.replaceFirst("/$", ""); | ||
} | ||
|
||
@Override | ||
public @Nullable String determineGroupPath(String path) { | ||
return path.substring(0, path.lastIndexOf("/")); | ||
} | ||
|
||
@Override | ||
public String determineRepositoryName(String path) { | ||
return path.substring(path.lastIndexOf("/") + 1); | ||
} | ||
|
||
@Override | ||
public String cleanHostAndPath(String url) { | ||
UrlComponents uri = UrlComponents.parse(url); | ||
String hostAndPath = uri.getHost() + uri.maybePort() + "/" + uri.getPath() | ||
.replaceFirst("^/", "") | ||
.replaceFirst("\\.git$", ""); | ||
return hostAndPath.replaceFirst("/$", ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.openrewrite.scm; | ||
pstreef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import org.openrewrite.internal.lang.Nullable; | ||
|
||
public interface Scm extends Comparable<Scm> { | ||
String getOrigin(); | ||
|
||
String cleanHostAndPath(String cloneUrl); | ||
|
||
default boolean belongsToScm(String cloneUrl) { | ||
return cleanHostAndPath(cloneUrl).startsWith(getOrigin()); | ||
} | ||
|
||
@Nullable | ||
default String determineOrganization(String path) { | ||
return path.substring(0, path.indexOf("/")); | ||
} | ||
|
||
@Nullable | ||
default String determineGroupPath(String path) { | ||
return null; | ||
} | ||
|
||
@Nullable | ||
default String determineProject(String path) { | ||
return null; | ||
} | ||
|
||
default String determineRepositoryName(String path) { | ||
return path.substring(path.indexOf("/") + 1); | ||
} | ||
|
||
default ScmUrlComponents determineScmUrlComponents(String cloneUrl) { | ||
String path = cleanHostAndPath(cloneUrl).substring(getOrigin().length() + 1); | ||
return new ScmUrlComponents(getOrigin(), determineOrganization(path), determineGroupPath(path), determineProject(path), determineRepositoryName(path)); | ||
} | ||
|
||
@Override | ||
default int compareTo(Scm o) { | ||
return getOrigin().compareTo(o.getOrigin()); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
rewrite-core/src/main/java/org/openrewrite/scm/ScmUrlComponents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.openrewrite.scm; | ||
pstreef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import lombok.Builder; | ||
import lombok.Value; | ||
import org.openrewrite.internal.lang.Nullable; | ||
|
||
@Value | ||
@Builder | ||
public class ScmUrlComponents { | ||
|
||
@Nullable | ||
String origin; | ||
|
||
@Nullable | ||
String organization; | ||
|
||
@Nullable | ||
String groupPath; | ||
|
||
@Nullable | ||
String project; | ||
|
||
String repositoryName; | ||
|
||
public String getPath() { | ||
if (groupPath != null) { | ||
return groupPath + "/" + repositoryName; | ||
} | ||
if (organization != null) { | ||
if (project != null) { | ||
return organization + "/" + project + "/" + repositoryName; | ||
} | ||
return organization + "/" + repositoryName; | ||
} | ||
return repositoryName; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
rewrite-core/src/main/java/org/openrewrite/scm/SimpleScm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.openrewrite.scm; | ||
pstreef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import lombok.Getter; | ||
|
||
/** | ||
* Simple as in just split by origin/path, and where the path is organization/repositoryName, like GitHub | ||
*/ | ||
@Getter | ||
public class SimpleScm implements Scm { | ||
private final String origin; | ||
|
||
public SimpleScm(String origin) { | ||
if (origin.startsWith("ssh://") || origin.startsWith("http://") || origin.startsWith("https://")) { | ||
origin = cleanHostAndPath(origin); | ||
} | ||
this.origin = origin.replaceFirst("/$", ""); | ||
} | ||
|
||
@Override | ||
public String cleanHostAndPath(String url) { | ||
UrlComponents uri = UrlComponents.parse(url); | ||
String hostAndPath = uri.getHost() + uri.maybePort() + "/" + uri.getPath() | ||
.replaceFirst("^/", "") | ||
.replaceFirst("\\.git$", ""); | ||
return hostAndPath.replaceFirst("/$", ""); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
rewrite-core/src/main/java/org/openrewrite/scm/UnknownScm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.openrewrite.scm; | ||
pstreef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import lombok.Getter; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Fallback that assumes the last 2 path segments of a cloneUrl contains the repository path, not the same as SimpleScm | ||
* which splits on a known origin. | ||
*/ | ||
@Getter | ||
public class UnknownScm extends SimpleScm { | ||
public UnknownScm(String cloneUrl) { | ||
super(cloneUrlToOrigin(cloneUrl)); | ||
} | ||
|
||
private static String cloneUrlToOrigin(String cloneUrl) { | ||
UrlComponents uri = UrlComponents.parse(cloneUrl); | ||
String fullPath = uri.getPath() | ||
.replaceFirst("^/", "") | ||
.replaceFirst("\\.git$", ""); | ||
|
||
String[] segments = fullPath.split("/"); | ||
if (segments.length <= 2) { | ||
return uri.getHost() + uri.maybePort(); | ||
} | ||
String contextPath = String.join("/", Arrays.copyOfRange(segments, 0, segments.length - 2)); | ||
return uri.getHost() + uri.maybePort() + "/" + contextPath; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm not sure it's necessary to deprecate since it is in widespread use. Fine to have a convenience method for organization on
CloneUrl
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.
actually I think we should point back to the "old" method that does not take an argument, and deprecate this one which (again) takes the origin (with scheme).