Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cyclops-ctrl/internal/controller/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,22 @@ func (c *Templates) DeleteTemplatesStore(ctx *gin.Context) {

ctx.Status(http.StatusOK)
}

func (c *Templates) GetTemplateRevisions(ctx *gin.Context) {
ctx.Header("Access-Control-Allow-Origin", "*")

repo := ctx.Query("repo")

if repo == "" {
ctx.JSON(http.StatusOK, []string{})
return
}

revisions, err := c.templatesRepo.GetTemplateRevisions(repo)
if err != nil {
ctx.JSON(http.StatusBadRequest, dto.NewError("Error loading template", err.Error()))
return
}

ctx.JSON(http.StatusOK, revisions)
}
2 changes: 2 additions & 0 deletions cyclops-ctrl/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func (h *Handler) Start() error {
h.router.GET("/templates", templatesController.GetTemplate)
h.router.GET("/templates/initial", templatesController.GetTemplateInitialValues)

h.router.GET("/templates/revisions", templatesController.GetTemplateRevisions)

// templates store
h.router.GET("/templates/store", templatesController.ListTemplatesStore)
h.router.PUT("/templates/store", templatesController.CreateTemplatesStore)
Expand Down
58 changes: 58 additions & 0 deletions cyclops-ctrl/mocks/ITemplateRepo.go

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

25 changes: 25 additions & 0 deletions cyclops-ctrl/pkg/template/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,31 @@ func (r Repo) LoadInitialTemplateValues(repoURL, path, commit string) (map[strin
return initialValues, nil
}

func (r Repo) listRemoteRefs(repo string, creds *auth.Credentials) ([]string, error) {
rem := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
Name: "origin",
URLs: []string{repo},
})

refs, err := rem.List(&git.ListOptions{
PeelingOption: git.AppendPeeled,
Auth: httpBasicAuthCredentials(creds),
})
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("repo %s was not cloned successfully; authentication might be required; check if repository exists and you referenced it correctly", repo))
}

branches := make([]string, 0)

for _, ref := range refs {
if ref.Name().IsBranch() {
branches = append(branches, ref.Name().Short())
}
}

return branches, nil
}

func resolveRef(repo, version string, creds *auth.Credentials) (string, error) {
if len(version) == 0 {
return resolveDefaultBranchRef(repo, creds)
Expand Down
15 changes: 15 additions & 0 deletions cyclops-ctrl/pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package template

import (
"fmt"
gitproviders2 "github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/template/gitproviders"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/auth"

Expand Down Expand Up @@ -29,6 +30,7 @@ type ITemplateRepo interface {
version string,
source cyclopsv1alpha1.TemplateSourceType,
) (map[string]interface{}, error)
GetTemplateRevisions(repo string) ([]string, error)
ReturnCache() *ristretto.Cache
}

Expand Down Expand Up @@ -179,6 +181,19 @@ func (r Repo) assumeTemplateSourceType(repo string) (cyclopsv1alpha1.TemplateSou
return cyclopsv1alpha1.TemplateSourceTypeGit, nil
}

func (r Repo) GetTemplateRevisions(repo string) ([]string, error) {
if !gitproviders2.IsGitHubSource(repo) {
return nil, nil
}

creds, err := r.credResolver.RepoAuthCredentials(repo)
if err != nil {
return nil, err
}

return r.listRemoteRefs(repo, creds)
}

func (r Repo) ReturnCache() *ristretto.Cache {
return r.cache.ReturnCache()
}
35 changes: 33 additions & 2 deletions cyclops-ui/src/components/pages/TemplateStore/TemplateStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Popover,
Checkbox,
Switch,
AutoComplete,
} from "antd";
import axios from "axios";
import {
Expand Down Expand Up @@ -66,6 +67,9 @@ const TemplateStore = () => {
const [templateSourceTypeFilter, setTemplateSourceTypeFilter] =
useState<string[]>(sourceTypeFilter);

const [repoRevisions, setRepoRevisions] = useState<string[]>([]);
const [repoRevisionOptions, setRepoRevisionOptions] = useState([]);

const [addForm] = Form.useForm();
const [editForm] = Form.useForm();
const [notificationApi, contextHolder] = notification.useNotification();
Expand Down Expand Up @@ -346,6 +350,27 @@ const TemplateStore = () => {
);
};

const fetchRepoRevisions = (e) => {
axios
.get(`/api/templates/revisions?repo=` + e.target.value)
.then((res) => {
setRepoRevisions(res.data);
})
.catch(() => {});
};

const handleRepoInput = (value) => {
if (repoRevisions.length === 0) {
setRepoRevisionOptions([]);
return;
}

const filtered = repoRevisions
.filter((item) => item.toLowerCase().includes(value.toLowerCase()))
.map((item) => ({ value: item }));
setRepoRevisionOptions(filtered);
};

return (
<div>
{error.message.length !== 0 && (
Expand Down Expand Up @@ -636,7 +661,7 @@ const TemplateStore = () => {
rules={[{ required: true, message: "Repo URL is required" }]}
style={{ marginBottom: "12px" }}
>
<Input />
<Input onBlur={fetchRepoRevisions} />
</Form.Item>

<Form.Item
Expand All @@ -653,7 +678,13 @@ const TemplateStore = () => {
name={["ref", "version"]}
style={{ marginBottom: "12px" }}
>
<Input />
<AutoComplete
options={repoRevisionOptions}
onSearch={handleRepoInput}
allowClear
>
<Input />
</AutoComplete>
</Form.Item>

{advancedTemplateGitOpsWrite()}
Expand Down