Skip to content

Conversation

@nakabonne
Copy link
Member

What this PR does / why we need it:
This PR does:

  • adding utility functions to make URLs
  • calling one of them MakeDirURL when adding an Application
  • calling one of them MakeCommitURL when building a Deployment

Which issue(s) this PR fixes:

Fixes #414

Does this PR introduce a user-facing change?:

NONE


func buildDeploment(app *model.Application, branch string, commit git.Commit, commander string, now time.Time) (*model.Deployment, error) {
func buildDeployment(app *model.Application, branch string, commit git.Commit, commander string, now time.Time) (*model.Deployment, error) {
commitURL, err := git.MakeCommitURL(app.GitPath.Repo.Remote, commit.Hash)
Copy link
Member Author

Choose a reason for hiding this comment

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

Panic could be occurred because old Applications don't have Repo.
But proto validate ensures that Repo is populated, so I keep this implementation.
https://github.com/pipe-cd/pipe/blob/af263d98e824fd97f5b5e22934f1e188c8f32608/pkg/model/common.proto#L32

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 21.43%. This pull request increases coverage by 0.53%.

File Function Base Head Diff
pkg/app/api/api/web_api.go WebAPI.makeGitPath -- 0.00% +0.00%
pkg/app/piped/trigger/deployment.go buildDeployment -- 0.00% +0.00%
pkg/git/url.go MakeCommitURL -- 100.00% +100.00%
pkg/git/url.go MakeDirURL -- 93.75% +93.75%
pkg/git/url.go parseGitURL -- 100.00% +100.00%
pkg/git/url.go parseTransport -- 100.00% +100.00%
pkg/git/url.go parseScp -- 100.00% +100.00%
pkg/app/api/api/web_api.go WebAPI.AddApplication 0.00% 0.00% +0.00%
pkg/app/piped/trigger/deployment.go buildDeploment 0.00% -- +-0.00%

pkg/git/url.go Outdated
case "bitbucket.org":
subPath = "commits"
default:
return "", fmt.Errorf("unsupported git host: %q", u.Host)
Copy link
Member

Choose a reason for hiding this comment

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

How about GHE where its host could be customized?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm... actually I'm still debating for it.
For now, I'm wondering if "commit" is applied for all of unsupported hosts.

	switch u.Host {
	case "github.com", "gitlab.com":
		subPath = "commit"
	case "bitbucket.org":
		subPath = "commits"
	default:
		subPath = "commit"
	}

Copy link
Member

Choose a reason for hiding this comment

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

I think it is ok to apply this way at this time. In the future, we can allow the user to specify those fields from Web UI while registering the application.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it, gonna do so.

})
}
}
func Test_parseGitURL(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: TestParseGitURL

Copy link
Member Author

Choose a reason for hiding this comment

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

Intellij IDEA generates this name (maybe to distinguish between public ParseGitURL and private)...
But a bit gross, fix it.

return &webservice.AddApplicationResponse{}, nil
}

// Adds Repository info and then makes the GitPath URL.
Copy link
Member

Choose a reason for hiding this comment

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

nit: "makeGitPath ..."


// Adds Repository info and then makes the GitPath URL.
func (a *WebAPI) makeGitPath(ctx context.Context, repoID, path, cfgFilename, pipedID string) (*model.ApplicationGitPath, error) {

Copy link
Member

Choose a reason for hiding this comment

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

nit: Remove empty line.

}
u, err := git.MakeDirURL(repo.Remote, path, repo.Branch)
if err != nil {
return nil, err
Copy link
Member

Choose a reason for hiding this comment

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

Return a gRPC error. This should error be logged and then return an internal error.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're quite right.

"ftp": struct{}{},
"ftps": struct{}{},
"rsync": struct{}{},
"file": struct{}{},
Copy link
Member

Choose a reason for hiding this comment

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

https://git-scm.com/docs/git-clone#_git_urls

Git supports ssh, git, http, and https protocols (in addition, ftp, and ftps can be used for fetching, but this is inefficient and deprecated; do not use it).

Because ftp and other protocols were deprecated, so we only need to support ssh, git, http[s].

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I was not sure that. Thanks.

}

func buildDeploment(app *model.Application, branch string, commit git.Commit, commander string, now time.Time) (*model.Deployment, error) {
func buildDeployment(app *model.Application, branch string, commit git.Commit, commander string, now time.Time) (*model.Deployment, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Better than panic, let's add a check for the existence of Repo field and return an error if it was nil.

Copy link
Member Author

Choose a reason for hiding this comment

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

Instead of returning an error, I decided to ignore if it's nil

if err != nil {
return nil, err
}
repo := &model.ApplicationGitRepository{}
Copy link
Member

Choose a reason for hiding this comment

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

var repo *model.ApplicationGitRepository

and return an error when the repository was not found.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 21.41%. This pull request increases coverage by 0.51%.

File Function Base Head Diff
pkg/app/api/api/web_api.go WebAPI.makeGitPath -- 0.00% +0.00%
pkg/app/piped/trigger/deployment.go buildDeployment -- 0.00% +0.00%
pkg/git/url.go MakeCommitURL -- 100.00% +100.00%
pkg/git/url.go MakeDirURL -- 93.75% +93.75%
pkg/git/url.go parseGitURL -- 100.00% +100.00%
pkg/git/url.go parseTransport -- 100.00% +100.00%
pkg/git/url.go parseScp -- 100.00% +100.00%
pkg/app/api/api/web_api.go WebAPI.AddApplication 0.00% 0.00% +0.00%
pkg/app/piped/trigger/deployment.go buildDeploment 0.00% -- +-0.00%

func TestParseGitURL(t *testing.T) {
tests := []struct {
name string
rawurl string
Copy link
Member

Choose a reason for hiding this comment

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

nit: rawURL

tests := []struct {
name string
rawurl string
wantU *url.URL
Copy link
Member

Choose a reason for hiding this comment

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

nit: wantURL

if !reflect.DeepEqual(gotU, tt.wantU) {
t.Errorf("parseGitURL() got = %#v, want %#v", gotU, tt.wantU)
}
})
Copy link
Member

Choose a reason for hiding this comment

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

We are using testify for testing so let's use its functions to unify the code.

assert.Equal(t, tc.expectedURL, gotURL)
assert.Equal(t, tc.exepcetedErr, err)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok

@nghialv
Copy link
Member

nghialv commented Aug 12, 2020

LGTM

@pipecd-bot pipecd-bot added size/L and removed size/XL labels Aug 12, 2020
@pipecd-bot
Copy link
Collaborator

TODO

The following ISSUES will be created once got merged. If you want me to skip creating the issue, you can use /todo skip command.

Details

1. Allow users to specify git host

https://github.com/pipe-cd/pipe/blob/b023edfd7d2fe7ea724001bc32f40dd0705b4d4c/pkg/git/url.go#L46-L49

This was created by todo plugin since "TODO:" was found in b023edf when #594 was merged. cc: @nakabonne.

2. Support more git host

https://github.com/pipe-cd/pipe/blob/b023edfd7d2fe7ea724001bc32f40dd0705b4d4c/pkg/git/url.go#L75-L78

This was created by todo plugin since "TODO:" was found in b023edf when #594 was merged. cc: @nakabonne.

3. Allow users to specify git host

https://github.com/pipe-cd/pipe/blob/b023edfd7d2fe7ea724001bc32f40dd0705b4d4c/pkg/git/url.go#L79-L82

This was created by todo plugin since "TODO:" was found in b023edf when #594 was merged. cc: @nakabonne.

@pipecd-bot
Copy link
Collaborator

COVERAGE

Code coverage for golang is 21.41%. This pull request increases coverage by 0.51%.

File Function Base Head Diff
pkg/app/api/api/web_api.go WebAPI.makeGitPath -- 0.00% +0.00%
pkg/app/piped/trigger/deployment.go buildDeployment -- 0.00% +0.00%
pkg/git/url.go MakeCommitURL -- 100.00% +100.00%
pkg/git/url.go MakeDirURL -- 93.75% +93.75%
pkg/git/url.go parseGitURL -- 100.00% +100.00%
pkg/git/url.go parseTransport -- 100.00% +100.00%
pkg/git/url.go parseScp -- 100.00% +100.00%
pkg/app/api/api/web_api.go WebAPI.AddApplication 0.00% 0.00% +0.00%
pkg/app/piped/trigger/deployment.go buildDeploment 0.00% -- +-0.00%

@nakabonne
Copy link
Member Author

Fixed

@nghialv
Copy link
Member

nghialv commented Aug 12, 2020

Thank you.
/approve

@pipecd-bot
Copy link
Collaborator

APPROVE

This pull request is APPROVED by nghialv.

Approvers can cancel the approval by writing /approve cancel in a comment. Any additional commits also will change this pull request to be not-approved.

@pipecd-bot pipecd-bot merged commit 25cf0ea into master Aug 12, 2020
@pipecd-bot pipecd-bot deleted the git-urls branch August 12, 2020 03:09
@pipecd-bot
Copy link
Collaborator

TEMPLATE

failed to handle template rule examples

Details
Error: unabled to push rendered files to destination

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generation for repository HTML URL

4 participants