Merge pull request #321 from xeres/feature-remote-codecommit

Support AWS CodeCommit HTTP (GRC)
This commit is contained in:
Masayuki Matsuki 2021-05-23 00:27:53 +09:00 committed by GitHub
commit c62fd67820
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 99 additions and 7 deletions

View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
@ -72,7 +73,11 @@ func (g *getter) getRemoteRepository(remote RemoteRepository) error {
switch {
case newPath:
logger.Log("clone", fmt.Sprintf("%s -> %s", remoteURL, fpath))
if remoteURL.Scheme == "codecommit" {
logger.Log("clone", fmt.Sprintf("%s -> %s", remoteURL.Opaque, fpath))
} else {
logger.Log("clone", fmt.Sprintf("%s -> %s", remoteURL, fpath))
}
var (
localRepoRoot = fpath
repoURL = remoteURL
@ -88,6 +93,9 @@ func (g *getter) getRemoteRepository(remote RemoteRepository) error {
localRepoRoot = filepath.Join(local.RootPath, remoteURL.Hostname(), l)
}
if remoteURL.Scheme == "codecommit" {
repoURL, _ = url.Parse(remoteURL.Opaque)
}
if getRepoLock(localRepoRoot) {
return vcs.Clone(&vcsGetOption{
url: repoURL,

View file

@ -97,8 +97,11 @@ func LocalRepositoryFromURL(remoteURL *url.URL) (*LocalRepository, error) {
if localRepository != nil {
return localRepository, nil
}
prim, err := getRoot(remoteURL.String())
var remoteURLStr = remoteURL.String()
if remoteURL.Scheme == "codecommit" {
remoteURLStr = remoteURL.Opaque
}
prim, err := getRoot(remoteURLStr)
if err != nil {
return nil, err
}

View file

@ -93,6 +93,27 @@ func (repo *DarksHubRepository) VCS() (*VCSBackend, *url.URL, error) {
return DarcsBackend, repo.URL(), nil
}
// A CodeCommitRepository represents a CodeCommit repository. Implements RemoteRepository.
type CodeCommitRepository struct {
url *url.URL
}
// URL reutrns URL of the repository
func (repo *CodeCommitRepository) URL() *url.URL {
return repo.url
}
// IsValid determine if the repository is valid or not
func (repo *CodeCommitRepository) IsValid() bool {
return true
}
// VCS returns VCSBackend of the repository
func (repo *CodeCommitRepository) VCS() (*VCSBackend, *url.URL, error) {
u := *repo.url
return GitBackend, &u, nil
}
// OtherRepository represents other repository
type OtherRepository struct {
url *url.URL
@ -109,11 +130,12 @@ func (repo *OtherRepository) IsValid() bool {
}
var (
vcsSchemeReg = regexp.MustCompile(`^(git|svn|bzr)(?:\+|$)`)
vcsSchemeReg = regexp.MustCompile(`^(git|svn|bzr|codecommit)(?:\+|$)`)
scheme2vcs = map[string]*VCSBackend{
"git": GitBackend,
"svn": SubversionBackend,
"bzr": BazaarBackend,
"git": GitBackend,
"codecommit": GitBackend,
"svn": SubversionBackend,
"bzr": BazaarBackend,
}
)
@ -165,6 +187,9 @@ func (repo *OtherRepository) VCS() (*VCSBackend, *url.URL, error) {
// NewRemoteRepository returns new RemoteRepository object from URL
func NewRemoteRepository(u *url.URL) (RemoteRepository, error) {
repo := func() RemoteRepository {
if u.Scheme == "codecommit" {
return &CodeCommitRepository{u}
}
switch u.Host {
case "github.com":
return &GitHubRepository{u}

55
url.go
View file

@ -1,9 +1,11 @@
package main
import (
"bytes"
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
@ -20,6 +22,7 @@ var (
hasSchemePattern = regexp.MustCompile("^[^:]+://")
scpLikeURLPattern = regexp.MustCompile("^([^@]+@)?([^:]+):(/?.+)$")
looksLikeAuthorityPattern = regexp.MustCompile(`[A-Za-z0-9]\.[A-Za-z]+(?::\d{1,5})?$`)
codecommitLikeURLPattern = regexp.MustCompile(`^(codecommit):(?::([a-z][a-z0-9-]+):)?//(?:([^]]+)@)?([\w\.-]+)$`)
)
func newURL(ref string, ssh, forceMe bool) (*url.URL, error) {
@ -51,6 +54,58 @@ func newURL(ref string, ssh, forceMe bool) (*url.URL, error) {
}
}
if codecommitLikeURLPattern.MatchString(ref) {
// SEE ALSO:
// https://github.com/aws/git-remote-codecommit/blob/master/git_remote_codecommit/__init__.py#L68
matched := codecommitLikeURLPattern.FindStringSubmatch(ref)
region := matched[2]
if matched[2] == "" {
// Region detection priority:
// 1. Explicit specification (codecommit::region://...)
// 2. Environment variables
// a. AWS_REGION (implicit priority)
// b. AWS_DEFAULT_REGION
// 3. AWS CLI profiles
// SEE ALSO:
// https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-quickstart.html#cli-configure-quickstart-precedence
var exists bool
region, exists = os.LookupEnv("AWS_REGION")
if !exists {
region, exists = os.LookupEnv("AWS_DEFAULT_REGION")
}
if !exists {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command("aws", "configure", "get", "region")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
if stderr.String() == "" {
fmt.Fprintln(os.Stderr, "You must specify a region. You can also configure your region by running \"aws configure\".")
} else {
fmt.Fprint(os.Stderr, stderr.String())
}
os.Exit(1)
}
region = strings.TrimSpace(stdout.String())
}
}
return &url.URL{
Scheme: matched[1],
Host: region,
User: url.User(matched[3]),
Path: matched[4],
Opaque: ref,
}, nil
}
if !hasSchemePattern.MatchString(ref) {
if scpLikeURLPattern.MatchString(ref) {
matched := scpLikeURLPattern.FindStringSubmatch(ref)

1
vcs.go
View file

@ -360,6 +360,7 @@ var BazaarBackend = &VCSBackend{
var vcsRegistry = map[string]*VCSBackend{
"git": GitBackend,
"github": GitBackend,
"codecommit": GitBackend,
"svn": SubversionBackend,
"subversion": SubversionBackend,
"git-svn": GitsvnBackend,