-
Notifications
You must be signed in to change notification settings - Fork 754
feat: github cicd enricher from run to pipeline #2680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ba76b61
2bdbaac
04108dd
3b7bacb
de920c0
8e7a301
068d77e
5ebf11d
87ac903
fac1011
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| 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"` | ||
| RepoId int `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)"` | ||
| Result string `json:"results" gorm:"type:varchar(255)"` | ||
| Type string `json:"type" gorm:"type:varchar(255)"` | ||
| } | ||
|
|
||
| func (GithubPipeline20220803) TableName() string { | ||
| return "_tool_github_pipelines" | ||
| } | ||
|
|
||
| type addGithubPipelineTable struct{} | ||
|
|
||
| func (u *addGithubPipelineTable) Up(ctx context.Context, db *gorm.DB) error { | ||
| // create table | ||
| err := db.Migrator().CreateTable(GithubPipeline20220803{}) | ||
| if err != nil { | ||
| return fmt.Errorf("create table _tool_github_pipelines error") | ||
| } | ||
| return nil | ||
|
|
||
| } | ||
|
|
||
| func (*addGithubPipelineTable) Version() uint64 { | ||
| return 20220803000001 | ||
| } | ||
|
|
||
| func (*addGithubPipelineTable) Name() string { | ||
| return "Github add github_pipelines table" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| 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"` | ||
| RepoId int `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)"` | ||
| Result string `json:"results" gorm:"type:varchar(255)"` | ||
| Type string `json:"type" gorm:"type:varchar(255)"` | ||
| } | ||
|
|
||
| func (GithubPipeline) TableName() string { | ||
| return "_tool_github_pipelines" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ import ( | |
| type GithubRun struct { | ||
| common.NoPKModel | ||
| ConnectionId uint64 `gorm:"primaryKey"` | ||
| GithubId int `gorm:"primaryKey"` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
| RepoId int `gorm:"primaryKey"` | ||
| ID int64 `json:"id" gorm:"primaryKey;autoIncrement:false"` | ||
| Name string `json:"name" gorm:"type:varchar(255)"` | ||
| NodeID string `json:"node_id" gorm:"type:varchar(255)"` | ||
|
|
@@ -53,6 +53,7 @@ type GithubRun struct { | |
| CancelURL string `json:"cancel_url" gorm:"type:varchar(255)"` | ||
| RerunURL string `json:"rerun_url" gorm:"type:varchar(255)"` | ||
| WorkflowURL string `json:"workflow_url" gorm:"type:varchar(255)"` | ||
| Type string `json:"type" gorm:"type:varchar(255)"` | ||
| } | ||
|
|
||
| func (GithubRun) TableName() string { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| 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) | ||
| 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"), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can also order by github_created_at, then you can get the earliest github_created_at easily |
||
| dal.From(&githubModels.GithubRun{}), | ||
| dal.Orderby("head_sha, github_created_at"), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer cursor.Close() | ||
|
|
||
| for cursor.Next() { | ||
| entity := &githubModels.GithubPipeline{} | ||
| 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.RepoId = repoId | ||
| entity.Commit = item.HeadSha | ||
| entity.Branch = item.HeadBranch | ||
| 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.Result = item.Conclusion | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please modify Result and Status after #2683 get merged |
||
| // TODO | ||
| entity.Type = "CI/CD" | ||
| } else { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you'd better create a new entity, for this, I'm wondering if there is a bug. In my thought, if |
||
| if item.GithubCreatedAt.Before(*entity.StartedDate) { | ||
| entity.StartedDate = item.GithubCreatedAt | ||
| } | ||
| if item.GithubUpdatedAt.After(*entity.FinishedDate) && item.Status == "completed" { | ||
| entity.FinishedDate = item.GithubCreatedAt | ||
| entity.Duration = float64(item.GithubUpdatedAt.Sub(*item.GithubCreatedAt).Seconds()) | ||
| } | ||
| if item.Status != "completed" { | ||
| entity.Status = item.Status | ||
| } | ||
| if item.Conclusion != "success" { | ||
| entity.Result = item.Conclusion | ||
| } | ||
|
|
||
| } | ||
| err := db.CreateOrUpdate(entity) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return err | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't this one have a githubId?