feat: implemented first version
This commit is contained in:
49
src/config/action.go
Normal file
49
src/config/action.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
githubactions "github.com/sethvargo/go-githubactions"
|
||||
)
|
||||
|
||||
// DrafterConfig holds all configurations we need for the drafter to run
|
||||
type DrafterConfig struct {
|
||||
// RepoOwner as provided by the github context
|
||||
RepoOwner string
|
||||
// RepoName as provided by the github context
|
||||
RepoName string
|
||||
// ApiUrl of gitea as provided by the "GITHUB_SERVER_URL" env var
|
||||
ApiUrl string
|
||||
// Token as provided by the "GITHUB_TOKEN" env var
|
||||
Token string
|
||||
// ConfigPath as provided by the "config-path" action input. defaults to ".gitea/release-drafter.yml"
|
||||
ConfigPath string
|
||||
}
|
||||
|
||||
// NewFromInputs creates a new drafter config by using the action inputs and the github context
|
||||
func NewFromInputs(action *githubactions.Action) (*DrafterConfig, error) {
|
||||
actionCtx, err := action.Context()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var configPath string
|
||||
inputConfigPath := action.GetInput("config-path")
|
||||
|
||||
if inputConfigPath == "" {
|
||||
configPath = ".gitea/release-drafter.yml"
|
||||
} else {
|
||||
configPath = inputConfigPath
|
||||
}
|
||||
|
||||
owner, name := actionCtx.Repo()
|
||||
c := DrafterConfig{
|
||||
RepoOwner: owner,
|
||||
RepoName: name,
|
||||
ApiUrl: actionCtx.ServerURL,
|
||||
Token: os.Getenv("GITHUB_TOKEN"),
|
||||
ConfigPath: configPath,
|
||||
}
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
63
src/config/repo.go
Normal file
63
src/config/repo.go
Normal file
@@ -0,0 +1,63 @@
|
||||
// this file implements configurations for repositories
|
||||
package config
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// RepoConfig holds all configurations for the repo we're running on
|
||||
type RepoConfig struct {
|
||||
// DefaultBranch where we look for the configuration file
|
||||
DefaultBranch string `mapstructure:"default-branch"`
|
||||
// NameTemplate template for the release name
|
||||
NameTemplate string `mapstructure:"name-template"`
|
||||
// TagTemplate template for the release tag
|
||||
TagTemplate string `mapstructure:"tag-template"`
|
||||
VersionResolver struct {
|
||||
Major struct {
|
||||
Labels []string
|
||||
}
|
||||
Minor struct {
|
||||
Labels []string
|
||||
}
|
||||
Patch struct {
|
||||
Labels []string
|
||||
}
|
||||
Default string
|
||||
} `mapstructure:"version-resolver"`
|
||||
}
|
||||
|
||||
// ReadRepoConfig reads in the yaml config found in the default branch of the project and adds sensible defaults if values aren't set
|
||||
func ReadRepoConfig(in io.Reader, defaultBranch string) (*RepoConfig, error) {
|
||||
vv := viper.New()
|
||||
vv.SetConfigType("yaml")
|
||||
|
||||
err := vv.ReadConfig(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// we set defaults here but if they are present in the configuration file they will be overwritten
|
||||
cfg := &RepoConfig{
|
||||
DefaultBranch: defaultBranch,
|
||||
NameTemplate: "v$RESOLVED_VERSION",
|
||||
TagTemplate: "v$RESOLVED_VERSION",
|
||||
VersionResolver: struct {
|
||||
Major struct{ Labels []string }
|
||||
Minor struct{ Labels []string }
|
||||
Patch struct{ Labels []string }
|
||||
Default string
|
||||
}{
|
||||
Major: struct{ Labels []string }{[]string{"major"}},
|
||||
Minor: struct{ Labels []string }{[]string{"minor"}},
|
||||
Patch: struct{ Labels []string }{[]string{"patch"}},
|
||||
Default: "minor",
|
||||
},
|
||||
}
|
||||
|
||||
vv.Unmarshal(&cfg)
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
95
src/config/repo_test.go
Normal file
95
src/config/repo_test.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidConfigShouldWork(t *testing.T) {
|
||||
// Given
|
||||
// A valid yml config
|
||||
config := `
|
||||
name-template: 'v$RESOLVED_VERSION 🌈'
|
||||
tag-template: 'tag-v$RESOLVED_VERSION'
|
||||
version-resolver:
|
||||
major:
|
||||
labels:
|
||||
- 'major-test'
|
||||
minor:
|
||||
labels:
|
||||
- 'minor-test'
|
||||
patch:
|
||||
labels:
|
||||
- 'patch-test'
|
||||
default: 'minor-test'`
|
||||
|
||||
in := strings.NewReader(config)
|
||||
|
||||
// When
|
||||
// Reading in the config
|
||||
cfg, err := ReadRepoConfig(in, "main")
|
||||
|
||||
// Then
|
||||
// No error should've occurred
|
||||
assert.NoError(t, err)
|
||||
|
||||
// The name template should've been read in properly
|
||||
assert.Equal(t, "v$RESOLVED_VERSION 🌈", cfg.NameTemplate)
|
||||
|
||||
// The name template should've been read in properly
|
||||
assert.Equal(t, "tag-v$RESOLVED_VERSION", cfg.TagTemplate)
|
||||
|
||||
// The version resolver major labels should've been read in properly
|
||||
assert.Len(t, cfg.VersionResolver.Major.Labels, 1)
|
||||
assert.Contains(t, cfg.VersionResolver.Major.Labels, "major-test")
|
||||
|
||||
// The version resolver minor labels should've been read in properly
|
||||
assert.Len(t, cfg.VersionResolver.Minor.Labels, 1)
|
||||
assert.Contains(t, cfg.VersionResolver.Minor.Labels, "minor-test")
|
||||
|
||||
// The version resolver patch labels should've been read in properly
|
||||
assert.Len(t, cfg.VersionResolver.Patch.Labels, 1)
|
||||
assert.Contains(t, cfg.VersionResolver.Patch.Labels, "patch-test")
|
||||
|
||||
// The version resolver default should've been read in properly
|
||||
assert.Equal(t, "minor-test", cfg.VersionResolver.Default)
|
||||
}
|
||||
|
||||
func TestEmptyConfigShouldUseDefaults(t *testing.T) {
|
||||
// Given
|
||||
// An empty yml config
|
||||
config := ``
|
||||
|
||||
in := strings.NewReader(config)
|
||||
|
||||
// When
|
||||
// Reading in the config
|
||||
cfg, err := ReadRepoConfig(in, "main")
|
||||
|
||||
// Then
|
||||
// No error should've occurred
|
||||
assert.NoError(t, err)
|
||||
|
||||
// The name template should've been read in properly
|
||||
assert.Equal(t, "v$RESOLVED_VERSION", cfg.NameTemplate)
|
||||
|
||||
// The name template should've been read in properly
|
||||
assert.Equal(t, "v$RESOLVED_VERSION", cfg.TagTemplate)
|
||||
|
||||
// The version resolver major labels should've been read in properly
|
||||
assert.Len(t, cfg.VersionResolver.Major.Labels, 1)
|
||||
assert.Contains(t, cfg.VersionResolver.Major.Labels, "major")
|
||||
|
||||
// The version resolver minor labels should've been read in properly
|
||||
assert.Len(t, cfg.VersionResolver.Minor.Labels, 1)
|
||||
assert.Contains(t, cfg.VersionResolver.Minor.Labels, "minor")
|
||||
|
||||
// The version resolver patch labels should've been read in properly
|
||||
assert.Len(t, cfg.VersionResolver.Patch.Labels, 1)
|
||||
assert.Contains(t, cfg.VersionResolver.Patch.Labels, "patch")
|
||||
|
||||
// The version resolver default should've been read in properly
|
||||
assert.Equal(t, "minor", cfg.VersionResolver.Default)
|
||||
}
|
||||
Reference in New Issue
Block a user