Skip to content

Commit

Permalink
Compare original PR body on release notes mapping
Browse files Browse the repository at this point in the history
When creating a mapping we now preserve the original PR body in the YAML
map. This allows us to track modifications to the PR if their values
already got mapped during the release cycle and warn the end user in
that case.

Fixes #3510

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Apr 2, 2024
1 parent 0c522db commit 0623915
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cmd/krel/cmd/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func createDraftPR(repoPath, tag string) (err error) {
}

// Run the release notes fix flow
err := fixReleaseNotes(filepath.Join(releaseDir, releaseNotesWorkDir), releaseNotes)
err := FixReleaseNotes(filepath.Join(releaseDir, releaseNotesWorkDir), releaseNotes)

Check failure on line 403 in cmd/krel/cmd/release_notes.go

View workflow job for this annotation

GitHub Actions / lint

undefined: FixReleaseNotes) (typecheck)

Check failure on line 403 in cmd/krel/cmd/release_notes.go

View workflow job for this annotation

GitHub Actions / lint

undefined: FixReleaseNotes (typecheck)
if err != nil {
return fmt.Errorf("while running release notes fix flow: %w", err)
}
Expand Down Expand Up @@ -1154,6 +1154,7 @@ func fixReleaseNotes(workDir string, releaseNotes *notes.ReleaseNotes) error {
ActionRequired: note.ActionRequired,
Documentation: note.Documentation,
DoNotPublish: note.DoNotPublish,
PRBody: note.PRBody,
}

if noteMaps != nil {
Expand Down Expand Up @@ -1410,6 +1411,8 @@ func editReleaseNote(pr int, workDir string, originalNote, modifiedNote *notes.R
return true, errors.New("invalid map: the YAML code did not have a PR number")
}

testMap.PRBody = &originalNote.PRBody

// Remarshall the newyaml to save only the new values
newYAML, err := yaml.Marshal(testMap)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/notes/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

gogithub "github.com/google/go-github/v58/github"
"github.com/nozzle/throttler"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/sirupsen/logrus"
"golang.org/x/text/cases"
"golang.org/x/text/language"
Expand Down Expand Up @@ -139,6 +140,9 @@ type ReleaseNote struct {

// IsMapped is set if the note got modified from a map
IsMapped bool `json:"is_mapped,omitempty"`

// PRBody is the full PR body of the release note
PRBody string `json:"pr_body,omitempty"`
}

type Documentation struct {
Expand Down Expand Up @@ -518,6 +522,7 @@ func (g *Gatherer) ReleaseNoteFromCommit(result *Result) (*ReleaseNote, error) {
DuplicateKind: isDuplicateKind,
ActionRequired: labelExactMatch(pr, "release-note-action-required"),
DoNotPublish: labelExactMatch(pr, "release-note-none"),
PRBody: prBody,
}, nil
}

Expand Down Expand Up @@ -757,6 +762,7 @@ func (g *Gatherer) ReleaseNoteForPullRequest(prNr int) (*ReleaseNote, error) {
ActionRequired: false,
DoNotPublish: doNotPublish,
DataFields: map[string]ReleaseNotesDataField{},
PRBody: prBody,
}

if s != "" {
Expand Down Expand Up @@ -1151,6 +1157,14 @@ func (rn *ReleaseNote) ApplyMap(noteMap *ReleaseNotesMap, markdownLinks bool) er
}).Debugf("Applying map to note")
rn.IsMapped = true

if noteMap.PRBody != nil && rn.PRBody != "" && rn.PRBody != *noteMap.PRBody {
logrus.Warnf("Original PR body of release note mapping changed for PR: #%d", rn.PrNumber)

dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(rn.PRBody, *noteMap.PRBody, false)
logrus.Warnf("The diff between actual release note body and mapped one is:\n%s", dmp.DiffPrettyText(diffs))
}

reRenderMarkdown := false
if noteMap.ReleaseNote.Author != nil {
rn.Author = *noteMap.ReleaseNote.Author
Expand Down Expand Up @@ -1231,6 +1245,7 @@ func (rn *ReleaseNote) ToNoteMap() (string, error) {
noteMap.ReleaseNote.Feature = &rn.Feature
noteMap.ReleaseNote.ActionRequired = &rn.ActionRequired
noteMap.ReleaseNote.DoNotPublish = &rn.DoNotPublish
noteMap.PRBody = &rn.PRBody

yamlCode, err := yaml.Marshal(&noteMap)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/notes/notes_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ type ReleaseNotesMap struct {
} `json:"releasenote"`

DataFields map[string]ReleaseNotesDataField `json:"datafields,omitempty" yaml:"datafields,omitempty"`

// PRBody is the full original PR body.
PRBody *string `json:"pr_body,omitempty" yaml:"pr_body,omitempty"`
}

// ReleaseNotesDataField extra data added to a release note
Expand Down

0 comments on commit 0623915

Please sign in to comment.