Skip to content

Commit

Permalink
feat(providers): auto-sync provider on create (closes #478)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Jan 5, 2021
1 parent 1358a8a commit 73f366d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
7 changes: 6 additions & 1 deletion server/api/provider/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}
21 changes: 12 additions & 9 deletions server/api/provider/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions server/core/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions server/store/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 73f366d

Please sign in to comment.