2
0

auth.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. package test
  2. import (
  3. "errors"
  4. "github.com/porter-dev/porter/internal/repository"
  5. "gorm.io/gorm"
  6. ints "github.com/porter-dev/porter/internal/models/integrations"
  7. )
  8. // KubeIntegrationRepository implements repository.KubeIntegrationRepository
  9. type KubeIntegrationRepository struct {
  10. canQuery bool
  11. kubeIntegrations []*ints.KubeIntegration
  12. }
  13. // NewKubeIntegrationRepository will return errors if canQuery is false
  14. func NewKubeIntegrationRepository(canQuery bool) repository.KubeIntegrationRepository {
  15. return &KubeIntegrationRepository{
  16. canQuery,
  17. []*ints.KubeIntegration{},
  18. }
  19. }
  20. // CreateKubeIntegration creates a new kube auth mechanism
  21. func (repo *KubeIntegrationRepository) CreateKubeIntegration(
  22. am *ints.KubeIntegration,
  23. ) (*ints.KubeIntegration, error) {
  24. if !repo.canQuery {
  25. return nil, errors.New("Cannot write database")
  26. }
  27. repo.kubeIntegrations = append(repo.kubeIntegrations, am)
  28. am.ID = uint(len(repo.kubeIntegrations))
  29. return am, nil
  30. }
  31. // ReadKubeIntegration finds a kube auth mechanism by id
  32. func (repo *KubeIntegrationRepository) ReadKubeIntegration(
  33. projectID, id uint,
  34. ) (*ints.KubeIntegration, error) {
  35. if !repo.canQuery {
  36. return nil, errors.New("Cannot read from database")
  37. }
  38. if int(id-1) >= len(repo.kubeIntegrations) || repo.kubeIntegrations[id-1] == nil {
  39. return nil, gorm.ErrRecordNotFound
  40. }
  41. index := int(id - 1)
  42. return repo.kubeIntegrations[index], nil
  43. }
  44. // ListKubeIntegrationsByProjectID finds all kube auth mechanisms
  45. // for a given project id
  46. func (repo *KubeIntegrationRepository) ListKubeIntegrationsByProjectID(
  47. projectID uint,
  48. ) ([]*ints.KubeIntegration, error) {
  49. if !repo.canQuery {
  50. return nil, errors.New("Cannot read from database")
  51. }
  52. res := make([]*ints.KubeIntegration, 0)
  53. for _, kubeAM := range repo.kubeIntegrations {
  54. if kubeAM.ProjectID == projectID {
  55. res = append(res, kubeAM)
  56. }
  57. }
  58. return res, nil
  59. }
  60. // BasicIntegrationRepository implements repository.BasicIntegrationRepository
  61. type BasicIntegrationRepository struct {
  62. canQuery bool
  63. basicIntegrations []*ints.BasicIntegration
  64. }
  65. // NewBasicIntegrationRepository will return errors if canQuery is false
  66. func NewBasicIntegrationRepository(canQuery bool) repository.BasicIntegrationRepository {
  67. return &BasicIntegrationRepository{
  68. canQuery,
  69. []*ints.BasicIntegration{},
  70. }
  71. }
  72. // CreateBasicIntegration creates a new basic auth mechanism
  73. func (repo *BasicIntegrationRepository) CreateBasicIntegration(
  74. am *ints.BasicIntegration,
  75. ) (*ints.BasicIntegration, error) {
  76. if !repo.canQuery {
  77. return nil, errors.New("Cannot write database")
  78. }
  79. repo.basicIntegrations = append(repo.basicIntegrations, am)
  80. am.ID = uint(len(repo.basicIntegrations))
  81. return am, nil
  82. }
  83. // ReadBasicIntegration finds a basic auth mechanism by id
  84. func (repo *BasicIntegrationRepository) ReadBasicIntegration(
  85. projectID, id uint,
  86. ) (*ints.BasicIntegration, error) {
  87. if !repo.canQuery {
  88. return nil, errors.New("Cannot read from database")
  89. }
  90. if int(id-1) >= len(repo.basicIntegrations) || repo.basicIntegrations[id-1] == nil {
  91. return nil, gorm.ErrRecordNotFound
  92. }
  93. index := int(id - 1)
  94. return repo.basicIntegrations[index], nil
  95. }
  96. // ListBasicIntegrationsByProjectID finds all basic auth mechanisms
  97. // for a given project id
  98. func (repo *BasicIntegrationRepository) ListBasicIntegrationsByProjectID(
  99. projectID uint,
  100. ) ([]*ints.BasicIntegration, error) {
  101. if !repo.canQuery {
  102. return nil, errors.New("Cannot read from database")
  103. }
  104. res := make([]*ints.BasicIntegration, 0)
  105. for _, basicAM := range repo.basicIntegrations {
  106. if basicAM.ProjectID == projectID {
  107. res = append(res, basicAM)
  108. }
  109. }
  110. return res, nil
  111. }
  112. // DeleteBasicIntegration deletes a basic integration
  113. func (repo *BasicIntegrationRepository) DeleteBasicIntegration(
  114. am *ints.BasicIntegration,
  115. ) (*ints.BasicIntegration, error) {
  116. if !repo.canQuery {
  117. return nil, errors.New("Cannot read from database")
  118. }
  119. var newInts []*ints.BasicIntegration
  120. for _, basicInt := range repo.basicIntegrations {
  121. if basicInt.ID != am.ID {
  122. newInts = append(newInts, basicInt)
  123. }
  124. }
  125. repo.basicIntegrations = newInts
  126. return am, nil
  127. }
  128. // OIDCIntegrationRepository implements repository.OIDCIntegrationRepository
  129. type OIDCIntegrationRepository struct {
  130. canQuery bool
  131. oidcIntegrations []*ints.OIDCIntegration
  132. }
  133. // NewOIDCIntegrationRepository will return errors if canQuery is false
  134. func NewOIDCIntegrationRepository(canQuery bool) repository.OIDCIntegrationRepository {
  135. return &OIDCIntegrationRepository{
  136. canQuery,
  137. []*ints.OIDCIntegration{},
  138. }
  139. }
  140. // CreateOIDCIntegration creates a new oidc auth mechanism
  141. func (repo *OIDCIntegrationRepository) CreateOIDCIntegration(
  142. am *ints.OIDCIntegration,
  143. ) (*ints.OIDCIntegration, error) {
  144. if !repo.canQuery {
  145. return nil, errors.New("Cannot write database")
  146. }
  147. repo.oidcIntegrations = append(repo.oidcIntegrations, am)
  148. am.ID = uint(len(repo.oidcIntegrations))
  149. return am, nil
  150. }
  151. // ReadOIDCIntegration finds a oidc auth mechanism by id
  152. func (repo *OIDCIntegrationRepository) ReadOIDCIntegration(
  153. projectID, id uint,
  154. ) (*ints.OIDCIntegration, error) {
  155. if !repo.canQuery {
  156. return nil, errors.New("Cannot read from database")
  157. }
  158. if int(id-1) >= len(repo.oidcIntegrations) || repo.oidcIntegrations[id-1] == nil {
  159. return nil, gorm.ErrRecordNotFound
  160. }
  161. index := int(id - 1)
  162. return repo.oidcIntegrations[index], nil
  163. }
  164. // ListOIDCIntegrationsByProjectID finds all oidc auth mechanisms
  165. // for a given project id
  166. func (repo *OIDCIntegrationRepository) ListOIDCIntegrationsByProjectID(
  167. projectID uint,
  168. ) ([]*ints.OIDCIntegration, error) {
  169. if !repo.canQuery {
  170. return nil, errors.New("Cannot read from database")
  171. }
  172. res := make([]*ints.OIDCIntegration, 0)
  173. for _, oidcAM := range repo.oidcIntegrations {
  174. if oidcAM.ProjectID == projectID {
  175. res = append(res, oidcAM)
  176. }
  177. }
  178. return res, nil
  179. }
  180. // OAuthIntegrationRepository implements repository.OAuthIntegrationRepository
  181. type OAuthIntegrationRepository struct {
  182. canQuery bool
  183. oIntegrations []*ints.OAuthIntegration
  184. }
  185. // NewOAuthIntegrationRepository will return errors if canQuery is false
  186. func NewOAuthIntegrationRepository(canQuery bool) repository.OAuthIntegrationRepository {
  187. return &OAuthIntegrationRepository{
  188. canQuery,
  189. []*ints.OAuthIntegration{},
  190. }
  191. }
  192. // CreateOAuthIntegration creates a new o auth mechanism
  193. func (repo *OAuthIntegrationRepository) CreateOAuthIntegration(
  194. am *ints.OAuthIntegration,
  195. ) (*ints.OAuthIntegration, error) {
  196. if !repo.canQuery {
  197. return nil, errors.New("cannot write database")
  198. }
  199. repo.oIntegrations = append(repo.oIntegrations, am)
  200. am.ID = uint(len(repo.oIntegrations))
  201. return am, nil
  202. }
  203. // ReadOAuthIntegration finds a o auth mechanism by id
  204. func (repo *OAuthIntegrationRepository) ReadOAuthIntegration(
  205. projectID, id uint,
  206. ) (*ints.OAuthIntegration, error) {
  207. if !repo.canQuery {
  208. return nil, errors.New("Cannot read from database")
  209. }
  210. if int(id-1) >= len(repo.oIntegrations) || repo.oIntegrations[id-1] == nil {
  211. return nil, gorm.ErrRecordNotFound
  212. }
  213. index := int(id - 1)
  214. return repo.oIntegrations[index], nil
  215. }
  216. // ListOAuthIntegrationsByProjectID finds all o auth mechanisms
  217. // for a given project id
  218. func (repo *OAuthIntegrationRepository) ListOAuthIntegrationsByProjectID(
  219. projectID uint,
  220. ) ([]*ints.OAuthIntegration, error) {
  221. if !repo.canQuery {
  222. return nil, errors.New("Cannot read from database")
  223. }
  224. res := make([]*ints.OAuthIntegration, 0)
  225. for _, oAM := range repo.oIntegrations {
  226. if oAM.ProjectID == projectID {
  227. res = append(res, oAM)
  228. }
  229. }
  230. return res, nil
  231. }
  232. // UpdateOAuthIntegration updates an oauth integration in the DB
  233. func (repo *OAuthIntegrationRepository) UpdateOAuthIntegration(
  234. am *ints.OAuthIntegration,
  235. ) (*ints.OAuthIntegration, error) {
  236. if !repo.canQuery {
  237. return nil, errors.New("Cannot write database")
  238. }
  239. if int(am.ID-1) >= len(repo.oIntegrations) || repo.oIntegrations[am.ID-1] == nil {
  240. return nil, gorm.ErrRecordNotFound
  241. }
  242. index := int(am.ID - 1)
  243. repo.oIntegrations[index] = am
  244. return am, nil
  245. }
  246. // AWSIntegrationRepository implements repository.AWSIntegrationRepository
  247. type AWSIntegrationRepository struct {
  248. canQuery bool
  249. awsIntegrations []*ints.AWSIntegration
  250. }
  251. // NewAWSIntegrationRepository will return errors if canQuery is false
  252. func NewAWSIntegrationRepository(canQuery bool) repository.AWSIntegrationRepository {
  253. return &AWSIntegrationRepository{
  254. canQuery,
  255. []*ints.AWSIntegration{},
  256. }
  257. }
  258. // CreateAWSIntegration creates a new aws auth mechanism
  259. func (repo *AWSIntegrationRepository) CreateAWSIntegration(
  260. am *ints.AWSIntegration,
  261. ) (*ints.AWSIntegration, error) {
  262. if !repo.canQuery {
  263. return nil, errors.New("Cannot write database")
  264. }
  265. repo.awsIntegrations = append(repo.awsIntegrations, am)
  266. am.ID = uint(len(repo.awsIntegrations))
  267. return am, nil
  268. }
  269. func (repo *AWSIntegrationRepository) OverwriteAWSIntegration(
  270. am *ints.AWSIntegration,
  271. ) (*ints.AWSIntegration, error) {
  272. if !repo.canQuery {
  273. return nil, errors.New("Cannot write database")
  274. }
  275. if int(am.ID-1) >= len(repo.awsIntegrations) || repo.awsIntegrations[am.ID-1] == nil {
  276. return nil, gorm.ErrRecordNotFound
  277. }
  278. index := int(am.ID - 1)
  279. repo.awsIntegrations[index] = am
  280. return am, nil
  281. }
  282. // ReadAWSIntegration finds a aws auth mechanism by id
  283. func (repo *AWSIntegrationRepository) ReadAWSIntegration(
  284. projectID, id uint,
  285. ) (*ints.AWSIntegration, error) {
  286. if !repo.canQuery {
  287. return nil, errors.New("Cannot read from database")
  288. }
  289. if int(id-1) >= len(repo.awsIntegrations) || repo.awsIntegrations[id-1] == nil {
  290. return nil, gorm.ErrRecordNotFound
  291. }
  292. index := int(id - 1)
  293. return repo.awsIntegrations[index], nil
  294. }
  295. // ListAWSIntegrationsByProjectID finds all aws auth mechanisms
  296. // for a given project id
  297. func (repo *AWSIntegrationRepository) ListAWSIntegrationsByProjectID(
  298. projectID uint,
  299. ) ([]*ints.AWSIntegration, error) {
  300. if !repo.canQuery {
  301. return nil, errors.New("Cannot read from database")
  302. }
  303. res := make([]*ints.AWSIntegration, 0)
  304. for _, awsAM := range repo.awsIntegrations {
  305. if awsAM.ProjectID == projectID {
  306. res = append(res, awsAM)
  307. }
  308. }
  309. return res, nil
  310. }
  311. // GCPIntegrationRepository implements repository.GCPIntegrationRepository
  312. type GCPIntegrationRepository struct {
  313. canQuery bool
  314. gcpIntegrations []*ints.GCPIntegration
  315. }
  316. // NewGCPIntegrationRepository will return errors if canQuery is false
  317. func NewGCPIntegrationRepository(canQuery bool) repository.GCPIntegrationRepository {
  318. return &GCPIntegrationRepository{
  319. canQuery,
  320. []*ints.GCPIntegration{},
  321. }
  322. }
  323. // CreateGCPIntegration creates a new gcp auth mechanism
  324. func (repo *GCPIntegrationRepository) CreateGCPIntegration(
  325. am *ints.GCPIntegration,
  326. ) (*ints.GCPIntegration, error) {
  327. if !repo.canQuery {
  328. return nil, errors.New("Cannot write database")
  329. }
  330. repo.gcpIntegrations = append(repo.gcpIntegrations, am)
  331. am.ID = uint(len(repo.gcpIntegrations))
  332. return am, nil
  333. }
  334. // ReadGCPIntegration finds a gcp auth mechanism by id
  335. func (repo *GCPIntegrationRepository) ReadGCPIntegration(
  336. projectID, id uint,
  337. ) (*ints.GCPIntegration, error) {
  338. if !repo.canQuery {
  339. return nil, errors.New("Cannot read from database")
  340. }
  341. if int(id-1) >= len(repo.gcpIntegrations) || repo.gcpIntegrations[id-1] == nil {
  342. return nil, gorm.ErrRecordNotFound
  343. }
  344. index := int(id - 1)
  345. return repo.gcpIntegrations[index], nil
  346. }
  347. // ListGCPIntegrationsByProjectID finds all gcp auth mechanisms
  348. // for a given project id
  349. func (repo *GCPIntegrationRepository) ListGCPIntegrationsByProjectID(
  350. projectID uint,
  351. ) ([]*ints.GCPIntegration, error) {
  352. if !repo.canQuery {
  353. return nil, errors.New("Cannot read from database")
  354. }
  355. res := make([]*ints.GCPIntegration, 0)
  356. for _, gcpAM := range repo.gcpIntegrations {
  357. if gcpAM.ProjectID == projectID {
  358. res = append(res, gcpAM)
  359. }
  360. }
  361. return res, nil
  362. }
  363. // GithubAppInstallationRepository implements repository.GithubAppInstallationRepository
  364. type GithubAppInstallationRepository struct {
  365. canQuery bool
  366. githubAppInstallations []*ints.GithubAppInstallation
  367. }
  368. func NewGithubAppInstallationRepository(canQuery bool) repository.GithubAppInstallationRepository {
  369. return &GithubAppInstallationRepository{
  370. canQuery,
  371. []*ints.GithubAppInstallation{},
  372. }
  373. }
  374. func (repo *GithubAppInstallationRepository) CreateGithubAppInstallation(am *ints.GithubAppInstallation) (*ints.GithubAppInstallation, error) {
  375. if !repo.canQuery {
  376. return nil, errors.New("cannot write database")
  377. }
  378. repo.githubAppInstallations = append(repo.githubAppInstallations, am)
  379. am.ID = uint(len(repo.githubAppInstallations))
  380. return am, nil
  381. }
  382. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallation(projectID, id uint) (*ints.GithubAppInstallation, error) {
  383. if !repo.canQuery {
  384. return nil, errors.New("cannot write database")
  385. }
  386. if int(id-1) >= len(repo.githubAppInstallations) || repo.githubAppInstallations[id-1] == nil {
  387. return nil, gorm.ErrRecordNotFound
  388. }
  389. return repo.githubAppInstallations[int(id-1)], nil
  390. }
  391. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByInstallationID(gaID uint) (*ints.GithubAppInstallation, error) {
  392. panic("unimplemented")
  393. }
  394. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID int64) (*ints.GithubAppInstallation, error) {
  395. if !repo.canQuery {
  396. return nil, errors.New("cannot write database")
  397. }
  398. for _, installation := range repo.githubAppInstallations {
  399. if installation != nil && installation.AccountID == accountID {
  400. return installation, nil
  401. }
  402. }
  403. return nil, gorm.ErrRecordNotFound
  404. }
  405. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountIDs(accountIDs []int64) ([]*ints.GithubAppInstallation, error) {
  406. if !repo.canQuery {
  407. return nil, errors.New("cannot write database")
  408. }
  409. ret := make([]*ints.GithubAppInstallation, 0)
  410. for _, installation := range repo.githubAppInstallations {
  411. // O(n^2) can be made into O(n) if this is too slow
  412. for _, id := range accountIDs {
  413. if installation.AccountID == id {
  414. ret = append(ret, installation)
  415. }
  416. }
  417. }
  418. return ret, nil
  419. }
  420. func (repo *GithubAppInstallationRepository) DeleteGithubAppInstallationByAccountID(accountID int64) error {
  421. if !repo.canQuery {
  422. return errors.New("cannot write database")
  423. }
  424. for i, installation := range repo.githubAppInstallations {
  425. if installation != nil && installation.AccountID == accountID {
  426. repo.githubAppInstallations[i] = nil
  427. }
  428. }
  429. return nil
  430. }
  431. type GithubAppOAuthIntegrationRepository struct {
  432. canQuery bool
  433. githubAppOauthIntegrations []*ints.GithubAppOAuthIntegration
  434. }
  435. func NewGithubAppOAuthIntegrationRepository(canQuery bool) repository.GithubAppOAuthIntegrationRepository {
  436. return &GithubAppOAuthIntegrationRepository{
  437. canQuery,
  438. []*ints.GithubAppOAuthIntegration{},
  439. }
  440. }
  441. func (repo *GithubAppOAuthIntegrationRepository) CreateGithubAppOAuthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
  442. if !repo.canQuery {
  443. return nil, errors.New("cannot write database")
  444. }
  445. repo.githubAppOauthIntegrations = append(repo.githubAppOauthIntegrations, am)
  446. am.ID = uint(len(repo.githubAppOauthIntegrations))
  447. return am, nil
  448. }
  449. func (repo *GithubAppOAuthIntegrationRepository) ReadGithubAppOauthIntegration(id uint) (*ints.GithubAppOAuthIntegration, error) {
  450. if !repo.canQuery {
  451. return nil, errors.New("cannot write database")
  452. }
  453. if int(id-1) >= len(repo.githubAppOauthIntegrations) || repo.githubAppOauthIntegrations[id-1] == nil {
  454. return nil, gorm.ErrRecordNotFound
  455. }
  456. return repo.githubAppOauthIntegrations[int(id-1)], nil
  457. }
  458. func (repo *GithubAppOAuthIntegrationRepository) UpdateGithubAppOauthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
  459. if !repo.canQuery {
  460. return nil, errors.New("Cannot write database")
  461. }
  462. if int(am.ID-1) >= len(repo.githubAppOauthIntegrations) || repo.githubAppOauthIntegrations[am.ID-1] == nil {
  463. return nil, gorm.ErrRecordNotFound
  464. }
  465. index := int(am.ID - 1)
  466. repo.githubAppOauthIntegrations[index] = am
  467. return am, nil
  468. }
  469. // AzureIntegrationRepository (unimplemented)
  470. type AzureIntegrationRepository struct{}
  471. // NewAzureIntegrationRepository returns a AzureIntegrationRepository which uses
  472. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  473. // sensitive data
  474. func NewAzureIntegrationRepository() repository.AzureIntegrationRepository {
  475. return &AzureIntegrationRepository{}
  476. }
  477. // CreateAzureIntegration creates a new Azure auth mechanism
  478. func (repo *AzureIntegrationRepository) CreateAzureIntegration(
  479. az *ints.AzureIntegration,
  480. ) (*ints.AzureIntegration, error) {
  481. panic("unimplemented")
  482. }
  483. // OverwriteAzureIntegration overwrites the Azure credential in the DB
  484. func (repo *AzureIntegrationRepository) OverwriteAzureIntegration(
  485. az *ints.AzureIntegration,
  486. ) (*ints.AzureIntegration, error) {
  487. panic("unimplemented")
  488. }
  489. // ReadAzureIntegration finds a Azure auth mechanism by id
  490. func (repo *AzureIntegrationRepository) ReadAzureIntegration(
  491. projectID, id uint,
  492. ) (*ints.AzureIntegration, error) {
  493. panic("unimplemented")
  494. }
  495. // ListAzureIntegrationsByProjectID finds all Azure auth mechanisms
  496. // for a given project id
  497. func (repo *AzureIntegrationRepository) ListAzureIntegrationsByProjectID(
  498. projectID uint,
  499. ) ([]*ints.AzureIntegration, error) {
  500. panic("unimplemented")
  501. }
  502. type GitlabIntegrationRepository struct {
  503. canQuery bool
  504. gitlabIntegrations []*ints.GitlabIntegration
  505. }
  506. func NewGitlabIntegrationRepository(canQuery bool) repository.GitlabIntegrationRepository {
  507. return &GitlabIntegrationRepository{
  508. canQuery,
  509. []*ints.GitlabIntegration{},
  510. }
  511. }
  512. func (repo *GitlabIntegrationRepository) CreateGitlabIntegration(gi *ints.GitlabIntegration) (*ints.GitlabIntegration, error) {
  513. if !repo.canQuery {
  514. return nil, errors.New("cannot write database")
  515. }
  516. repo.gitlabIntegrations = append(repo.gitlabIntegrations, gi)
  517. gi.ID = uint(len(repo.gitlabIntegrations))
  518. return gi, nil
  519. }
  520. func (repo *GitlabIntegrationRepository) ReadGitlabIntegration(projectID, id uint) (*ints.GitlabIntegration, error) {
  521. if !repo.canQuery {
  522. return nil, errors.New("Cannot read from database")
  523. }
  524. if int(id-1) >= len(repo.gitlabIntegrations) || repo.gitlabIntegrations[id-1] == nil {
  525. return nil, gorm.ErrRecordNotFound
  526. }
  527. index := int(id - 1)
  528. return repo.gitlabIntegrations[index], nil
  529. }
  530. func (repo *GitlabIntegrationRepository) ListGitlabIntegrationsByProjectID(projectID uint) ([]*ints.GitlabIntegration, error) {
  531. if !repo.canQuery {
  532. return nil, errors.New("Cannot read from database")
  533. }
  534. res := make([]*ints.GitlabIntegration, 0)
  535. for _, gitlabAM := range repo.gitlabIntegrations {
  536. if gitlabAM.ProjectID == projectID {
  537. res = append(res, gitlabAM)
  538. }
  539. }
  540. return res, nil
  541. }
  542. func (repo *GitlabIntegrationRepository) DeleteGitlabIntegrationByID(projectID, id uint) error {
  543. if !repo.canQuery {
  544. return errors.New("Cannot write database")
  545. }
  546. if int(id-1) >= len(repo.gitlabIntegrations) || repo.gitlabIntegrations[id-1] == nil || repo.gitlabIntegrations[id-1].ProjectID != projectID {
  547. return gorm.ErrRecordNotFound
  548. }
  549. index := int(id - 1)
  550. repo.gitlabIntegrations[index] = nil
  551. return nil
  552. }
  553. type GitlabAppOAuthIntegrationRepository struct {
  554. canQuery bool
  555. gitlabAppOAuthIntegrations []*ints.GitlabAppOAuthIntegration
  556. }
  557. func NewGitlabAppOAuthIntegrationRepository(canQuery bool) repository.GitlabAppOAuthIntegrationRepository {
  558. return &GitlabAppOAuthIntegrationRepository{
  559. canQuery,
  560. []*ints.GitlabAppOAuthIntegration{},
  561. }
  562. }
  563. func (repo *GitlabAppOAuthIntegrationRepository) CreateGitlabAppOAuthIntegration(
  564. gi *ints.GitlabAppOAuthIntegration,
  565. ) (*ints.GitlabAppOAuthIntegration, error) {
  566. if !repo.canQuery {
  567. return nil, errors.New("cannot write database")
  568. }
  569. repo.gitlabAppOAuthIntegrations = append(repo.gitlabAppOAuthIntegrations, gi)
  570. gi.ID = uint(len(repo.gitlabAppOAuthIntegrations))
  571. return gi, nil
  572. }
  573. func (repo *GitlabAppOAuthIntegrationRepository) ReadGitlabAppOAuthIntegration(
  574. userID, projectID, integrationID uint,
  575. ) (*ints.GitlabAppOAuthIntegration, error) {
  576. panic("not implemented")
  577. }