Created by barquenuagique 131 days ago Viewed 0 times 1 Point
To use this script
No comments yet
package inner
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
wmill "github.com/windmill-labs/windmill-go-client"
)
type payload struct {
Repo string // a repo full name: owner/repo
LabelWith []string
IssueNumber int
}
// Issues will be mapped to team(s) or label(s) based on keywords
var keyWordToLabel = map[string]string{
"backend": "team/backend",
"worker": "team/backend",
"logs": "team/backend",
"frontend": "team/frontend",
"client": "team/frontend",
"editor": "team/frontend",
"UI": "team/frontend",
"doc": "team/product",
"improvement": "feature-request",
"request": "feature-request",
"add support": "feature-request",
"bug": "bug",
}
func getRequest(gh_token string, url string, target interface{}) error {
bearer := "token " + gh_token
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("Authorization", bearer)
res, err := http.DefaultClient.Do(req)
if err != nil {
return errors.New(fmt.Sprintf("request failed %s", err))
}
if !(res.StatusCode >= 200 && res.StatusCode < 300) {
return errors.New(fmt.Sprintf("request failed %s at %s", res.Status, url))
}
defer res.Body.Close()
json.NewDecoder(res.Body).Decode(&target)
return nil
}
func getIssue(repo_fullname string, issue_number int) (description string, title string, e error) {
gh_token, err := wmill.GetVariable("u/mm/gh_token")
URL := "https://api.github.com/repos/" + repo_fullname + "/issues/" + strconv.Itoa(issue_number)
fmt.Println(URL)
if err != nil {
return "", "", err
}
issue := new(struct{
Body string `json:"body"`
Title string `json:"title"`
})
err = getRequest(gh_token, URL, issue)
if err != nil{
return "", "", errors.New(fmt.Sprintf("request failed %s at %s", err))
}
return issue.Body, issue.Title, nil
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func main(
issue struct {
Number int `json:"number"`
},
action string,
sender interface{},
repository struct {
Full_name string `json:"full_name"`
}) (interface{}, error) {
labels := []string{}
if action == "opened" {
description, title, err := getIssue(repository.Full_name, issue.Number)
if err!= nil{
return nil, err
}
for keyword, label := range keyWordToLabel{
if (strings.Contains(title, keyword) || strings.Contains(description, keyword)) && !(contains(labels, label)) {
labels = append(labels, label)
}
}
return payload{Repo: repository.Full_name, LabelWith: labels, IssueNumber: issue.Number}, nil
} else {
return "", nil
}
}
No comments yet