Skip to content

Commit 29881b8

Browse files
authored
fix(librarian): add release related fields to state (#1630)
add new fields related to release according to [go/librarian:release-command](http://goto.google.com/librarian:release-command). Fixes #1006
1 parent 80ea37a commit 29881b8

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

doc/state-schema.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Each object in the `libraries` list represents a single library and has the foll
2424
| `source_roots` | list | A list of directories in the language repository where Librarian contributes code. | Yes | Must not be empty, and each path must be a valid directory path. |
2525
| `preserve_regex` | list | A list of regular expressions for files and directories to preserve during the copy and remove process. | No | Each entry must be a valid regular expression. |
2626
| `remove_regex` | list | A list of regular expressions for files and directories to remove before copying generated code. If not set, this defaults to the `source_roots`. A more specific `preserve_regex` takes precedence. | No | Each entry must be a valid regular expression. |
27+
| `release_exclude_paths` | list | A list of directories to exclude from the release. | No | Each entry must be a valid directory path. |
28+
| `tag_format` | string | A format string for the release tag. The supported placeholders are `{id}` and `{version}`. | No | Must only contain `{id}` and `{version}` as placeholders. |
2729

2830
## `apis` Object
2931

internal/config/state.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ type LibraryState struct {
9999
// If not set, this defaults to the `source_roots`.
100100
// A more specific `preserve_regex` takes precedence.
101101
RemoveRegex []string `yaml:"remove_regex" json:"remove_regex"`
102+
// Path of commits to be excluded from parsing while calculating library changes.
103+
// If all files from commit belong to one of the paths it will be skipped.
104+
ReleaseExcludePaths []string `yaml:"release_exclude_paths,omitempty" json:"release_exclude_paths,omitempty"`
105+
// Specifying a tag format allows librarian to honor this format when creating
106+
// a tag for the release of the library. The replacement values of {id} and {version}
107+
// permitted to reference the values configured in the library. If not specified
108+
// the assumed format is {id}-{version}.
109+
TagFormat string `yaml:"tag_format,omitempty" json:"tag_format,omitempty"`
102110
// An error message from the docker response.
103111
// This field is ignored when writing to state.yaml.
104112
ErrorMessage string `yaml:"-" json:"error,omitempty"`
@@ -108,6 +116,7 @@ var (
108116
libraryIDRegex = regexp.MustCompile(`^[a-zA-Z0-9/._-]+$`)
109117
semverRegex = regexp.MustCompile(`^v?\d+\.\d+\.\d+$`)
110118
hexRegex = regexp.MustCompile("^[a-fA-F0-9]+$")
119+
tagFormatRegex = regexp.MustCompile(`{[^{}]*}`)
111120
)
112121

113122
// Validate checks that the Library is valid.
@@ -148,6 +157,19 @@ func (l *LibraryState) Validate() error {
148157
return fmt.Errorf("invalid source_path at index %d: %q", i, p)
149158
}
150159
}
160+
for i, p := range l.ReleaseExcludePaths {
161+
if !isValidDirPath(p) {
162+
return fmt.Errorf("invalid release_exclude_path at index %d: %q", i, p)
163+
}
164+
}
165+
if l.TagFormat != "" {
166+
matches := tagFormatRegex.FindAllString(l.TagFormat, -1)
167+
for _, match := range matches {
168+
if match != "{id}" && match != "{version}" {
169+
return fmt.Errorf("invalid placeholder in tag_format: %s", match)
170+
}
171+
}
172+
}
151173
for i, r := range l.PreserveRegex {
152174
if _, err := regexp.Compile(r); err != nil {
153175
return fmt.Errorf("invalid preserve_regex at index %d: %w", i, err)

internal/config/state_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,44 @@ func TestLibrary_Validate(t *testing.T) {
242242
},
243243
wantErr: true,
244244
},
245+
{
246+
name: "valid release_exclude_path",
247+
library: &LibraryState{
248+
ID: "a/b",
249+
SourceRoots: []string{"src/a"},
250+
APIs: []*API{{Path: "a/b/v1"}},
251+
ReleaseExcludePaths: []string{"a/b", "c"},
252+
},
253+
},
254+
{
255+
name: "invalid release_exclude_path",
256+
library: &LibraryState{
257+
ID: "a/b",
258+
SourceRoots: []string{"src/a"},
259+
APIs: []*API{{Path: "a/b/v1"}},
260+
ReleaseExcludePaths: []string{"/a/b"},
261+
},
262+
wantErr: true,
263+
},
264+
{
265+
name: "valid tag_format",
266+
library: &LibraryState{
267+
ID: "a/b",
268+
SourceRoots: []string{"src/a"},
269+
APIs: []*API{{Path: "a/b/v1"}},
270+
TagFormat: "v{id}-{version}",
271+
},
272+
},
273+
{
274+
name: "invalid tag_format placeholder",
275+
library: &LibraryState{
276+
ID: "a/b",
277+
SourceRoots: []string{"src/a"},
278+
APIs: []*API{{Path: "a/b/v1"}},
279+
TagFormat: "{id}-{foo}",
280+
},
281+
wantErr: true,
282+
},
245283
} {
246284
t.Run(test.name, func(t *testing.T) {
247285
if err := test.library.Validate(); (err != nil) != test.wantErr {

0 commit comments

Comments
 (0)