parseLabelTriggerShorthand parses a string in the format "issue labeled label1 label2 ...", "pull_request labeled label1 label2 ...", "pull-request labeled label1 label2 ...", or "discussion labeled label1 label2 ..." and returns the entity type and label names. Returns an empty string for entityTyp
(input string)
| 17 | // When isLabelTrigger is true, entityType is always non-empty even if an error is returned. |
| 18 | // Returns an error if the format is invalid. |
| 19 | func parseLabelTriggerShorthand(input string) (entityType string, labelNames []string, isLabelTrigger bool, err error) { |
| 20 | input = strings.TrimSpace(input) |
| 21 | |
| 22 | // Split into tokens |
| 23 | tokens := strings.Fields(input) |
| 24 | if len(tokens) < 3 { |
| 25 | // Need at least: "issue/pull_request/discussion labeled label1" |
| 26 | return "", nil, false, nil |
| 27 | } |
| 28 | |
| 29 | // Check for different patterns: |
| 30 | // 1. "issue labeled label1 label2 ..." or "issue labeled label1, label2, ..." |
| 31 | // 2. "pull_request labeled label1 label2 ..." or "pull-request labeled label1, label2, ..." |
| 32 | // 3. "discussion labeled label1 label2 ..." or "discussion labeled label1, label2, ..." |
| 33 | |
| 34 | var startIdx int |
| 35 | |
| 36 | if tokens[0] == "issue" && tokens[1] == "labeled" { |
| 37 | // Pattern 1: "issue labeled label1 label2 ..." or "issue labeled label1, label2, ..." |
| 38 | entityType = "issues" |
| 39 | startIdx = 2 |
| 40 | } else if (tokens[0] == "pull_request" || tokens[0] == "pull-request") && tokens[1] == "labeled" { |
| 41 | // Pattern 2: "pull_request labeled label1 label2 ..." or "pull-request labeled label1, label2, ..." |
| 42 | entityType = "pull_request" |
| 43 | startIdx = 2 |
| 44 | } else if tokens[0] == "discussion" && tokens[1] == "labeled" { |
| 45 | // Pattern 3: "discussion labeled label1 label2 ..." or "discussion labeled label1, label2, ..." |
| 46 | entityType = "discussion" |
| 47 | startIdx = 2 |
| 48 | } else { |
| 49 | // Not a label trigger shorthand |
| 50 | labelTriggerParserLog.Printf("Input %q does not match any label trigger pattern (first token: %q)", input, tokens[0]) |
| 51 | return "", nil, false, nil |
| 52 | } |
| 53 | |
| 54 | // Extract label names |
| 55 | if len(tokens) <= startIdx { |
| 56 | return entityType, nil, true, errors.New("label trigger shorthand requires at least one label name") |
| 57 | } |
| 58 | |
| 59 | // Process label names: handle both space-separated and comma-separated formats |
| 60 | rawLabels := tokens[startIdx:] |
| 61 | for _, token := range rawLabels { |
| 62 | // Split on commas to handle "label1,label2,label3" format |
| 63 | parts := strings.SplitSeq(token, ",") |
| 64 | for part := range parts { |
| 65 | cleanLabel := strings.TrimSpace(part) |
| 66 | if cleanLabel != "" { |
| 67 | labelNames = append(labelNames, cleanLabel) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Validate we have at least one label after processing |
| 73 | if len(labelNames) == 0 { |
| 74 | return entityType, nil, true, errors.New("label trigger shorthand requires at least one label name") |
| 75 | } |
| 76 |