Skip to content

Commit dcabd58

Browse files
authored
Merge pull request #117 from joshbeard/fix-changelog-title-attr
fix: changelog title validation
2 parents 270e64c + 656e02b commit dcabd58

File tree

4 files changed

+32
-78
lines changed

4 files changed

+32
-78
lines changed

go.mod

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/liveoaklabs/terraform-provider-readme
22

33
go 1.21
44

5-
require github.com/liveoaklabs/readme-api-go-client v0.2.1
5+
require github.com/liveoaklabs/readme-api-go-client v0.2.2
66

77
require (
88
github.com/adrg/frontmatter v0.2.0
@@ -34,10 +34,7 @@ require (
3434
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
3535
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
3636
github.com/armon/go-radix v1.0.0 // indirect
37-
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
3837
github.com/bgentry/speakeasy v0.1.0 // indirect
39-
github.com/charmbracelet/lipgloss v0.9.1 // indirect
40-
github.com/charmbracelet/log v0.3.0 // indirect
4138
github.com/cheggaaa/pb/v3 v3.1.4 // indirect
4239
github.com/cloudflare/circl v1.3.7 // indirect
4340
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
@@ -49,7 +46,6 @@ require (
4946
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
5047
github.com/go-git/go-billy/v5 v5.5.0 // indirect
5148
github.com/go-git/go-git/v5 v5.11.0 // indirect
52-
github.com/go-logfmt/logfmt v0.6.0 // indirect
5349
github.com/go-test/deep v1.0.7 // indirect
5450
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5551
github.com/golang/protobuf v1.5.3 // indirect
@@ -81,7 +77,6 @@ require (
8177
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
8278
github.com/kevinburke/ssh_config v1.2.0 // indirect
8379
github.com/logrusorgru/aurora/v4 v4.0.0 // indirect
84-
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
8580
github.com/magiconair/properties v1.8.7 // indirect
8681
github.com/mattn/go-colorable v0.1.13 // indirect
8782
github.com/mattn/go-isatty v0.0.20 // indirect
@@ -92,8 +87,6 @@ require (
9287
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
9388
github.com/mitchellh/mapstructure v1.5.0 // indirect
9489
github.com/mitchellh/reflectwalk v1.0.2 // indirect
95-
github.com/muesli/reflow v0.3.0 // indirect
96-
github.com/muesli/termenv v0.15.2 // indirect
9790
github.com/oklog/run v1.1.0 // indirect
9891
github.com/onsi/gomega v1.28.1 // indirect
9992
github.com/pelletier/go-toml/v2 v2.1.0 // indirect

go.sum

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,13 @@ github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
3636
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
3737
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
3838
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
39-
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
40-
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
4139
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
4240
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
4341
github.com/boumenot/gocover-cobertura v1.2.0 h1:g+VROIASoEHBrEilIyaCmgo7HGm+AV5yKEPLk0qIY+s=
4442
github.com/boumenot/gocover-cobertura v1.2.0/go.mod h1:fz7ly8dslE42VRR5ZWLt2OHGDHjkTiA2oNvKgJEjLT0=
4543
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
4644
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
4745
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
48-
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
49-
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
50-
github.com/charmbracelet/log v0.3.0 h1:u5aB2KJDgNZo4WOfOC8C+KvGIkJ2rCFNlPWDu6xhnqI=
51-
github.com/charmbracelet/log v0.3.0/go.mod h1:OR4E1hutLsax3ZKpXbgUqPtTjQfrh1pG3zwHGWuuq8g=
5246
github.com/cheggaaa/pb v2.0.7+incompatible/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
5347
github.com/cheggaaa/pb/v3 v3.0.4/go.mod h1:7rgWxLrAUcFMkvJuv09+DYi7mMUYi8nO9iOWcvGJPfw=
5448
github.com/cheggaaa/pb/v3 v3.1.4 h1:DN8j4TVVdKu3WxVwcRKu0sG00IIU6FewoABZzXbRQeo=
@@ -91,8 +85,6 @@ github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMj
9185
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
9286
github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4=
9387
github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY=
94-
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
95-
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
9688
github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M=
9789
github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
9890
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
@@ -192,12 +184,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
192184
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
193185
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
194186
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
195-
github.com/liveoaklabs/readme-api-go-client v0.2.1 h1:IWtpR9yk1eDl3fgdlK8Cvy6IvglnIW2bZT4RK2/+tLg=
196-
github.com/liveoaklabs/readme-api-go-client v0.2.1/go.mod h1:NAsaEOAPWlxCIth2HmoZNzSRJymWeqLUdVcQatpOEEs=
187+
github.com/liveoaklabs/readme-api-go-client v0.2.2 h1:TQ8CDv1+Yf9YlWL1HNfiF6KKQoxq8XuQGcRvaRsp/8c=
188+
github.com/liveoaklabs/readme-api-go-client v0.2.2/go.mod h1:Bpwle+Oj3pKT5WVXZkqYNJolDY6YULg+cB4jJdH2wo8=
197189
github.com/logrusorgru/aurora/v4 v4.0.0 h1:sRjfPpun/63iADiSvGGjgA1cAYegEWMPCJdUpJYn9JA=
198190
github.com/logrusorgru/aurora/v4 v4.0.0/go.mod h1:lP0iIa2nrnT/qoFXcOZSrZQpJ1o6n2CUf/hyHi2Q4ZQ=
199-
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
200-
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
201191
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
202192
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
203193
github.com/matryer/is v1.3.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
@@ -218,7 +208,6 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
218208
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
219209
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
220210
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
221-
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
222211
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
223212
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
224213
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
@@ -235,10 +224,6 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
235224
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
236225
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
237226
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
238-
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
239-
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
240-
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
241-
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
242227
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
243228
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
244229
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
@@ -265,7 +250,6 @@ github.com/princjef/mageutil v1.0.0 h1:1OfZcJUMsooPqieOz2ooLjI+uHUo618pdaJsbCXcF
265250
github.com/princjef/mageutil v1.0.0/go.mod h1:mkShhaUomCYfAoVvTKRcbAs8YSVPdtezI5j6K+VXhrs=
266251
github.com/princjef/termdiff v0.1.0 h1:O3PWhfPFzX6GqzQ+41B3XzzJpMlx0+9Vysm+Pv76C9U=
267252
github.com/princjef/termdiff v0.1.0/go.mod h1:JJOfCA/eR6T1JfsoxQQ6jsG3LGoQDoKUIRQrKqAO+p4=
268-
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
269253
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
270254
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
271255
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=

readme/changelog_resource.go

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package readme
22

33
import (
44
"context"
5-
"reflect"
65
"strings"
76

87
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -95,16 +94,17 @@ func (r *changelogResource) ModifyPlan(
9594
plan := &changelogResourceModel{}
9695
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
9796

98-
if resp.Diagnostics.HasError() || plan == nil {
97+
state := &changelogResourceModel{}
98+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
99+
100+
if resp.Diagnostics.HasError() || plan == nil || state == nil {
99101
return
100102
}
101103

102-
// Trim leading and trailing whitespace from the body.
103-
// The ReadMe API normalizes this, but we need to track the original value
104-
// provided by the user.
105-
// The 'body_clean' attribute is used to track the normalized value to
106-
// compare against the API response.
107104
body := strings.TrimSpace(plan.Body.ValueString())
105+
106+
// Expand newline escape sequences.
107+
body = strings.ReplaceAll(body, `\n`, "\n")
108108
plan.BodyClean = types.StringValue(body)
109109

110110
if plan.Hidden.IsNull() {
@@ -114,18 +114,14 @@ func (r *changelogResource) ModifyPlan(
114114
diags := resp.Plan.Set(ctx, plan)
115115
resp.Diagnostics.Append(diags...)
116116

117-
state := &changelogResourceModel{}
118-
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
119-
120-
if state == nil {
121-
return
122-
}
123-
124117
// Several attributes are refreshed whenever the changelog is modified.
125118
// This may need to be added to if additional attributes are discovered to
126119
// be dynamic.
127-
if plan.BodyClean != state.BodyClean ||
128-
plan.Hidden != state.Hidden {
120+
if !state.BodyClean.Equal(plan.BodyClean) ||
121+
!state.Hidden.Equal(plan.Hidden) ||
122+
!state.Title.Equal(plan.Title) ||
123+
!state.Type.Equal(plan.Type) {
124+
129125
tflog.Info(ctx, "Changelog body has changed. Refreshing dynamic attributes.")
130126

131127
plan.Algolia = types.ObjectUnknown(map[string]attr.Type{
@@ -145,43 +141,16 @@ func (r *changelogResource) ModifyPlan(
145141
})
146142
}
147143

148-
diags = resp.Plan.Set(ctx, plan)
149-
resp.Diagnostics.Append(diags...)
150-
}
144+
if plan.Title.ValueString() == "" {
145+
resp.Diagnostics.AddError("Title is not set.",
146+
"The 'title' attribute is not set. This is a required attribute for the changelog resource "+
147+
"and must be set either in the resource configuration or in the front matter of the changelog body.")
151148

152-
// ValidateConfig is used for validating attribute values.
153-
func (r changelogResource) ValidateConfig(
154-
ctx context.Context,
155-
req resource.ValidateConfigRequest,
156-
resp *resource.ValidateConfigResponse,
157-
) {
158-
var data changelogResourceModel
159-
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
160-
161-
if data.Title.IsNull() {
162-
// check front matter for 'title'.
163-
titleMatter, diag := frontmatter.GetValue(ctx, data.Body.ValueString(), "Title")
164-
if diag != "" {
165-
resp.Diagnostics.AddAttributeError(
166-
path.Root("title"),
167-
"Error checking front matter during validation.",
168-
diag,
169-
)
170-
171-
return
172-
}
173-
174-
// Fail if title is not set in front matter or the attribute.
175-
if titleMatter == (reflect.Value{}) {
176-
resp.Diagnostics.AddAttributeError(
177-
path.Root("title"),
178-
"Missing required attribute.",
179-
"'title' must be set using the attribute or in the body front matter.",
180-
)
181-
182-
return
183-
}
149+
return
184150
}
151+
152+
diags = resp.Plan.Set(ctx, plan)
153+
resp.Diagnostics.Append(diags...)
185154
}
186155

187156
// Create creates the changelog and sets the initial Terraform state.

readme/changelog_resource_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,19 @@ func TestChangelogResource(t *testing.T) {
138138
},
139139
// Test updating with no title results in error.
140140
{
141-
ExpectError: regexp.MustCompile("'title' must be set using the attribute or in the body front matter."),
141+
ExpectError: regexp.MustCompile("The 'title' attribute is not set."),
142142
Config: providerConfig + `
143143
resource "readme_changelog" "test" {
144144
body = "no title is set with front matter or attribute"
145145
}`,
146+
PreConfig: func() {
147+
gock.OffAll()
148+
gock.New(testURL).
149+
Get("/changelogs/" + mockChangelogs[0].Slug).
150+
Times(1).
151+
Reply(200).
152+
JSON(mockChangelogs[0])
153+
},
146154
},
147155
// Test updating with front matter.
148156
{

0 commit comments

Comments
 (0)