Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfa633e1

Browse files
bidhan-amichelvocks
authored andcommitted
Use auth info (if present) when updating pipelines (#62)
* abstract out code to get auth info to a function* use auth info (if present) when updating pipelines* change pollTicket to pollTicker* add test for getAuthInfo* split tests for getAuthInfo
1 parent7a905f4 commitfa633e1

File tree

3 files changed

+96
-29
lines changed

3 files changed

+96
-29
lines changed

‎pipeline/git.go‎

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,9 @@ func GitLSRemote(repo *gaia.GitRepo) error {
2929
}
3030

3131
// Attach credentials if provided
32-
varauth transport.AuthMethod
33-
ifrepo.Username!=""&&repo.Password!="" {
34-
// Basic auth provided
35-
auth=&http.BasicAuth{
36-
Username:repo.Username,
37-
Password:repo.Password,
38-
}
39-
}elseifrepo.PrivateKey.Key!="" {
40-
auth,err=ssh.NewPublicKeys(repo.PrivateKey.Username, []byte(repo.PrivateKey.Key),repo.PrivateKey.Password)
41-
iferr!=nil {
42-
returnerr
43-
}
32+
auth,err:=getAuthInfo(repo)
33+
iferr!=nil {
34+
returnerr
4435
}
4536

4637
// Create client
@@ -78,23 +69,13 @@ func GitLSRemote(repo *gaia.GitRepo) error {
7869
// The destination will be attached to the given repo obj.
7970
funcgitCloneRepo(repo*gaia.GitRepo)error {
8071
// Check if credentials were provided
81-
varauth transport.AuthMethod
82-
ifrepo.Username!=""&&repo.Password!="" {
83-
// Basic auth provided
84-
auth=&http.BasicAuth{
85-
Username:repo.Username,
86-
Password:repo.Password,
87-
}
88-
}elseifrepo.PrivateKey.Key!="" {
89-
varerrerror
90-
auth,err=ssh.NewPublicKeys(repo.PrivateKey.Username, []byte(repo.PrivateKey.Key),repo.PrivateKey.Password)
91-
iferr!=nil {
92-
returnerr
93-
}
72+
auth,err:=getAuthInfo(repo)
73+
iferr!=nil {
74+
returnerr
9475
}
9576

9677
// Clone repo
97-
_,err:=git.PlainClone(repo.LocalDest,false,&git.CloneOptions{
78+
_,err=git.PlainClone(repo.LocalDest,false,&git.CloneOptions{
9879
Auth:auth,
9980
URL:repo.URL,
10081
RecurseSubmodules:git.DefaultSubmoduleRecursionDepth,
@@ -131,9 +112,16 @@ func updateAllCurrentPipelines() {
131112
}
132113
gaia.Cfg.Logger.Debug("checking pipeline: ",pipe.Name)
133114
gaia.Cfg.Logger.Debug("selected branch : ",pipe.Repo.SelectedBranch)
115+
auth,err:=getAuthInfo(&pipe.Repo)
116+
iferr!=nil {
117+
// It's also an error if the repo is already up to date so we just move on.
118+
gaia.Cfg.Logger.Error("error getting auth info while doing a pull request : ",err.Error())
119+
return
120+
}
134121
tree,_:=r.Worktree()
135122
err=tree.Pull(&git.PullOptions{
136123
RemoteName:"origin",
124+
Auth:auth,
137125
})
138126
iferr!=nil {
139127
// It's also an error if the repo is already up to date so we just move on.
@@ -152,3 +140,21 @@ func updateAllCurrentPipelines() {
152140
}
153141
wg.Wait()
154142
}
143+
144+
funcgetAuthInfo(repo*gaia.GitRepo) (transport.AuthMethod,error) {
145+
varauth transport.AuthMethod
146+
ifrepo.Username!=""&&repo.Password!="" {
147+
// Basic auth provided
148+
auth=&http.BasicAuth{
149+
Username:repo.Username,
150+
Password:repo.Password,
151+
}
152+
}elseifrepo.PrivateKey.Key!="" {
153+
varerrerror
154+
auth,err=ssh.NewPublicKeys(repo.PrivateKey.Username, []byte(repo.PrivateKey.Key),repo.PrivateKey.Password)
155+
iferr!=nil {
156+
returnnil,err
157+
}
158+
}
159+
returnauth,nil
160+
}

‎pipeline/git_test.go‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,64 @@ func TestUpdateAllPipelinesHundredPipelines(t *testing.T) {
156156
t.Fatal("log output did not contain error message that the repo is up-to-date.: ",b.String())
157157
}
158158
}
159+
160+
funcTestGetAuthInfoWithUsernameAndPassword(t*testing.T) {
161+
repoWithUsernameAndPassword:=&gaia.GitRepo{
162+
URL:"https://github.com/gaia-pipeline/go-test-example",
163+
LocalDest:"tmp",
164+
Username:"username",
165+
Password:"password",
166+
}
167+
168+
auth,_:=getAuthInfo(repoWithUsernameAndPassword)
169+
ifauth==nil {
170+
t.Fatal("auth should not be nil when username and password is provided")
171+
}
172+
}
173+
174+
funcTestGetAuthInfoWithPrivateKey(t*testing.T) {
175+
samplePrivateKey:=`
176+
-----BEGIN RSA PRIVATE KEY-----
177+
MD8CAQACCQDB9DczYvFuZQIDAQABAgkAtqAKvH9QoQECBQDjAl9BAgUA2rkqJQIE
178+
Xbs5AQIEIzWnmQIFAOEml+E=
179+
-----END RSA PRIVATE KEY-----
180+
`
181+
repoWithValidPrivateKey:=&gaia.GitRepo{
182+
URL:"https://github.com/gaia-pipeline/go-test-example",
183+
LocalDest:"tmp",
184+
PrivateKey: gaia.PrivateKey{
185+
Key:samplePrivateKey,
186+
Username:"username",
187+
Password:"password",
188+
},
189+
}
190+
_,err:=getAuthInfo(repoWithValidPrivateKey)
191+
iferr!=nil {
192+
t.Fatal(err)
193+
}
194+
195+
repoWithInvalidPrivateKey:=&gaia.GitRepo{
196+
URL:"https://github.com/gaia-pipeline/go-test-example",
197+
LocalDest:"tmp",
198+
PrivateKey: gaia.PrivateKey{
199+
Key:"random_key",
200+
Username:"username",
201+
Password:"password",
202+
},
203+
}
204+
auth,_:=getAuthInfo(repoWithInvalidPrivateKey)
205+
ifauth!=nil {
206+
t.Fatal("auth should be nil for invalid private key")
207+
}
208+
}
209+
210+
funcTestGetAuthInfoEmpty(t*testing.T) {
211+
repoWithoutAuthInfo:=&gaia.GitRepo{
212+
URL:"https://github.com/gaia-pipeline/go-test-example",
213+
LocalDest:"tmp",
214+
}
215+
auth,_:=getAuthInfo(repoWithoutAuthInfo)
216+
ifauth!=nil {
217+
t.Fatal("auth should be nil when no authentication info is provided")
218+
}
219+
}

‎pipeline/ticker.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ func InitTicker(store *store.Store, scheduler *scheduler.Scheduler) {
6060
gaia.Cfg.Logger.Info(errorMessage)
6161
gaia.Cfg.PVal=1
6262
}
63-
pollTicket:=time.NewTicker(time.Duration(gaia.Cfg.PVal)*time.Minute)
63+
pollTicker:=time.NewTicker(time.Duration(gaia.Cfg.PVal)*time.Minute)
6464
gofunc() {
65-
deferpollTicket.Stop()
65+
deferpollTicker.Stop()
6666
for {
6767
select {
68-
case<-pollTicket.C:
68+
case<-pollTicker.C:
6969
updateAllCurrentPipelines()
7070
}
7171
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp