Skip to content

Commit eec8fe6

Browse files
authored
suggest branches on template ref creation #849
suggest branches on template ref creation
2 parents ce704ad + 0389f2b commit eec8fe6

File tree

6 files changed

+152
-2
lines changed

6 files changed

+152
-2
lines changed

cyclops-ctrl/internal/controller/templates.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,22 @@ func (c *Templates) DeleteTemplatesStore(ctx *gin.Context) {
207207

208208
ctx.Status(http.StatusOK)
209209
}
210+
211+
func (c *Templates) GetTemplateRevisions(ctx *gin.Context) {
212+
ctx.Header("Access-Control-Allow-Origin", "*")
213+
214+
repo := ctx.Query("repo")
215+
216+
if repo == "" {
217+
ctx.JSON(http.StatusOK, []string{})
218+
return
219+
}
220+
221+
revisions, err := c.templatesRepo.GetTemplateRevisions(repo)
222+
if err != nil {
223+
ctx.JSON(http.StatusBadRequest, dto.NewError("Error loading template", err.Error()))
224+
return
225+
}
226+
227+
ctx.JSON(http.StatusOK, revisions)
228+
}

cyclops-ctrl/internal/handler/handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func (h *Handler) Start() error {
7979
h.router.GET("/templates", templatesController.GetTemplate)
8080
h.router.GET("/templates/initial", templatesController.GetTemplateInitialValues)
8181

82+
h.router.GET("/templates/revisions", templatesController.GetTemplateRevisions)
83+
8284
// templates store
8385
h.router.GET("/templates/store", templatesController.ListTemplatesStore)
8486
h.router.PUT("/templates/store", templatesController.CreateTemplatesStore)

cyclops-ctrl/mocks/ITemplateRepo.go

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cyclops-ctrl/pkg/template/git.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,31 @@ func (r Repo) LoadInitialTemplateValues(repoURL, path, commit string) (map[strin
274274
return initialValues, nil
275275
}
276276

277+
func (r Repo) listRemoteRefs(repo string, creds *auth.Credentials) ([]string, error) {
278+
rem := git.NewRemote(memory.NewStorage(), &config.RemoteConfig{
279+
Name: "origin",
280+
URLs: []string{repo},
281+
})
282+
283+
refs, err := rem.List(&git.ListOptions{
284+
PeelingOption: git.AppendPeeled,
285+
Auth: httpBasicAuthCredentials(creds),
286+
})
287+
if err != nil {
288+
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))
289+
}
290+
291+
branches := make([]string, 0)
292+
293+
for _, ref := range refs {
294+
if ref.Name().IsBranch() {
295+
branches = append(branches, ref.Name().Short())
296+
}
297+
}
298+
299+
return branches, nil
300+
}
301+
277302
func resolveRef(repo, version string, creds *auth.Credentials) (string, error) {
278303
if len(version) == 0 {
279304
return resolveDefaultBranchRef(repo, creds)

cyclops-ctrl/pkg/template/template.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package template
22

33
import (
44
"fmt"
5+
gitproviders2 "github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/template/gitproviders"
56

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

@@ -29,6 +30,7 @@ type ITemplateRepo interface {
2930
version string,
3031
source cyclopsv1alpha1.TemplateSourceType,
3132
) (map[string]interface{}, error)
33+
GetTemplateRevisions(repo string) ([]string, error)
3234
ReturnCache() *ristretto.Cache
3335
}
3436

@@ -179,6 +181,19 @@ func (r Repo) assumeTemplateSourceType(repo string) (cyclopsv1alpha1.TemplateSou
179181
return cyclopsv1alpha1.TemplateSourceTypeGit, nil
180182
}
181183

184+
func (r Repo) GetTemplateRevisions(repo string) ([]string, error) {
185+
if !gitproviders2.IsGitHubSource(repo) {
186+
return nil, nil
187+
}
188+
189+
creds, err := r.credResolver.RepoAuthCredentials(repo)
190+
if err != nil {
191+
return nil, err
192+
}
193+
194+
return r.listRemoteRefs(repo, creds)
195+
}
196+
182197
func (r Repo) ReturnCache() *ristretto.Cache {
183198
return r.cache.ReturnCache()
184199
}

cyclops-ui/src/components/pages/TemplateStore/TemplateStore.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Popover,
1717
Checkbox,
1818
Switch,
19+
AutoComplete,
1920
} from "antd";
2021
import axios from "axios";
2122
import {
@@ -66,6 +67,9 @@ const TemplateStore = () => {
6667
const [templateSourceTypeFilter, setTemplateSourceTypeFilter] =
6768
useState<string[]>(sourceTypeFilter);
6869

70+
const [repoRevisions, setRepoRevisions] = useState<string[]>([]);
71+
const [repoRevisionOptions, setRepoRevisionOptions] = useState([]);
72+
6973
const [addForm] = Form.useForm();
7074
const [editForm] = Form.useForm();
7175
const [notificationApi, contextHolder] = notification.useNotification();
@@ -346,6 +350,27 @@ const TemplateStore = () => {
346350
);
347351
};
348352

353+
const fetchRepoRevisions = (e) => {
354+
axios
355+
.get(`/api/templates/revisions?repo=` + e.target.value)
356+
.then((res) => {
357+
setRepoRevisions(res.data);
358+
})
359+
.catch(() => {});
360+
};
361+
362+
const handleRepoInput = (value) => {
363+
if (repoRevisions.length === 0) {
364+
setRepoRevisionOptions([]);
365+
return;
366+
}
367+
368+
const filtered = repoRevisions
369+
.filter((item) => item.toLowerCase().includes(value.toLowerCase()))
370+
.map((item) => ({ value: item }));
371+
setRepoRevisionOptions(filtered);
372+
};
373+
349374
return (
350375
<div>
351376
{error.message.length !== 0 && (
@@ -636,7 +661,7 @@ const TemplateStore = () => {
636661
rules={[{ required: true, message: "Repo URL is required" }]}
637662
style={{ marginBottom: "12px" }}
638663
>
639-
<Input />
664+
<Input onBlur={fetchRepoRevisions} />
640665
</Form.Item>
641666

642667
<Form.Item
@@ -653,7 +678,13 @@ const TemplateStore = () => {
653678
name={["ref", "version"]}
654679
style={{ marginBottom: "12px" }}
655680
>
656-
<Input />
681+
<AutoComplete
682+
options={repoRevisionOptions}
683+
onSearch={handleRepoInput}
684+
allowClear
685+
>
686+
<Input />
687+
</AutoComplete>
657688
</Form.Item>
658689

659690
{advancedTemplateGitOpsWrite()}

0 commit comments

Comments
 (0)