You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ly...@apache.org on 2024/05/28 03:30:54 UTC

(incubator-devlake) branch main updated: Clear history data when running linker (#7526)

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

lynwee 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 a4cb023ba Clear history data when running linker (#7526)
a4cb023ba is described below

commit a4cb023ba2464855efd0f2fb377f783b148a75ef
Author: Lynwee <li...@merico.dev>
AuthorDate: Tue May 28 11:30:49 2024 +0800

    Clear history data when running linker (#7526)
    
    * fix(apikey): abort request if api key doesn't match
    
    * feat(webhook): update deployment commit id
    
    * fix(plugins): update path parameter, adapt to latest plugin helper
    
    * feat(plugins): add new plugin linker
    
    * feat(linker): fix compile errors
    
    * feat(linker): remove `since` in options
    
    * feat(linker): add e2e test, fix dora errors
    
    * fix(test): fix test errors
    
    * docs(linker): remove some comments
    
    * fix(linker): fix plugin name
    
    * fix(linker): fix unpected error
    
    * feat(linker): find all candidate issue keys from pr's title and description
    
    * fix(test): fix lint errors
    
    * fix(linker): clear history data when running linker
    
    * fix(linker): use `repos` to get pull_requests instead of `cicd_scopes`
---
 .../linker/e2e/snapshot_tables/project_mapping.csv |  2 +-
 backend/plugins/linker/tasks/link_pr_and_issue.go  | 22 +++++++++++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/backend/plugins/linker/e2e/snapshot_tables/project_mapping.csv b/backend/plugins/linker/e2e/snapshot_tables/project_mapping.csv
index 70e57627e..1fb85d1d1 100644
--- a/backend/plugins/linker/e2e/snapshot_tables/project_mapping.csv
+++ b/backend/plugins/linker/e2e/snapshot_tables/project_mapping.csv
@@ -1,2 +1,2 @@
 "project_name","table","row_id","created_at","updated_at","_raw_data_params","_raw_data_table","_raw_data_id","_raw_data_remark"
-"GitHub1","cicd_scopes","github:GithubRepo:1:384111310","2024-05-15 12:02:13.590","2024-05-15 12:02:13.590","GitHub1","",0,""
\ No newline at end of file
+"GitHub1","repos","github:GithubRepo:1:384111310","2024-05-15 12:02:13.590","2024-05-15 12:02:13.590","GitHub1","",0,""
\ No newline at end of file
diff --git a/backend/plugins/linker/tasks/link_pr_and_issue.go b/backend/plugins/linker/tasks/link_pr_and_issue.go
index a920c4fe7..35db8cf4b 100644
--- a/backend/plugins/linker/tasks/link_pr_and_issue.go
+++ b/backend/plugins/linker/tasks/link_pr_and_issue.go
@@ -45,12 +45,32 @@ func normalizeIssueKey(issueKey string) string {
 	return issueKey
 }
 
+func clearHistoryData(db dal.Dal, data *LinkerTaskData) errors.Error {
+	sql := `
+	DELETE FROM pull_request_issues
+		WHERE pull_request_id IN (
+			SELECT pr.id
+				FROM pull_requests pr
+					LEFT JOIN project_mapping pm
+					ON pm.table = 'repos'
+						AND pm.row_id = pr.base_repo_id
+						AND pm.project_name = ?
+	)
+`
+	return db.Exec(sql, data.Options.ProjectName)
+}
+
 func LinkPrToIssue(taskCtx plugin.SubTaskContext) errors.Error {
 	db := taskCtx.GetDal()
 	data := taskCtx.GetData().(*LinkerTaskData)
+
+	if err := clearHistoryData(db, data); err != nil {
+		return err
+	}
+
 	var clauses = []dal.Clause{
 		dal.From(&code.PullRequest{}),
-		dal.Join("LEFT JOIN project_mapping pm ON (pm.table = 'cicd_scopes' AND pm.row_id = pull_requests.base_repo_id)"),
+		dal.Join("LEFT JOIN project_mapping pm ON (pm.table = 'repos' AND pm.row_id = pull_requests.base_repo_id)"),
 		dal.Where("pm.project_name = ?", data.Options.ProjectName),
 	}
 	cursor, err := db.Cursor(clauses...)