Skip to content

Commit

Permalink
cherry pick #7652 #7783 to v1.0 (#7790)
Browse files Browse the repository at this point in the history
* feat(dora): add incident related tables

* fix(ci): fix lint/e2e/test errors

* fix(ci): fix unit test errors

* feat(webhook): issues with type = "INCIDENT" will be stored/updated in table `incidents`

* fix(incident): fix method `IsIncident`

* feat(dora): add new option `DisableIssueToIncidentGenerator`

* fix(dora): fix test
  • Loading branch information
d4x1 authored Jul 29, 2024
1 parent 483c93e commit e196ad3
Show file tree
Hide file tree
Showing 27 changed files with 615 additions and 50 deletions.
53 changes: 53 additions & 0 deletions backend/core/models/domainlayer/code/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package code

import (
"fmt"
"github.com/apache/incubator-devlake/core/models/domainlayer/ticket"
"time"

"github.com/apache/incubator-devlake/core/models/domainlayer"
Expand Down Expand Up @@ -63,3 +65,54 @@ type PullRequest struct {
func (PullRequest) TableName() string {
return "pull_requests"
}

func (pr PullRequest) ConvertStatusToIncidentStatus() string {
switch pr.Status {
case OPEN:
return ticket.TODO
case CLOSED:
return ticket.OTHER
case MERGED:
return ticket.DONE
default:
return ticket.OTHER
}
}

func (pr PullRequest) ToIncident() (*ticket.Incident, error) {
incident := &ticket.Incident{
DomainEntity: pr.DomainEntity,
Url: pr.Url,
IncidentKey: fmt.Sprintf("%d", pr.PullRequestKey),
Title: pr.Title,
Description: pr.Description,
Status: pr.ConvertStatusToIncidentStatus(),
OriginalStatus: pr.OriginalStatus,
ResolutionDate: pr.MergedDate,
CreatedDate: &pr.CreatedDate,
OriginalEstimateMinutes: nil,
TimeSpentMinutes: nil,
TimeRemainingMinutes: nil,
CreatorId: pr.AuthorId,
CreatorName: pr.AuthorName,
ParentIncidentId: pr.ParentPrId,
Priority: "",
Severity: "",
Urgency: "",
Component: pr.Component,
OriginalProject: "",
}

if pr.MergedDate != nil {
incident.UpdatedDate = pr.MergedDate
}
if incident.UpdatedDate == nil {
incident.UpdatedDate = pr.ClosedDate
}

if pr.MergedDate != nil {
temp := uint(pr.MergedDate.Sub(pr.CreatedDate).Minutes())
incident.LeadTimeMinutes = &temp
}
return incident, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"github.com/apache/incubator-devlake/core/models/domainlayer"
)

type ProjectIssueMetric struct {
type ProjectIncidentDeploymentRelationship struct {
domainlayer.DomainEntity
ProjectName string `gorm:"primaryKey;type:varchar(100)"`
DeploymentId string
}

func (ProjectIssueMetric) TableName() string {
return "project_issue_metrics"
func (ProjectIncidentDeploymentRelationship) TableName() string {
return "project_incident_deployment_relationships"
}
4 changes: 3 additions & 1 deletion backend/core/models/domainlayer/domaininfo/domaininfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetDomainTablesInfo() []dal.Tabler {
&crossdomain.IssueCommit{},
&crossdomain.IssueRepoCommit{},
&crossdomain.ProjectMapping{},
&crossdomain.ProjectIssueMetric{},
&crossdomain.ProjectIncidentDeploymentRelationship{},
&crossdomain.ProjectPrMetric{},
&crossdomain.PullRequestIssue{},
&crossdomain.RefsIssuesDiffs{},
Expand Down Expand Up @@ -91,5 +91,7 @@ func GetDomainTablesInfo() []dal.Tabler {
&ticket.IssueAssignee{},
&ticket.IssueRelationship{},
&ticket.IssueCustomArrayField{},
&ticket.Incident{},
&ticket.IncidentAssignee{},
}
}
55 changes: 55 additions & 0 deletions backend/core/models/domainlayer/ticket/incident.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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 ticket

import (
"github.com/apache/incubator-devlake/core/models/domainlayer"
"time"
)

// Incident comes from two tables: issues and pull_requests, it won't be produced from tool layer tables so far.
// 1.From issues, all fields are the same with issues' record, include id field.
// 2.From pull_requests, id field is pull_requests.id, other fields should be transformed or calculated.
type Incident struct {
domainlayer.DomainEntity
Url string `gorm:"type:varchar(255)"`
IncidentKey string `gorm:"type:varchar(255)"` // issue_key/pull_request_key
Title string
Description string
Status string `gorm:"type:varchar(100)"`
OriginalStatus string `gorm:"type:varchar(100)"`
ResolutionDate *time.Time
CreatedDate *time.Time
UpdatedDate *time.Time
LeadTimeMinutes *uint
OriginalEstimateMinutes *int64
TimeSpentMinutes *int64
TimeRemainingMinutes *int64
CreatorId string `gorm:"type:varchar(255)"`
CreatorName string `gorm:"type:varchar(255)"`
ParentIncidentId string `gorm:"type:varchar(255)"`
Priority string `gorm:"type:varchar(255)"`
Severity string `gorm:"type:varchar(255)"`
Urgency string `gorm:"type:varchar(255)"`
Component string `gorm:"type:varchar(255)"`
OriginalProject string `gorm:"type:varchar(255)"`
}

func (Incident) TableName() string {
return "incidents"
}
32 changes: 32 additions & 0 deletions backend/core/models/domainlayer/ticket/incident_assginee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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 ticket

import "github.com/apache/incubator-devlake/core/models/common"

type IncidentAssignee struct {
IncidentId string `gorm:"primaryKey;type:varchar(255)"`
AssigneeId string `gorm:"primaryKey;type:varchar(255)"`
AssigneeName string `gorm:"type:varchar(255)"`

common.NoPKModel
}

func (IncidentAssignee) TableName() string {
return "incident_assignees"
}
51 changes: 49 additions & 2 deletions backend/core/models/domainlayer/ticket/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ limitations under the License.
package ticket

import (
"time"

"errors"
"github.com/apache/incubator-devlake/core/models/domainlayer"
"time"
)

type Issue struct {
Expand Down Expand Up @@ -105,3 +105,50 @@ func GetStatus(rule *StatusRule, input interface{}) string {
}
return rule.Default
}

func (issue Issue) IsIncident() bool {
return issue.Type == INCIDENT
}

func (issue Issue) ToIncidentAssignee() (*IncidentAssignee, error) {
if !issue.IsIncident() {
return nil, errors.New("issue type is not INCIDENT, cannot generate incident_assignee")
}
return &IncidentAssignee{
IncidentId: issue.Id,
AssigneeId: issue.AssigneeId,
AssigneeName: issue.AssigneeName,
NoPKModel: issue.DomainEntity.NoPKModel,
}, nil
}

func (issue Issue) ToIncident() (*Incident, error) {
if !issue.IsIncident() {
return nil, errors.New("issue type is not INCIDENT, cannot generate incident")
}
incident := &Incident{
DomainEntity: issue.DomainEntity,
Url: issue.Url,
IncidentKey: issue.IssueKey,
Title: issue.Title,
Description: issue.Description,
Status: issue.Status,
OriginalStatus: issue.OriginalStatus,
ResolutionDate: issue.ResolutionDate,
CreatedDate: issue.CreatedDate,
UpdatedDate: issue.UpdatedDate,
LeadTimeMinutes: issue.LeadTimeMinutes,
OriginalEstimateMinutes: issue.OriginalEstimateMinutes,
TimeSpentMinutes: issue.TimeSpentMinutes,
TimeRemainingMinutes: issue.TimeRemainingMinutes,
CreatorId: issue.CreatorId,
CreatorName: issue.CreatorName,
ParentIncidentId: issue.ParentIssueId,
Priority: issue.Priority,
Severity: issue.Severity,
Urgency: issue.Urgency,
Component: issue.Component,
OriginalProject: issue.OriginalProject,
}
return incident, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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 (
"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
"github.com/apache/incubator-devlake/helpers/migrationhelper"
)

type initIncidentRelatedTables struct{}

func (u *initIncidentRelatedTables) Up(basicRes context.BasicRes) errors.Error {
return migrationhelper.AutoMigrateTables(
basicRes,
&archived.IncidentAssignee{},
&archived.Incident{},
)
}

func (*initIncidentRelatedTables) Version() uint64 {
return 20240621152500
}

func (*initIncidentRelatedTables) Name() string {
return "init tables: incidents and incident_assignees"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
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 (
"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
)

type renameProjectIssueMetrics struct{}

func (u *renameProjectIssueMetrics) Up(basicRes context.BasicRes) errors.Error {
db := basicRes.GetDal()
return db.RenameTable(archived.ProjectIssueMetric{}.TableName(), "project_incident_deployment_relationships")
}

func (*renameProjectIssueMetrics) Version() uint64 {
return 20240621162000
}

func (*renameProjectIssueMetrics) Name() string {
return "rename project_issue_metrics to project_incident_deployment_relationships"
}
52 changes: 52 additions & 0 deletions backend/core/models/migrationscripts/archived/incident.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
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 archived

import (
"time"
)

type Incident struct {
DomainEntity
Url string `gorm:"type:varchar(255)"`
IncidentKey string `gorm:"type:varchar(255)"` // issue_key/pull_request_key
Title string
Description string
Status string `gorm:"type:varchar(100)"`
OriginalStatus string `gorm:"type:varchar(100)"`
ResolutionDate *time.Time
CreatedDate *time.Time
UpdatedDate *time.Time
LeadTimeMinutes *uint
OriginalEstimateMinutes *int64
TimeSpentMinutes *int64
TimeRemainingMinutes *int64
CreatorId string `gorm:"type:varchar(255)"`
CreatorName string `gorm:"type:varchar(255)"`
ParentIncidentId string `gorm:"type:varchar(255)"`
Priority string `gorm:"type:varchar(255)"`
Severity string `gorm:"type:varchar(255)"`
Urgency string `gorm:"type:varchar(255)"`
Component string `gorm:"type:varchar(255)"`

OriginalProject string `gorm:"type:varchar(255)"`
}

func (Incident) TableName() string {
return "incidents"
}
Loading

0 comments on commit e196ad3

Please sign in to comment.