-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGitMaterial.go
More file actions
152 lines (136 loc) · 4.94 KB
/
GitMaterial.go
File metadata and controls
152 lines (136 loc) · 4.94 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed 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 sql
import (
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
"time"
)
type SourceType string
const (
SOURCE_TYPE_BRANCH_FIXED SourceType = "SOURCE_TYPE_BRANCH_FIXED"
SOURCE_TYPE_BRANCH_REGEX SourceType = "SOURCE_TYPE_BRANCH_REGEX"
SOURCE_TYPE_TAG_ANY SourceType = "SOURCE_TYPE_TAG_ANY"
SOURCE_TYPE_WEBHOOK SourceType = "WEBHOOK"
)
// TODO: add support for submodule
type GitMaterial struct {
tableName struct{} `sql:"git_material"`
Id int `sql:"id,pk"`
GitProviderId int `sql:"git_provider_id,notnull"`
Url string `sql:"url,omitempty"`
FetchSubmodules bool `sql:"fetch_submodules,notnull"`
Name string `sql:"name, omitempty"`
CheckoutLocation string `sql:"checkout_location"`
CheckoutStatus bool `sql:"checkout_status,notnull"`
CheckoutMsgAny string `sql:"checkout_msg_any"`
Deleted bool `sql:"deleted,notnull"`
//------
LastFetchTime time.Time `json:"last_fetch_time"`
FetchStatus bool `json:"fetch_status"`
LastFetchErrorCount int `json:"last_fetch_error_count"` //continues fetch error
FetchErrorMessage string `json:"fetch_error_message"`
GitProvider *GitProvider
CiPipelineMaterials []*CiPipelineMaterial
}
type MaterialRepository interface {
GetConnection() *pg.DB
FindById(id int) (*GitMaterial, error)
Update(material *GitMaterial) error
Save(material *GitMaterial) error
SaveWithTransaction(material *GitMaterial, tx *pg.Tx) error
FindActive() ([]*GitMaterial, error)
FindActiveForOrdinalIndex(ordinalIndex int) ([]*GitMaterial, error)
FindAll() ([]*GitMaterial, error)
FindAllActiveByUrls(urls []string) ([]*GitMaterial, error)
}
type MaterialRepositoryImpl struct {
dbConnection *pg.DB
}
func NewMaterialRepositoryImpl(dbConnection *pg.DB) *MaterialRepositoryImpl {
return &MaterialRepositoryImpl{dbConnection: dbConnection}
}
func (repo MaterialRepositoryImpl) GetConnection() *pg.DB {
return repo.dbConnection
}
func (repo MaterialRepositoryImpl) Save(material *GitMaterial) error {
err := repo.dbConnection.Insert(material)
return err
}
func (repo MaterialRepositoryImpl) SaveWithTransaction(material *GitMaterial, tx *pg.Tx) error {
err := tx.Insert(material)
return err
}
func (repo MaterialRepositoryImpl) Update(material *GitMaterial) error {
_, err := repo.dbConnection.Model(material).WherePK().Update()
return err
}
func (repo MaterialRepositoryImpl) FindActive() ([]*GitMaterial, error) {
var materials []*GitMaterial
err := repo.dbConnection.Model(&materials).
Column("git_material.*", "GitProvider").
Relation("CiPipelineMaterials", func(q *orm.Query) (*orm.Query, error) {
return q.Where("active IS TRUE"), nil
}).
Where("deleted =? ", false).
Where("checkout_status=? ", true).
Order("id ASC").
Select()
return materials, err
}
func (repo MaterialRepositoryImpl) FindActiveForOrdinalIndex(ordinalIndex int) ([]*GitMaterial, error) {
var materials []*GitMaterial
err := repo.dbConnection.Model(&materials).
Column("git_material.*", "GitProvider").
Join("INNER JOIN git_material_node_mapping gmnp ON gmnp.git_material_id = git_material.id").
Relation("CiPipelineMaterials", func(q *orm.Query) (*orm.Query, error) {
return q.Where("active IS TRUE"), nil
}).
Where("deleted =? ", false).
Where("checkout_status=? ", true).
Where("gmnp.ordinal_index = ?", ordinalIndex).
Order("id ASC").
Select()
return materials, err
}
func (repo MaterialRepositoryImpl) FindAll() ([]*GitMaterial, error) {
var materials []*GitMaterial
err := repo.dbConnection.Model(&materials).
Column("git_material.*", "GitProvider").
Where("deleted =? ", false).
Select()
return materials, err
}
func (repo MaterialRepositoryImpl) FindById(id int) (*GitMaterial, error) {
var material GitMaterial
err := repo.dbConnection.Model(&material).
Column("git_material.*", "GitProvider").
Where("git_material.id =? ", id).
Where("git_material.deleted =? ", false).
Select()
return &material, err
}
func (repo MaterialRepositoryImpl) FindAllActiveByUrls(urls []string) ([]*GitMaterial, error) {
var materials []*GitMaterial
err := repo.dbConnection.Model(&materials).
Relation("CiPipelineMaterials", func(q *orm.Query) (*orm.Query, error) {
return q.Where("active IS TRUE"), nil
}).
Where("deleted =? ", false).
Where("url in (?) ", pg.In(urls)).
Select()
return materials, err
}