Split Author and Message from Commit

The drone-slack plugin has structs for Author and Message. Since this is the most
popular notification plugin it makes sense to follow along with it.
This commit is contained in:
Don 2020-09-10 11:01:00 -07:00
parent a8409c1215
commit c5ec06ee53
2 changed files with 75 additions and 36 deletions

View File

@ -5,8 +5,9 @@
package drone
type (
// Commit represents the current commit being built.
type Commit struct {
Commit struct {
// SHA for the current commit.
SHA string
@ -27,10 +28,10 @@ type Commit struct {
Link string
// Message for the current commit.
Message string
Message Message
// Author of the commit.
Author string
Author Author
// AuthorName of the commit.
AuthorName string
@ -41,3 +42,32 @@ type Commit struct {
// AuthorAvatar of the commit.
AuthorAvatar string
}
// Author of a Commit.
Author struct {
// Username of the Commit author.
Username string
// Name of the Commit author.
Name string
// Email for the Commit author.
Email string
// Avatar for the Commit author.
Avatar string
}
// Message for a Commit.
Message struct {
// Title for the Commit.
Title string
// Body of the Commit message.
Body string
}
)
func (a Author) String() string {
return a.Username
}
func (m Message) String() string {
return m.Title + m.Body
}

View File

@ -6,6 +6,8 @@
package urfave
import (
"strings"
"github.com/drone-plugins/drone-plugin-lib/drone"
"github.com/urfave/cli/v2"
)
@ -95,6 +97,8 @@ func commitFlags() []cli.Flag {
// commitFromContext creates a drone.Commit from the cli.Context.
func commitFromContext(ctx *cli.Context) drone.Commit {
splitMsg := strings.Split(ctx.String("commit.message"), "\n")
return drone.Commit{
SHA: ctx.String("commit.sha"),
Before: ctx.String("commit.before"),
@ -102,10 +106,15 @@ func commitFromContext(ctx *cli.Context) drone.Commit {
Ref: ctx.String("commit.ref"),
Branch: ctx.String("commit.branch"),
Link: ctx.String("commit.link"),
Message: ctx.String("commit.message"),
Author: ctx.String("commit.author"),
AuthorName: ctx.String("commit.author-name"),
AuthorEmail: ctx.String("commit.author-email"),
AuthorAvatar: ctx.String("commit.author-avatar"),
Message: drone.Message{
Title: strings.TrimSpace(splitMsg[0]),
Body: strings.TrimSpace(strings.Join(splitMsg[1:], "\n")),
},
Author: drone.Author{
Username: ctx.String("commit.author"),
Name: ctx.String("commit.author-name"),
Email: ctx.String("commit.author-email"),
Avatar: ctx.String("commit.author-avatar"),
},
}
}