summaryrefslogtreecommitdiff
path: root/src/marlin.web/Controllers/JobsController.cs
blob: 9aaf6eb9cbde4dc088e3f9bdd3af52bf8ecaa931 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
namespace marlin.web.Controllers;

using core.models;
using Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Models;
using Util;

[Route("jobs")]
public class JobsController(ApplicationDbContext dbContext) : Controller
{
    // list jobs (pending, active, cancelled, success, failed), filter to vcs, filter to recipe
    public record JobListFilter(JobStatus? Status = null, Guid? Vcs = null, Guid? Branch = null, string? Recipe = null, Guid? Agent = null);

    [HttpGet("list")]
    public async Task<IActionResult> List([FromQuery] JobListFilter filter, CancellationToken ct)
    {
        var results = await dbContext
            .Jobs
            .WhereIf(filter.Status != null, x => x.Status == filter.Status)
            .WhereIf(filter.Vcs != null, x => x.SourceId == filter.Vcs)
            .WhereIf(filter.Branch != null, x => x.BranchId == filter.Branch)
            .WhereIf(filter.Recipe != null, x => x.Recipe == filter.Recipe)
            .WhereIf(filter.Agent != null, x => x.AgentId == filter.Agent)
            .OrderByDescending(x => x.CreatedAt)

            .Include(x => x.Commit)
            .Include(x => x.Branch)
            .Include(x => x.Source)
            .Include(x => x.Agent)
            .ToListAsync(ct);

        return this.View(results);
    }

    public record JobSpawnDetails(
        ICollection<VersionControlSource> Sources,
        Guid? Vcs = null,
        string? Ref = null,
        string? Commit = null,
        string? Recipe = null);
    
    [HttpGet("spawn")]
    public async Task<IActionResult> Spawn(CancellationToken ct)
    {
        var sources = await dbContext
            .Sources
            .Include(x => x.Branches)
            .ThenInclude(x => x.Commit)
            .ThenInclude(x => x.WorkflowSnapshot)
            .ToListAsync(ct);
        
        var details = new JobSpawnDetails(sources);
        return this.View(details);
    }

    public record JobSpawnRequest(Guid Vcs, Guid Branch, string Recipe);
    
    [HttpPost("spawn")]
    public async Task<IActionResult> SpawnPost(JobSpawnRequest request, CancellationToken ct)
    {
        var branch = await dbContext
            .SourceBranches
            .FirstOrDefaultAsync(x => x.Id == request.Branch, ct);

        if (branch == null)
            return this.NotFound();
        
        Job job = new()
        {
            SourceId = branch.SourceId,
            BranchId = branch.Id,
            CommitId = branch.CommitId,
            Recipe = request.Recipe,
            
            CreatedAt = DateTime.UtcNow,
            CreatedReason = "user created",
            Status = JobStatus.Pending
        };

        await dbContext.Jobs.AddAsync(job, ct);
        await dbContext.SaveChangesAsync(ct);

        return this.RedirectToAction("Details", new { id = job.Id });
    }
    
    // cancel job
    
    // details job (recipe, status, agent, target, plan, log, artifacts)
    [HttpGet("details/{id}")]
    public async Task<IActionResult> Details(Guid id)
    {
        var job = await dbContext.Jobs
            .Include(x => x.Agent)
            .Include(x => x.Source)
            .Include(x => x.StatusUpdates)
            .Include(x => x.Logs)
            .Include(x => x.Branch)
            .Include(x => x.Commit)
            .AsSplitQuery()
            .FirstOrDefaultAsync(x => x.Id == id);

        if (job == null)
            return this.NotFound();

        return this.View(job);
    }
}