File: message_builder.go

package info (click to toggle)
hub 2.7.0~ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,176 kB
  • sloc: sh: 1,072; ruby: 825; makefile: 74
file content (60 lines) | stat: -rw-r--r-- 1,165 bytes parent folder | download | duplicates (3)
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
package github

import (
	"regexp"
	"strings"
)

type MessageBuilder struct {
	Title             string
	Filename          string
	Message           string
	Edit              bool
	commentedSections []string
	editor            *Editor
}

func (b *MessageBuilder) AddCommentedSection(section string) {
	b.commentedSections = append(b.commentedSections, section)
}

func (b *MessageBuilder) Extract() (title, body string, err error) {
	content := b.Message

	if b.Edit {
		b.editor, err = NewEditor(b.Filename, b.Title, content)
		if err != nil {
			return
		}
		for _, section := range b.commentedSections {
			b.editor.AddCommentedSection(section)
		}
		content, err = b.editor.EditContent()
		if err != nil {
			return
		}
	} else {
		nl := regexp.MustCompile(`\r?\n`)
		content = nl.ReplaceAllString(content, "\n")
	}

	parts := strings.SplitN(content, "\n\n", 2)
	if len(parts) >= 1 {
		title = strings.TrimSpace(strings.Replace(parts[0], "\n", " ", -1))
	}
	if len(parts) >= 2 {
		body = strings.TrimSpace(parts[1])
	}

	if title == "" {
		defer b.Cleanup()
	}

	return
}

func (b *MessageBuilder) Cleanup() {
	if b.editor != nil {
		b.editor.DeleteFile()
	}
}