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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
namespace marlin.web.Controllers;
using System.Text.Json;
using core;
using core.models;
using Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Models;
using Services;
[Route("vcs")]
public class VCSController(ApplicationDbContext dbContext, VersionControlSyncService syncService) : Controller
{
[HttpGet("list")]
public async Task<IActionResult> List(CancellationToken ct)
{
var items = await dbContext.Sources
.Include(x => x.Branches)
.ToListAsync(ct);
return this.View(items);
}
public record SecretOption(Guid Id, string Label);
[HttpGet("details/{id}")]
public async Task<IActionResult> Details(Guid id, CancellationToken ct)
{
var item = await dbContext
.Sources
.Include(x => x.Branches)
.ThenInclude(x => x.Commit)
.ThenInclude(x => x.WorkflowSnapshot)
.FirstOrDefaultAsync(x => x.Id == id, ct);
if (item == null)
return this.NotFound();
var secretOptions = await dbContext.Secrets
.Where(x => x.SourceId == id)
.Select(x => new SecretOption(x.Id, x.Label))
.ToListAsync(ct);
return this.View((item, secretOptions));
}
[HttpGet("details/{sourceId}/{commitId}/blobs")]
public async Task<IActionResult> Blobs(Guid sourceId, Guid commitId)
{
var blobs = await dbContext
.SourceBlobs
.Include(x => x.Commit)
.ThenInclude(x => x.Source)
.Where(x => x.CommitId == commitId && x.SourceId == sourceId)
.ToListAsync();
if (!blobs.Any())
return this.NotFound();
return this.View(blobs);
}
[HttpGet("details/{sourceId}/{commitId}/workflow")]
public async Task<IActionResult> Workflow(Guid sourceId, Guid commitId)
{
var snapshot = await dbContext
.WorkflowSnapshots
.Include(x => x.Commit)
.ThenInclude(x => x.Source)
.FirstOrDefaultAsync(x => x.CommitId == commitId && x.Commit.SourceId == sourceId);
if (snapshot == null)
return this.NotFound();
return this.View(snapshot);
}
[HttpPost("edit/{id}")]
public async Task<IActionResult> Edit(Guid id, VersionControlSource vcs, CancellationToken ct)
{
var item = await dbContext.Sources
.FirstOrDefaultAsync(x => x.Id == id, ct);
if (item == null)
return this.NotFound();
item.UpstreamUrl = vcs.UpstreamUrl;
item.BranchFilter = vcs.BranchFilter;
item.MainBranch = vcs.MainBranch;
item.MinPollingInterval = vcs.MinPollingInterval;
item.AccessSecretId = vcs.AccessSecretId;
await dbContext.SaveChangesAsync(ct);
await syncService.FetchSources(ct);
return this.RedirectToAction("List");
}
[HttpGet("create")]
public async Task<IActionResult> Create()
{
return this.View();
}
[HttpPost("create")]
public async Task<IActionResult> CreatePost(VersionControlSource vcs, CancellationToken ct)
{
vcs.Id = Guid.Empty;
await dbContext.AddAsync(vcs, ct);
await dbContext.SaveChangesAsync(ct);
await syncService.FetchSources(ct);
return this.RedirectToAction("Details", new { id = vcs.Id });
}
public record SecretCreateRequest(string Label, string? Kind, bool MayReveal, string ContentJson);
[HttpGet("details/{sourceId}/secrets/create")]
public async Task<IActionResult> CreateSecret(Guid sourceId, CancellationToken ct)
{
var source = await dbContext.Sources
.FirstOrDefaultAsync(x => x.Id == sourceId, ct);
if (source == null)
return this.NotFound();
return this.View(source);
}
[HttpPost("details/{sourceId}/secrets/create")]
public async Task<IActionResult> CreateSecretPost(Guid sourceId, SecretCreateRequest request, CancellationToken ct)
{
var source = await dbContext.Sources
.FirstOrDefaultAsync(x => x.Id == sourceId, ct);
if (source == null)
return this.NotFound();
var content = JsonSerializer.Deserialize<Dictionary<string, string>>(request.ContentJson) ?? new();
var secret = new VersionControlSecret
{
SourceId = sourceId,
Label = request.Label,
Kind = request.Kind,
MayReveal = request.MayReveal,
Content = content
};
await dbContext.Secrets.AddAsync(secret, ct);
await dbContext.SaveChangesAsync(ct);
return this.RedirectToAction("ListSecret", new { sourceId });
}
[HttpGet("details/{sourceId}/secrets/list")]
public async Task<IActionResult> ListSecret(Guid sourceId, CancellationToken ct)
{
var source = await dbContext.Sources
.FirstOrDefaultAsync(x => x.Id == sourceId, ct);
if (source == null)
return this.NotFound();
var items = await dbContext.Secrets
.Where(x => x.SourceId == source.Id)
.ToListAsync<ISecret>(ct);
return this.View((source, items));
}
[HttpPost("details/{sourceId}/secrets/delete/{id}")]
public async Task<IActionResult> DeleteSecret(Guid sourceId, Guid id, CancellationToken ct)
{
var secret = await dbContext.Secrets
.FirstOrDefaultAsync(x => x.SourceId == sourceId && x.Id == id, ct);
if (secret == null)
return this.NotFound();
dbContext.Secrets.Remove(secret);
await dbContext.SaveChangesAsync(ct);
return this.RedirectToAction("ListSecret", new { sourceId = sourceId });
}
}
|