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

Optional Git URL rewrite rule #908

Closed

Conversation

MayukhSobo
Copy link
Contributor

@MayukhSobo MayukhSobo commented Oct 20, 2021

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:

HTTPS style, e.g. https://github.com/shipwright-io/build.git
Git+SSH style, e.g. git@github.com:shipwright-io/build.git

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

  • Includes tests if functionality changed/was added
  • Includes docs if changes are user-facing
  • Set a kind label on this PR
  • Release notes block has been filled in, or marked NONE

See the contributor guide
for details on coding conventions, github and prow interactions, and the code review process.

Release Notes

@openshift-ci openshift-ci bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note Label for when a PR has specified a release note labels Oct 20, 2021
@MayukhSobo MayukhSobo changed the title Updated the env configs for the rewrite rule Introduce optional Git URL rewrite rule Oct 20, 2021
@MayukhSobo MayukhSobo changed the title Introduce optional Git URL rewrite rule Optional Git URL rewrite rule Oct 20, 2021
@MayukhSobo
Copy link
Contributor Author

/assign @gabemontero

@MayukhSobo MayukhSobo marked this pull request as ready for review October 20, 2021 09:30
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 20, 2021
@gabemontero
Copy link
Member

@MayukhSobo can you please update the description so that a release note is specified

@MayukhSobo
Copy link
Contributor Author

@gabemontero Can you please elaborate what exactly you meant?

Copy link
Member

@SaschaSchwarze0 SaschaSchwarze0 left a 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 {
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

@gabemontero
Copy link
Member

@gabemontero Can you please elaborate what exactly you meant?

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.

Copy link
Contributor

@HeavyWombat HeavyWombat left a 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
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Comment on lines +323 to +329
if flagValues.gitURLRewrite {
sshURL := convertToSSH(repoURL.Host)
if _, err := git(ctx, "config", "--global", sshURL); err != nil {
return err
}
}

Copy link
Contributor

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.

Suggested change
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'
Copy link
Contributor

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.

Suggested change
value: 'true'
value: "true"

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Oct 26, 2021

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please ask for approval from gabemontero after the PR has been reviewed.

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@HeavyWombat
Copy link
Contributor

Superseded by #937

@HeavyWombat HeavyWombat closed this Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note Label for when a PR has specified a release note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants