Skip to content

Commit c3ec279

Browse files
committed
Feat: Version strings can now be read from complex multiline files.
1 parent 6be627d commit c3ec279

File tree

8 files changed

+274
-23
lines changed

8 files changed

+274
-23
lines changed

.github/workflows/ci.yaml

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ jobs:
1111
PROJECT_PATH: /go/src/github.com/packagrio/releasr
1212
strategy:
1313
matrix:
14-
package_type: ['chef', 'golang', 'node', 'python', 'ruby']
14+
package_type: ['chef', 'golang', 'node', 'python', 'ruby', 'generic']
1515
steps:
1616
- name: Checkout
17-
uses: actions/checkout@v2
17+
uses: actions/checkout@v4
1818
- name: Build Test Binaries
1919
env:
2020
GOOS: linux
@@ -27,36 +27,49 @@ jobs:
2727
go mod vendor
2828
./ci/test-build.sh ${{ matrix.package_type }}
2929
- name: Archive
30-
uses: actions/upload-artifact@v2
30+
uses: actions/upload-artifact@v4
3131
with:
3232
name: "test-binary-${{ matrix.package_type }}"
3333
path: /caches/test-binaries/
3434
test:
3535
name: Test
3636
needs: build-test
3737
runs-on: ubuntu-latest
38-
container: "ghcr.io/packagrio/packagr:latest-${{ matrix.package_type }}"
38+
container: "ghcr.io/packagrio/packagr:${{ matrix.package_type.image_tag }}"
3939

4040
strategy:
4141
matrix:
42-
package_type: ['chef', 'golang', 'node', 'python', 'ruby']
42+
package_type:
43+
- name: chef
44+
image_tag: latest-chef
45+
- name: golang
46+
image_tag: latest-golang
47+
- name: node
48+
image_tag: latest-node
49+
- name: python
50+
image_tag: latest-python
51+
- name: ruby
52+
image_tag: latest-ruby
53+
- name: generic
54+
image_tag: latest-ubuntu
55+
fail-fast: false
4356
steps:
4457
- name: Download test binaries
45-
uses: actions/download-artifact@v2
58+
uses: actions/download-artifact@v4
4659
with:
47-
name: "test-binary-${{ matrix.package_type }}"
60+
name: "test-binary-${{ matrix.package_type.name }}"
4861
- name: Test
4962
env:
5063
GOOS: linux
5164
GOARCH: amd64
5265
run: |
5366
chmod -R +x .
54-
./test-execute.sh ${{ matrix.package_type }}
67+
./test-execute.sh ${{ matrix.package_type.name }}
5568
- name: Archive
56-
uses: actions/upload-artifact@v2
69+
uses: actions/upload-artifact@v4
5770
with:
58-
name: test-coverage
59-
path: /coverage/coverage-${{ matrix.package_type }}.txt
71+
name: test-coverage-${{ matrix.package_type.name }}
72+
path: /coverage/coverage-${{ matrix.package_type.name }}.txt
6073

6174
build:
6275
name: Build
@@ -66,7 +79,7 @@ jobs:
6679
PROJECT_PATH: /go/src/github.com/packagrio/releasr
6780
steps:
6881
- name: Checkout
69-
uses: actions/checkout@v2
82+
uses: actions/checkout@v4
7083
- name: Build
7184
env:
7285
GOOS: linux
@@ -91,12 +104,12 @@ jobs:
91104
echo "listing linked libraries" && ldd packagr-releasr-linux-amd64
92105
93106
- name: Archive
94-
uses: actions/upload-artifact@v2
107+
uses: actions/upload-artifact@v4
95108
with:
96109
name: releasr-linux-binary
97110
path: ${{ env.PROJECT_PATH }}/packagr-releasr-linux-amd64
98111
- name: Archive
99-
uses: actions/upload-artifact@v2
112+
uses: actions/upload-artifact@v4
100113
with:
101114
name: releasr-mac-binary
102115
path: ${{ env.PROJECT_PATH }}/packagr-releasr-darwin-amd64

pkg/config/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const PACKAGR_SCM = "scm"
2727
const PACKAGR_VERSION_METADATA_PATH = "version_metadata_path"
2828
const PACKAGR_VERSION_BUMP_MESSAGE = "engine_version_bump_msg"
2929
const PACKAGR_GENERIC_VERSION_TEMPLATE = "generic_version_template"
30+
const PACKAGR_GENERIC_MERGE_VERSION_FILE = "generic_merge_version_file"
3031
const PACKAGR_GIT_AUTHOR_NAME = "engine_git_author_name"
3132
const PACKAGR_GIT_AUTHOR_EMAIL = "engine_git_author_email"
3233
const PACKAGR_ENGINE_REPO_CONFIG_PATH = "engine_repo_config_path"

pkg/engine/engine_generic.go

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package engine
22

33
import (
4+
"bufio"
45
"fmt"
6+
"github.com/packagrio/go-common/errors"
57
"github.com/packagrio/go-common/metadata"
68
"github.com/packagrio/go-common/pipeline"
79
"github.com/packagrio/go-common/scm"
810
"github.com/packagrio/go-common/utils/git"
911
"github.com/packagrio/releasr/pkg/config"
10-
"io/ioutil"
12+
"os"
1113
"path"
1214
"strings"
1315
)
@@ -65,20 +67,67 @@ func (g *engineGeneric) PopulateReleaseVersion() error {
6567
return nil
6668
}
6769

68-
//Helpers
70+
// Helpers
6971
func (g *engineGeneric) retrieveCurrentMetadata(gitLocalPath string) error {
7072
//read VERSION file.
71-
versionContent, rerr := ioutil.ReadFile(path.Join(gitLocalPath, g.Config.GetString(config.PACKAGR_VERSION_METADATA_PATH)))
73+
filePath := path.Join(gitLocalPath, g.Config.GetString(config.PACKAGR_VERSION_METADATA_PATH))
74+
template := g.Config.GetString(config.PACKAGR_GENERIC_VERSION_TEMPLATE)
75+
76+
// Handle if the user wants to merge the version file and not overwrite it
77+
if g.Config.GetBool(config.PACKAGR_GENERIC_MERGE_VERSION_FILE) {
78+
versionContent, err := g.matchAsSingleLine(filePath, template)
79+
if err != nil {
80+
return err
81+
}
82+
g.NextMetadata.Version = versionContent
83+
return nil
84+
}
85+
86+
match, err := g.matchAsMultiLine(filePath, template)
87+
if err != nil {
88+
return err
89+
}
90+
g.NextMetadata.Version = match
91+
return nil
92+
}
93+
94+
// Matches the template with the entire file, useful for simple version files
95+
func (g *engineGeneric) matchAsMultiLine(filePath string, template string) (string, error) {
96+
versionContent, err := os.ReadFile(filePath)
97+
if err != nil {
98+
return "", err
99+
}
100+
return g.getVersionFromString(string(versionContent), template)
101+
}
102+
103+
// Only matches the version for a single line, used when you have a version on a single line within a complete multiline file
104+
func (g *engineGeneric) matchAsSingleLine(filePath string, template string) (string, error) {
105+
fileReader, rerr := os.Open(filePath)
106+
scanner := bufio.NewScanner(fileReader)
72107
if rerr != nil {
73-
return rerr
108+
return "", rerr
74109
}
75110

111+
for scanner.Scan() {
112+
readLine := scanner.Text()
113+
version, err := g.getVersionFromString(readLine, template)
114+
if err != nil {
115+
continue
116+
}
117+
return version, nil
118+
}
119+
return "", errors.EngineUnspecifiedError(fmt.Sprintf(
120+
"Was unable to find a version with the format `%s` in file %s", template, filePath,
121+
))
122+
}
123+
124+
func (g *engineGeneric) getVersionFromString(versionContent string, template string) (string, error) {
76125
major := 0
77126
minor := 0
78127
patch := 0
79-
template := g.Config.GetString(config.PACKAGR_GENERIC_VERSION_TEMPLATE)
80-
fmt.Sscanf(strings.TrimSpace(string(versionContent)), template, &major, &minor, &patch)
81-
82-
g.NextMetadata.Version = fmt.Sprintf("%d.%d.%d", major, minor, patch)
83-
return nil
128+
_, err := fmt.Sscanf(strings.TrimSpace(string(versionContent)), template, &major, &minor, &patch)
129+
if err != nil {
130+
return "", err
131+
}
132+
return fmt.Sprintf("%d.%d.%d", major, minor, patch), nil
84133
}

pkg/engine/engine_generic_test.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
//go:build generic
2+
// +build generic
3+
4+
package engine_test
5+
6+
import (
7+
"github.com/analogj/go-util/utils"
8+
"github.com/golang/mock/gomock"
9+
"github.com/packagrio/go-common/metadata"
10+
"github.com/packagrio/go-common/pipeline"
11+
"github.com/packagrio/go-common/scm"
12+
"github.com/packagrio/go-common/scm/mock"
13+
"github.com/packagrio/releasr/pkg/config"
14+
"github.com/packagrio/releasr/pkg/engine"
15+
"github.com/stretchr/testify/require"
16+
"github.com/stretchr/testify/suite"
17+
"io/ioutil"
18+
"net/http"
19+
"os"
20+
"path"
21+
"testing"
22+
)
23+
24+
func TestEngineGeneric_Create(t *testing.T) {
25+
//setup
26+
testConfig, err := config.Create()
27+
require.NoError(t, err)
28+
testConfig.Set(config.PACKAGR_SCM, "github")
29+
testConfig.Set(config.PACKAGR_PACKAGE_TYPE, "generic")
30+
pipelineData := new(pipeline.Data)
31+
githubScm, err := scm.Create("github", pipelineData, testConfig, &http.Client{})
32+
require.NoError(t, err)
33+
34+
//test
35+
genericEngine, err := engine.Create(engine.PACKAGR_ENGINE_TYPE_GENERIC, pipelineData, testConfig, githubScm)
36+
37+
println("genericEngine", genericEngine)
38+
39+
//assert
40+
require.NoError(t, err)
41+
require.NotNil(t, genericEngine)
42+
}
43+
44+
// Define the suite, and absorb the built-in basic suite
45+
// functionality from testify - including a T() method which
46+
// returns the current testing context
47+
type EngineGenericTestSuite struct {
48+
suite.Suite
49+
MockCtrl *gomock.Controller
50+
Scm *mock_scm.MockInterface
51+
Config config.Interface
52+
PipelineData *pipeline.Data
53+
}
54+
55+
// Make sure that VariableThatShouldStartAtFive is set to five
56+
// before each test
57+
func (suite *EngineGenericTestSuite) SetupTest() {
58+
suite.MockCtrl = gomock.NewController(suite.T())
59+
60+
suite.PipelineData = new(pipeline.Data)
61+
62+
testConfig, err := config.Create()
63+
require.NoError(suite.T(), err)
64+
testConfig.Set(config.PACKAGR_SCM, "github")
65+
testConfig.Set(config.PACKAGR_PACKAGE_TYPE, "generic")
66+
suite.Config = testConfig
67+
suite.Scm = mock_scm.NewMockInterface(suite.MockCtrl)
68+
69+
}
70+
71+
func (suite *EngineGenericTestSuite) TearDownTest() {
72+
suite.MockCtrl.Finish()
73+
}
74+
75+
// In order for 'go test' to run this suite, we need to create
76+
// a normal test function and pass our suite to suite.Run
77+
func TestEngineGeneric_TestSuite(t *testing.T) {
78+
suite.Run(t, new(EngineGenericTestSuite))
79+
}
80+
81+
func (suite *EngineGenericTestSuite) TestEngineGeneric_ValidateTools() {
82+
//setup
83+
84+
genericEngine, err := engine.Create(engine.PACKAGR_ENGINE_TYPE_GENERIC, suite.PipelineData, suite.Config, suite.Scm)
85+
require.NoError(suite.T(), err)
86+
87+
//test
88+
berr := genericEngine.ValidateTools()
89+
90+
//assert
91+
require.NoError(suite.T(), berr)
92+
}
93+
94+
func (suite *EngineGenericTestSuite) TestEngineGeneric_GetVersion() {
95+
//copy into a temp directory.
96+
parentPath, err := ioutil.TempDir("", "")
97+
defer os.RemoveAll(parentPath)
98+
suite.PipelineData.GitParentPath = parentPath
99+
suite.PipelineData.GitLocalPath = path.Join(parentPath, "generic_analogj_test")
100+
cerr := utils.CopyDir(path.Join("testdata", "generic", "generic_analogj_test"), suite.PipelineData.GitLocalPath)
101+
require.NoError(suite.T(), cerr)
102+
103+
genericEngine, err := engine.Create(engine.PACKAGR_ENGINE_TYPE_GENERIC, suite.PipelineData, suite.Config, suite.Scm)
104+
require.NoError(suite.T(), err)
105+
106+
//test
107+
berr := genericEngine.PopulateReleaseVersion()
108+
require.NoError(suite.T(), berr)
109+
110+
//assert
111+
require.Equal(suite.T(), "0.0.1", genericEngine.GetNextMetadata().(*metadata.GenericMetadata).Version)
112+
113+
}
114+
115+
func (suite *EngineGenericTestSuite) TestEngineGeneric_GetVersion_Metadata() {
116+
//setup
117+
suite.Config.Set(config.PACKAGR_VERSION_METADATA_PATH, "version.txt")
118+
//copy into a temp directory.
119+
parentPath, err := ioutil.TempDir("", "")
120+
defer os.RemoveAll(parentPath)
121+
suite.PipelineData.GitParentPath = parentPath
122+
suite.PipelineData.GitLocalPath = path.Join(parentPath, "generic_analogj_test")
123+
cerr := utils.CopyDir(path.Join("testdata", "generic", "generic_metadata_analogj_test"), suite.PipelineData.GitLocalPath)
124+
require.NoError(suite.T(), cerr)
125+
126+
genericEngine, err := engine.Create(engine.PACKAGR_ENGINE_TYPE_GENERIC, suite.PipelineData, suite.Config, suite.Scm)
127+
require.NoError(suite.T(), err)
128+
129+
//test
130+
berr := genericEngine.PopulateReleaseVersion()
131+
require.NoError(suite.T(), berr)
132+
133+
//assert
134+
require.Equal(suite.T(), "0.0.1", genericEngine.GetNextMetadata().(*metadata.GenericMetadata).Version)
135+
136+
}
137+
138+
func (suite *EngineGenericTestSuite) TestEngineGeneric_GetVersion_Merge() {
139+
//setup
140+
suite.Config.Set(config.PACKAGR_GENERIC_MERGE_VERSION_FILE, "true")
141+
//copy into a temp directory.
142+
parentPath, err := ioutil.TempDir("", "")
143+
defer os.RemoveAll(parentPath)
144+
suite.PipelineData.GitParentPath = parentPath
145+
suite.PipelineData.GitLocalPath = path.Join(parentPath, "generic_analogj_test")
146+
cerr := utils.CopyDir(path.Join("testdata", "generic", "generic_merge_analogj_test"), suite.PipelineData.GitLocalPath)
147+
require.NoError(suite.T(), cerr)
148+
149+
genericEngine, err := engine.Create(engine.PACKAGR_ENGINE_TYPE_GENERIC, suite.PipelineData, suite.Config, suite.Scm)
150+
require.NoError(suite.T(), err)
151+
152+
//test
153+
berr := genericEngine.PopulateReleaseVersion()
154+
require.NoError(suite.T(), berr)
155+
156+
//assert
157+
require.Equal(suite.T(), "0.0.1", genericEngine.GetNextMetadata().(*metadata.GenericMetadata).Version)
158+
159+
}
160+
161+
func (suite *EngineGenericTestSuite) TestEngineGeneric_GetVersion_Template() {
162+
//setup
163+
suite.Config.Set(config.PACKAGR_GENERIC_VERSION_TEMPLATE, "%d.%d.%d")
164+
//copy into a temp directory.
165+
parentPath, err := ioutil.TempDir("", "")
166+
defer os.RemoveAll(parentPath)
167+
suite.PipelineData.GitParentPath = parentPath
168+
suite.PipelineData.GitLocalPath = path.Join(parentPath, "generic_analogj_test")
169+
cerr := utils.CopyDir(path.Join("testdata", "generic", "generic_template_analogj_test"), suite.PipelineData.GitLocalPath)
170+
require.NoError(suite.T(), cerr)
171+
172+
genericEngine, err := engine.Create(engine.PACKAGR_ENGINE_TYPE_GENERIC, suite.PipelineData, suite.Config, suite.Scm)
173+
require.NoError(suite.T(), err)
174+
175+
//test
176+
berr := genericEngine.PopulateReleaseVersion()
177+
require.NoError(suite.T(), berr)
178+
179+
//assert
180+
require.Equal(suite.T(), "0.0.1", genericEngine.GetNextMetadata().(*metadata.GenericMetadata).Version)
181+
182+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version := "0.0.1"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This is a line
2+
version := "0.0.1"
3+
# This is another line
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version := "0.0.1"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.1

0 commit comments

Comments
 (0)