Skip to content

Commit

Permalink
Extract ACTIONS_REQUIRED blocks from releases and PRs. (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Jul 16, 2021
1 parent bc193e7 commit d28450c
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 261 deletions.
56 changes: 56 additions & 0 deletions pkg/markdown/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package markdown

import (
"fmt"
"strings"
)

var NoSuchBlockError = fmt.Errorf("no such block")

func isHeading(l string) bool {
return strings.HasPrefix(l, "#")
}

func headingLevel(l string) int {
level := 0
for _, char := range l {
if char != '#' {
break
}
level++
}
return level
}

func ExtractAnnotatedBlock(annotation string, s string) (string, error) {
parts := strings.SplitN(s, "```"+annotation, 2)
if len(parts) != 2 {
return "", NoSuchBlockError
}

parts = strings.SplitN(parts[1], "```", 2)
if len(parts) != 2 {
return "", NoSuchBlockError
}

return strings.TrimSpace(parts[0]), nil
}

func ToListItem(lines string) []string {
var result []string

for i, line := range SplitLines(lines) {
if i == 0 {
result = append(result, "* "+line)
continue
}

result = append(result, " "+line)
}

return result
}

func SplitLines(s string) []string {
return strings.Split(strings.Replace(s, `\r\n`, "\n", -1), "\n")
}
92 changes: 11 additions & 81 deletions pkg/utils/markdown.go → pkg/markdown/markdown.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,14 @@
package utils
package markdown

import (
"fmt"
"strings"
)

type Markdown struct {
sections []*MarkdownSection
}

func (m *Markdown) allSections() []*MarkdownSection {
var result []*MarkdownSection

for _, s := range m.sections {
result = append(result, s.allSections()...)
}

return result
}

func (m *MarkdownSection) allSections() []*MarkdownSection {
var result []*MarkdownSection

result = append(result, m)

for _, s := range m.SubSections {
result = append(result, s.allSections()...)
}

return result
}

type MarkdownSection struct {
Level int
Heading string
ContentLines []string
SubSections []*MarkdownSection
}

func ParseMarkdown(content string) *Markdown {
func Parse(content string) *Markdown {
m := &Markdown{}
lines := strings.Split(content, "\n")

Expand Down Expand Up @@ -82,19 +52,14 @@ func ParseMarkdown(content string) *Markdown {
return m
}

func isHeading(l string) bool {
return strings.HasPrefix(l, "#")
}
func (m *Markdown) allSections() []*MarkdownSection {
var result []*MarkdownSection

func headingLevel(l string) int {
level := 0
for _, char := range l {
if char != '#' {
break
}
level++
for _, s := range m.sections {
result = append(result, s.allSections()...)
}
return level

return result
}

func (m *Markdown) AppendSection(s *MarkdownSection) {
Expand All @@ -105,22 +70,6 @@ func (m *Markdown) PrependSection(s *MarkdownSection) {
m.sections = append([]*MarkdownSection{s}, m.sections...)
}

func (m *MarkdownSection) AppendContent(contentLines []string) {
m.ContentLines = append(m.ContentLines, contentLines...)
}

func (m *MarkdownSection) PrependContent(contentLines []string) {
m.ContentLines = append(contentLines, m.ContentLines...)
}

func (m *MarkdownSection) AppendChild(child *MarkdownSection) {
m.SubSections = append(m.SubSections, child)
}

func (m *MarkdownSection) PrependChild(child *MarkdownSection) {
m.SubSections = append([]*MarkdownSection{child}, m.SubSections...)
}

func (m *Markdown) FindSectionByHeading(level int, headline string) *MarkdownSection {
for _, s := range m.allSections() {
if s.Level == level {
Expand Down Expand Up @@ -148,28 +97,9 @@ func (m *Markdown) FindSectionByHeadingPrefix(level int, headlinePrefix string)
func (m *Markdown) String() string {
var result string
for _, s := range m.sections {
result += s.String()
}
return strings.TrimSpace(result)
}

func (m *MarkdownSection) String() string {
var result string

if m.Level > 0 {
for i := 0; i < m.Level; i++ {
result += "#"
}
result += " " + m.Heading + "\n"
result += "\n" + s.String()
result = strings.Trim(result, "\n")
}

for _, l := range m.ContentLines {
result += fmt.Sprintf("%s\n", l)
}

for _, sub := range m.SubSections {
result += sub.String()
}

return result
return strings.TrimSpace(result)
}
4 changes: 2 additions & 2 deletions pkg/utils/markdown_test.go → pkg/markdown/markdown_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package markdown

import (
"testing"
Expand Down Expand Up @@ -173,7 +173,7 @@ content 1b`,
for _, tt := range tests {
// regex := regexp.MustCompile("\n\n")
t.Run(tt.name, func(t *testing.T) {
m := ParseMarkdown(tt.content)
m := Parse(tt.content)
if diff := cmp.Diff(m.sections, tt.want); diff != "" {
t.Errorf("parseMarkdown(), differs in sections: %v", diff)
}
Expand Down
76 changes: 76 additions & 0 deletions pkg/markdown/section.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package markdown

import (
"fmt"
"strings"
)

type MarkdownSection struct {
Level int
Heading string
ContentLines []string
SubSections []*MarkdownSection
}

func (m *MarkdownSection) allSections() []*MarkdownSection {
var result []*MarkdownSection

result = append(result, m)

for _, s := range m.SubSections {
result = append(result, s.allSections()...)
}

return result
}

func (m *MarkdownSection) FindSectionByHeading(level int, headline string) *MarkdownSection {
for _, s := range m.allSections() {
if s.Level == level {
if headline == s.Heading {
return s
}
}
}

return nil
}

func (m *MarkdownSection) AppendContent(contentLines []string) {
m.ContentLines = append(m.ContentLines, contentLines...)
}

func (m *MarkdownSection) PrependContent(contentLines []string) {
m.ContentLines = append(contentLines, m.ContentLines...)
}

func (m *MarkdownSection) AppendChild(child *MarkdownSection) {
m.SubSections = append(m.SubSections, child)
}

func (m *MarkdownSection) PrependChild(child *MarkdownSection) {
m.SubSections = append([]*MarkdownSection{child}, m.SubSections...)
}

func (m *MarkdownSection) String() string {
var result string

if m.Level > 0 {
for i := 0; i < m.Level; i++ {
result += "#"
}
result += " " + m.Heading + "\n"
}

for _, l := range m.ContentLines {
result += fmt.Sprintf("%s\n", l)
}

result = strings.Trim(result, "\n")

for _, sub := range m.SubSections {
result += "\n" + sub.String()
}

return result
}
3 changes: 2 additions & 1 deletion pkg/webhooks/github/actions/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ func (w *WebhookActions) ProcessPullRequestEvent(payload *ghwebhooks.PullRequest
}

params := &releaseDrafterParams{
RepositoryName: payload.Repository.Name,
RepositoryName: payload.Repository.Name,
ComponentReleaseInfo: &payload.PullRequest.Body,
}
err := a.appendMergedPR(ctx, payload.PullRequest.Title, payload.PullRequest.Number, payload.PullRequest.User.Login, params)
if err != nil {
Expand Down
Loading

0 comments on commit d28450c

Please sign in to comment.