You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ab...@apache.org on 2022/08/05 11:36:14 UTC

[incubator-devlake] branch main updated (283d7f49 -> f9d707de)

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

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


    from 283d7f49 fix: cached plan must not change result type (#2687)
     new 7d7cbbee feat: github cicd convertor
     new af27f01a feat: github cicd convertor
     new f9d707de feat: github cicd convertor

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 plugins/github/impl/impl.go                        |  2 +
 .../{issue_convertor.go => cicd_job_convertor.go}  | 96 ++++++++++++----------
 ...ent_convertor.go => cicd_pipeline_convertor.go} | 75 ++++++++++-------
 plugins/github/tasks/cicd_run_enricher.go          | 20 ++++-
 4 files changed, 118 insertions(+), 75 deletions(-)
 copy plugins/github/tasks/{issue_convertor.go => cicd_job_convertor.go} (50%)
 copy plugins/github/tasks/{issue_comment_convertor.go => cicd_pipeline_convertor.go} (53%)


[incubator-devlake] 01/03: feat: github cicd convertor

Posted by ab...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 7d7cbbeeff958465d106e6c8157a1e3f89b104ed
Author: abeizn <zi...@merico.dev>
AuthorDate: Fri Aug 5 17:04:24 2022 +0800

    feat: github cicd convertor
---
 plugins/github/impl/impl.go                     |   2 +
 plugins/github/tasks/cicd_job_convertor.go      | 124 ++++++++++++++++++++++++
 plugins/github/tasks/cicd_pipeline_convertor.go | 105 ++++++++++++++++++++
 plugins/github/tasks/cicd_run_enricher.go       |  20 +++-
 4 files changed, 249 insertions(+), 2 deletions(-)

