diff --git a/server/api/provider/create.go b/server/api/provider/create.go index ced6ceee9..a8956ca4e 100644 --- a/server/api/provider/create.go +++ b/server/api/provider/create.go @@ -37,7 +37,7 @@ func HandleCreate(providers core.ProviderStore) http.HandlerFunc { return } - provider := core.Provider{ + provider := &core.Provider{ Name: f.Name, URL: f.URL, Host: f.Host, @@ -51,6 +51,11 @@ func HandleCreate(providers core.ProviderStore) http.HandlerFunc { return } + if err := providers.Sync(provider.ID); err != nil { + render.InternalServerError(w, err.Error()) + return + } + render.JSON(w, http.StatusOK, provider) } } diff --git a/server/api/provider/update.go b/server/api/provider/update.go index a69ab7c58..5b4925f63 100644 --- a/server/api/provider/update.go +++ b/server/api/provider/update.go @@ -18,7 +18,7 @@ func HandleUpdate(providers core.ProviderStore, users core.UserStore) http.Handl Name string `json:"name" valid:"stringlength(4|12),required"` URL string `json:"url" valid:"url,required"` Host string `json:"host" valid:"url,required"` - AccessToken string `json:"accessToken" valid:"stringlength(12|50),required"` + AccessToken string `json:"accessToken"` Secret string `json:"secret" valid:"stringlength(5|50),required"` } @@ -51,14 +51,17 @@ func HandleUpdate(providers core.ProviderStore, users core.UserStore) http.Handl } if p.UserID == claims.ID || user.Role == "admin" { - provider := core.Provider{ - ID: f.ID, - Name: f.Name, - URL: f.URL, - Host: f.Host, - AccessToken: f.AccessToken, - Secret: f.Secret, - UserID: claims.ID, + provider := &core.Provider{ + ID: f.ID, + Name: f.Name, + URL: f.URL, + Host: f.Host, + Secret: f.Secret, + UserID: claims.ID, + } + + if f.AccessToken != "" { + provider.AccessToken = f.AccessToken } if err := providers.Update(provider); err != nil { diff --git a/server/core/provider.go b/server/core/provider.go index df2b00ef3..9ea79da34 100644 --- a/server/core/provider.go +++ b/server/core/provider.go @@ -24,23 +24,23 @@ type ( // ProviderStore defines operations on `providers` table. ProviderStore interface { // Find returns provider from datastore. - Find(uint) (Provider, error) + Find(uint) (*Provider, error) // List returns providers from datastore. List() ([]*Provider, error) // ListUser returns providers from datastore based // by user ID. - ListUser(uint) ([]Provider, error) + ListUser(uint) ([]*Provider, error) // Create persists a new provider to the datastore. - Create(Provider) error + Create(*Provider) error // Update persists updated provider to the datastore. - Update(Provider) error + Update(*Provider) error // Delete deletes a provider from the datastore. - Delete(Provider) error + Delete(*Provider) error // Sync synchronizes provider repositories with local repositories. Sync(uint) error diff --git a/server/store/provider/provider.go b/server/store/provider/provider.go index 581c20e63..ecc93be75 100644 --- a/server/store/provider/provider.go +++ b/server/store/provider/provider.go @@ -21,9 +21,9 @@ type providerStore struct { repos core.RepositoryStore } -func (s providerStore) Find(id uint) (core.Provider, error) { - var provider core.Provider - err := s.db.Model(&provider).Where("id = ?", id).First(&provider).Error +func (s providerStore) Find(id uint) (*core.Provider, error) { + provider := &core.Provider{} + err := s.db.Model(&core.Provider{}).Where("id = ?", id).First(&provider).Error return provider, err } @@ -33,21 +33,21 @@ func (s providerStore) List() ([]*core.Provider, error) { return providers, err } -func (s providerStore) ListUser(userID uint) ([]core.Provider, error) { - var providers []core.Provider +func (s providerStore) ListUser(userID uint) ([]*core.Provider, error) { + var providers []*core.Provider err := s.db.Where("user_id = ?", userID).Find(&providers).Error return providers, err } -func (s providerStore) Create(provider core.Provider) error { - return s.db.Create(&provider).Error +func (s providerStore) Create(provider *core.Provider) error { + return s.db.Create(provider).Error } -func (s providerStore) Update(provider core.Provider) error { - return s.db.Model(&provider).Updates(&provider).Error +func (s providerStore) Update(provider *core.Provider) error { + return s.db.Model(provider).Updates(&provider).Error } -func (s providerStore) Delete(provider core.Provider) error { +func (s providerStore) Delete(provider *core.Provider) error { return s.db.Delete(&provider).Error }