|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/google/go-github/v55/github" |
| 11 | + "golang.org/x/oauth2" |
| 12 | + yaml "gopkg.in/yaml.v3" |
| 13 | +) |
| 14 | + |
| 15 | +// LabelConfig represents the structure of labels.yaml |
| 16 | +// Only the fields needed for logic are included here |
| 17 | + |
| 18 | +type Label struct { |
| 19 | + Name string `yaml:"name"` |
| 20 | + Color string `yaml:"color"` |
| 21 | + Description string `yaml:"description"` |
| 22 | + Previously []struct { |
| 23 | + Name string `yaml:"name"` |
| 24 | + } `yaml:"previously"` |
| 25 | +} |
| 26 | + |
| 27 | +type Action struct { |
| 28 | + Kind string `yaml:"kind"` |
| 29 | + Label string `yaml:"label,omitempty"` |
| 30 | + Spec map[string]interface{} `yaml:"spec,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +type RuleSpec struct { |
| 34 | + Command string `yaml:"command,omitempty"` |
| 35 | + Rules []interface{} `yaml:"rules,omitempty"` |
| 36 | + Match string `yaml:"match,omitempty"` |
| 37 | + MatchCondition string `yaml:"matchCondition,omitempty"` |
| 38 | + MatchPath string `yaml:"matchPath,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +type Rule struct { |
| 42 | + Name string `yaml:"name"` |
| 43 | + Kind string `yaml:"kind"` |
| 44 | + Spec RuleSpec `yaml:"spec"` |
| 45 | + Actions []Action `yaml:"actions"` |
| 46 | +} |
| 47 | + |
| 48 | +type LabelsYAML struct { |
| 49 | + Labels []Label `yaml:"labels"` |
| 50 | + Ruleset []Rule `yaml:"ruleset"` |
| 51 | +} |
| 52 | + |
| 53 | +func loadConfig(path string) (*LabelsYAML, error) { |
| 54 | + f, err := os.Open(path) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + defer f.Close() |
| 59 | + var cfg LabelsYAML |
| 60 | + dec := yaml.NewDecoder(f) |
| 61 | + if err := dec.Decode(&cfg); err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + return &cfg, nil |
| 65 | +} |
| 66 | + |
| 67 | +func githubClient() *github.Client { |
| 68 | + token := os.Getenv("GITHUB_TOKEN") |
| 69 | + if token == "" { |
| 70 | + log.Fatal("GITHUB_TOKEN environment variable not set") |
| 71 | + } |
| 72 | + ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) |
| 73 | + return github.NewClient(oauth2.NewClient(context.Background(), ts)) |
| 74 | +} |
| 75 | + |
| 76 | +func main() { |
| 77 | + if len(os.Args) < 5 { |
| 78 | + fmt.Println("Usage: labeler <owner> <repo> <issue_number> <comment_body>") |
| 79 | + os.Exit(1) |
| 80 | + } |
| 81 | + owner := os.Args[1] |
| 82 | + repo := os.Args[2] |
| 83 | + issueNum := os.Args[3] |
| 84 | + commentBody := os.Args[4] |
| 85 | + |
| 86 | + cfg, err := loadConfig("labels.yaml") |
| 87 | + if err != nil { |
| 88 | + log.Fatalf("failed to load labels.yaml: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + client := githubClient() |
| 92 | + ctx := context.Background() |
| 93 | + |
| 94 | + issue, _, err := client.Issues.Get(ctx, owner, repo, toInt(issueNum)) |
| 95 | + if err != nil { |
| 96 | + log.Fatalf("failed to fetch issue: %v", err) |
| 97 | + } |
| 98 | + fmt.Printf("Issue #%d: %s\n", *issue.Number, *issue.Title) |
| 99 | + |
| 100 | + // Scan for all supported commands anywhere in the comment body |
| 101 | + lines := strings.Split(commentBody, "\n") |
| 102 | + for _, rule := range cfg.Ruleset { |
| 103 | + if rule.Spec.Command != "" { |
| 104 | + for _, line := range lines { |
| 105 | + line = strings.TrimSpace(line) |
| 106 | + if strings.HasPrefix(line, rule.Spec.Command) { |
| 107 | + // Split command and args |
| 108 | + parts := strings.Fields(line) |
| 109 | + argv := []string{} |
| 110 | + if len(parts) > 1 { |
| 111 | + argv = parts[1:] |
| 112 | + } |
| 113 | + for _, action := range rule.Actions { |
| 114 | + switch action.Kind { |
| 115 | + case "apply-label": |
| 116 | + label := renderLabel(action.Label, argv) |
| 117 | + applyLabel(ctx, client, owner, repo, toInt(issueNum), label) |
| 118 | + case "remove-label": |
| 119 | + match, _ := action.Spec["match"].(string) |
| 120 | + if match != "" { |
| 121 | + removeLabel(ctx, client, owner, repo, toInt(issueNum), match) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// renderLabel replaces {{ argv.0 }} etc. in label templates |
| 132 | +func renderLabel(template string, argv []string) string { |
| 133 | + label := template |
| 134 | + for i, v := range argv { |
| 135 | + label = replaceAll(label, fmt.Sprintf("{{ argv.%d }}", i), v) |
| 136 | + } |
| 137 | + return label |
| 138 | +} |
| 139 | + |
| 140 | +func replaceAll(s, old, new string) string { |
| 141 | + for { |
| 142 | + idx := indexOf(s, old) |
| 143 | + if idx == -1 { |
| 144 | + break |
| 145 | + } |
| 146 | + s = s[:idx] + new + s[idx+len(old):] |
| 147 | + } |
| 148 | + return s |
| 149 | +} |
| 150 | + |
| 151 | +func indexOf(s, substr string) int { |
| 152 | + return strings.Index(s, substr) |
| 153 | +} |
| 154 | + |
| 155 | +func applyLabel(ctx context.Context, client *github.Client, owner, repo string, issueNum int, label string) { |
| 156 | + fmt.Printf("Applying label: %s\n", label) |
| 157 | + _, _, err := client.Issues.AddLabelsToIssue(ctx, owner, repo, issueNum, []string{label}) |
| 158 | + if err != nil { |
| 159 | + log.Printf("failed to apply label %s: %v", label, err) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func removeLabel(ctx context.Context, client *github.Client, owner, repo string, issueNum int, label string) { |
| 164 | + fmt.Printf("Removing label: %s\n", label) |
| 165 | + _, err := client.Issues.RemoveLabelForIssue(ctx, owner, repo, issueNum, label) |
| 166 | + if err != nil { |
| 167 | + log.Printf("failed to remove label %s: %v", label, err) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func toInt(s string) int { |
| 172 | + n, err := fmt.Sscanf(s, "%d", new(int)) |
| 173 | + if err != nil || n != 1 { |
| 174 | + log.Fatalf("invalid issue number: %s", s) |
| 175 | + } |
| 176 | + var i int |
| 177 | + fmt.Sscanf(s, "%d", &i) |
| 178 | + return i |
| 179 | +} |
0 commit comments