summaryrefslogtreecommitdiff
path: root/src/marlin.web/Views/Jobs/Spawn.cshtml
blob: 8f7ee43fc10d90a36a6d45604ea8f17c38878c5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@using System.Linq
@using System.Text.Json
@using marlin.web.Controllers
@model marlin.web.Controllers.JobsController.JobSpawnDetails

@{
    var sourcesJson = JsonSerializer.Serialize(Model.Sources.Select(s => new
    {
        id = s.Id,
        label = s.UpstreamUrl,
        mainBranch = s.MainBranch,
        branches = s.Branches.Select(b => new { name = b.Name, commit = b.Commit.Hash })
    }));
}

<form method="post" asp-action="Spawn">
    VCS: <select id="vcs" name="@nameof(JobsController.JobSpawnRequest.Vcs)"></select><br/>
    Ref: <select id="ref" name="@nameof(JobsController.JobSpawnRequest.Ref)"></select><br/>
    Commit: <input type="text" id="commit" name="@nameof(JobsController.JobSpawnRequest.Commit)" readonly/><br/>
    Recipe: <input type="text" name="@nameof(JobsController.JobSpawnRequest.Recipe)" value="@Model.Recipe"/><br/>
    <button type="submit">Save</button>
</form>

<script>
    const sources = @Html.Raw(sourcesJson);
    const vcsSelect = document.getElementById('vcs');
    const refSelect = document.getElementById('ref');
    const commitInput = document.getElementById('commit');

    for (const s of sources) {
        vcsSelect.add(new Option(s.label, s.id));
    }

    function currentSource() {
        return sources.find(s => s.id === vcsSelect.value);
    }

    function populateRefs() {
        refSelect.innerHTML = '';
        for (const b of currentSource()?.branches ?? []) {
            refSelect.add(new Option(b.name, b.name));
        }
        refSelect.value = currentSource()?.mainBranch ?? '';
        populateCommit();
    }

    function populateCommit() {
        const branch = currentSource()?.branches.find(b => b.name === refSelect.value);
        commitInput.value = branch?.commit ?? '';
    }

    vcsSelect.addEventListener('change', populateRefs);
    refSelect.addEventListener('change', populateCommit);

    populateRefs();
</script>