Skip to content

Commit

Permalink
chore: code dedup
Browse files Browse the repository at this point in the history
Signed-off-by: Valery Piashchynski <[email protected]>
  • Loading branch information
rustatian committed Oct 8, 2023
1 parent cf9e77f commit 3fe6b54
Showing 1 changed file with 16 additions and 36 deletions.
52 changes: 16 additions & 36 deletions github/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ func (p *processor) run() {
)

if v.pluginCfg.Ref == "" {
p.mu.Lock()
p.errs = append(p.errs, fmt.Errorf("ref can't be empty"))
p.mu.Unlock()
p.wg.Done()
p.appendErr(errors.New("ref can't be empty"))
continue
}

Expand All @@ -82,18 +79,12 @@ func (p *processor) run() {
path.Join(v.pluginCfg.Folder, gomod), &github.RepositoryContentGetOptions{Ref: v.pluginCfg.Ref},
)
if err != nil {
p.mu.Lock()
p.errs = append(p.errs, err)
p.mu.Unlock()
p.wg.Done()
p.appendErr(err)
continue
}

if resp.StatusCode != http.StatusOK {
p.mu.Lock()
p.errs = append(p.errs, fmt.Errorf("bad response status: %d", resp.StatusCode))
p.mu.Unlock()
p.wg.Done()
p.appendErr(fmt.Errorf("bad response status: %d", resp.StatusCode))
continue
}

Expand All @@ -107,10 +98,7 @@ func (p *processor) run() {
// module github.com/roadrunner-server/logger/v2, we split and get the second part
retMod := strings.Split(line, " ")
if len(retMod) < 2 || len(retMod) > 2 {
p.mu.Lock()
p.errs = append(p.errs, fmt.Errorf("failed to parse module info for the plugin: %s", line))
p.mu.Unlock()
p.wg.Done()
p.appendErr(fmt.Errorf("failed to parse module info for the plugin: %s", line))
continue
}

Expand All @@ -121,10 +109,7 @@ func (p *processor) run() {

out:
if errs := scanner.Err(); errs != nil {
p.mu.Lock()
p.errs = append(p.errs, errs)
p.mu.Unlock()
p.wg.Done()
p.appendErr(errs)
continue
}

Expand All @@ -143,37 +128,25 @@ func (p *processor) run() {
},
})
if err != nil {
p.mu.Lock()
p.errs = append(p.errs, err)
p.mu.Unlock()
p.wg.Done()
p.appendErr(err)
continue
}

if rsp.StatusCode != http.StatusOK {
p.mu.Lock()
p.errs = append(p.errs, fmt.Errorf("bad response status: %d", rsp.StatusCode))
p.mu.Unlock()
p.wg.Done()
p.appendErr(fmt.Errorf("bad response status: %d", rsp.StatusCode))
continue
}

if len(commits) == 0 {
p.mu.Lock()
p.errs = append(p.errs, errors.New("empty commit SHA"))
p.mu.Unlock()
p.wg.Done()
p.appendErr(errors.New("no commits in the repository"))
continue
}

// should be only one commit
at := commits[0].GetCommit().GetCommitter().GetDate()
// [:12] because of go.mod pseudo format specs
if len(commits[0].GetSHA()) < 12 {
p.mu.Lock()
p.errs = append(p.errs, fmt.Errorf("commit SHA is too short: %s", commits[0].GetSHA()))
p.mu.Unlock()
p.wg.Done()
p.appendErr(fmt.Errorf("commit SHA is too short: %s", commits[0].GetSHA()))
continue
}

Expand All @@ -194,6 +167,13 @@ func (p *processor) run() {
}
}

func (p *processor) appendErr(err error) {
p.mu.Lock()
p.errs = append(p.errs, err)
p.mu.Unlock()
p.wg.Done()
}

func (p *processor) add(pjob *pcfg) {
p.wg.Add(1)
p.queueCh <- pjob
Expand Down

0 comments on commit 3fe6b54

Please sign in to comment.