mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-01-13 13:21:00 +00:00
feat: add updater for package.json (#213)
This commit is contained in:
parent
6237c9b666
commit
1e9e0aa5d9
14 changed files with 174 additions and 44 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
|
|
@ -144,7 +145,7 @@ func (r *Repository) UpdateFile(_ context.Context, path string, create bool, upd
|
|||
updatedContent := string(content)
|
||||
|
||||
for _, update := range updaters {
|
||||
updatedContent, err = update(updatedContent)
|
||||
updatedContent, err = update(updatedContent, filepath.Base(path))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to run updater on file %s", path)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ var (
|
|||
)
|
||||
|
||||
func Changelog(info ReleaseInfo) Updater {
|
||||
return func(content string) (string, error) {
|
||||
return func(content string, filename string) (string, error) {
|
||||
headerIndex := ChangelogUpdaterHeaderRegex.FindStringIndex(content)
|
||||
if headerIndex == nil && len(content) != 0 {
|
||||
return "", fmt.Errorf("unexpected format of CHANGELOG.md, header does not match")
|
||||
|
|
|
|||
|
|
@ -9,11 +9,12 @@ import (
|
|||
func TestChangelogUpdater_UpdateContent(t *testing.T) {
|
||||
tests := []updaterTestCase{
|
||||
{
|
||||
name: "empty file",
|
||||
content: "",
|
||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n"},
|
||||
want: "# Changelog\n\n## v1.0.0\n",
|
||||
wantErr: assert.NoError,
|
||||
name: "empty file",
|
||||
content: "",
|
||||
filename: "CHANGELOG.md",
|
||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n"},
|
||||
want: "# Changelog\n\n## v1.0.0\n",
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "well-formatted changelog",
|
||||
|
|
@ -27,7 +28,8 @@ func TestChangelogUpdater_UpdateContent(t *testing.T) {
|
|||
|
||||
### Bazuuum
|
||||
`,
|
||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n\n- Version 1, juhu.\n"},
|
||||
filename: "CHANGELOG.md",
|
||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n\n- Version 1, juhu.\n"},
|
||||
want: `# Changelog
|
||||
|
||||
## v1.0.0
|
||||
|
|
@ -45,11 +47,12 @@ func TestChangelogUpdater_UpdateContent(t *testing.T) {
|
|||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "error on invalid header",
|
||||
content: "What even is this file?",
|
||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n\n- Version 1, juhu.\n"},
|
||||
want: "",
|
||||
wantErr: assert.Error,
|
||||
name: "error on invalid header",
|
||||
content: "What even is this file?",
|
||||
filename: "CHANGELOG.md",
|
||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n\n- Version 1, juhu.\n"},
|
||||
want: "",
|
||||
wantErr: assert.Error,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import (
|
|||
var GenericUpdaterSemVerRegex = regexp.MustCompile(`\d+\.\d+\.\d+(-[\w.]+)?(.*x-releaser-pleaser-version)`)
|
||||
|
||||
func Generic(info ReleaseInfo) Updater {
|
||||
return func(content string) (string, error) {
|
||||
return func(content string, filename string) (string, error) {
|
||||
// We strip the "v" prefix to avoid adding/removing it from the users input.
|
||||
version := strings.TrimPrefix(info.Version, "v")
|
||||
|
||||
|
|
|
|||
|
|
@ -9,8 +9,9 @@ import (
|
|||
func TestGenericUpdater_UpdateContent(t *testing.T) {
|
||||
tests := []updaterTestCase{
|
||||
{
|
||||
name: "single line",
|
||||
content: "v1.0.0 // x-releaser-pleaser-version",
|
||||
name: "single line",
|
||||
content: "v1.0.0 // x-releaser-pleaser-version",
|
||||
filename: "version.txt",
|
||||
info: ReleaseInfo{
|
||||
Version: "v1.2.0",
|
||||
},
|
||||
|
|
@ -18,8 +19,9 @@ func TestGenericUpdater_UpdateContent(t *testing.T) {
|
|||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "multiline line",
|
||||
content: "Foooo\n\v1.2.0\nv1.0.0 // x-releaser-pleaser-version\n",
|
||||
name: "multiline line",
|
||||
content: "Foooo\n\v1.2.0\nv1.0.0 // x-releaser-pleaser-version\n",
|
||||
filename: "version.txt",
|
||||
info: ReleaseInfo{
|
||||
Version: "v1.2.0",
|
||||
},
|
||||
|
|
@ -27,8 +29,9 @@ func TestGenericUpdater_UpdateContent(t *testing.T) {
|
|||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "invalid existing version",
|
||||
content: "1.0 // x-releaser-pleaser-version",
|
||||
name: "invalid existing version",
|
||||
content: "1.0 // x-releaser-pleaser-version",
|
||||
filename: "version.txt",
|
||||
info: ReleaseInfo{
|
||||
Version: "v1.2.0",
|
||||
},
|
||||
|
|
@ -36,8 +39,9 @@ func TestGenericUpdater_UpdateContent(t *testing.T) {
|
|||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "complicated line",
|
||||
content: "version: v1.2.0-alpha.1 => Awesome, isnt it? x-releaser-pleaser-version foobar",
|
||||
name: "complicated line",
|
||||
content: "version: v1.2.0-alpha.1 => Awesome, isnt it? x-releaser-pleaser-version foobar",
|
||||
filename: "version.txt",
|
||||
info: ReleaseInfo{
|
||||
Version: "v1.2.0",
|
||||
},
|
||||
|
|
|
|||
30
internal/updater/packagejson.go
Normal file
30
internal/updater/packagejson.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package updater
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// PackageJson creates an updater that modifies the version field in package.json files
|
||||
func PackageJson(info ReleaseInfo) Updater {
|
||||
return func(content string, filename string) (string, error) {
|
||||
if filename != "package.json" {
|
||||
return content, nil // No update needed for non-package.json files
|
||||
}
|
||||
// We strip the "v" prefix to match npm versioning convention
|
||||
version := strings.TrimPrefix(info.Version, "v")
|
||||
|
||||
// Regex to match "version": "..." with flexible whitespace and quote styles
|
||||
versionRegex := regexp.MustCompile(`("version"\s*:\s*)"[^"]*"`)
|
||||
|
||||
// Check if the file contains a version field
|
||||
if !versionRegex.MatchString(content) {
|
||||
return content, nil
|
||||
}
|
||||
|
||||
// Replace the version value while preserving the original formatting
|
||||
updatedContent := versionRegex.ReplaceAllString(content, `${1}"`+version+`"`)
|
||||
|
||||
return updatedContent, nil
|
||||
}
|
||||
}
|
||||
70
internal/updater/packagejson_test.go
Normal file
70
internal/updater/packagejson_test.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package updater
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPackageJsonUpdater(t *testing.T) {
|
||||
tests := []updaterTestCase{
|
||||
{
|
||||
name: "simple package.json",
|
||||
content: `{"name":"test","version":"1.0.0"}`,
|
||||
filename: "package.json",
|
||||
info: ReleaseInfo{
|
||||
Version: "v2.0.5",
|
||||
},
|
||||
want: `{"name":"test","version":"2.0.5"}`,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "simple package.json, wrong name",
|
||||
content: `{"name":"test","version":"1.0.0"}`,
|
||||
filename: "nopackage.json",
|
||||
info: ReleaseInfo{
|
||||
Version: "v2.0.5",
|
||||
},
|
||||
want: `{"name":"test","version":"1.0.0"}`,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "complex package.json",
|
||||
content: "{\n \"name\": \"test\",\n \"version\": \"1.0.0\",\n \"dependencies\": {\n \"foo\": \"^1.0.0\"\n }\n}",
|
||||
filename: "package.json",
|
||||
info: ReleaseInfo{
|
||||
Version: "v2.0.0",
|
||||
},
|
||||
want: "{\n \"name\": \"test\",\n \"version\": \"2.0.0\",\n \"dependencies\": {\n \"foo\": \"^1.0.0\"\n }\n}",
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "invalid json",
|
||||
content: `not json`,
|
||||
filename: "package.json",
|
||||
info: ReleaseInfo{
|
||||
Version: "v2.0.0",
|
||||
},
|
||||
want: `not json`,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "json without version",
|
||||
content: `{"name":"test"}`,
|
||||
filename: "package.json",
|
||||
info: ReleaseInfo{
|
||||
Version: "v2.0.0",
|
||||
},
|
||||
want: `{"name":"test"}`,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
fmt.Println("Running updater test for PackageJson")
|
||||
runUpdaterTest(t, PackageJson, tt)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ type ReleaseInfo struct {
|
|||
ChangelogEntry string
|
||||
}
|
||||
|
||||
type Updater func(string) (string, error)
|
||||
type Updater func(content string, filename string) (string, error)
|
||||
|
||||
type NewUpdater func(ReleaseInfo) Updater
|
||||
|
||||
|
|
|
|||
|
|
@ -8,19 +8,20 @@ import (
|
|||
)
|
||||
|
||||
type updaterTestCase struct {
|
||||
name string
|
||||
content string
|
||||
info ReleaseInfo
|
||||
want string
|
||||
wantErr assert.ErrorAssertionFunc
|
||||
name string
|
||||
content string
|
||||
filename string
|
||||
info ReleaseInfo
|
||||
want string
|
||||
wantErr assert.ErrorAssertionFunc
|
||||
}
|
||||
|
||||
func runUpdaterTest(t *testing.T, constructor NewUpdater, tt updaterTestCase) {
|
||||
t.Helper()
|
||||
|
||||
got, err := constructor(tt.info)(tt.content)
|
||||
if !tt.wantErr(t, err, fmt.Sprintf("Updater(%v, %v)", tt.content, tt.info)) {
|
||||
got, err := constructor(tt.info)(tt.content, tt.filename)
|
||||
if !tt.wantErr(t, err, fmt.Sprintf("Updater(%v, %v, %v)", tt.content, tt.filename, tt.info)) {
|
||||
return
|
||||
}
|
||||
assert.Equalf(t, tt.want, got, "Updater(%v, %v)", tt.content, tt.info)
|
||||
assert.Equalf(t, tt.want, got, "Updater(%v, %v, %v)", tt.content, tt.filename, tt.info)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue