go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/bugs/commentary.go (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bugs
    16  
    17  import (
    18  	"strings"
    19  )
    20  
    21  // Commentary represents part of a bug comment.
    22  type Commentary struct {
    23  	// The comment bodies. This should be the most important information to surface
    24  	// to the user, and appears first. Do not include leading or trailing new line
    25  	// character.
    26  	Bodies []string
    27  	// Text to appear in the footer of the comment, such as links to more information.
    28  	// This information appears last. Do not include leading or trailing new line
    29  	// character.
    30  	Footers []string
    31  }
    32  
    33  // ToComment prepares a comment from commentary.
    34  func (c Commentary) ToComment() string {
    35  	// Footer content is packed together tightly, without blank lines.
    36  	footer := strings.Join(c.Footers, "\n")
    37  
    38  	var bodies []string
    39  	bodies = append(bodies, c.Bodies...)
    40  	if footer != "" {
    41  		bodies = append(bodies, footer)
    42  	}
    43  
    44  	// Bodies (and the final footer) are separated by a blank line.
    45  	return strings.Join(bodies, "\n\n")
    46  }
    47  
    48  // MergeCommentary merges one or more commentary items into a bug comment.
    49  // All commentary bodies appear first, followed by all footers.
    50  func MergeCommentary(cs ...Commentary) Commentary {
    51  	var bodies []string
    52  	var footers []string
    53  	for _, c := range cs {
    54  		bodies = append(bodies, c.Bodies...)
    55  		footers = append(footers, c.Footers...)
    56  	}
    57  	return Commentary{
    58  		Bodies:  bodies,
    59  		Footers: footers,
    60  	}
    61  }