1
0
mirror of https://github.com/ru-de/faq.git synced 2024-11-24 03:12:19 +00:00
faq-de/files/go/check_links/main.go

92 lines
2.2 KiB
Go
Raw Normal View History

2018-05-03 22:28:41 +00:00
package main
import (
"fmt"
"os"
"flag"
"bufio"
"regexp"
2018-05-05 14:01:50 +00:00
"github.com/ewgRa/ci-utils/src/diff_liner"
"github.com/ewgRa/ci-utils/src/links_checker"
2018-05-03 22:28:41 +00:00
"gopkg.in/russross/blackfriday.v2"
"encoding/json"
"github.com/google/go-github/github"
)
func main() {
prLiner := flag.String("pr-liner", "", "Pull request liner")
fileName := flag.String("file", "", "Hunspell parsed file name")
2018-05-04 22:36:18 +00:00
commit := flag.String("commit", "", "Commit")
2018-05-03 22:28:41 +00:00
expectedCodesFile := flag.String("expected-codes", "", "Expected codes file name")
flag.Parse()
2018-05-04 22:36:18 +00:00
if *prLiner == "" || *fileName == "" || *commit == "" || *expectedCodesFile == "" {
2018-05-03 22:28:41 +00:00
flag.Usage()
os.Exit(1)
}
linkRegexp := regexp.MustCompile("href=\"(http[^\"]*)\"")
linerResp := diff_liner.ReadLinerResponse(*prLiner)
checker := links_checker.NewChecker(*expectedCodesFile)
line := 0
file, err := os.Open(*fileName)
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
2018-05-05 14:01:50 +00:00
var comments []*github.PullRequestComment
2018-05-03 22:28:41 +00:00
for scanner.Scan() {
line++
prLine := linerResp.GetDiffLine(*fileName, line)
if prLine == 0 {
continue
}
output := blackfriday.Run(scanner.Bytes())
matches := linkRegexp.FindAllStringSubmatch(string(output), -1)
for _, match := range matches {
link := match[1]
ok, respCode, expectedCodes := checker.Check(link)
if ok {
continue
}
2018-05-05 14:01:50 +00:00
body := fmt.Sprintf("Ссылка **%s** ... недоступна с кодом **%v**, ожидается **%v**.\nЕсли это ожидаемый ответ, внесите \"%v,%s\" в files/expected_codes.csv", link, respCode, expectedCodes, respCode, link)
2018-05-03 22:28:41 +00:00
2018-05-05 14:01:50 +00:00
comments = append(comments, &github.PullRequestComment{
2018-05-03 22:28:41 +00:00
Body: &body,
2018-05-04 22:36:18 +00:00
CommitID: commit,
2018-05-03 22:28:41 +00:00
Path: fileName,
Position: &prLine,
2018-05-05 14:01:50 +00:00
})
2018-05-03 22:28:41 +00:00
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
2018-05-05 14:01:50 +00:00
jsonData, err := json.Marshal(comments)
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
2018-05-03 22:28:41 +00:00
}