drone/types/enum/pullreq.go

128 lines
3.4 KiB
Go

// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package enum
import (
"sort"
"strings"
)
// PullReqState defines pull request state.
type PullReqState string
// PullReqState enumeration.
const (
PullReqStateOpen PullReqState = "open"
PullReqStateMerged PullReqState = "merged"
PullReqStateClosed PullReqState = "closed"
PullReqStateRejected PullReqState = "rejected"
)
// PullReqSort defines pull request attribute that can be used for sorting.
type PullReqSort int
// PullReqAttr enumeration.
const (
PullReqSortNone PullReqSort = iota
PullReqSortNumber
PullReqSortCreated
PullReqSortUpdated
)
// ParsePullReqSort parses the pull request attribute string
// and returns the equivalent enumeration.
func ParsePullReqSort(s string) PullReqSort {
switch strings.ToLower(s) {
case number:
return PullReqSortNumber
case created, createdAt:
return PullReqSortCreated
case updated, updatedAt:
return PullReqSortUpdated
default:
return PullReqSortNone
}
}
// String returns the string representation of the attribute.
func (a PullReqSort) String() string {
switch a {
case PullReqSortNumber:
return number
case PullReqSortCreated:
return created
case PullReqSortUpdated:
return updated
case PullReqSortNone:
return ""
default:
return undefined
}
}
// PullReqActivityType defines pull request activity message type.
// Essentially, the Type determines the structure of the pull request activity's Payload structure.
type PullReqActivityType string
// PullReqActivityType enumeration.
const (
PullReqActivityTypeComment PullReqActivityType = "comment"
PullReqActivityTypeCodeComment PullReqActivityType = "code-comment"
PullReqActivityTypeTitleChange PullReqActivityType = "title-change"
)
var pullReqActivityTypes = []string{
string(PullReqActivityTypeComment),
string(PullReqActivityTypeCodeComment),
string(PullReqActivityTypeTitleChange),
}
func init() {
sort.Strings(pullReqActivityTypes)
}
// ParsePullReqActivityType parses the pull request activity type.
func ParsePullReqActivityType(s string) (PullReqActivityType, bool) {
if existsInSortedSlice(pullReqActivityTypes, s) {
return PullReqActivityType(s), true
}
return "", false
}
// PullReqActivityKind defines kind of pull request activity system message.
// Kind defines the source of the pull request activity entry:
// Whether it's generated by the system, it's a user comment or a part of code review.
type PullReqActivityKind string
// PullReqActivityKind enumeration.
const (
PullReqActivityKindSystem PullReqActivityKind = "system"
PullReqActivityKindComment PullReqActivityKind = "comment"
PullReqActivityKindCodeComment PullReqActivityKind = "code"
)
var pullReqActivityKinds = []string{
string(PullReqActivityKindSystem),
string(PullReqActivityKindComment),
string(PullReqActivityTypeCodeComment),
}
func init() {
sort.Strings(pullReqActivityKinds)
}
// ParsePullReqActivityKind parses the pull request activity type.
func ParsePullReqActivityKind(s string) (PullReqActivityKind, bool) {
if existsInSortedSlice(pullReqActivityKinds, s) {
return PullReqActivityKind(s), true
}
return "", false
}
func existsInSortedSlice(strs []string, s string) bool {
idx := sort.SearchStrings(strs, s)
return idx >= 0 && idx < len(strs) && strs[idx] == s
}