Improved links cutter regexp.

This commit is contained in:
Stanislav Nikitin 2017-09-19 00:53:24 +05:00
parent c0b57eed14
commit 729fa71442
2 changed files with 43 additions and 68 deletions

View File

@ -34,50 +34,25 @@ func (gp GiteaParser) Initialize() {
c.Log.Infoln("Initializing Gitea parser...") c.Log.Infoln("Initializing Gitea parser...")
} }
func (gp GiteaParser) cutCommitLink(data string) [][]string { func (gp GiteaParser) cutLinks(data string) [][]string {
var links [][]string
c.Log.Debugln("Passed:", data) c.Log.Debugln("Passed:", data)
r := regexp.MustCompile("(http[?s]://[a-zA-Z1-9./-]+)|([a-zA-Z1-9]+)>") r := regexp.MustCompile("((https??://[a-zA-Z0-9.#!*/ _-]+)\\|([a-zA-Z0-9.#!*/ _-]+))")
found := r.FindAllStringSubmatch(data, -1) found := r.FindAllStringSubmatch(data, -1)
var result []string // [i][0] - link
// [i][1] - string for link
var result [][]string
for i := range found { for i := range found {
if i%2 == 0 { res := make([]string, 0, 2)
result = make([]string, 0, 2) res = append(res, found[i][2])
result = append(result, found[i][1]) res = append(res, found[i][3])
} else { result = append(result, res)
result = append(result, found[i][2])
links = append(links, result)
}
} }
c.Log.Debugln("Links cutted:", links) c.Log.Debugln("Links cutted:", result)
return links return result
}
func (gp GiteaParser) cutHeaderLinks(data string) [][]string {
var links [][]string
c.Log.Debugln("Passed:", data)
r := regexp.MustCompile("<(http[?s]://[a-zA-Z0-9./-]+)|([a-zA-Z0-9_-]+)>")
found := r.FindAllStringSubmatch(data, -1)
var result []string
for i := range found {
if i%2 == 0 {
result = make([]string, 0, 2)
result = append(result, found[i][1])
} else {
result = append(result, found[i][2])
links = append(links, result)
}
}
c.Log.Debugln("Links cutted:", links)
return links
} }
func (gp GiteaParser) parseCommitNew(message slackmessage.SlackMessage) map[string]string { func (gp GiteaParser) parseCommitNew(message slackmessage.SlackMessage) map[string]string {
@ -86,7 +61,7 @@ func (gp GiteaParser) parseCommitNew(message slackmessage.SlackMessage) map[stri
// Parse header. // Parse header.
// [0] is repo, [1] is branch. // [0] is repo, [1] is branch.
header_data := gp.cutHeaderLinks(message.Text) header_data := gp.cutLinks(message.Text)
data["repo"] = header_data[0][1] data["repo"] = header_data[0][1]
data["repo_url"] = header_data[0][0] data["repo_url"] = header_data[0][0]
data["branch"] = header_data[1][1] data["branch"] = header_data[1][1]
@ -100,7 +75,7 @@ func (gp GiteaParser) parseCommitNew(message slackmessage.SlackMessage) map[stri
data["repeatables"] = "commit,message" data["repeatables"] = "commit,message"
idx := 0 idx := 0
for i := range message.Attachments { for i := range message.Attachments {
attachment_link := gp.cutCommitLink(message.Attachments[i].Text) attachment_link := gp.cutLinks(message.Attachments[i].Text)
data["repeatable_item_commit" + strconv.Itoa(idx)] = attachment_link[0][1] data["repeatable_item_commit" + strconv.Itoa(idx)] = attachment_link[0][1]
data["repeatable_item_commit" + strconv.Itoa(idx) + "_url"] = attachment_link[0][0] data["repeatable_item_commit" + strconv.Itoa(idx) + "_url"] = attachment_link[0][0]
data["repeatable_item_message" + strconv.Itoa(idx)] = strings.Split(message.Attachments[i].Text, ">: ")[1] data["repeatable_item_message" + strconv.Itoa(idx)] = strings.Split(message.Attachments[i].Text, ">: ")[1]

View File

@ -30,6 +30,25 @@ import (
type GitlabParser struct {} type GitlabParser struct {}
func (gp GitlabParser) cutLinks(data string) [][]string {
r := regexp.MustCompile("((https??://[a-zA-Z0-9.#!*/ _-]+)\\|([a-zA-Z0-9.#!*/ _-]+))")
found := r.FindAllStringSubmatch(data, -1)
// [i][0] - link
// [i][1] - string for link
var result [][]string
for i := range found {
res := make([]string, 0, 2)
res = append(res, found[i][2])
res = append(res, found[i][3])
result = append(result, res)
}
c.Log.Debugln("Links cutted:", result)
return result
}
func (gp GitlabParser) Initialize() { func (gp GitlabParser) Initialize() {
c.Log.Infoln("Initializing Gitlab parser...") c.Log.Infoln("Initializing Gitlab parser...")
} }
@ -40,7 +59,7 @@ func (gp GitlabParser) parseCommit(message slackmessage.SlackMessage) map[string
data["user"] = strings.TrimSpace(strings.Split(message.Text, "pushed to")[0]) data["user"] = strings.TrimSpace(strings.Split(message.Text, "pushed to")[0])
// Parse links. // Parse links.
links_data := gp.parseIssueCommentLink(message.Text) links_data := gp.cutLinks(message.Text)
data["branch"] = links_data[0][1] data["branch"] = links_data[0][1]
data["branch_url"] = links_data[0][0] data["branch_url"] = links_data[0][0]
data["repo"] = links_data[1][1] data["repo"] = links_data[1][1]
@ -53,7 +72,7 @@ func (gp GitlabParser) parseCommit(message slackmessage.SlackMessage) map[string
data["repeatables"] = "commit,commit_text" data["repeatables"] = "commit,commit_text"
idx := 0 idx := 0
for i := range message.Attachments { for i := range message.Attachments {
commit_data := gp.parseIssueCommentLink(message.Attachments[i].Text) commit_data := gp.cutLinks(message.Attachments[i].Text)
data["repeatable_item_commit" + strconv.Itoa(idx)] = commit_data[0][1] data["repeatable_item_commit" + strconv.Itoa(idx)] = commit_data[0][1]
data["repeatable_item_commit" + strconv.Itoa(idx) + "_url"] = commit_data[0][0] data["repeatable_item_commit" + strconv.Itoa(idx) + "_url"] = commit_data[0][0]
data["repeatable_item_commit_text" + strconv.Itoa(idx)] = strings.Split(message.Attachments[i].Text, ">: ")[1] data["repeatable_item_commit_text" + strconv.Itoa(idx)] = strings.Split(message.Attachments[i].Text, ">: ")[1]
@ -94,7 +113,7 @@ func (gp GitlabParser) parseIssueClosed(text string) map[string]string {
// Parse links. // Parse links.
// Same as for parseIssueComment because this regexp returns // Same as for parseIssueComment because this regexp returns
// needed data. // needed data.
links_data := gp.parseIssueCommentLink(text) links_data := gp.cutLinks(text)
data["project"] = links_data[0][1] data["project"] = links_data[0][1]
data["project_url"] = links_data[0][0] data["project_url"] = links_data[0][0]
data["issue"] = links_data[1][1] data["issue"] = links_data[1][1]
@ -109,7 +128,7 @@ func (gp GitlabParser) parseIssueComment(message slackmessage.SlackMessage) map[
data["user"] = strings.TrimSpace(strings.Split(message.Text, " <")[0]) data["user"] = strings.TrimSpace(strings.Split(message.Text, " <")[0])
// Parse links in main message. // Parse links in main message.
links_data := gp.parseIssueCommentLink(message.Text) links_data := gp.cutLinks(message.Text)
data["commented_on_issue"] = links_data[0][1] data["commented_on_issue"] = links_data[0][1]
data["commented_on_issue_url"] = links_data[0][0] data["commented_on_issue_url"] = links_data[0][0]
data["project"] = links_data[1][1] data["project"] = links_data[1][1]
@ -129,30 +148,11 @@ func (gp GitlabParser) parseIssueComment(message slackmessage.SlackMessage) map[
return data return data
} }
func (gp GitlabParser) parseIssueCommentLink(data string) [][]string {
r := regexp.MustCompile("((htt[?p|ps]://[a-zA-Z0-9.#!*/ _-]+)\\|([a-zA-Z0-9.#!*/ _-]+))")
found := r.FindAllStringSubmatch(data, -1)
// [i][0] - link
// [i][1] - string for link
var result [][]string
for i := range found {
res := make([]string, 0, 2)
res = append(res, found[i][2])
res = append(res, found[i][3])
result = append(result, res)
}
c.Log.Debugln("Links cutted:", result)
return result
}
func (gp GitlabParser) parseIssueOpened(message slackmessage.SlackMessage) map[string]string { func (gp GitlabParser) parseIssueOpened(message slackmessage.SlackMessage) map[string]string {
data := make(map[string]string) data := make(map[string]string)
data["message"] = "[{project}] {user} opened an issue: {issue}{newline}{issue_text}" data["message"] = "[{project}] {user} opened an issue: {issue}{newline}{issue_text}"
links_data := gp.parseIssueCommentLink(message.Text) links_data := gp.cutLinks(message.Text)
data["project"] = links_data[0][1] data["project"] = links_data[0][1]
data["project_url"] = links_data[0][0] data["project_url"] = links_data[0][0]
data["user"] = strings.Split(message.Text, "Issue opened by ")[1] data["user"] = strings.Split(message.Text, "Issue opened by ")[1]
@ -179,7 +179,7 @@ func (gp GitlabParser) parseMergeRequestClosed(message slackmessage.SlackMessage
data["message"] = "[{project}] {user} closed merge request: {merge_request}" data["message"] = "[{project}] {user} closed merge request: {merge_request}"
data["user"] = strings.Split(message.Text, " closed <")[0] data["user"] = strings.Split(message.Text, " closed <")[0]
links_data := gp.parseIssueCommentLink(message.Text) links_data := gp.cutLinks(message.Text)
data["project"] = links_data[1][1] data["project"] = links_data[1][1]
data["project_url"] = links_data[1][0] data["project_url"] = links_data[1][0]
data["merge_request"] = links_data[0][1] data["merge_request"] = links_data[0][1]
@ -194,7 +194,7 @@ func (gp GitlabParser) parseMergeRequestComment(message slackmessage.SlackMessag
data["user"] = strings.TrimSpace(strings.Split(message.Text, " <")[0]) data["user"] = strings.TrimSpace(strings.Split(message.Text, " <")[0])
// Parse links in main message. // Parse links in main message.
links_data := gp.parseIssueCommentLink(message.Text) links_data := gp.cutLinks(message.Text)
data["commented_on_merge_request"] = links_data[0][1] data["commented_on_merge_request"] = links_data[0][1]
data["commented_on_merge_request_url"] = links_data[0][0] data["commented_on_merge_request_url"] = links_data[0][0]
data["project"] = links_data[1][1] data["project"] = links_data[1][1]
@ -219,7 +219,7 @@ func (gp GitlabParser) parseMergeRequestMerged(message slackmessage.SlackMessage
data["message"] = "[{project}] {user} merged {merge_request}" data["message"] = "[{project}] {user} merged {merge_request}"
data["user"] = strings.Split(message.Text, " merged <")[0] data["user"] = strings.Split(message.Text, " merged <")[0]
links_data := gp.parseIssueCommentLink(message.Text) links_data := gp.cutLinks(message.Text)
data["project"] = links_data[1][1] data["project"] = links_data[1][1]
data["project_url"] = links_data[1][0] data["project_url"] = links_data[1][0]
data["merge_request"] = links_data[0][1] data["merge_request"] = links_data[0][1]
@ -234,7 +234,7 @@ func (gp GitlabParser) parseMergeRequestOpened(message slackmessage.SlackMessage
data["message"] = "[{project}] {user} opened new merge request: {merge_request}" data["message"] = "[{project}] {user} opened new merge request: {merge_request}"
data["user"] = strings.Split(message.Text, " opened <")[0] data["user"] = strings.Split(message.Text, " opened <")[0]
links_data := gp.parseIssueCommentLink(message.Text) links_data := gp.cutLinks(message.Text)
data["project"] = links_data[1][1] data["project"] = links_data[1][1]
data["project_url"] = links_data[1][0] data["project_url"] = links_data[1][0]
data["merge_request"] = links_data[0][1] data["merge_request"] = links_data[0][1]
@ -275,7 +275,7 @@ func (gp GitlabParser) parsePushedNewBranch(message slackmessage.SlackMessage) m
data := make(map[string]string) data := make(map[string]string)
data["message"] = "[{project}] {user} pushed new branch: {branch}" data["message"] = "[{project}] {user} pushed new branch: {branch}"
links_data := gp.parseIssueCommentLink(message.Text) links_data := gp.cutLinks(message.Text)
data["branch"] = links_data[0][1] data["branch"] = links_data[0][1]
data["branch_url"] = links_data[0][0] data["branch_url"] = links_data[0][0]
data["project"] = links_data[1][1] data["project"] = links_data[1][1]
@ -291,7 +291,7 @@ func (gp GitlabParser) parseTagPush(message slackmessage.SlackMessage) map[strin
data["message"] = "[{project}] {user} pushed new tag: {tag}" data["message"] = "[{project}] {user} pushed new tag: {tag}"
data["user"] = strings.Split(message.Text, " pushed new tag")[0] data["user"] = strings.Split(message.Text, " pushed new tag")[0]
links_data := gp.parseIssueCommentLink(message.Text) links_data := gp.cutLinks(message.Text)
data["tag"] = links_data[0][1] data["tag"] = links_data[0][1]
data["tag_url"] = links_data[0][0] data["tag_url"] = links_data[0][0]
data["project"] = links_data[1][1] data["project"] = links_data[1][1]