You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by GitBox <gi...@apache.org> on 2022/08/04 16:36:07 UTC

[GitHub] [incubator-devlake] warren830 commented on a diff in pull request #2680: feat: github cicd enricher from run to pipeline

warren830 commented on code in PR #2680:
URL: https://github.com/apache/incubator-devlake/pull/2680#discussion_r938007366


##########
plugins/github/tasks/cicd_run_enricher.go:
##########
@@ -0,0 +1,94 @@
+/*
+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 (
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/core/dal"
+	githubModels "github.com/apache/incubator-devlake/plugins/github/models"
+)
+
+var EnrichPipelinesMeta = core.SubTaskMeta{
+	Name:             "enrichPipelines",
+	EntryPoint:       EnrichPipelines,
+	EnabledByDefault: true,
+	Description:      "Create tool layer table github_pipelines from github_runs",
+	DomainTypes:      []string{core.DOMAIN_TYPE_CICD},
+}
+
+func EnrichPipelines(taskCtx core.SubTaskContext) (err error) {
+	db := taskCtx.GetDal()
+	data := taskCtx.GetData().(*GithubTaskData)
+
+	entity := &githubModels.GithubPipeline{}
+	cursor, err := db.Cursor(
+		dal.Select("head_sha, head_branch, status, conclusion, github_created_at, github_updated_at, run_attempt, run_started_at"),
+		dal.From(&githubModels.GithubRun{}),
+		dal.Orderby("head_sha"),
+	)
+	if err != nil {
+		return err
+	}
+	defer cursor.Close()
+
+	for cursor.Next() {
+		var item githubModels.GithubRun
+		err = db.Fetch(cursor, &item)
+		if err != nil {
+			return err
+		}
+
+		if item.HeadSha != entity.Commit {
+			entity.ConnectionId = data.Options.ConnectionId
+			entity.Commit = item.HeadSha
+			entity.Branch = item.HeadBranch // to do
+			entity.StartedDate = item.GithubCreatedAt
+			entity.FinishedDate = item.GithubUpdatedAt
+			entity.Status = item.Status
+			if entity.Status == "completed" {
+				entity.Duration = float64(item.GithubUpdatedAt.Sub(*item.GithubCreatedAt).Seconds())
+			}
+			entity.Results = item.Conclusion

Review Comment:
   Our result should be a enum value



##########
plugins/github/models/migrationscripts/20220728_add_runs_table.go:
##########
@@ -29,7 +29,6 @@ import (
 type GithubRun20220728 struct {
 	archived.NoPKModel
 	ConnectionId     uint64     `gorm:"primaryKey"`
-	GithubId         int        `gorm:"primaryKey"`

Review Comment:
   doesn't this one have a githubId?



##########
plugins/github/tasks/cicd_run_enricher.go:
##########
@@ -0,0 +1,94 @@
+/*
+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 (
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/core/dal"
+	githubModels "github.com/apache/incubator-devlake/plugins/github/models"
+)
+
+var EnrichPipelinesMeta = core.SubTaskMeta{
+	Name:             "enrichPipelines",
+	EntryPoint:       EnrichPipelines,
+	EnabledByDefault: true,
+	Description:      "Create tool layer table github_pipelines from github_runs",
+	DomainTypes:      []string{core.DOMAIN_TYPE_CICD},
+}
+
+func EnrichPipelines(taskCtx core.SubTaskContext) (err error) {
+	db := taskCtx.GetDal()
+	data := taskCtx.GetData().(*GithubTaskData)
+
+	entity := &githubModels.GithubPipeline{}
+	cursor, err := db.Cursor(
+		dal.Select("head_sha, head_branch, status, conclusion, github_created_at, github_updated_at, run_attempt, run_started_at"),
+		dal.From(&githubModels.GithubRun{}),
+		dal.Orderby("head_sha"),
+	)
+	if err != nil {
+		return err
+	}
+	defer cursor.Close()
+
+	for cursor.Next() {
+		var item githubModels.GithubRun
+		err = db.Fetch(cursor, &item)
+		if err != nil {
+			return err
+		}
+
+		if item.HeadSha != entity.Commit {
+			entity.ConnectionId = data.Options.ConnectionId
+			entity.Commit = item.HeadSha
+			entity.Branch = item.HeadBranch // to do
+			entity.StartedDate = item.GithubCreatedAt
+			entity.FinishedDate = item.GithubUpdatedAt
+			entity.Status = item.Status
+			if entity.Status == "completed" {
+				entity.Duration = float64(item.GithubUpdatedAt.Sub(*item.GithubCreatedAt).Seconds())
+			}
+			entity.Results = item.Conclusion
+			// TODO
+			entity.Type = "CI/CD"
+		} else {

Review Comment:
   I think you'd better create a new entity, for this, I'm wondering if there is a bug. In my thought, if `err := db.CreateOrUpdate(entity)` is doing batch save, the value of entity might be the last one



##########
plugins/github/models/pipeline.go:
##########
@@ -0,0 +1,41 @@
+/*
+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 models
+
+import (
+	"time"
+
+	"github.com/apache/incubator-devlake/models/common"
+)
+
+type GithubPipeline struct {
+	common.NoPKModel
+	ConnectionId uint64     `gorm:"primaryKey"`
+	Branch       string     `json:"branch" gorm:"primaryKey;type:varchar(255)"`
+	Commit       string     `json:"commit" gorm:"primaryKey;type:varchar(255)"`
+	StartedDate  *time.Time `json:"started_time"`
+	FinishedDate *time.Time `json:"finished_time"`
+	Duration     float64    `json:"duration"`
+	Status       string     `json:"status" gorm:"type:varchar(255)"`
+	Results      string     `json:"results" gorm:"type:varchar(255)"`

Review Comment:
   This should be Result?



##########
plugins/github/models/run.go:
##########
@@ -26,7 +26,6 @@ import (
 type GithubRun struct {
 	common.NoPKModel
 	ConnectionId     uint64     `gorm:"primaryKey"`
-	GithubId         int        `gorm:"primaryKey"`

Review Comment:
   same as above



##########
plugins/github/tasks/cicd_run_enricher.go:
##########
@@ -0,0 +1,94 @@
+/*
+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 (
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/core/dal"
+	githubModels "github.com/apache/incubator-devlake/plugins/github/models"
+)
+
+var EnrichPipelinesMeta = core.SubTaskMeta{
+	Name:             "enrichPipelines",
+	EntryPoint:       EnrichPipelines,
+	EnabledByDefault: true,
+	Description:      "Create tool layer table github_pipelines from github_runs",
+	DomainTypes:      []string{core.DOMAIN_TYPE_CICD},
+}
+
+func EnrichPipelines(taskCtx core.SubTaskContext) (err error) {
+	db := taskCtx.GetDal()
+	data := taskCtx.GetData().(*GithubTaskData)
+
+	entity := &githubModels.GithubPipeline{}
+	cursor, err := db.Cursor(
+		dal.Select("head_sha, head_branch, status, conclusion, github_created_at, github_updated_at, run_attempt, run_started_at"),

Review Comment:
   I think we can also order by github_created_at, then you can get the earliest github_created_at easily 



##########
plugins/github/models/migrationscripts/20220802_add_pipeline_table.go:
##########
@@ -0,0 +1,64 @@
+/*
+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 migrationscripts
+
+import (
+	"context"
+	"fmt"
+	"time"
+
+	"github.com/apache/incubator-devlake/models/migrationscripts/archived"
+	"gorm.io/gorm"
+)
+
+type GithubPipeline20220803 struct {
+	archived.NoPKModel
+	ConnectionId uint64     `gorm:"primaryKey"`
+	Branch       string     `json:"branch" gorm:"primaryKey;type:varchar(255)"`
+	Commit       string     `json:"commit" gorm:"primaryKey;type:varchar(255)"`
+	StartedDate  *time.Time `json:"started_time"`
+	FinishedDate *time.Time `json:"finished_time"`
+	Duration     float64    `json:"duration"`
+	Status       string     `json:"status" gorm:"type:varchar(255)"`
+	Results      string     `json:"results" gorm:"type:varchar(255)"`

Review Comment:
   same as above



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org