Skip to content

Commit

Permalink
feat: added the ability to manuall refresh a module
Browse files Browse the repository at this point in the history
  • Loading branch information
robbert229 committed Mar 9, 2024
1 parent f92b23b commit ae7945d
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/bitbucketserver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ func (g *TokenClient) ListTags(ctx context.Context, opts vcs.ListTagsOptions) ([
response, err := g.client.DefaultApi.GetTags(
owner,
name,
map[string]interface{}{"filterText": opts.Prefix},
map[string]interface{}{
"filterText": opts.Prefix,
"orderBy": "MODIFICATION",
"start": 0,
"limit": 1000,
},
)
if err != nil {
return nil, fmt.Errorf("failed to get tags: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/http/html/paths/funcmap.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/http/html/paths/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ var specs = []controllerSpec{
{
Name: "module",
controllerType: resourcePath,
actions: []action{{name: "refresh", collection: false}},
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions internal/http/html/paths/module_paths.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/http/html/static/templates/content/module_get.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,9 @@
<form id="module-delete-button" action="{{ deleteModulePath .Module.ID }}" method="POST">
<button class="btn-danger" onclick="return confirm('Are you sure you want to delete?')">Delete module</button>
</form>

<form id="module-refresh-button" action="{{ refreshModulePath .Module.ID }}" method="POST">
<button class="btn" onclick="return confirm('Are you sure you want to refresh?')">Refresh module</button>
</form>
</div>
{{ end }}
72 changes: 72 additions & 0 deletions internal/module/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,78 @@ func (s *Service) DeleteModule(ctx context.Context, id string) (*Module, error)
return module, nil
}

func (s *Service) RefreshModule(ctx context.Context, id string) (*Module, error) {
module, err := s.db.getModuleByID(ctx, id)
if err != nil {
return nil, err
}

_, err = s.organization.CanAccess(ctx, rbac.CreateModuleVersionAction, module.Organization)
if err != nil {
return nil, err
}

client, err := s.vcsproviders.GetVCSClient(ctx, module.Connection.VCSProviderID)
if err != nil {
return nil, err
}

tags, err := client.ListTags(ctx, vcs.ListTagsOptions{
Repo: module.Connection.Repo,
})
if err != nil {
return nil, err
}

exists := map[string]bool{}
for _, tag := range tags {
_, version, found := strings.Cut(tag, "/")
finalVersion := strings.TrimPrefix(version, "v")

if found {
exists[finalVersion] = true
}
}

for _, tag := range tags {
// tags/<version> -> <version>
_, version, found := strings.Cut(tag, "/")
if !found {
return nil, fmt.Errorf("malformed git ref: %s", tag)
}
// skip tags that are not semantic versions
if !semver.IsValid(version) {
continue
}

finalVersion := strings.TrimPrefix(version, "v")

// if it already exists then continue
if _, ok := exists[finalVersion]; ok {
continue
}

err := s.PublishVersion(ctx, PublishVersionOptions{
ModuleID: module.ID,
// strip off v prefix if it has one
Version: finalVersion,
Ref: tag,
Repo: Repo(module.Connection.Repo),
Client: client,
})
if err != nil {
return nil, err
}
}

module, err = s.db.getModuleByID(ctx, id)
if err != nil {
return nil, err
}

return module, nil
}

func (s *Service) CreateVersion(ctx context.Context, opts CreateModuleVersionOptions) (*ModuleVersion, error) {
module, err := s.db.getModuleByID(ctx, opts.ModuleID)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions internal/module/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type (
ListModules(context.Context, ListModulesOptions) ([]*Module, error)
PublishModule(context.Context, PublishOptions) (*Module, error)
DeleteModule(ctx context.Context, id string) (*Module, error)
RefreshModule(ctx context.Context, id string) (*Module, error)
}

// vcsprovidersClient provides web handlers with access to vcs providers
Expand All @@ -66,6 +67,7 @@ func (h *webHandlers) addHandlers(r *mux.Router) {
r.HandleFunc("/organizations/{organization_name}/modules/create", h.publish).Methods("POST")
r.HandleFunc("/modules/{module_id}", h.get).Methods("GET")
r.HandleFunc("/modules/{module_id}/delete", h.delete).Methods("POST")
r.HandleFunc("/modules/{module_id}/refresh", h.refresh).Methods("POST")
}

func (h *webHandlers) list(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -165,6 +167,25 @@ func (h *webHandlers) get(w http.ResponseWriter, r *http.Request) {
})
}

func (h *webHandlers) refresh(w http.ResponseWriter, r *http.Request) {
var params struct {
ID string `schema:"module_id,required"`
}
if err := decode.All(&params, r); err != nil {
h.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}

module, err := h.client.RefreshModule(r.Context(), params.ID)
if err != nil {
h.Error(w, err.Error(), http.StatusInternalServerError)
return
}

html.FlashSuccess(w, "refreshed module: "+module.Name)
http.Redirect(w, r, paths.Module(module.ID), http.StatusFound)
}

func (h *webHandlers) new(w http.ResponseWriter, r *http.Request) {
var params struct {
Step newModuleStep `schema:"step"`
Expand Down

0 comments on commit ae7945d

Please sign in to comment.