This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
forked from fabric8-services/fabric8-wit
/
trackeritem_repository.go
86 lines (73 loc) · 2.73 KB
/
trackeritem_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package remoteworkitem
import (
"fmt"
"golang.org/x/net/context"
"github.com/almighty/almighty-core/app"
"github.com/almighty/almighty-core/criteria"
"github.com/almighty/almighty-core/models"
"github.com/jinzhu/gorm"
)
// upload imports the items into database
func upload(db *gorm.DB, tID int, item TrackerItemContent) error {
remoteID := item.ID
content := string(item.Content)
var ti TrackerItem
if db.Where("remote_item_id = ? AND tracker_id = ?", remoteID, tID).Find(&ti).RecordNotFound() {
ti = TrackerItem{
Item: content,
RemoteItemID: remoteID,
TrackerID: uint64(tID)}
return db.Create(&ti).Error
}
ti.Item = content
return db.Save(&ti).Error
}
// Map a remote work item into an ALM work item and persist it into the database.
func convert(db *gorm.DB, tID int, item TrackerItemContent, provider string) (*app.WorkItem, error) {
remoteID := item.ID
content := string(item.Content)
wir := models.NewWorkItemRepository(db)
ti := TrackerItem{Item: content, RemoteItemID: remoteID, TrackerID: uint64(tID)}
// Converting the remote item to a local work item
remoteTrackerItemMethodRef, ok := RemoteWorkItemImplRegistry[provider]
if !ok {
return nil, BadParameterError{parameter: provider, value: provider}
}
remoteTrackerItem, err := remoteTrackerItemMethodRef(ti)
if err != nil {
return nil, InternalError{simpleError{message: " Error parsing the tracker data "}}
}
workItem, err := Map(remoteTrackerItem, WorkItemKeyMaps[provider])
if err != nil {
return nil, ConversionError{simpleError{message: " Error mapping to local work item "}}
}
// Get the remote item identifier ( which is currently the url ) to check if the work item exists in the database.
workItemRemoteID := workItem.Fields[models.SystemRemoteItemID]
sqlExpression := criteria.Equals(criteria.Field(models.SystemRemoteItemID), criteria.Literal(workItemRemoteID))
var newWorkItem *app.WorkItem
// Querying the database
existingWorkItems, _, err := wir.List(context.Background(), sqlExpression, nil, nil)
if len(existingWorkItems) != 0 {
fmt.Println("Workitem exists, will be updated")
existingWorkItem := existingWorkItems[0]
for key, value := range workItem.Fields {
existingWorkItem.Fields[key] = value
}
newWorkItem, err = wir.Save(context.Background(), *existingWorkItem)
if err != nil {
fmt.Println("Error updating work item : ", err)
}
} else {
fmt.Println("Work item not found , will now create new work item")
c := workItem.Fields[models.SystemCreator]
var creator string
if c != nil {
creator = c.(string)
}
newWorkItem, err = wir.Create(context.Background(), models.SystemBug, workItem.Fields, creator)
if err != nil {
fmt.Println("Error creating work item : ", err)
}
}
return newWorkItem, err
}