Skip to content

Commit 6fdcfcc

Browse files
committed
fix(clone): use unique temp dir for workspace
1 parent 05f2473 commit 6fdcfcc

File tree

3 files changed

+86
-56
lines changed

3 files changed

+86
-56
lines changed

config/config.go

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -29,13 +30,15 @@ type RepoConfig struct {
2930
}
3031

3132
type Refs struct {
32-
// Org is the organization name, e.g. "qiniu"
33-
Org string `json:"org,omitempty"`
34-
// Repo is the repository name, e.g. "kodo"
35-
Repo string `json:"repo,omitempty"`
36-
// Host is the git provider, e.g. "github.com", "gitlab.com"
37-
Host string `json:"host,omitempty"`
38-
// CloneURL is the git clone url, e.g. "https://github.com/qiniu/kodo.git"
33+
// Org, Repo, and Host form a set that will ultimately be combined into a CloneURL
34+
// For example: Org="qiniu", Repo="kodo", Host="github.com"
35+
// will generate CloneURL="https://github.com/qiniu/kodo.git"
36+
//
37+
// Alternatively, you can directly provide the CloneURL, which will be parsed into the corresponding Org, Repo, and Host
38+
// Org, Repo, Host, and CloneURL are equivalent; only one set needs to be filled
39+
Org string `json:"org,omitempty"`
40+
Repo string `json:"repo,omitempty"`
41+
Host string `json:"host,omitempty"`
3942
CloneURL string `json:"cloneUrl,omitempty"`
4043

4144
// PathAlias is the location under $parentDir/reviewbot-code/$org-$repo-$num/
@@ -140,21 +143,10 @@ func NewConfig(conf string) (Config, error) {
140143
return c, err
141144
}
142145

143-
// parse cloneURL to org repo and host
144-
re := regexp.MustCompile(`^(?:git@|https://)?([^:/]+)[:/]{1}(.*?)/(.*?)\.git$`)
145-
for orgRepo, refConfig := range c.CustomConfig {
146-
for k, ref := range refConfig.ExtraRefs {
147-
if ref.CloneURL == "" {
148-
continue
149-
}
150-
matches := re.FindStringSubmatch(ref.CloneURL)
151-
if len(matches) != 4 {
152-
return c, fmt.Errorf("failed to parse CloneURL, please check the format of %s", ref.CloneURL)
153-
}
154-
c.CustomConfig[orgRepo].ExtraRefs[k].Host = matches[1]
155-
c.CustomConfig[orgRepo].ExtraRefs[k].Org = matches[2]
156-
c.CustomConfig[orgRepo].ExtraRefs[k].Repo = matches[3]
157-
}
146+
// ============ validate and update the config ============
147+
148+
if err := c.parseCloneURLs(); err != nil {
149+
return c, err
158150
}
159151

160152
// set default value
@@ -329,3 +321,36 @@ func (*baseModifier) Modify(cfg *Linter) (*Linter, error) {
329321

330322
return &newCfg, nil
331323
}
324+
325+
func (c *Config) parseCloneURLs() error {
326+
re := regexp.MustCompile(`^(?:git@|https://)?([^:/]+)[:/]{1}(.*?)/(.*?)\.git$`)
327+
328+
for orgRepo, refConfig := range c.CustomConfig {
329+
for k, ref := range refConfig.ExtraRefs {
330+
if ref.CloneURL == "" {
331+
continue
332+
}
333+
334+
if err := c.parseAndUpdateCloneURL(re, orgRepo, k); err != nil {
335+
return err
336+
}
337+
}
338+
}
339+
340+
return nil
341+
}
342+
343+
func (c *Config) parseAndUpdateCloneURL(re *regexp.Regexp, orgRepo string, k int) error {
344+
ref := &c.CustomConfig[orgRepo].ExtraRefs[k]
345+
matches := re.FindStringSubmatch(ref.CloneURL)
346+
if len(matches) != 4 {
347+
log.Errorf("failed to parse CloneURL, please check the format of %s", ref.CloneURL)
348+
return errors.New("invalid CloneURL")
349+
}
350+
351+
ref.Host = matches[1]
352+
ref.Org = matches[2]
353+
ref.Repo = matches[3]
354+
355+
return nil
356+
}

config/config.yaml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@ customConfig: # custom config for specific orgs or repos
2121
golangci-lint:
2222
enable: false
2323

24-
# temporary disable, will be enabled after fixing the go mod issue in kodo
25-
qbox/kodoe-manager:
26-
linters:
27-
gomodcheck:
28-
enable: false
2924
qbox/zrs:
30-
linters:
31-
gomodcheck:
32-
enable: false
25+
extraRefs:
26+
- org: qbox
27+
repo: kodo
28+
qbox/kodoe-manager:
29+
extraRefs:
30+
- org: qbox
31+
repo: kodo
32+
qbox/configcenter:
33+
extraRefs:
34+
- org: qbox
35+
repo: kodo
3336
qbox/logverse:
34-
linters:
35-
gomodcheck:
36-
enable: false
37+
extraRefs:
38+
- org: qbox
39+
repo: kodo
3740

3841
qbox/kodo:
3942
linters:
@@ -56,6 +59,9 @@ customConfig: # custom config for specific orgs or repos
5659
golangci-lint run --timeout=10m0s --allow-parallel-runners=true --print-issued-lines=false --out-format=line-number >> $ARTIFACT/lint.log 2>&1
5760
5861
qbox/kodo-ops:
62+
extraRefs:
63+
- org: qbox
64+
repo: kodo
5965
linters:
6066
golangci-lint:
6167
enable: true

server.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,21 @@ func (s *Server) handle(ctx context.Context, event *github.PullRequestEvent) err
293293
}
294294
log.Infof("found %d files affected by pull request %d\n", len(pullRequestAffectedFiles), num)
295295

296-
defaultWorkdir, err := prepareRepoDir(org, repo, num)
296+
defaultWorkDir, err := prepareRepoDir(org, repo, num)
297297
if err != nil {
298298
return fmt.Errorf("failed to prepare repo dir: %w", err)
299299
}
300+
defer func() {
301+
if s.debug {
302+
return // do not remove the repository in debug mode
303+
}
304+
if err := os.RemoveAll(defaultWorkDir); err != nil {
305+
log.Errorf("failed to remove the repository , err : %v", err)
306+
}
307+
}()
300308

301309
r, err := s.gitClientFactory.ClientForWithRepoOpts(org, repo, gitv2.RepoOpts{
302-
CopyTo: defaultWorkdir + "/" + repo,
310+
CopyTo: defaultWorkDir + "/" + repo,
303311
})
304312
if err != nil {
305313
log.Errorf("failed to create git client: %v", err)
@@ -326,26 +334,12 @@ func (s *Server) handle(ctx context.Context, event *github.PullRequestEvent) err
326334
log.Infof("no .gitmodules file in repo %s", repo)
327335
}
328336

329-
repoClients, err := s.PrepareExtraRef(org, repo, defaultWorkdir)
337+
_, err = s.PrepareExtraRef(org, repo, defaultWorkDir)
330338
if err != nil {
331339
log.Errorf("failed to prepare and clone ExtraRef : %v", err)
332340
return err
333341
}
334342

335-
repoClients = append(repoClients, r)
336-
337-
defer func() {
338-
if s.debug {
339-
return // do not remove the repository in debug mode
340-
}
341-
for _, r := range repoClients {
342-
err := r.Clean()
343-
if err != nil {
344-
log.Errorf("failed to remove the repository , err : %v", err)
345-
}
346-
}
347-
}()
348-
349343
for name, fn := range linters.TotalPullRequestHandlers() {
350344
linterConfig := s.config.GetLinterConfig(org, repo, name)
351345

@@ -434,19 +428,24 @@ func prepareRepoDir(org, repo string, num int) (string, error) {
434428
if err != nil {
435429
return "", fmt.Errorf("failed to get user home dir: %w", err)
436430
}
437-
parentDir = filepath.Join(homeDir, "reviewbot-code", fmt.Sprintf("%s-%s-%d", org, repo, num))
431+
parentDir = filepath.Join(homeDir, "reviewbot-code-workspace")
438432
} else {
439-
parentDir = filepath.Join("/tmp", "reviewbot-code", fmt.Sprintf("%s-%s-%d", org, repo, num))
433+
parentDir = filepath.Join("/tmp", "reviewbot-code-workspace")
440434
}
441435

442436
if err := os.MkdirAll(parentDir, 0o755); err != nil {
443437
return "", fmt.Errorf("failed to create parent dir: %w", err)
444438
}
445439

446-
return parentDir, nil
440+
dir, err := os.MkdirTemp(parentDir, fmt.Sprintf("%s-%s-%d", org, repo, num))
441+
if err != nil {
442+
return "", fmt.Errorf("failed to create temp dir: %w", err)
443+
}
444+
445+
return dir, nil
447446
}
448447

449-
func (s *Server) PrepareExtraRef(org, repo, defaultWorkdir string) (repoClients []gitv2.RepoClient, err error) {
448+
func (s *Server) PrepareExtraRef(org, repo, defaultWorkDir string) (repoClients []gitv2.RepoClient, err error) {
450449
var repoCfg config.RepoConfig
451450
config := s.config
452451
if v, ok := config.CustomConfig[org]; ok {
@@ -472,9 +471,9 @@ func (s *Server) PrepareExtraRef(org, repo, defaultWorkdir string) (repoClients
472471
log.Fatalf("failed to create git client factory: %v", err)
473472
}
474473

475-
repoPath := defaultWorkdir + "/" + refConfig.Repo
474+
repoPath := defaultWorkDir + "/" + refConfig.Repo
476475
if refConfig.PathAlias != "" {
477-
repoPath = defaultWorkdir + refConfig.PathAlias
476+
repoPath = defaultWorkDir + refConfig.PathAlias
478477
}
479478
r, err := gitClient.ClientForWithRepoOpts(refConfig.Org, refConfig.Repo, gitv2.RepoOpts{
480479
CopyTo: repoPath,

0 commit comments

Comments
 (0)