Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/app/api/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
"//pkg/app/api/service/webservice:go_default_library",
"//pkg/app/api/stagelogstore:go_default_library",
"//pkg/datastore:go_default_library",
"//pkg/git:go_default_library",
"//pkg/model:go_default_library",
"//pkg/rpc/rpcauth:go_default_library",
"@com_github_google_uuid//:go_default_library",
Expand Down
34 changes: 32 additions & 2 deletions pkg/app/api/api/web_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pipe-cd/pipe/pkg/app/api/service/webservice"
"github.com/pipe-cd/pipe/pkg/app/api/stagelogstore"
"github.com/pipe-cd/pipe/pkg/datastore"
"github.com/pipe-cd/pipe/pkg/git"
"github.com/pipe-cd/pipe/pkg/model"
"github.com/pipe-cd/pipe/pkg/rpc/rpcauth"
)
Expand Down Expand Up @@ -288,14 +289,17 @@ func (a *WebAPI) AddApplication(ctx context.Context, req *webservice.AddApplicat
if err != nil {
return nil, err
}

gitpath, err := a.makeGitPath(ctx, req.GitPath.Repo.Id, req.GitPath.Path, req.GitPath.ConfigFilename, req.PipedId)
if err != nil {
return nil, err
}
app := model.Application{
Id: uuid.New().String(),
Name: req.Name,
EnvId: req.EnvId,
PipedId: req.PipedId,
ProjectId: claims.Role.ProjectId,
GitPath: req.GitPath,
GitPath: gitpath,
Kind: req.Kind,
CloudProvider: req.CloudProvider,
}
Expand All @@ -311,6 +315,32 @@ func (a *WebAPI) AddApplication(ctx context.Context, req *webservice.AddApplicat
return &webservice.AddApplicationResponse{}, nil
}

// Adds Repository info and then makes the GitPath URL.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: "makeGitPath ..."

func (a *WebAPI) makeGitPath(ctx context.Context, repoID, path, cfgFilename, pipedID string) (*model.ApplicationGitPath, error) {

Copy link
Copy Markdown
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.

piped, err := a.getPiped(ctx, pipedID)
if err != nil {
return nil, err
}
repo := &model.ApplicationGitRepository{}

Copy link
Copy Markdown
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.

for _, r := range piped.Repositories {
if r.Id == repoID {
repo = r
break
}
}
u, err := git.MakeDirURL(repo.Remote, path, repo.Branch)
if err != nil {
return nil, err

Copy link
Copy Markdown
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
Copy Markdown
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.

}
return &model.ApplicationGitPath{
Repo: repo,
Path: path,
ConfigFilename: cfgFilename,
Url: u,
}, nil
}

func (a *WebAPI) EnableApplication(ctx context.Context, req *webservice.EnableApplicationRequest) (*webservice.EnableApplicationResponse, error) {
if err := a.updateApplicationEnable(ctx, req.ApplicationId, true); err != nil {
return nil, err
Expand Down
9 changes: 7 additions & 2 deletions pkg/app/piped/trigger/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func (t *Trigger) triggerDeployment(ctx context.Context, app *model.Application, branch string, commit git.Commit, commander string) (runErr error) {
deployment, err := buildDeploment(app, branch, commit, commander, time.Now())
deployment, err := buildDeployment(app, branch, commit, commander, time.Now())
if err != nil {
return err
}
Expand Down Expand Up @@ -109,7 +109,11 @@ func (t *Trigger) reportMostRecentlyTriggeredDeployment(ctx context.Context, d *
return err
}

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
Copy Markdown
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
Copy Markdown
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

commitURL, err := git.MakeCommitURL(app.GitPath.Repo.Remote, commit.Hash)

Copy link
Copy Markdown
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

if err != nil {
return nil, err
}
deployment := &model.Deployment{
Id: uuid.New().String(),
ApplicationId: app.Id,
Expand All @@ -124,6 +128,7 @@ func buildDeploment(app *model.Application, branch string, commit git.Commit, co
Message: commit.Message,
Author: commit.Author,
Branch: branch,
Url: commitURL,
CreatedAt: int64(commit.CreatedAt),
},
Commander: commander,
Expand Down
2 changes: 2 additions & 0 deletions pkg/git/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"commit.go",
"repo.go",
"ssh_config.go",
"url.go",
],
importpath = "github.com/pipe-cd/pipe/pkg/git",
visibility = ["//visibility:public"],
Expand All @@ -24,6 +25,7 @@ go_test(
"commit_test.go",
"repo_test.go",
"ssh_config_test.go",
"url_test.go",
],
data = glob(["testdata/**"]),
embed = [":go_default_library"],
Expand Down
138 changes: 138 additions & 0 deletions pkg/git/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2020 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package git

import (
"fmt"
"net/url"
"regexp"
"strings"
)

// MakeCommitURL builds a link to the HTML page of the commit, using the given repoURL and hash.
func MakeCommitURL(repoURL, hash string) (string, error) {
u, err := parseGitURL(repoURL)
if err != nil {
return "", err
}

scheme := "https"
if u.Scheme != "ssh" {
scheme = u.Scheme
}

repoPath := strings.Trim(u.Path, "/")
repoPath = strings.TrimSuffix(repoPath, ".git")

subPath := ""
switch u.Host {
case "github.com", "gitlab.com":
subPath = "commit"
case "bitbucket.org":
subPath = "commits"
default:
return "", fmt.Errorf("unsupported git host: %q", u.Host)

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

}

return fmt.Sprintf("%s://%s/%s/%s/%s", scheme, u.Host, repoPath, subPath, hash), nil
}

// MakeDirURL builds a link to the HTML page of the directory.
func MakeDirURL(repoURL, dir, branch string) (string, error) {
if branch == "" {
return "", fmt.Errorf("no branch given")
}
u, err := parseGitURL(repoURL)
if err != nil {
return "", err
}

scheme := "https"
if u.Scheme != "ssh" {
scheme = u.Scheme
}

repoPath := strings.Trim(u.Path, "/")
repoPath = strings.TrimSuffix(repoPath, ".git")

subPath := ""
switch u.Host {
// TODO: Support more git host
case "github.com":
subPath = "tree"
default:
return "", fmt.Errorf("unsupported git host: %q", u.Host)
}

dir = strings.Trim(dir, "/")

return fmt.Sprintf("%s://%s/%s/%s/%s/%s", scheme, u.Host, repoPath, subPath, branch, dir), nil
}

var (
knownSchemes = map[string]interface{}{
"ssh": struct{}{},
"git": struct{}{},
"git+ssh": struct{}{},
"http": struct{}{},
"https": struct{}{},
"ftp": struct{}{},
"ftps": struct{}{},
"rsync": struct{}{},
"file": struct{}{},

Copy link
Copy Markdown
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
Copy Markdown
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.

}
scpRegex = regexp.MustCompile(`^([a-zA-Z0-9_]+@)?([a-zA-Z0-9._-]+):(.*)$`)
)

// parseGitURL parses git url into a URL structure.
func parseGitURL(rawurl string) (u *url.URL, err error) {
u, err = parseTransport(rawurl)
if err == nil {
return
}
return parseScp(rawurl)
}

// Return a structured URL only when scheme is a known Git transport.
func parseTransport(rawurl string) (*url.URL, error) {
u, err := url.Parse(rawurl)
if err != nil {
return nil, fmt.Errorf("failed to parse git url: %w", err)
}
if _, ok := knownSchemes[u.Scheme]; !ok {
return nil, fmt.Errorf("unknown scheme %q", u.Scheme)
}
return u, nil
}

// Return a structured URL only when the rawurl is an SCP-like URL.
func parseScp(rawurl string) (*url.URL, error) {
match := scpRegex.FindAllStringSubmatch(rawurl, -1)
if len(match) == 0 {
return nil, fmt.Errorf("no scp URL found in %q", rawurl)
}
m := match[0]
user := strings.TrimRight(m[1], "@")
var userinfo *url.Userinfo
if user != "" {
userinfo = url.User(user)
}
return &url.URL{
Scheme: "ssh",
User: userinfo,
Host: m[2],
Path: m[3],
}, nil
}
Loading