forked from dionomusuko/gh-release-with-wf-dispatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.go
126 lines (107 loc) · 2.91 KB
/
release.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"io"
"log"
"os"
"regexp"
"strconv"
"strings"
"github.com/go-git/go-billy/v5"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/parser"
)
const (
defaultPath = "RELEASE"
)
func readReleaseFile(fs billy.Filesystem, filePath string) (string, *ast.StringNode, *yaml.Path, *ast.File) {
if filePath == "" {
filePath = defaultPath
}
file, _ := fs.Open(filePath)
f, _ := io.ReadAll(file)
parseFile, err := parser.ParseBytes(f, parser.ParseComments)
if err != nil {
log.Fatalf("failed to read file: %v", err)
}
yamlPath, err := yaml.PathString("$.tag")
if err != nil {
log.Fatalf("failed to file path: %v", err)
}
oldNode, err := yamlPath.FilterFile(parseFile)
if err != nil {
log.Fatalf("failed to read old node")
}
newNode := &ast.StringNode{
BaseNode: &ast.BaseNode{},
Token: oldNode.GetToken(),
}
if c := oldNode.GetComment(); c != nil {
if err := newNode.SetComment(c); err != nil {
log.Fatalf("failed to set comment: %v", err)
}
}
return oldNode.String(), newNode, yamlPath, parseFile
}
func writeFile(yamlPath *yaml.Path, fs billy.Filesystem, parseFile *ast.File, newNode *ast.StringNode, filePath string) {
if err := yamlPath.ReplaceWithNode(parseFile, newNode); err != nil {
log.Fatalf("failed to replace file: %v", err)
}
b, _ := io.ReadAll(parseFile)
f, _ := fs.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if _, err := f.Write(b); err != nil {
log.Fatalf("failed to write file: %v", err)
}
}
func generateTag(newNode *ast.StringNode, oldTag, tag string) (*ast.StringNode, string) {
if oldTag == "" {
log.Fatalf("failed to get oldTag")
}
if tag == "" {
newTag, _ := increment(oldTag)
newNode.Value = newTag
return newNode, newTag
}
newTag := replacement(tag)
newNode.Value = newTag
return newNode, newTag
}
// version の increment実装
// If the tag is not added, it will be patch release.
func increment(oldTag string) (string, string) {
if !validate(oldTag) {
log.Fatalf("Input value is not a tag: %v", oldTag)
}
ary := strings.Split(oldTag, ".")
oldVersion, err := strconv.Atoi(ary[len(ary)-1])
if err != nil {
log.Fatalf("failed to conv version: %v", err)
}
oldVersion++
ary[len(ary)-1] = strconv.Itoa(oldVersion)
return strings.Join(ary, "."), getPrefix(oldTag)
}
// workflow dispatch 入力値を反映させる
// 既存タグとの比較 or
func replacement(tag string) string {
if !validate(tag) {
log.Fatalf("Input value is not a tag: %v", tag)
}
return tag
}
func validate(str string) bool {
// v1.0.0, hogehoge/v1.0.0 のパターンに対応する
reg := regexp.MustCompile(`v([0-9]+).([0-9]+).([0-9]+)`)
return reg.MatchString(str)
}
func getPrefix(tag string) string {
splitTag := strings.Split(tag, "/")
// hoge/microservice/v1.0.0 のようなパターンに対応
var pr string
for _, v := range splitTag {
if !validate(v) {
pr += v + "/"
}
}
return pr
}