forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from jstrachan/changes
fix: refactor the label comment code
- Loading branch information
Showing
5 changed files
with
107 additions
and
63 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
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
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,56 @@ | ||
package labels | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/jenkins-x/go-scm/scm" | ||
) | ||
|
||
const ( | ||
addLabel = "/jx-label " | ||
removeLabel = " remove" | ||
emptyString = "" | ||
) | ||
|
||
// ConvertLabelComments converts comments to labels for git providers which don't support native labels | ||
func ConvertLabelComments(cs []*scm.Comment) ([]*scm.Label, error) { | ||
sort.SliceStable(cs, func(i, j int) bool { | ||
return cs[i].Created.UnixNano() < cs[j].Created.UnixNano() | ||
}) | ||
m := make(map[string]bool) | ||
for _, com := range cs { | ||
if strings.HasPrefix(com.Body, addLabel) { | ||
t := strings.ReplaceAll(com.Body, addLabel, emptyString) | ||
if strings.HasSuffix(t, removeLabel) { | ||
t = strings.ReplaceAll(t, removeLabel, emptyString) | ||
m[t] = true | ||
} else { | ||
m[t] = false | ||
} | ||
} | ||
} | ||
var ls []*scm.Label | ||
for l, i := range m { | ||
if i == false { | ||
ls = append(ls, &scm.Label{Name: l}) | ||
} | ||
} | ||
return ls, nil | ||
} | ||
|
||
// CreateLabelAddComment creates a label comment for git providers which don't support labels with comment with /jx-label <name> | ||
func CreateLabelAddComment(label string) *scm.CommentInput { | ||
return &scm.CommentInput{ | ||
Body: fmt.Sprintf("%s%s", addLabel, label), | ||
} | ||
} | ||
|
||
// CreateLabelRemoveComment adds a comment with /jx-label <name> remove | ||
func CreateLabelRemoveComment(label string) *scm.CommentInput { | ||
input := &scm.CommentInput{ | ||
Body: fmt.Sprintf("%s%s%s", addLabel, label, removeLabel), | ||
} | ||
return input | ||
} |