forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split Bitbucket Server comments if over max length
- Refactor the comment split method into a common package
- Loading branch information
Showing
5 changed files
with
132 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package common | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
// SplitComment splits comment into a slice of comments that are under maxSize. | ||
// It appends sepEnd to all comments that have a following comment. | ||
// It prepends sepStart to all comments that have a preceding comment. | ||
func SplitComment(comment string, maxSize int, sepEnd string, sepStart string) []string { | ||
if len(comment) <= maxSize { | ||
return []string{comment} | ||
} | ||
|
||
maxWithSep := maxSize - len(sepEnd) - len(sepStart) | ||
var comments []string | ||
numComments := int(math.Ceil(float64(len(comment)) / float64(maxWithSep))) | ||
for i := 0; i < numComments; i++ { | ||
upTo := min(len(comment), (i+1)*maxWithSep) | ||
portion := comment[i*maxWithSep : upTo] | ||
if i < numComments-1 { | ||
portion += sepEnd | ||
} | ||
if i > 0 { | ||
portion = sepStart + portion | ||
} | ||
comments = append(comments, portion) | ||
} | ||
return comments | ||
} | ||
|
||
func min(a, b int) int { | ||
if a < b { | ||
return a | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2017 HootSuite Media Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the License); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an AS IS BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// Modified hereafter by contributors to runatlantis/atlantis. | ||
|
||
package common_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/runatlantis/atlantis/server/events/vcs/common" | ||
|
||
. "github.com/runatlantis/atlantis/testing" | ||
) | ||
|
||
// If under the maximum number of chars, we shouldn't split the comments. | ||
func TestSplitComment_UnderMax(t *testing.T) { | ||
comment := "comment under max size" | ||
split := common.SplitComment(comment, len(comment)+1, "sepEnd", "sepStart") | ||
Equals(t, []string{comment}, split) | ||
} | ||
|
||
// If the comment needs to be split into 2 we should do the split and add the | ||
// separators properly. | ||
func TestSplitComment_TwoComments(t *testing.T) { | ||
comment := strings.Repeat("a", 1000) | ||
sepEnd := "-sepEnd" | ||
sepStart := "-sepStart" | ||
split := common.SplitComment(comment, len(comment)-1, sepEnd, sepStart) | ||
|
||
expCommentLen := len(comment) - len(sepEnd) - len(sepStart) - 1 | ||
expFirstComment := comment[:expCommentLen] | ||
expSecondComment := comment[expCommentLen:] | ||
Equals(t, 2, len(split)) | ||
Equals(t, expFirstComment+sepEnd, split[0]) | ||
Equals(t, sepStart+expSecondComment, split[1]) | ||
} | ||
|
||
// If the comment needs to be split into 4 we should do the split and add the | ||
// separators properly. | ||
func TestSplitComment_FourComments(t *testing.T) { | ||
comment := strings.Repeat("a", 1000) | ||
sepEnd := "-sepEnd" | ||
sepStart := "-sepStart" | ||
max := (len(comment) / 4) + len(sepEnd) + len(sepStart) | ||
split := common.SplitComment(comment, max, sepEnd, sepStart) | ||
|
||
expMax := len(comment) / 4 | ||
Equals(t, []string{ | ||
comment[:expMax] + sepEnd, | ||
sepStart + comment[expMax:expMax*2] + sepEnd, | ||
sepStart + comment[expMax*2:expMax*3] + sepEnd, | ||
sepStart + comment[expMax*3:]}, split) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters