-
Notifications
You must be signed in to change notification settings - Fork 323
Enable to make git URLs #594
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
| ) | ||
|
|
@@ -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, | ||
| } | ||
|
|
@@ -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. | ||
| func (a *WebAPI) makeGitPath(ctx context.Context, repoID, path, cfgFilename, pipedID string) (*model.ApplicationGitPath, error) { | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| } | ||
|
|
@@ -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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better than panic, let's add a check for the existence of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Panic could be occurred because old Applications don't have Repo. |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| deployment := &model.Deployment{ | ||
| Id: uuid.New().String(), | ||
| ApplicationId: app.Id, | ||
|
|
@@ -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, | ||
|
|
||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about GHE where its host could be customized?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... actually I'm still debating for it. switch u.Host {
case "github.com", "gitlab.com":
subPath = "commit"
case "bitbucket.org":
subPath = "commits"
default:
subPath = "commit"
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{}{}, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://git-scm.com/docs/git-clone#_git_urls
Because
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
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: "makeGitPath ..."