Skip to content

Commit 347c092

Browse files
authored
feat: Change git library to go-git (#15)
1 parent 20e8aae commit 347c092

File tree

13 files changed

+341
-908
lines changed

13 files changed

+341
-908
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ARG USER_GID=$USER_UID
1313

1414
ENV GO111MODULE=auto
1515

16-
ENV GOLANGCI_LINT_V="v1.43.0"
16+
ENV GOLANGCI_LINT_V="v1.44.2"
1717
ENV DOCKER_COMPOSE_V="1.29.2"
1818

1919
# Configure apt, install packages and tools

.github/workflows/pull_request_tests.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v2
16-
- uses: actions/[email protected]
16+
- name: Configure python
17+
uses: actions/[email protected]
1718
with:
1819
python-version: '3.9'
19-
- uses: actions/setup-go@v2
20+
- name: Configure Go
21+
uses: actions/setup-go@v2
2022
with:
2123
go-version: 1.17
2224
- name: Get golangci-lint
2325
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v${GOLANGCI_LINT_VERSION}
2426
env:
25-
GOLANGCI_LINT_VERSION: "1.44.0"
26-
- uses: pre-commit/[email protected]
27+
GOLANGCI_LINT_VERSION: "1.44.2"
28+
- name: Run pre-commit
29+
uses: pre-commit/[email protected]
2730

2831
unit-tests:
2932
name: Unit Tests
@@ -44,8 +47,11 @@ jobs:
4447
steps:
4548
- uses: actions/checkout@v2
4649

47-
- run: make build
50+
- name: Build app
51+
run: make build
4852

49-
- run: make test-coverage
53+
- name: Launch unit tests
54+
run: make test-coverage
5055
env:
5156
isCI: true
57+
SSH_KNOWN_HOSTS: "/root/.ssh/known_hosts"

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ type HelmUpdaterConfig struct {
2424
GitConf *git.Conf
2525
}
2626

27+
// GitConf contains the configuration for the git repository
2728
type GitConf struct {
2829
RepoURL string
2930
Branch string
3031
File string
3132
}
3233

34+
// UpdateApp contains the information about the update app
3335
type UpdateApp struct {
3436
Name string
3537
File string

cmd/run.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,11 @@ func runImageUpdater(cfg updater.HelmUpdaterConfig) error {
112112

113113
syncState := updater.NewSyncIterationState()
114114

115-
fmt.Println(cfg.UpdateApps)
116-
117115
err := func(cfg updater.HelmUpdaterConfig) error {
118116
log.Debugf("Processing application %s in directory %s", cfg.AppName, cfg.File)
119117

120118
_, err := updater.UpdateApplication(cfg, syncState)
121119
if err != nil {
122-
fmt.Printf("err is %v\n", err)
123120
return err
124121
}
125122

go.mod

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,32 @@ module github.com/docplanner/helm-repo-updater
33
go 1.17
44

55
require (
6-
github.com/argoproj-labs/argocd-image-updater v0.11.0
7-
github.com/google/go-cmp v0.5.6
86
github.com/mikefarah/yq/v4 v4.16.2
97
github.com/pkg/errors v0.9.1
108
github.com/spf13/cobra v1.3.0
119
github.com/spf13/viper v1.10.0
1210
github.com/stretchr/testify v1.7.0
1311
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473
1412
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
13+
gotest.tools v2.2.0+incompatible
1514
)
1615

1716
require (
1817
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/google/go-cmp v0.5.6 // indirect
1919
github.com/pmezard/go-difflib v1.0.0 // indirect
20+
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
2021
)
2122

2223
require (
23-
github.com/argoproj/argo-cd/v2 v2.1.2 // indirect
24-
github.com/argoproj/gitops-engine v0.4.1 // indirect
25-
github.com/argoproj/pkg v0.9.1 // indirect
26-
github.com/bombsimon/logrusr v1.0.0 // indirect
27-
github.com/bradleyfalzon/ghinstallation v1.1.1 // indirect
28-
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
2924
github.com/elliotchance/orderedmap v1.4.0 // indirect
3025
github.com/emirpasic/gods v1.12.0 // indirect
3126
github.com/fatih/color v1.13.0 // indirect
3227
github.com/fsnotify/fsnotify v1.5.1 // indirect
3328
github.com/go-git/gcfg v1.5.0 // indirect
3429
github.com/go-git/go-billy/v5 v5.0.0 // indirect
35-
github.com/go-git/go-git/v5 v5.2.0 // indirect
36-
github.com/go-logr/logr v0.4.0 // indirect
30+
github.com/go-git/go-git/v5 v5.2.0
3731
github.com/goccy/go-yaml v1.9.4 // indirect
38-
github.com/google/go-github/v29 v29.0.2 // indirect
39-
github.com/google/go-querystring v1.1.0 // indirect
4032
github.com/hashicorp/hcl v1.0.0 // indirect
4133
github.com/imdario/mergo v0.3.10 // indirect
4234
github.com/inconshreveable/mousetrap v1.0.0 // indirect
@@ -48,7 +40,6 @@ require (
4840
github.com/mattn/go-isatty v0.0.14 // indirect
4941
github.com/mitchellh/go-homedir v1.1.0 // indirect
5042
github.com/mitchellh/mapstructure v1.4.3 // indirect
51-
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
5243
github.com/pelletier/go-toml v1.9.4 // indirect
5344
github.com/sergi/go-diff v1.1.0 // indirect
5445
github.com/sirupsen/logrus v1.8.1

go.sum

Lines changed: 0 additions & 657 deletions
Large diffs are not rendered by default.

internal/app/git/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package git
33
import "text/template"
44

55
const DefaultGitCommitMessage = `🚀 automatic update of {{ .AppName }}
6-
76
{{ range .KeyChanges -}}
87
updates key {{ .Key }} value from '{{ .OldValue }}' to '{{ .NewValue }}'
98
{{ end -}}

internal/app/git/creds.go

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ package git
22

33
import (
44
"fmt"
5+
"regexp"
56

6-
"github.com/argoproj-labs/argocd-image-updater/ext/git"
7+
"github.com/docplanner/helm-repo-updater/internal/app/log"
8+
"github.com/go-git/go-git/v5/plumbing/transport"
9+
"github.com/go-git/go-git/v5/plumbing/transport/http"
10+
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
11+
)
12+
13+
var (
14+
sshURLRegex = regexp.MustCompile("^(ssh://)?([^/:]*?)@[^@]+$")
15+
httpsURLRegex = regexp.MustCompile("^(https://).*")
716
)
817

918
// Credentials is a git credential config
@@ -14,61 +23,107 @@ type Credentials struct {
1423
SSHPrivKey string
1524
}
1625

17-
// NewGitCreds returns the credentials for the given repo url.
18-
func (c Credentials) NewGitCreds(repoURL string) (git.Creds, error) {
19-
if isSshUrl(repoURL) {
20-
return c.fromSsh(repoURL)
26+
// NewGitCreds returns credentials for use with go-git library
27+
func (c Credentials) NewGitCreds(repoURL string, password string) (transport.AuthMethod, error) {
28+
if isSSHURL(repoURL) {
29+
gitSSHCredentials, err := c.fromSSH(repoURL, password)
30+
if err != nil {
31+
return nil, err
32+
}
33+
return gitSSHCredentials, nil
2134
}
2235

23-
if isHttpsUrl(repoURL) {
24-
return c.fromHttps(repoURL)
36+
if isHTTPSURL(repoURL) {
37+
gitCreds, err := c.from(repoURL)
38+
if err != nil {
39+
return nil, err
40+
}
41+
return gitCreds, nil
2542
}
2643

2744
return nil, unknownRepositoryType(repoURL)
2845
}
2946

30-
func isSshUrl(repoUrl string) bool {
31-
ok, _ := git.IsSSHURL(repoUrl)
47+
// isSSHURL returns true if supplied URL is SSH URL
48+
func isSSHURL(url string) bool {
49+
matches := sshURLRegex.FindStringSubmatch(url)
50+
return len(matches) > 2
51+
}
3252

33-
return ok
53+
// isHTTPSURL returns true if supplied URL is a valid HTTPS URL
54+
func isHTTPSURL(url string) bool {
55+
return httpsURLRegex.MatchString(url)
3456
}
3557

36-
func isHttpsUrl(repoUrl string) bool {
37-
return git.IsHTTPSURL(repoUrl)
58+
// generateAuthForSSH generate the necessary public keys as auth for git repository using
59+
// the provided privateKeyFile containing a valid SSH private key
60+
func generateAuthForSSH(repoUrl string, userName string, privateKeyFile string, password string) (ssh.AuthMethod, error) {
61+
publicKeys, err := ssh.NewPublicKeysFromFile("git", privateKeyFile, password)
62+
if err != nil {
63+
log.Warnf("generate publickeys failed: %s\n", err.Error())
64+
return nil, err
65+
}
66+
return publicKeys, err
3867
}
3968

40-
func (c Credentials) fromSsh(repoUrl string) (git.Creds, error) {
69+
// fromSSH generate a valid credentials using ssh key
70+
func (c Credentials) fromSSH(repoUrl string, password string) (ssh.AuthMethod, error) {
4171
if c.allowsSshAuth() {
42-
return git.NewSSHCreds(c.SSHPrivKey, "", true), nil
72+
sshPublicKeys, err := generateAuthForSSH(repoUrl, c.Username, c.SSHPrivKey, password)
73+
if err != nil {
74+
return nil, err
75+
}
76+
return sshPublicKeys, nil
4377
}
4478

4579
return nil, sshPrivateKeyNotProvided(repoUrl)
4680
}
4781

48-
func (c Credentials) fromHttps(repoURL string) (git.Creds, error) {
49-
if c.allowsHttpsAuth() {
50-
return git.NewHTTPSCreds(c.Username, c.Password, "", "", true, ""), nil
82+
// generatAuthFor generate a valid credentials for go-git library using
83+
// username and password
84+
func generatAuthFor(username string, password string) *http.BasicAuth {
85+
return &http.BasicAuth{
86+
Username: username,
87+
Password: password,
88+
}
89+
}
90+
91+
// from generate a valid credentials for go-git library using
92+
// username and passowrd
93+
func (c Credentials) from(repoURL string) (*http.BasicAuth, error) {
94+
if c.allowsAuth() {
95+
return generatAuthFor(c.Username, c.Password), nil
5196
}
5297

53-
return nil, httpsUserAndPasswordNotProvided(repoURL)
98+
return nil, UserAndPasswordNotProvided(repoURL)
5499
}
55100

101+
// allowSshAuth check if necessary attributes for generate an SSH
102+
// credentials are provided
56103
func (c Credentials) allowsSshAuth() bool {
57104
return c.SSHPrivKey != ""
58105
}
59106

60-
func (c Credentials) allowsHttpsAuth() bool {
107+
// allowsAuth check if necessary attributes for generate and
108+
// credentials are provided
109+
func (c Credentials) allowsAuth() bool {
61110
return c.Username != "" && c.Password != ""
62111
}
63112

113+
// sshPrivateKeyNotProvided return an error used when sshPrivKey
114+
// is not provided for generate and SSH credentials
64115
func sshPrivateKeyNotProvided(repoUrl string) error {
65116
return fmt.Errorf("sshPrivKey not provided for authenticatication to repository %s", repoUrl)
66117
}
67118

68-
func httpsUserAndPasswordNotProvided(repoUrl string) error {
119+
// UserAndPasswordNotProvided return an error used when
120+
// username or password are not provided for generate and credentials
121+
func UserAndPasswordNotProvided(repoUrl string) error {
69122
return fmt.Errorf("no value provided for username and password for authentication to repository %s", repoUrl)
70123
}
71124

125+
// unknownRepositoryType return an error used when
126+
// the repository provided is not or SSH type
72127
func unknownRepositoryType(repoUrl string) error {
73128
return fmt.Errorf("unknown repository type for git repository URL %s", repoUrl)
74129
}

0 commit comments

Comments
 (0)