-
Notifications
You must be signed in to change notification settings - Fork 113
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
Optional Git URL rewrite rule #908
Optional Git URL rewrite rule #908
Conversation
…r and pass it in the git module
/assign @gabemontero |
@MayukhSobo can you please update the description so that a release note is specified |
@gabemontero Can you please elaborate what exactly you meant? |
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.
@@ -313,6 +320,13 @@ func clone(ctx context.Context) error { | |||
if _, err := git(ctx, "config", "--global", "credential.helper", fmt.Sprintf("store --file %s", credHelperFile.Name())); err != nil { | |||
return err | |||
} | |||
if flagValues.gitURLRewrite { | |||
sshURL := convertToSSH(repoURL.Host) | |||
if _, err := git(ctx, "config", "--global", sshURL); err != nil { |
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.
Does that work? In examples such as https://stackoverflow.com/questions/1722807/how-to-convert-git-urls-to-http-urls, there are always two arguments ("url.https://github.com/.insteadOf" and "git://github.com/"). You're passing in the sshURL
variable that contains both as one argument.
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.
@SaschaSchwarze0 @HeavyWombat Well I haven't tested it but I am sure it would work. If you would take a careful look into the method/function convertToSSH
, you would notice that there is a space between url.ssh://git@%s/.insteadOf
and https://%s/"
. This ensures that you are passing two space-separated commands. So unless the git(ctx, "config", "--global", sshURL);
works differently, this would work well.
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, the reason to merge this into one function is that for such a trivial task, I felt 2 function calls or polluting the local namespace would be costlier in the future.
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.
@MayukhSobo you can be sure that I take a careful look at code that I am reviewing. And I obviously saw the space. Nevertheless, you are running this command:
git config --global "url.ssh://git@github.com/.insteadOf https://github.com/"
while this is probably the correct one:
git config --global url.ssh://git@github.com/.insteadOf https://github.com/
But once you test this, I am sure you notice that on your own.
the release note section in your PR's description here should have some text following the Release Note heading. See @SaschaSchwarze0 's PR #909 (comment) for example. Your change here seems like it should have release not text. From what I see from examining your description, you did not make any changes from what our PR template provided. Try editing the description, reading the instructions provided by the PR template, and see if you can get a properly formatted release note. If you have problem interpreting the instructions, let us know here in the PR and we'll go from there. |
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.
First of all, thanks for your efforts here. I left a comment and I agree with @SaschaSchwarze0, it does not look correct how the config command looks like. Left a suggestion as a starting point. Also, please have a look into options on how to create a unit test for this feature. Lastly, like @gabemontero already pointed out, we need a release notes section. Have a look at previous PRs to get a feeling.
@@ -52,6 +52,7 @@ type settings struct { | |||
resultFileCommitAuthor string | |||
secretPath string | |||
skipValidation bool | |||
gitURLRewrite bool |
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.
Nit: There seems to be a mix up of spaces versus tabs. Have a look whether you can unify that.
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.
Yes, I saw that previously. Not sure how to fix that. It seems like I am using Goland IDE
which is causing it. I would try to fix it but don't you think go fmt
should have auto-corrected it?
if flagValues.gitURLRewrite { | ||
sshURL := convertToSSH(repoURL.Host) | ||
if _, err := git(ctx, "config", "--global", sshURL); err != nil { | ||
return err | ||
} | ||
} | ||
|
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 agree with @SaschaSchwarze0, the Git config command seems incomplete and is also nested under the wrong if branch. This logic should be independent of the input type. With that being said, I hacked together a suggestion that is extracting the host from the flagValues.url
. It then uses the host name to create the Git config command. Maybe you can use this as a start to continue to work.
if flagValues.gitURLRewrite { | |
sshURL := convertToSSH(repoURL.Host) | |
if _, err := git(ctx, "config", "--global", sshURL); err != nil { | |
return err | |
} | |
} | |
} | |
if flagValues.gitURLRewrite { | |
var hostname string | |
switch { | |
case strings.HasPrefix(flagValues.url, "git@"): | |
trimmed := strings.TrimPrefix(flagValues.url, "git@") | |
splitted := strings.SplitN(trimmed, ":", 2) | |
hostname = splitted[0] | |
case strings.HasPrefix(flagValues.url, "http"): | |
repoURL, err := url.Parse(flagValues.url) | |
if err != nil { | |
return err | |
} | |
hostname = repoURL.Host | |
default: | |
return fmt.Errorf("unknown URL type: %q", flagValues.url) | |
} | |
_, err := git(ctx, | |
"config", | |
"--global", | |
fmt.Sprintf("url.ssh://git@%s/.insteadOf", hostname), | |
fmt.Sprintf("https://%s/", hostname)) | |
if err != nil { | |
return err | |
} | |
} |
@@ -34,6 +34,8 @@ spec: | |||
value: "shipwright-build" | |||
- name: GIT_CONTAINER_IMAGE | |||
value: ko://github.com/shipwright-io/build/cmd/git | |||
- name: GIT_REWRITE_RULE | |||
value: 'true' |
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.
Nit: Keeping the "style" of the other string.
value: 'true' | |
value: "true" |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Superseded by #937 |
Changes
Is your feature request related to a problem? Please describe.
Based on user feedback, there could be a scenario where someone defines a Git repository as the build source that contains a submodule that uses a different Git URL style. There are two known styles:
For the HTTPS style, we assume users configure publicly available repositories that do not require any authorization. For the sake of completeness, the HTTPS URLs can be used with deploy key credentials, but we do not openly advertise this options in the docs. The Git+SSH style must come with credentials that contain SSH keys, obviously.
Describe the solution you'd like
When working with GitHub Enterprise (or any other private Git instance for that matter), developers often implement a rewrite rule to use the somewhat more readable HTTPS URLs while Git is working via SSH under the cover. For this, users run the following command once:
git config --global url.ssh://git@github.ibm.com/.insteadOf https://github.ibm.com/
This makes for a smoother working as you do not have to check the style each time, however, it covers a bit of the actual things that happen. Now, we could introduce the same "magic" in the Git step used by Shipwright build. However, this needs to be generic, as we work with different Git services, like GitHub, or GitLab. Also, we should not make that the default behavior right from the start, so I would suggest to make it configurable via a command line flag (--git-url-rewrite-rule).
The flag would be set by our controller setting up the Git step. And the controller would look-up the setting by checking an environment variable (since we have no config map for this).
Describe alternatives you've considered
Provide documentation about this pit-fall and reference it whenever someone runs into it.
Additional context
References:
Submitter Checklist
See the contributor guide
for details on coding conventions, github and prow interactions, and the code review process.
Release Notes