Skip to content

Commit

Permalink
chore: more improvenments
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 dd027b4 commit 2930056
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 47 deletions.
13 changes: 7 additions & 6 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
t.Entries = make([]*templates.Entry, len(b.modules))
for i := 0; i < len(b.modules); i++ {
t.Entries[i] = &templates.Entry{
Time: b.modules[i].Time,
Module: b.modules[i].ModuleName,
Prefix: randStringBytes(5),
Structure: pluginStructureStr,
Version: b.modules[i].Version,
Replace: b.modules[i].Replace,
Module: b.modules[i].ModuleName,
Prefix: randStringBytes(5),
Structure: pluginStructureStr,
PseudoVersion: b.modules[i].PseudoVersion,
Replace: b.modules[i].Replace,
}
}

Expand Down Expand Up @@ -148,6 +147,8 @@ func (b *Builder) Build(rrModule string) error { //nolint:gocyclo
return fmt.Errorf("unknown module version: %s", t.ModuleVersion)
}

b.log.Debug("[RESULTING TEMPLATE]", zap.String("template", buf.String()))

_, err = goModFile.Write(buf.Bytes())
if err != nil {
return err
Expand Down
24 changes: 24 additions & 0 deletions builder/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ package container
import (
"github.com/roadrunner-server/informer/v4"
"github.com/roadrunner-server/resetter/v4"
aba "github.com/roadrunner-server/some_plugin"
abc "github.com/roadrunner-server/some_plugin/v2"
abd "github.com/roadrunner-server/some_plugin/v22234"
ab "github.com/roadrunner-server/rpc/v4"
cd "github.com/roadrunner-server/http/v4"
ef "github.com/roadrunner-server/grpc/v4"
Expand All @@ -30,6 +33,9 @@ func Plugins() []any {
&resetter.Plugin{},
// std and custom plugins
&aba.Plugin{},
&abc.Plugin{},
&abd.Plugin{},
&ab.Plugin{},
&cd.Plugin{},
&ef.Plugin{},
Expand All @@ -44,6 +50,24 @@ func TestCompile(t *testing.T) {
Entries: make([]*templates.Entry, 0, 10),
}

tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/some_plugin",
Structure: "Plugin{}",
Prefix: "aba",
})

tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/some_plugin/v2",
Structure: "Plugin{}",
Prefix: "abc",
})

tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/some_plugin/v22234",
Structure: "Plugin{}",
Prefix: "abd",
})

tt.Entries = append(tt.Entries, &templates.Entry{
Module: "github.com/roadrunner-server/rpc/v4",
Structure: "Plugin{}",
Expand Down
8 changes: 5 additions & 3 deletions builder/templates/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (

// Entry represents all info about module
type Entry struct {
Time string
// Module is the module name (github.com/roadrunner-server/logger/v2)
Module string
Structure string
Prefix string
Version string
// Prefix is the prefix for the plugin to avoid collisions
Prefix string
// PseudoVersion is the pseudo version of the module (v0.0.0-20210101000000-000000000000)
PseudoVersion string
// Replace directive, should include a path
Replace string
}
Expand Down
6 changes: 4 additions & 2 deletions builder/templates/templateV2023.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ require (
github.com/joho/godotenv v1.5.1
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.2
go.uber.org/automaxprocs v1.5.2
github.com/roadrunner-server/informer/v4 latest
github.com/roadrunner-server/resetter/v4 latest
// Go module pseudo-version
{{range $v := .Entries}}{{$v.Module}} v4.0.0-{{$v.Time}}-{{$v.Version}}
{{range $v := .Entries}}{{$v.Module}} {{$v.PseudoVersion}}
{{end}}
)
Expand Down
33 changes: 33 additions & 0 deletions github/modinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package github

import (
"fmt"
"regexp"
"strconv"
"time"

m "golang.org/x/mod/module"
)

var vr = regexp.MustCompile(`/v(\\d+)$`)

// here we accept a module name and return the version
// e.g.: github.com/roadrunner-server/logger/v2 => v2
func parseModuleInfo(module string, t time.Time, rev string) string {
match := vr.FindStringSubmatch(module)
var version string
if len(match) > 1 {
if !IsDigit(match[1]) {
return m.PseudoVersion("", "", t, rev)
}

version = fmt.Sprintf("v%s", match[1])
}

return m.PseudoVersion(version, "", t, rev)
}

func IsDigit(num string) bool {
_, err := strconv.ParseInt(num, 10, 64)
return err == nil
}
62 changes: 62 additions & 0 deletions github/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package github

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type tm struct {
module string
tm time.Time
sha string
expect string
}

func TestParse(t *testing.T) {
tn, err := time.Parse("20060102150405", "20231008162055")
require.NoError(t, err)

tests := []tm{
{
module: "github.com/roadrunner-server/logger/va",
tm: tn,
sha: "1234567890",
expect: "v0.0.0-20231008162055-1234567890",
},
{
module: "github.com/roadrunner-server/logger/v2",
tm: tn,
sha: "1234567890",
expect: "v2.0.0-20231008162055-1234567890",
},
{
module: "github.com/roadrunner-server/logger/v2222222222222",
tm: tn,
sha: "1234567890",
expect: "v2222222222222.0.0-20231008162055-1234567890",
},
{
module: "github.com/roadrunner-server/logger",
tm: tn,
sha: "1234567890",
expect: "v0.0.0-20231008162055-1234567890",
},
{
module: "github.com/roadrunner-server/logger/v2",
tm: tn,
sha: "",
expect: "v2.0.0-20231008162055-",
},
{
expect: "v0.0.0-00010101000000-",
},
}

for _, tt := range tests {
out := parseModuleInfo(tt.module, tt.tm, tt.sha)
assert.Equal(t, tt.expect, out)
}
}
75 changes: 48 additions & 27 deletions github/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"bufio"
"context"
"errors"
"fmt"
"net/http"
"path"
Expand All @@ -16,7 +17,8 @@ import (
)

const (
referenceFormat string = "20060102150405"
gomod string = "go.mod"
modLine string = "module"
)

type processor struct {
Expand Down Expand Up @@ -77,7 +79,7 @@ func (p *processor) run() {
rc, resp, err := p.client.Repositories.DownloadContents(context.Background(),
v.pluginCfg.Owner,
v.pluginCfg.Repo,
path.Join(v.pluginCfg.Folder, "go.mod"), &github.RepositoryContentGetOptions{Ref: v.pluginCfg.Ref},
path.Join(v.pluginCfg.Folder, gomod), &github.RepositoryContentGetOptions{Ref: v.pluginCfg.Ref},
)
if err != nil {
p.mu.Lock()
Expand All @@ -95,39 +97,43 @@ func (p *processor) run() {
continue
}

rdr := bufio.NewReader(rc)
ret, err := rdr.ReadString('\n')
if err != nil {
p.mu.Lock()
p.errs = append(p.errs, err)
p.mu.Unlock()
p.wg.Done()
continue
scanner := bufio.NewScanner(rc)
for scanner.Scan() {
line := scanner.Text()
switch { //nolint:gocritic
case strings.HasPrefix(line, modLine):
p.log.Debug("[READING MODULE INFO]", zap.String("plugin", v.name), zap.String("module", line))

// 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()
continue
}

modInfo.ModuleName = strings.TrimRight(retMod[1], "\n")
goto out
}
}

p.log.Debug("[READING MODULE INFO]", zap.String("plugin", v.name), zap.String("mod", ret))
out:

// module github.com/roadrunner-server/logger/v2, we split and get the second part
retMod := strings.Split(ret, " ")
if len(retMod) < 2 {
if errs := scanner.Err(); errs != nil {
p.mu.Lock()
p.errs = append(p.errs, fmt.Errorf("failed to parse module info for the plugin: %s", ret))
p.errs = append(p.errs, errs)
p.mu.Unlock()
p.wg.Done()
continue
}

err = resp.Body.Close()
if err != nil {
p.mu.Lock()
p.errs = append(p.errs, err)
p.mu.Unlock()
p.wg.Done()
continue
p.log.Warn("[FAILED TO CLOSE RESPONSE BODY]", zap.Error(err))
}

modInfo.ModuleName = strings.TrimRight(retMod[1], "\n")

p.log.Debug("[REQUESTING COMMIT SHA-1]", zap.String("plugin", v.name), zap.String("ref", v.pluginCfg.Ref))
commits, rsp, err := p.client.Repositories.ListCommits(context.Background(), v.pluginCfg.Owner, v.pluginCfg.Repo, &github.CommitsListOptions{
SHA: v.pluginCfg.Ref,
Expand All @@ -153,13 +159,28 @@ func (p *processor) run() {
continue
}

for j := 0; j < len(commits); j++ {
at := commits[j].GetCommit().GetCommitter().GetDate()
modInfo.Time = at.Format(referenceFormat)
// [:12] because of go.mod pseudo format specs
modInfo.Version = commits[j].GetSHA()[:12]
if len(commits) == 0 {
p.mu.Lock()
p.errs = append(p.errs, errors.New("empty commit SHA"))
p.mu.Unlock()
p.wg.Done()
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()
continue
}

modInfo.Version = commits[0].GetSHA()[:12]
modInfo.PseudoVersion = parseModuleInfo(modInfo.ModuleName, at.Time, commits[0].GetSHA()[:12])

if v.pluginCfg.Replace != "" {
p.log.Debug("[REPLACE REQUESTED]", zap.String("plugin", v.name), zap.String("path", v.pluginCfg.Replace))
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ require (
github.com/stretchr/testify v1.8.4
github.com/xanzy/go-gitlab v0.93.0
go.uber.org/zap v1.26.0
golang.org/x/mod v0.13.0
golang.org/x/oauth2 v0.13.0
)

require (
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down
Loading

0 comments on commit 2930056

Please sign in to comment.