diff --git a/plugins/github/impl/impl.go b/plugins/github/impl/impl.go
index bdb99223..94dc18de 100644
--- a/plugins/github/impl/impl.go
+++ b/plugins/github/impl/impl.go
@@ -83,6 +83,8 @@ func (plugin Github) SubTaskMetas() []core.SubTaskMeta {
 		tasks.CollectJobsMeta,
 		tasks.ExtractJobsMeta,
 		tasks.EnrichPipelinesMeta,
+		tasks.ConvertPipelinesMeta,
+		tasks.ConvertTasksMeta,
 		tasks.EnrichPullRequestIssuesMeta,
 		tasks.ConvertRepoMeta,
 		tasks.ConvertIssuesMeta,
diff --git a/plugins/github/tasks/cicd_job_convertor.go b/plugins/github/tasks/cicd_job_convertor.go
new file mode 100644
index 00000000..b5318959
--- /dev/null
+++ b/plugins/github/tasks/cicd_job_convertor.go
@@ -0,0 +1,124 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package tasks
+
+import (
+	"fmt"
+	"reflect"
+
+	"github.com/apache/incubator-devlake/plugins/core/dal"
+
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/helper"
+
+	"github.com/apache/incubator-devlake/models/domainlayer"
+	"github.com/apache/incubator-devlake/models/domainlayer/devops"
+	"github.com/apache/incubator-devlake/models/domainlayer/didgen"
+	githubModels "github.com/apache/incubator-devlake/plugins/github/models"
+)
+
+var ConvertTasksMeta = core.SubTaskMeta{
+	Name:             "convertTasks",
+	EntryPoint:       ConvertTasks,
+	EnabledByDefault: true,
+	Description:      "Convert tool layer table github_jobs into  domain layer table cicd_tasks",
+	DomainTypes:      []string{core.DOMAIN_TYPE_CICD},
+}
+
+type SimpleBranch struct {
+	HeadBranch string `json:"head_branch" gorm:"type:varchar(255)"`
+}
+
+func ConvertTasks(taskCtx core.SubTaskContext) error {
+	db := taskCtx.GetDal()
+	data := taskCtx.GetData().(*GithubTaskData)
+	repoId := data.Repo.GithubId
+
+	job := &githubModels.GithubJob{}
+	cursor, err := db.Cursor(
+		dal.From(job),
+		dal.Where("repo_id = ? and connection_id=?", repoId, data.Options.ConnectionId),
+	)
+	if err != nil {
+		return err
+	}
+	defer cursor.Close()
+
+	jobIdGen := didgen.NewDomainIdGenerator(&githubModels.GithubJob{})
+	converter, err := helper.NewDataConverter(helper.DataConverterArgs{
+		RawDataSubTaskArgs: helper.RawDataSubTaskArgs{
+			Ctx: taskCtx,
+			Params: GithubApiParams{
+				ConnectionId: data.Options.ConnectionId,
+				Owner:        data.Options.Owner,
+				Repo:         data.Options.Repo,
+			},
+			Table: RAW_JOB_TABLE,
+		},
+		InputRowType: reflect.TypeOf(githubModels.GithubJob{}),
+		Input:        cursor,
+		Convert: func(inputRow interface{}) ([]interface{}, error) {
+			line := inputRow.(*githubModels.GithubJob)
+
+			tmp := make([]*SimpleBranch, 0)
+			clauses := []dal.Clause{
+				dal.Select("head_branch"),
+				dal.From("_tool_github_runs"),
+				dal.Where("id = ?", line.RunID),
+			}
+			err = db.All(&tmp, clauses...)
+			if err != nil {
+				return nil, err
+			}
+
+			domainjob := &devops.CICDTask{
+				DomainEntity: domainlayer.DomainEntity{Id: jobIdGen.Generate(data.Options.ConnectionId, repoId, line.ID)},
+				Name:         line.Name,
+				Status:       line.Status,
+				Type:         line.Type,
+				StatedDate:   *line.StartedAt,
+				FinishedDate: line.CompletedAt,
+			}
+			if len(tmp) > 0 {
+				domainjob.PipelineId = fmt.Sprintf("%s:%s:%d:%d:%s:%s", "github", "GithubPipeline", data.Options.ConnectionId, repoId, tmp[0].HeadBranch, line.HeadSha)
+			}
+			if line.Status == "completed" {
+				domainjob.DurationSec = uint64(line.CompletedAt.Sub(*line.StartedAt).Seconds())
+			}
+			if line.Conclusion == "success" {
+				domainjob.Result = devops.SUCCESS
+			} else if line.Conclusion == "cancelled" {
+				domainjob.Result = devops.ABORT
+			} else {
+				domainjob.Result = devops.FAILURE
+			}
+			if line.Status != "completed" {
+				domainjob.Result = devops.IN_PROGRESS
+			}
+
+			return []interface{}{
+				domainjob,
+			}, nil
+		},
+	})
+	if err != nil {
+		return err
+	}
+
+	return converter.Execute()
+}
diff --git a/plugins/github/tasks/cicd_pipeline_convertor.go b/plugins/github/tasks/cicd_pipeline_convertor.go
new file mode 100644
index 00000000..eb28abed
--- /dev/null
+++ b/plugins/github/tasks/cicd_pipeline_convertor.go
@@ -0,0 +1,105 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package tasks
+
+import (
+	"reflect"
+	"strconv"
+
+	"github.com/apache/incubator-devlake/plugins/core/dal"
+
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/helper"
+
+	"github.com/apache/incubator-devlake/models/domainlayer"
+	"github.com/apache/incubator-devlake/models/domainlayer/devops"
+	"github.com/apache/incubator-devlake/models/domainlayer/didgen"
+	githubModels "github.com/apache/incubator-devlake/plugins/github/models"
+)
+
+var ConvertPipelinesMeta = core.SubTaskMeta{
+	Name:             "convertPipelines",
+	EntryPoint:       ConvertPipelines,
+	EnabledByDefault: true,
+	Description:      "Convert tool layer table github_pipelines into  domain layer table cicd_pipeline",
+	DomainTypes:      []string{core.DOMAIN_TYPE_CICD},
+}
+
+func ConvertPipelines(taskCtx core.SubTaskContext) error {
+	db := taskCtx.GetDal()
+	data := taskCtx.GetData().(*GithubTaskData)
+	repoId := data.Repo.GithubId
+
+	pipeline := &githubModels.GithubPipeline{}
+	cursor, err := db.Cursor(
+		dal.From(pipeline),
+		dal.Where("repo_id = ? and connection_id=?", repoId, data.Options.ConnectionId),
+	)
+	if err != nil {
+		return err
+	}
+	defer cursor.Close()
+
+	pipelineIdGen := didgen.NewDomainIdGenerator(&githubModels.GithubPipeline{})
+	converter, err := helper.NewDataConverter(helper.DataConverterArgs{
+		RawDataSubTaskArgs: helper.RawDataSubTaskArgs{
+			Ctx: taskCtx,
+			Params: GithubApiParams{
+				ConnectionId: data.Options.ConnectionId,
+				Owner:        data.Options.Owner,
+				Repo:         data.Options.Repo,
+			},
+			Table: RAW_RUN_TABLE,
+		},
+		InputRowType: reflect.TypeOf(githubModels.GithubPipeline{}),
+		Input:        cursor,
+		Convert: func(inputRow interface{}) ([]interface{}, error) {
+			line := inputRow.(*githubModels.GithubPipeline)
+			domainPipeline := &devops.CICDPipeline{
+				DomainEntity: domainlayer.DomainEntity{Id: pipelineIdGen.Generate(data.Options.ConnectionId, repoId, line.Branch, line.Commit)},
+				CommitSha:    line.Commit,
+				Branch:       line.Branch,
+				Repo:         strconv.Itoa(repoId),
+				Status:       line.Status,
+				Type:         line.Type,
+				DurationSec:  uint64(line.Duration),
+				CreatedDate:  *line.StartedDate,
+				FinishedDate: line.FinishedDate,
+			}
+			if line.Result == "success" {
+				domainPipeline.Result = devops.SUCCESS
+			} else if line.Result == "cancelled" {
+				domainPipeline.Result = devops.ABORT
+			} else {
+				domainPipeline.Result = devops.FAILURE
+			}
+			if line.Status != "completed" {
+				domainPipeline.Result = devops.IN_PROGRESS
+			}
+
+			return []interface{}{
+				domainPipeline,
+			}, nil
+		},
+	})
+	if err != nil {
+		return err
+	}
+
+	return converter.Execute()
+}
diff --git a/plugins/github/tasks/cicd_run_enricher.go b/plugins/github/tasks/cicd_run_enricher.go
index ee90e7d6..ccd330f6 100644
--- a/plugins/github/tasks/cicd_run_enricher.go
+++ b/plugins/github/tasks/cicd_run_enricher.go
@@ -18,11 +18,15 @@ limitations under the License.
 package tasks
 
 import (
+	"encoding/json"
+
 	"github.com/apache/incubator-devlake/plugins/core"
 	"github.com/apache/incubator-devlake/plugins/core/dal"
 	githubModels "github.com/apache/incubator-devlake/plugins/github/models"
 )
 
+const TOOL_PIPELINE_TABLE = "_tool_github_pipelines"
+
 var EnrichPipelinesMeta = core.SubTaskMeta{
 	Name:             "enrichPipelines",
 	EntryPoint:       EnrichPipelines,
@@ -37,7 +41,7 @@ func EnrichPipelines(taskCtx core.SubTaskContext) (err error) {
 	repoId := data.Repo.GithubId
 
 	cursor, err := db.Cursor(
-		dal.Select("head_sha, head_branch, status, conclusion, github_created_at, github_updated_at, run_attempt, run_started_at"),
+		dal.Select("head_sha, head_branch, status, conclusion, github_created_at, github_updated_at, run_attempt, run_started_at, _raw_data_id"),
 		dal.From(&githubModels.GithubRun{}),
 		dal.Orderby("head_sha, github_created_at"),
 	)
@@ -46,6 +50,15 @@ func EnrichPipelines(taskCtx core.SubTaskContext) (err error) {
 	}
 	defer cursor.Close()
 
+	apiParamsJson, err := json.Marshal(GithubApiParams{
+		ConnectionId: data.Options.ConnectionId,
+		Owner:        data.Options.Owner,
+		Repo:         data.Options.Repo,
+	})
+	if err != nil {
+		return err
+	}
+
 	for cursor.Next() {
 		entity := &githubModels.GithubPipeline{}
 		var item githubModels.GithubRun
@@ -55,6 +68,9 @@ func EnrichPipelines(taskCtx core.SubTaskContext) (err error) {
 		}
 
 		if item.HeadSha != entity.Commit {
+			entity.NoPKModel.RawDataId = item.NoPKModel.RawDataId
+			entity.NoPKModel.RawDataTable = RAW_RUN_TABLE
+			entity.NoPKModel.RawDataParams = string(apiParamsJson)
 			entity.ConnectionId = data.Options.ConnectionId
 			entity.RepoId = repoId
 			entity.Commit = item.HeadSha
@@ -84,7 +100,7 @@ func EnrichPipelines(taskCtx core.SubTaskContext) (err error) {
 			}
 
 		}
-		err := db.CreateOrUpdate(entity)
+		err = db.CreateOrUpdate(entity)
 		if err != nil {
 			return err
 		}


[incubator-devlake] 02/03: feat: github cicd convertor

Posted by ab...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit af27f01a30213c0ad6a3e392eaaeb190e1b85e3a
Author: abeizn <zi...@merico.dev>
AuthorDate: Fri Aug 5 19:11:53 2022 +0800

    feat: github cicd convertor
---
 plugins/github/tasks/cicd_job_convertor.go      | 17 +++++++++--------
 plugins/github/tasks/cicd_pipeline_convertor.go | 12 +++++++-----
 plugins/github/tasks/cicd_run_enricher.go       |  2 --
 3 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/plugins/github/tasks/cicd_job_convertor.go b/plugins/github/tasks/cicd_job_convertor.go
index b5318959..034a2ca7 100644
--- a/plugins/github/tasks/cicd_job_convertor.go
+++ b/plugins/github/tasks/cicd_job_convertor.go
@@ -89,7 +89,6 @@ func ConvertTasks(taskCtx core.SubTaskContext) error {
 			domainjob := &devops.CICDTask{
 				DomainEntity: domainlayer.DomainEntity{Id: jobIdGen.Generate(data.Options.ConnectionId, repoId, line.ID)},
 				Name:         line.Name,
-				Status:       line.Status,
 				Type:         line.Type,
 				StatedDate:   *line.StartedAt,
 				FinishedDate: line.CompletedAt,
@@ -97,18 +96,20 @@ func ConvertTasks(taskCtx core.SubTaskContext) error {
 			if len(tmp) > 0 {
 				domainjob.PipelineId = fmt.Sprintf("%s:%s:%d:%d:%s:%s", "github", "GithubPipeline", data.Options.ConnectionId, repoId, tmp[0].HeadBranch, line.HeadSha)
 			}
-			if line.Status == "completed" {
-				domainjob.DurationSec = uint64(line.CompletedAt.Sub(*line.StartedAt).Seconds())
-			}
+
 			if line.Conclusion == "success" {
 				domainjob.Result = devops.SUCCESS
-			} else if line.Conclusion == "cancelled" {
-				domainjob.Result = devops.ABORT
-			} else {
+			} else if line.Conclusion == "failure" || line.Conclusion == "startup_failure" {
 				domainjob.Result = devops.FAILURE
+			} else {
+				domainjob.Result = devops.ABORT
 			}
+
 			if line.Status != "completed" {
-				domainjob.Result = devops.IN_PROGRESS
+				domainjob.Status = devops.IN_PROGRESS
+			} else {
+				domainjob.Status = devops.DONE
+				domainjob.DurationSec = uint64(line.CompletedAt.Sub(*line.StartedAt).Seconds())
 			}
 
 			return []interface{}{
diff --git a/plugins/github/tasks/cicd_pipeline_convertor.go b/plugins/github/tasks/cicd_pipeline_convertor.go
index eb28abed..85b8d8bb 100644
--- a/plugins/github/tasks/cicd_pipeline_convertor.go
+++ b/plugins/github/tasks/cicd_pipeline_convertor.go
@@ -75,7 +75,6 @@ func ConvertPipelines(taskCtx core.SubTaskContext) error {
 				CommitSha:    line.Commit,
 				Branch:       line.Branch,
 				Repo:         strconv.Itoa(repoId),
-				Status:       line.Status,
 				Type:         line.Type,
 				DurationSec:  uint64(line.Duration),
 				CreatedDate:  *line.StartedDate,
@@ -83,13 +82,16 @@ func ConvertPipelines(taskCtx core.SubTaskContext) error {
 			}
 			if line.Result == "success" {
 				domainPipeline.Result = devops.SUCCESS
-			} else if line.Result == "cancelled" {
-				domainPipeline.Result = devops.ABORT
-			} else {
+			} else if line.Result == "failure" || line.Result == "startup_failure" {
 				domainPipeline.Result = devops.FAILURE
+			} else {
+				domainPipeline.Result = devops.ABORT
 			}
+
 			if line.Status != "completed" {
-				domainPipeline.Result = devops.IN_PROGRESS
+				domainPipeline.Status = devops.IN_PROGRESS
+			} else {
+				domainPipeline.Status = devops.DONE
 			}
 
 			return []interface{}{
diff --git a/plugins/github/tasks/cicd_run_enricher.go b/plugins/github/tasks/cicd_run_enricher.go
index ccd330f6..21054aaa 100644
--- a/plugins/github/tasks/cicd_run_enricher.go
+++ b/plugins/github/tasks/cicd_run_enricher.go
@@ -25,8 +25,6 @@ import (
 	githubModels "github.com/apache/incubator-devlake/plugins/github/models"
 )
 
-const TOOL_PIPELINE_TABLE = "_tool_github_pipelines"
-
 var EnrichPipelinesMeta = core.SubTaskMeta{
 	Name:             "enrichPipelines",
 	EntryPoint:       EnrichPipelines,


[incubator-devlake] 03/03: feat: github cicd convertor

Posted by ab...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit f9d707de52b01beb179c83c30922b3f576d6e4dc
Author: abeizn <zi...@merico.dev>
AuthorDate: Fri Aug 5 19:15:27 2022 +0800

    feat: github cicd convertor
---
 plugins/github/tasks/cicd_run_enricher.go | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/plugins/github/tasks/cicd_run_enricher.go b/plugins/github/tasks/cicd_run_enricher.go
index 21054aaa..a266adb5 100644
--- a/plugins/github/tasks/cicd_run_enricher.go
+++ b/plugins/github/tasks/cicd_run_enricher.go
@@ -47,6 +47,7 @@ func EnrichPipelines(taskCtx core.SubTaskContext) (err error) {
 		return err
 	}
 	defer cursor.Close()
+	taskCtx.SetProgress(0, -1)
 
 	apiParamsJson, err := json.Marshal(GithubApiParams{
 		ConnectionId: data.Options.ConnectionId,
@@ -102,6 +103,7 @@ func EnrichPipelines(taskCtx core.SubTaskContext) (err error) {
 		if err != nil {
 			return err
 		}
+		taskCtx.IncProgress(1)
 	}
 
 	return err