Skip to content

Commit b5b3e07

Browse files
brechtvllunny
andauthored
Pull request yaml template support for including commit body in a field (#22629)
When using a markdown template the commit body is prepended to text in the description. This adds the same functionality for yaml templates, adding the commit message in a field with id "body". Co-authored-by: Lunny Xiao <[email protected]>
1 parent 1b53a9e commit b5b3e07

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

routers/web/repo/compare.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"code.gitea.io/gitea/modules/log"
3131
"code.gitea.io/gitea/modules/markup"
3232
"code.gitea.io/gitea/modules/setting"
33+
api "code.gitea.io/gitea/modules/structs"
3334
"code.gitea.io/gitea/modules/upload"
3435
"code.gitea.io/gitea/modules/util"
3536
"code.gitea.io/gitea/services/gitdiff"
@@ -789,16 +790,29 @@ func CompareDiff(ctx *context.Context) {
789790
ctx.Flash.Warning(renderErrorOfTemplates(ctx, templateErrs), true)
790791
}
791792

792-
// If a template content is set, prepend the "content". In this case that's only
793-
// applicable if you have one commit to compare and that commit has a message.
794-
// In that case the commit message will be prepend to the template body.
795-
if templateContent, ok := ctx.Data[pullRequestTemplateKey].(string); ok && templateContent != "" {
796-
if content, ok := ctx.Data["content"].(string); ok && content != "" {
793+
if content, ok := ctx.Data["content"].(string); ok && content != "" {
794+
// If a template content is set, prepend the "content". In this case that's only
795+
// applicable if you have one commit to compare and that commit has a message.
796+
// In that case the commit message will be prepend to the template body.
797+
if templateContent, ok := ctx.Data[pullRequestTemplateKey].(string); ok && templateContent != "" {
797798
// Re-use the same key as that's priortized over the "content" key.
798799
// Add two new lines between the content to ensure there's always at least
799800
// one empty line between them.
800801
ctx.Data[pullRequestTemplateKey] = content + "\n\n" + templateContent
801802
}
803+
804+
// When using form fields, also add content to field with id "body".
805+
if fields, ok := ctx.Data["Fields"].([]*api.IssueFormField); ok {
806+
for _, field := range fields {
807+
if field.ID == "body" {
808+
if fieldValue, ok := field.Attributes["value"].(string); ok && fieldValue != "" {
809+
field.Attributes["value"] = content + "\n\n" + fieldValue
810+
} else {
811+
field.Attributes["value"] = content
812+
}
813+
}
814+
}
815+
}
802816
}
803817

804818
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled

0 commit comments

Comments
 (0)