Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 42 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,42 @@ func (a *WebAPI) AddApplication(ctx context.Context, req *webservice.AddApplicat
return &webservice.AddApplicationResponse{}, nil
}

// makeGitPath returns an ApplicationGitPath by adding Repository info and GitPath URL to given args.
func (a *WebAPI) makeGitPath(ctx context.Context, repoID, path, cfgFilename, pipedID string) (*model.ApplicationGitPath, error) {
piped, err := a.getPiped(ctx, pipedID)
if err != nil {
return nil, err
}

var repo *model.ApplicationGitRepository
for _, r := range piped.Repositories {
if r.Id == repoID {
repo = r
break
}
}
if repo == nil {
a.logger.Error("repository not found",
zap.String("repo-id", repoID),
zap.String("piped-id", pipedID),
zap.Error(err),
)
return nil, status.Error(codes.Internal, "repository not found")
}

u, err := git.MakeDirURL(repo.Remote, path, repo.Branch)
if err != nil {
a.logger.Error("failed to make GitPath URL", zap.Error(err))
return nil, status.Error(codes.Internal, "failed to make GitPath URL")
}
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
14 changes: 12 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,16 @@ 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
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

commitURL := ""
if r := app.GitPath.Repo; r != nil {
var err error
commitURL, err = git.MakeCommitURL(r.Remote, commit.Hash)
if err != nil {
return nil, err
}
}

deployment := &model.Deployment{
Id: uuid.New().String(),
ApplicationId: app.Id,
Expand All @@ -124,6 +133,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
140 changes: 140 additions & 0 deletions pkg/git/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// 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:
// TODO: Allow users to specify git host
// Currently, the same subPath as Github is applied for all of unsupported hosts,
// to support GHE where its host could be customized
subPath = "commit"
}

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:
// TODO: Allow users to specify git host
subPath = "tree"
}

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{}{},
"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.

}
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