This is an automated email from the ASF dual-hosted git repository.

warren pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 4529a46d style(all): resolve some small issues
4529a46d is described below

commit 4529a46df82e7360ee180e2852ece91d803646cf
Author: Yingchu Chen <[email protected]>
AuthorDate: Thu Sep 8 17:37:52 2022 +0800

    style(all): resolve some small issues
---
 api/api.go                                         |  2 +
 api/blueprints/blueprints.go                       |  7 +-
 api/pipelines/pipelines.go                         |  3 +-
 api/push/push.go                                   |  3 +-
 api/task/task.go                                   |  3 +-
 plugins/github/tasks/comment_collector.go          | 85 ++++++++++++----------
 .../github/tasks/pr_review_comment_extractor.go    | 38 ++++++----
 services/blueprint.go                              |  6 +-
 services/init.go                                   |  2 +
 9 files changed, 88 insertions(+), 61 deletions(-)

diff --git a/api/api.go b/api/api.go
index 1ef47e10..737ca3d0 100644
--- a/api/api.go
+++ b/api/api.go
@@ -100,3 +100,5 @@ func CreateApiService() {
                panic(err)
        }
 }
+
+const BadRequestBody = "bad request body format"
diff --git a/api/blueprints/blueprints.go b/api/blueprints/blueprints.go
index ef423420..5bbba7bf 100644
--- a/api/blueprints/blueprints.go
+++ b/api/blueprints/blueprints.go
@@ -18,6 +18,7 @@ limitations under the License.
 package blueprints
 
 import (
+       "github.com/apache/incubator-devlake/api"
        "github.com/apache/incubator-devlake/errors"
        "net/http"
        "strconv"
@@ -42,7 +43,7 @@ func Post(c *gin.Context) {
 
        err := c.ShouldBind(blueprint)
        if err != nil {
-               shared.ApiOutputError(c, errors.BadInput.Wrap(err, "bad request 
body format", errors.AsUserMessage()))
+               shared.ApiOutputError(c, errors.BadInput.Wrap(err, 
api.BadRequestBody, errors.AsUserMessage()))
                return
        }
 
@@ -67,7 +68,7 @@ func Index(c *gin.Context) {
        var query services.BlueprintQuery
        err := c.ShouldBindQuery(&query)
        if err != nil {
-               shared.ApiOutputError(c, errors.BadInput.Wrap(err, "bad request 
body format", errors.AsUserMessage()))
+               shared.ApiOutputError(c, errors.BadInput.Wrap(err, 
api.BadRequestBody, errors.AsUserMessage()))
                return
        }
        blueprints, count, err := services.GetBlueprints(&query)
@@ -167,7 +168,7 @@ func Patch(c *gin.Context) {
        var body map[string]interface{}
        err = c.ShouldBind(&body)
        if err != nil {
-               shared.ApiOutputError(c, errors.BadInput.Wrap(err, "bad request 
body format", errors.AsUserMessage()))
+               shared.ApiOutputError(c, errors.BadInput.Wrap(err, 
api.BadRequestBody, errors.AsUserMessage()))
                return
        }
        blueprint, err := services.PatchBlueprint(id, body)
diff --git a/api/pipelines/pipelines.go b/api/pipelines/pipelines.go
index 74e70e7f..ad23b81c 100644
--- a/api/pipelines/pipelines.go
+++ b/api/pipelines/pipelines.go
@@ -18,6 +18,7 @@ limitations under the License.
 package pipelines
 
 import (
+       "github.com/apache/incubator-devlake/api"
        "github.com/apache/incubator-devlake/errors"
        "net/http"
        "os"
@@ -95,7 +96,7 @@ func Index(c *gin.Context) {
        var query services.PipelineQuery
        err := c.ShouldBindQuery(&query)
        if err != nil {
-               shared.ApiOutputError(c, errors.BadInput.Wrap(err, "bad request 
body format", errors.AsUserMessage()))
+               shared.ApiOutputError(c, errors.BadInput.Wrap(err, 
api.BadRequestBody, errors.AsUserMessage()))
                return
        }
        pipelines, count, err := services.GetPipelines(&query)
diff --git a/api/push/push.go b/api/push/push.go
index d24ff875..6b19099c 100644
--- a/api/push/push.go
+++ b/api/push/push.go
@@ -19,6 +19,7 @@ package push
 
 import (
        "fmt"
+       "github.com/apache/incubator-devlake/api"
        "github.com/apache/incubator-devlake/errors"
        "net/http"
 
@@ -52,7 +53,7 @@ func Post(c *gin.Context) {
        var rowsToInsert []map[string]interface{}
        err = c.ShouldBindJSON(&rowsToInsert)
        if err != nil {
-               shared.ApiOutputError(c, errors.BadInput.Wrap(err, "bad request 
body format", errors.AsUserMessage()))
+               shared.ApiOutputError(c, errors.BadInput.Wrap(err, 
api.BadRequestBody, errors.AsUserMessage()))
                return
        }
        rowsAffected, err := services.InsertRow(tableName, rowsToInsert)
diff --git a/api/task/task.go b/api/task/task.go
index 46817545..a57997cc 100644
--- a/api/task/task.go
+++ b/api/task/task.go
@@ -18,6 +18,7 @@ limitations under the License.
 package task
 
 import (
+       "github.com/apache/incubator-devlake/api"
        "github.com/apache/incubator-devlake/errors"
        "net/http"
        "strconv"
@@ -57,7 +58,7 @@ func Index(c *gin.Context) {
        var query services.TaskQuery
        err := c.ShouldBindQuery(&query)
        if err != nil {
-               shared.ApiOutputError(c, errors.BadInput.Wrap(err, "bad request 
body format", errors.AsUserMessage()))
+               shared.ApiOutputError(c, errors.BadInput.Wrap(err, 
api.BadRequestBody, errors.AsUserMessage()))
                return
        }
        err = c.ShouldBindUri(&query)
diff --git a/plugins/github/tasks/comment_collector.go 
b/plugins/github/tasks/comment_collector.go
index aeb1bd7c..b9cf7f50 100644
--- a/plugins/github/tasks/comment_collector.go
+++ b/plugins/github/tasks/comment_collector.go
@@ -23,6 +23,7 @@ import (
        "github.com/apache/incubator-devlake/errors"
        "net/http"
        "net/url"
+       "time"
 
        "github.com/apache/incubator-devlake/plugins/core/dal"
 
@@ -40,45 +41,9 @@ func CollectApiComments(taskCtx core.SubTaskContext) error {
 
        since := data.Since
        incremental := false
+       var err error
        if since == nil {
-               var latestUpdatedIssueComt models.GithubIssueComment
-               err := db.All(
-                       &latestUpdatedIssueComt,
-                       dal.Join("left join _tool_github_issues on 
_tool_github_issues.github_id = _tool_github_issue_comments.issue_id"),
-                       dal.Where(
-                               "_tool_github_issues.repo_id = ? AND 
_tool_github_issues.connection_id = ?", data.Repo.GithubId, 
data.Repo.ConnectionId,
-                       ),
-                       dal.Orderby("github_updated_at DESC"),
-                       dal.Limit(1),
-               )
-               if err != nil {
-                       return errors.Default.Wrap(err, "failed to get latest 
github issue record")
-               }
-               var latestUpdatedPrComt models.GithubPrComment
-               err = db.All(
-                       &latestUpdatedPrComt,
-                       dal.Join("left join _tool_github_pull_requests on 
_tool_github_pull_requests.github_id = 
_tool_github_pull_request_comments.pull_request_id"),
-                       dal.Where("_tool_github_pull_requests.repo_id = ? AND 
_tool_github_pull_requests.connection_id = ?", data.Repo.GithubId, 
data.Repo.ConnectionId),
-                       dal.Orderby("github_updated_at DESC"),
-                       dal.Limit(1),
-               )
-               if err != nil {
-                       return errors.Default.Wrap(err, "failed to get latest 
github issue record")
-               }
-               if latestUpdatedIssueComt.GithubId > 0 && 
latestUpdatedPrComt.GithubId > 0 {
-                       if 
latestUpdatedIssueComt.GithubUpdatedAt.Before(latestUpdatedPrComt.GithubUpdatedAt)
 {
-                               since = &latestUpdatedPrComt.GithubUpdatedAt
-                       } else {
-                               since = &latestUpdatedIssueComt.GithubUpdatedAt
-                       }
-                       incremental = true
-               } else if latestUpdatedIssueComt.GithubId > 0 {
-                       since = &latestUpdatedIssueComt.GithubUpdatedAt
-                       incremental = true
-               } else if latestUpdatedPrComt.GithubId > 0 {
-                       since = &latestUpdatedPrComt.GithubUpdatedAt
-                       incremental = true
-               }
+               since, incremental, err = calculateSince(data, db)
        }
 
        collector, err := helper.NewApiCollector(helper.ApiCollectorArgs{
@@ -133,3 +98,47 @@ var CollectApiCommentsMeta = core.SubTaskMeta{
        Description:      "Collect comments data from Github api",
        DomainTypes:      []string{core.DOMAIN_TYPE_CODE_REVIEW, 
core.DOMAIN_TYPE_TICKET},
 }
+
+func calculateSince(data *GithubTaskData, db dal.Dal) (*time.Time, bool, 
error) {
+       since := &time.Time{}
+       incremental := false
+       var latestUpdatedIssueComt models.GithubIssueComment
+       err := db.All(
+               &latestUpdatedIssueComt,
+               dal.Join("left join _tool_github_issues on 
_tool_github_issues.github_id = _tool_github_issue_comments.issue_id"),
+               dal.Where(
+                       "_tool_github_issues.repo_id = ? AND 
_tool_github_issues.connection_id = ?", data.Repo.GithubId, 
data.Repo.ConnectionId,
+               ),
+               dal.Orderby("github_updated_at DESC"),
+               dal.Limit(1),
+       )
+       if err != nil {
+               return nil, false, errors.Default.Wrap(err, "failed to get 
latest github issue record")
+       }
+       var latestUpdatedPrComt models.GithubPrComment
+       err = db.All(
+               &latestUpdatedPrComt,
+               dal.Join("left join _tool_github_pull_requests on 
_tool_github_pull_requests.github_id = 
_tool_github_pull_request_comments.pull_request_id"),
+               dal.Where("_tool_github_pull_requests.repo_id = ? AND 
_tool_github_pull_requests.connection_id = ?", data.Repo.GithubId, 
data.Repo.ConnectionId),
+               dal.Orderby("github_updated_at DESC"),
+               dal.Limit(1),
+       )
+       if err != nil {
+               return nil, false, errors.Default.Wrap(err, "failed to get 
latest github issue record")
+       }
+       if latestUpdatedIssueComt.GithubId > 0 && latestUpdatedPrComt.GithubId 
> 0 {
+               if 
latestUpdatedIssueComt.GithubUpdatedAt.Before(latestUpdatedPrComt.GithubUpdatedAt)
 {
+                       since = &latestUpdatedPrComt.GithubUpdatedAt
+               } else {
+                       since = &latestUpdatedIssueComt.GithubUpdatedAt
+               }
+               incremental = true
+       } else if latestUpdatedIssueComt.GithubId > 0 {
+               since = &latestUpdatedIssueComt.GithubUpdatedAt
+               incremental = true
+       } else if latestUpdatedPrComt.GithubId > 0 {
+               since = &latestUpdatedPrComt.GithubUpdatedAt
+               incremental = true
+       }
+       return since, incremental, nil
+}
diff --git a/plugins/github/tasks/pr_review_comment_extractor.go 
b/plugins/github/tasks/pr_review_comment_extractor.go
index d6bc7081..3a41cd81 100644
--- a/plugins/github/tasks/pr_review_comment_extractor.go
+++ b/plugins/github/tasks/pr_review_comment_extractor.go
@@ -42,6 +42,7 @@ var ExtractApiPrReviewCommentsMeta = core.SubTaskMeta{
 
 func ExtractApiPrReviewComments(taskCtx core.SubTaskContext) error {
        data := taskCtx.GetData().(*GithubTaskData)
+       db := taskCtx.GetDal()
        prUrlPattern := 
fmt.Sprintf(`https\:\/\/api\.github\.com\/repos\/%s\/%s\/pulls\/(\d+)`, 
data.Options.Owner, data.Options.Repo)
 
        extractor, err := helper.NewApiExtractor(helper.ApiExtractorArgs{
@@ -88,20 +89,12 @@ func ExtractApiPrReviewComments(taskCtx 
core.SubTaskContext) error {
                        if err != nil {
                                return nil, errors.Default.Wrap(err, "regexp 
Compile prUrlPattern failed")
                        }
-                       if prUrlRegex != nil {
-                               groups := 
prUrlRegex.FindStringSubmatch(prReviewComment.PrUrl)
-                               if len(groups) > 0 {
-                                       prNumber, err := strconv.Atoi(groups[1])
-                                       if err != nil {
-                                               return nil, 
errors.Default.Wrap(err, "parse prId failed")
-                                       }
-                                       pr := &models.GithubPullRequest{}
-                                       err = taskCtx.GetDal().First(pr, 
dal.Where("connection_id = ? and number = ? and repo_id = ?", 
data.Options.ConnectionId, prNumber, data.Repo.GithubId))
-                                       if err != nil && err != 
gorm.ErrRecordNotFound {
-                                               return nil, err
-                                       }
-                                       githubPrComment.PullRequestId = 
pr.GithubId
-                               }
+                       prId, err := enrichGithubPrComment(data, db, 
prUrlRegex, prReviewComment.PrUrl)
+                       if err != nil {
+                               return nil, errors.Default.Wrap(err, "parse 
prId failed")
+                       }
+                       if prId != 0 {
+                               githubPrComment.PullRequestId = prId
                        }
                        results = append(results, githubPrComment)
                        githubAccount, err := 
convertAccount(prReviewComment.User, data.Repo.GithubId, 
data.Options.ConnectionId)
@@ -119,3 +112,20 @@ func ExtractApiPrReviewComments(taskCtx 
core.SubTaskContext) error {
 
        return extractor.Execute()
 }
+
+func enrichGithubPrComment(data *GithubTaskData, db dal.Dal, prUrlRegex 
*regexp.Regexp, prUrl string) (int, error) {
+       groups := prUrlRegex.FindStringSubmatch(prUrl)
+       if len(groups) > 0 {
+               prNumber, err := strconv.Atoi(groups[1])
+               if err != nil {
+                       return 0, errors.Default.Wrap(err, "parse prId failed")
+               }
+               pr := &models.GithubPullRequest{}
+               err = db.First(pr, dal.Where("connection_id = ? and number = ? 
and repo_id = ?", data.Options.ConnectionId, prNumber, data.Repo.GithubId))
+               if err != nil && err != gorm.ErrRecordNotFound {
+                       return 0, err
+               }
+               return pr.GithubId, nil
+       }
+       return 0, nil
+}
diff --git a/services/blueprint.go b/services/blueprint.go
index 9e8216ee..9a1dbd52 100644
--- a/services/blueprint.go
+++ b/services/blueprint.go
@@ -211,7 +211,7 @@ func ReloadBlueprints(c *cron.Cron) error {
                blueprint := parseBlueprint(pp)
                plan, err := blueprint.UnmarshalPlan()
                if err != nil {
-                       blueprintLog.Error(err, "created cron job failed")
+                       blueprintLog.Error(err, failToCreateCronJob)
                        return err
                }
                _, err = c.AddFunc(blueprint.CronConfig, func() {
@@ -223,7 +223,7 @@ func ReloadBlueprints(c *cron.Cron) error {
                        }
                })
                if err != nil {
-                       blueprintLog.Error(err, "created cron job failed")
+                       blueprintLog.Error(err, failToCreateCronJob)
                        return err
                }
        }
@@ -242,7 +242,7 @@ func createPipelineByBlueprint(blueprintId uint64, name 
string, plan core.Pipeli
        pipeline, err := CreatePipeline(&newPipeline)
        // Return all created tasks to the User
        if err != nil {
-               blueprintLog.Error(err, "created cron job failed")
+               blueprintLog.Error(err, failToCreateCronJob)
                return nil, err
        }
        return pipeline, err
diff --git a/services/init.go b/services/init.go
index 963eae69..f0690e03 100644
--- a/services/init.go
+++ b/services/init.go
@@ -38,6 +38,8 @@ var cronManager *cron.Cron
 var log core.Logger
 var migrationRequireConfirmation bool
 
+const failToCreateCronJob = "created cron job failed"
+
 // Init the services module
 func Init() {
        var err error

Reply via email to