Skip to content

Commit

Permalink
add issue checker
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Aug 28, 2020
1 parent 9adf546 commit 72f5749
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ assignees: ''

---

**Rod Version:** v0.0.0

The community is not charity. We welcome constructive questions, but not without limitation.

Check before you submit:
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/check-issues.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Check Issues
on:
issues:
types: [opened]

jobs:

run:

runs-on: ubuntu-latest

steps:

- uses: actions/setup-go@v2
with:
go-version: 1.14

- uses: actions/checkout@v2

- name: check
env:
GH_ROBOT_TOKEN: ${{ secrets.GH_ROBOT_TOKEN }}
run: go run ./lib/utils/check-issue
68 changes: 68 additions & 0 deletions lib/utils/check-issue/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// The .github/workflows/check-issues.yml will use it as an github action

package main

import (
"fmt"
"os"
"regexp"

"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/kit"
)

const ref = "\n_<sub>generated by [check-issue](https://github.com/go-rod/rod/tree/master/lib/utils/check-issue)</sub>_"

func main() {
data, err := kit.ReadFile(os.Getenv("GITHUB_EVENT_PATH"))
utils.E(err)

issue := kit.JSON(data).Get("issue")

labels := issue.Get("labels").Array()

for _, l := range labels {
name := l.Get("name").Str
if name != "question" && name != "bug" {
kit.Log("skip", name)
return
}
}

num := issue.Get("number").Int()
body := issue.Get("body").Str

kit.Log("check issue", num)

m := regexp.MustCompile(`\*\*Rod Version:\*\* v[0-9.]+`).FindString(body)
if m == "" || m == "**Rod Version:** v0.0.0" {
kit.Log("invalid issue format", body)

msg := fmt.Sprintf(
"Please add a valid `**Rod Version:** v0.0.0` to your issue. Current version is %s"+
ref,
currentVer(),
)

q := req(fmt.Sprintf("/repos/go-rod/rod/issues/%d/comments", num)).
Post().
JSONBody(map[string]string{"body": msg})

if q.MustResponse().StatusCode >= 400 {
panic(q.MustString())
}
}
}

func currentVer() string {
currentVer := req("/repos/go-rod/rod/releases").
Query("per_page", "1").
MustJSON().Get("0.tag_name").Str

kit.Log("current rod version", currentVer)
return currentVer
}

func req(u string) *kit.ReqContext {
return kit.Req("https://api.github.com"+u).Header("Authorization", "token "+os.Getenv("GH_ROBOT_TOKEN"))
}

0 comments on commit 72f5749

Please sign in to comment.