-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(github): extract relation between pr and issue
Closes #1257
- Loading branch information
Showing
11 changed files
with
218 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package code | ||
|
||
type PullRequestIssue struct { | ||
PullRequestId string `json:"id" gorm:"primaryKey;type:varchar(255);comment:This key is generated based on details from the original plugin"` // format: <Plugin>:<Entity>:<PK0>:<PK1> | ||
IssueId string `gorm:"primaryKey"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package models | ||
|
||
type GithubPullRequestIssue struct { | ||
PullRequestId int `gorm:"primaryKey"` | ||
IssueId int `gorm:"primaryKey"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
plugins/github/tasks/github_pull_request_issue_enricher.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package tasks | ||
|
||
import ( | ||
"context" | ||
"github.com/merico-dev/lake/errors" | ||
"gorm.io/gorm/clause" | ||
"strings" | ||
|
||
re2 "github.com/dlclark/regexp2" | ||
"github.com/merico-dev/lake/config" | ||
lakeModels "github.com/merico-dev/lake/models" | ||
githubModels "github.com/merico-dev/lake/plugins/github/models" | ||
) | ||
|
||
var prBodyCloseRegex *re2.Regexp | ||
|
||
func init() { | ||
var prBodyClose = config.V.GetString("GITHUB_PR_BODY_CLOSE") | ||
if len(prBodyClose) > 0 { | ||
prBodyCloseRegex = re2.MustCompile(prBodyClose, 0) | ||
} else { | ||
prBodyCloseRegex = re2.MustCompile(`(?mi)(fix|close|resolve|fixes|closes|resolves|fixed|closed|resolved)[\s]*(pingcap\/tidb)?(issue)?( )?((#\d+[ ]?)+)`, 0) | ||
|
||
} | ||
} | ||
|
||
func EnrichGithubPullRequestIssue(repoId int, ctx context.Context) (err error) { | ||
githubPullRequst := &githubModels.GithubPullRequest{} | ||
cursor, err := lakeModels.Db.Model(&githubPullRequst). | ||
Where("repo_id = ?", repoId). | ||
Rows() | ||
if err != nil { | ||
return err | ||
} | ||
defer cursor.Close() | ||
// iterate all rows | ||
for cursor.Next() { | ||
select { | ||
case <-ctx.Done(): | ||
return errors.TaskCanceled | ||
default: | ||
} | ||
err = lakeModels.Db.ScanRows(cursor, githubPullRequst) | ||
if err != nil { | ||
return err | ||
} | ||
issueNumberListStr := getCloseIssueId(githubPullRequst.Body) | ||
|
||
if issueNumberListStr == "" { | ||
continue | ||
} | ||
|
||
issueNumberListStr = strings.TrimPrefix(issueNumberListStr, "#") | ||
issueNumberList := strings.Split(issueNumberListStr, "#") | ||
issue := &githubModels.GithubIssue{} | ||
|
||
for _, issueNumber := range issueNumberList { | ||
issueNumber = strings.TrimSpace(issueNumber) | ||
err = lakeModels.Db.Where("number = ?", issueNumber).Limit(1).Find(issue).Error | ||
if err != nil { | ||
return err | ||
} | ||
if issue == nil { | ||
continue | ||
} | ||
githubPullRequstIssue := &githubModels.GithubPullRequestIssue{ | ||
PullRequestId: githubPullRequst.GithubId, | ||
IssueId: issue.GithubId, | ||
} | ||
|
||
err = lakeModels.Db.Clauses( | ||
clause.OnConflict{UpdateAll: true}).Create(githubPullRequstIssue).Error | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getCloseIssueId(body string) string { | ||
if prBodyCloseRegex != nil { | ||
if m, _ := prBodyCloseRegex.FindStringMatch(body); m != nil { | ||
if len(m.Groups()) > 2 { | ||
return m.Groups()[len(m.Groups())-2].String() | ||
} | ||
} | ||
} | ||
return "" | ||
} |
52 changes: 52 additions & 0 deletions
52
plugins/github/tasks/github_pull_request_issues_convertor.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package tasks | ||
|
||
import ( | ||
lakeModels "github.com/merico-dev/lake/models" | ||
"github.com/merico-dev/lake/models/domainlayer/code" | ||
"github.com/merico-dev/lake/models/domainlayer/didgen" | ||
githubModels "github.com/merico-dev/lake/plugins/github/models" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
func ConvertPullRequestIssues() error { | ||
githubPullRequestIssue := &githubModels.GithubPullRequestIssue{} | ||
cursor, err := lakeModels.Db.Model(githubPullRequestIssue). | ||
Select("github_pull_request_issues.*"). | ||
Order("pull_request_id ASC"). | ||
Rows() | ||
if err != nil { | ||
return err | ||
} | ||
defer cursor.Close() | ||
domainIdGeneratorPr := didgen.NewDomainIdGenerator(&githubModels.GithubPullRequest{}) | ||
domainIdGeneratorIssue := didgen.NewDomainIdGenerator(&githubModels.GithubIssue{}) | ||
lastPrId := 0 | ||
// iterate all rows | ||
for cursor.Next() { | ||
err = lakeModels.Db.ScanRows(cursor, githubPullRequestIssue) | ||
if err != nil { | ||
return err | ||
} | ||
pullRequestId := domainIdGeneratorPr.Generate(githubPullRequestIssue.PullRequestId) | ||
issueId := domainIdGeneratorIssue.Generate(githubPullRequestIssue.IssueId) | ||
if lastPrId != githubPullRequestIssue.PullRequestId { | ||
// Clean up old data | ||
err := lakeModels.Db.Where("pull_request_id = ?", | ||
pullRequestId).Delete(&code.PullRequestLabel{}).Error | ||
if err != nil { | ||
return err | ||
} | ||
lastPrId = githubPullRequestIssue.PullRequestId | ||
} | ||
|
||
pullRequestIssue := &code.PullRequestIssue{ | ||
PullRequestId: pullRequestId, | ||
IssueId: issueId, | ||
} | ||
err = lakeModels.Db.Clauses(clause.OnConflict{DoNothing: true}).Create(pullRequestIssue).Error | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |