Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make controller swagger/openid friendly #240

Merged
merged 2 commits into from
Oct 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ public IActionResult GetLocalCheckout([FromQuery] string format, [FromQuery] str
}

[HttpPost("{scopeIdentifier}/{hubName}/{planId}")]
public async Task<IActionResult> Get(Guid scopeIdentifier, string hubName, Guid planId)
public async Task<IActionResult> Get(Guid scopeIdentifier, string hubName, Guid planId, [FromBody, Vss] ActionReferenceList reflist)
{
var localcheckout = User.FindFirst("localcheckout")?.Value ?? "";
var runid = User.FindFirst("runid")?.Value ?? "";
var token = User.FindFirst("github_token")?.Value ?? "";
if(!string.IsNullOrEmpty(token)) {
GITHUB_TOKEN = token;
}
ActionReferenceList reflist = await FromBody<ActionReferenceList>();
var repository = User.FindFirst("repository")?.Value;
var defGhToken = await CreateGithubAppToken(repository);
try {
Expand Down
10 changes: 6 additions & 4 deletions src/Runner.Server/Controllers/AgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Runner.Server.Models;
using Swashbuckle.AspNetCore.Annotations;

namespace Runner.Server.Controllers
{
Expand All @@ -39,8 +40,8 @@ public AgentController(IMemoryCache cache, SqLiteDb context, IConfiguration conf

[HttpPost("{poolId}")]
[Authorize(AuthenticationSchemes = "Bearer", Policy = "AgentManagement")]
public async Task<IActionResult> Post(int poolId) {
TaskAgent agent = await FromBody<TaskAgent>();
[SwaggerResponse(200, type: typeof(TaskAgent))]
public async Task<IActionResult> Post(int poolId, [FromBody, Vss] TaskAgent agent) {
lock(lok) {
// Without a lock we get rsa message exchange problems, decryption error of rsa encrypted session aes key
agent.Authorization.AuthorizationUrl = new Uri(new Uri(ServerUrl), "_apis/v1/auth/");
Expand All @@ -52,6 +53,7 @@ public async Task<IActionResult> Post(int poolId) {
}

[HttpGet("{poolId}/{agentId}")]
[SwaggerResponse(200, type: typeof(TaskAgent))]
public async Task<ActionResult> Get(int poolId, int agentId)
{
return await Ok(Agent.GetAgent(_cache, _context, poolId, agentId).TaskAgent);
Expand All @@ -71,9 +73,8 @@ public IActionResult Delete(int poolId, int agentId)

[HttpPut("{poolId}/{agentId}")]
[Authorize(AuthenticationSchemes = "Bearer", Policy = "AgentManagement")]
public async Task<ActionResult> Replace(int poolId, int agentId)
public async Task<ActionResult> Replace(int poolId, int agentId, [FromBody, Vss] TaskAgent tagent)
{
TaskAgent tagent = await FromBody<TaskAgent>();
lock(lok) {
var agent = Agent.GetAgent(_cache, _context, poolId, agentId);
agent.TaskAgent.Authorization = new TaskAgentAuthorization() { ClientId = agent.ClientId, PublicKey = new TaskAgentPublicKey(agent.Exponent, agent.Modulus), AuthorizationUrl = new Uri(new Uri(ServerUrl), "_apis/v1/auth/") };
Expand Down Expand Up @@ -105,6 +106,7 @@ public async Task<ActionResult> Replace(int poolId, int agentId)
}

[HttpGet("{poolId}")]
[SwaggerResponse(200, type: typeof(VssJsonCollectionWrapper<IEnumerable<TaskAgent>>))]
public async Task<ActionResult> Get(int poolId, [FromQuery] string agentName)
{
return await Ok(new VssJsonCollectionWrapper<IEnumerable<TaskAgent>>(from agent in _context.Agents where (poolId == 0 || poolId == -1 || agent.Pool.Id == poolId) && (string.IsNullOrEmpty(agentName) || agent.TaskAgent.Name == agentName) select agent.TaskAgent));
Expand Down
2 changes: 2 additions & 0 deletions src/Runner.Server/Controllers/AgentPoolsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Runner.Server.Models;
using Swashbuckle.AspNetCore.Annotations;

namespace Runner.Server.Controllers
{
Expand Down Expand Up @@ -42,6 +43,7 @@ public AgentPoolsController(IMemoryCache cache, SqLiteDb db, IConfiguration conf
}

[HttpGet]
[SwaggerResponse(200, type: typeof(VssJsonCollectionWrapper<IEnumerable<TaskAgentPool>>))]
public Task<FileStreamResult> Get(string poolName = "", string properties = "", string poolType = "")
{
return Ok((from pool in db.Pools.Include(a => a.TaskAgentPool).AsEnumerable() select pool.TaskAgentPool).ToList());
Expand Down
3 changes: 1 addition & 2 deletions src/Runner.Server/Controllers/AgentRequestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public Task<FileStreamResult> GetAgentRequest(int poolId, long requestId, string
}

[HttpPatch("{poolId}/{requestId}")]
public async Task<IActionResult> UpdateAgentRequest(int poolId, long requestId)
public async Task<IActionResult> UpdateAgentRequest(int poolId, long requestId, [FromBody, Vss] TaskAgentJobRequest patch)
{
var patch = await FromBody<TaskAgentJobRequest>();
patch.LockedUntil = DateTime.UtcNow.AddDays(1);
return await Ok(patch);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Runner.Server/Controllers/AgentSessionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Runner.Server.Models;
using Swashbuckle.AspNetCore.Annotations;

namespace Runner.Server.Controllers
{
Expand All @@ -36,9 +37,9 @@ public AgentSessionController(IMemoryCache cache, SqLiteDb context, IConfigurati
}

[HttpPost("{poolId}")]
public async Task<IActionResult> Create(int poolId)
[SwaggerResponse(200, type: typeof(TaskAgentSession))]
public async Task<IActionResult> Create(int poolId, [FromBody, Vss] TaskAgentSession session)
{
var session = await FromBody<TaskAgentSession>();
session.SessionId = Guid.NewGuid();
var aes = Aes.Create();
Agent agent = Agent.GetAgent(_cache, _context, poolId, session.Agent.Id);
Expand Down
8 changes: 3 additions & 5 deletions src/Runner.Server/Controllers/ArtifactController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ArtifactController(SqLiteDb context, IConfiguration configuration) : base
Directory.CreateDirectory(_targetFilePath);
}

public async Task<ArtifactFileContainer> CreateContainer(long run, long attempt, CreateActionsStorageArtifactParameters req, long artifactsMinAttempt = -1) {
internal async Task<ArtifactFileContainer> CreateContainer(long run, long attempt, CreateActionsStorageArtifactParameters req, long artifactsMinAttempt = -1) {
var filecontainer = (from fileContainer in _context.ArtifactFileContainer where fileContainer.Container.Attempt.Attempt == attempt && fileContainer.Container.Attempt.WorkflowRun.Id == run && fileContainer.Id == req.ContainerId select fileContainer).Include(f => f.Container).Include(f => f.Files).FirstOrDefault();
if(filecontainer != null) {
var files = filecontainer.Files;
Expand Down Expand Up @@ -99,8 +99,7 @@ public async Task<ArtifactFileContainer> CreateContainer(long run, long attempt,
}

[HttpPost("{run}/artifacts")]
public async Task<FileStreamResult> CreateContainer(long run) {
var req = await FromBody<CreateActionsStorageArtifactParameters>();
public async Task<FileStreamResult> CreateContainer(long run, [FromBody, Vss] CreateActionsStorageArtifactParameters req) {
var attempt = Int64.Parse(User.FindFirst("attempt")?.Value ?? "1");
var artifactsMinAttempt = Int64.Parse(User.FindFirst("artifactsMinAttempt")?.Value ?? "-1");
// azp build artifact / parse "{\"id\":0,\"name\":\"drop\",\"source\":null,\"resource\":{\"type\":\"Container\",\"data\":\"#/10/drop\",\"properties\":{\"localpath\":\"/home/christopher/.local/share/gharun/a/l53fnlmg.djp/w/1/a\",\"artifactsize\":\"28710\"}}}"
Expand All @@ -112,8 +111,7 @@ public async Task<FileStreamResult> CreateContainer(long run) {
}

[HttpPatch("{run}/artifacts")]
public async Task<FileStreamResult> PatchContainer(long run, [FromQuery] string artifactName) {
var req = await FromBody<CreateActionsStorageArtifactParameters>();
public async Task<FileStreamResult> PatchContainer(long run, [FromQuery] string artifactName, [FromBody, Vss] CreateActionsStorageArtifactParameters req) {
var attempt = Int64.Parse(User.FindFirst("attempt")?.Value ?? "1");
var artifactsMinAttempt = Int64.Parse(User.FindFirst("artifactsMinAttempt")?.Value ?? "-1");
var container = (from fileContainer in _context.ArtifactFileContainer where (fileContainer.Container.Attempt.Attempt >= artifactsMinAttempt || artifactsMinAttempt == -1) && fileContainer.Container.Attempt.Attempt <= attempt && fileContainer.Container.Attempt.WorkflowRun.Id == run && fileContainer.Name.ToLower() == artifactName.ToLower() select fileContainer).First();
Expand Down
8 changes: 5 additions & 3 deletions src/Runner.Server/Controllers/CacheController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Configuration;
using Runner.Server.Models;
using Swashbuckle.AspNetCore.Annotations;

namespace Runner.Server.Controllers {

Expand All @@ -34,7 +35,7 @@ private class ReserveCacheResponse {
public int cacheId {get;set;}
}

private class ReserveCacheRequest {
public class ReserveCacheRequest {
public string key {get;set;}
public string version {get;set;}
}
Expand All @@ -47,8 +48,8 @@ public CacheController(SqLiteDb context, IWebHostEnvironment environment, IConfi
}

[HttpPost("caches")]
public async Task<FileStreamResult> ReserveCache(string owner, string repo) {
var req = await FromBody<ReserveCacheRequest>();
[SwaggerResponse(200, type: typeof(ReserveCacheResponse))]
public async Task<FileStreamResult> ReserveCache(string owner, string repo, [FromBody, Vss] ReserveCacheRequest req) {
var filename = Path.GetRandomFileName();
var reference = User.FindFirst("ref")?.Value ?? "refs/heads/main";
var repository = User.FindFirst("repository")?.Value ?? "Unknown/Unknown";
Expand All @@ -59,6 +60,7 @@ public async Task<FileStreamResult> ReserveCache(string owner, string repo) {
}

[HttpGet("cache")]
[SwaggerResponse(200, type: typeof(ArtifactCacheEntry))]
public async Task<IActionResult> GetCacheEntry( string owner, string repo, [FromQuery] string keys, [FromQuery] string version) {
var a = keys.Split(',');
var defaultRef = User.FindFirst("defaultRef")?.Value ?? "refs/heads/main";
Expand Down
2 changes: 2 additions & 0 deletions src/Runner.Server/Controllers/ConnectionDataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Swashbuckle.AspNetCore.Annotations;

namespace Runner.Server.Controllers
{
Expand All @@ -22,6 +23,7 @@ public ConnectionDataController(IConfiguration conf) : base(conf) {

[HttpGet]
[AllowAnonymous]
[SwaggerResponse(200, type: typeof(ConnectionData))]
public async Task<IActionResult> Get()
{
return await Ok(new ConnectionData() {
Expand Down
7 changes: 3 additions & 4 deletions src/Runner.Server/Controllers/FinishJobController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -38,7 +38,7 @@ public FinishJobController(IMemoryCache cache, SqLiteDb context, IConfiguration
public static event JobAssigned OnJobAssigned;
public static event JobStarted OnJobStarted;

public void InvokeJobCompleted(JobCompletedEvent ev) {
internal void InvokeJobCompleted(JobCompletedEvent ev) {
try {
{
var job = _cache.Get<Job>(ev.JobId);
Expand Down Expand Up @@ -81,9 +81,8 @@ public void InvokeJobCompleted(JobCompletedEvent ev) {

[HttpPost("{scopeIdentifier}/{hubName}/{planId}")]
[Authorize(AuthenticationSchemes = "Bearer", Policy = "AgentJob")]
public async Task<IActionResult> OnEvent(Guid scopeIdentifier, string hubName, Guid planId)
public IActionResult OnEvent(Guid scopeIdentifier, string hubName, Guid planId, [FromBody, Vss] JobEvent jevent)
{
var jevent = await FromBody<JobEvent>();
if (jevent is JobCompletedEvent ev) {
InvokeJobCompleted(ev);
return Ok();
Expand Down
5 changes: 3 additions & 2 deletions src/Runner.Server/Controllers/LogfilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Runner.Server.Models;
using Swashbuckle.AspNetCore.Annotations;

namespace Runner.Server.Controllers
{
Expand All @@ -31,9 +32,9 @@ public LogfilesController(SqLiteDb context, IConfiguration conf) : base(conf)
}
[HttpPost("{scopeIdentifier}/{hubName}/{planId}")]
[Authorize(AuthenticationSchemes = "Bearer", Policy = "AgentJob")]
public async Task<IActionResult> CreateLog(Guid scopeIdentifier, string hubName, Guid planId)
[SwaggerResponse(200, type: typeof(TaskLog))]
public async Task<IActionResult> CreateLog(Guid scopeIdentifier, string hubName, Guid planId, [FromBody, Vss] TaskLog log)
{
var log = await FromBody<TaskLog>();
_context.Logs.Add(new SqLiteDb.LogStorage() { Ref = log });
await _context.SaveChangesAsync();
return await Ok(log);
Expand Down
20 changes: 17 additions & 3 deletions src/Runner.Server/Controllers/MessageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
using System.Reflection;
using Quartz;
using Quartz.Impl.Matchers;
using Swashbuckle.AspNetCore.Annotations;

namespace Runner.Server.Controllers
{
Expand Down Expand Up @@ -4866,7 +4867,7 @@ public async Task UploadFile(Guid id) {
sh.Token.Cancel();
}

public async Task<string> GetFile(long runid, string path, string repository = null) {
internal async Task<string> GetFile(long runid, string path, string repository = null) {
var sh = new shared2();
sh.Token = new CancellationTokenSource(20 * 1000);
Guid id = Guid.NewGuid();
Expand All @@ -4877,7 +4878,7 @@ public async Task<string> GetFile(long runid, string path, string repository = n
return sh.Content;
}

public bool TryGetFile(long runid, string path, out string content, string repository = null) {
internal bool TryGetFile(long runid, string path, out string content, string repository = null) {
content = GetFile(runid, path, repository).GetAwaiter().GetResult();
return content != null;
}
Expand All @@ -4892,7 +4893,7 @@ public void UpRepoExists(Guid id) {
sh.Token.Cancel();
}

public async Task<bool> RepoExists(long runid, string repository = null) {
internal async Task<bool> RepoExists(long runid, string repository = null) {
var sh = new shared2();
sh.Token = new CancellationTokenSource(20 * 1000);
Guid id = Guid.NewGuid();
Expand Down Expand Up @@ -6395,6 +6396,9 @@ public async Task<IActionResult> IsAgentOnline([FromQuery] string name)

[HttpGet("{poolId}")]
[Authorize(AuthenticationSchemes = "Bearer", Policy = "Agent")]
[SwaggerResponse(200, type: typeof(TaskAgentMessage))]
[SwaggerResponse(204, "No message for 50s")]
[SwaggerResponse(403, type: typeof(WrappedException))]
public async Task<IActionResult> GetMessage(int poolId, Guid sessionId)
{
Session session;
Expand Down Expand Up @@ -7219,6 +7223,7 @@ public async Task<IActionResult> OnSchedule2([FromQuery] string job, [FromQuery]
private static ConcurrentDictionary<Guid, Job> initializingJobs = new ConcurrentDictionary<Guid, Job>();

[HttpGet]
[SwaggerResponse(200, type: typeof(Job[]))]
public async Task<IActionResult> GetJobs([FromQuery] string repo, [FromQuery] long[] runid, [FromQuery] int? depending, [FromQuery] Guid? jobid, [FromQuery] int? page) {
if(jobid != null) {
var j = GetJob(jobid.Value);
Expand All @@ -7235,41 +7240,48 @@ public async Task<IActionResult> GetJobs([FromQuery] string repo, [FromQuery] lo
}

[HttpGet("owners")]
[SwaggerResponse(200, type: typeof(Owner[]))]
public async Task<IActionResult> GetOwners([FromQuery] int? page) {
var query = from j in _context.Set<Owner>() orderby j.Id descending select j;
return await Ok(page.HasValue ? query.Skip(page.Value * 30).Take(30) : query, true);
}

[HttpGet("repositories")]
[SwaggerResponse(200, type: typeof(Repository[]))]
public async Task<IActionResult> GetRepositories([FromQuery] int? page, [FromQuery] string owner) {
var query = from j in _context.Set<Repository>() where j.Owner.Name.ToLower() == owner.ToLower() orderby j.Id descending select j;
return await Ok(page.HasValue ? query.Skip(page.Value * 30).Take(30) : query, true);
}

[HttpGet("workflow/runs")]
[SwaggerResponse(200, type: typeof(WorkflowRun[]))]
public async Task<IActionResult> GetWorkflows([FromQuery] int? page, [FromQuery] string owner, [FromQuery] string repo) {
var query = (from run in _context.Set<WorkflowRun>() from attempt in _context.Set<WorkflowRunAttempt>() where run.Id == attempt.WorkflowRun.Id && attempt.Attempt == (from a in _context.Set<WorkflowRunAttempt>() where run.Id == a.WorkflowRun.Id orderby a.Attempt descending select a.Attempt).First() && (string.IsNullOrEmpty(owner) || run.Workflow.Repository.Owner.Name.ToLower() == owner.ToLower()) && (string.IsNullOrEmpty(repo) || run.Workflow.Repository.Name.ToLower() == repo.ToLower()) orderby run.Id descending select new WorkflowRun() { EventName = attempt.EventName, Ref = attempt.Ref, Sha = attempt.Sha, Result = attempt.Result, FileName = run.FileName, DisplayName = run.DisplayName, Id = run.Id, Owner = run.Workflow.Repository.Owner.Name, Repo = run.Workflow.Repository.Name});
return await Ok(page.HasValue ? query.Skip(page.Value * 30).Take(30) : query, true);
}

[HttpGet("workflow/run/{id}")]
[SwaggerResponse(200, type: typeof(WorkflowRun))]
public async Task<IActionResult> GetWorkflows(long id) {
return await Ok((from r in _context.Set<WorkflowRun>() where r.Id == id select r).First(), true);
}

[HttpGet("workflow/run/{id}/attempts")]
[SwaggerResponse(200, type: typeof(WorkflowRunAttempt[]))]
public async Task<IActionResult> GetWorkflowAttempts(long id, [FromQuery] int? page) {
var query = from r in _context.Set<WorkflowRunAttempt>() where r.WorkflowRun.Id == id select r;
return await Ok(page.HasValue ? query.Skip(page.Value * 30).Take(30) : query, true);
}

[HttpGet("workflow/run/{id}/attempt/{attempt}")]
[SwaggerResponse(200, type: typeof(WorkflowRunAttempt))]
public async Task<IActionResult> GetWorkflows(long id, int attempt) {
var query = from r in _context.Set<WorkflowRunAttempt>() where r.WorkflowRun.Id == id && r.Attempt == attempt select r;
return await Ok(query.First(), true);
}

[HttpGet("workflow/run/{id}/attempt/{attempt}/jobs")]
[SwaggerResponse(200, type: typeof(Job[]))]
public async Task<IActionResult> GetWorkflowAttempts(long id, int attempt, [FromQuery] int? page) {
var query = from j in _context.Jobs where j.WorkflowRunAttempt.WorkflowRun.Id == id && j.WorkflowRunAttempt.Attempt == attempt select j;
return await Ok(page.HasValue ? query.Skip(page.Value * 30).Take(30) : query, true);
Expand Down Expand Up @@ -7515,6 +7527,7 @@ public class WorkflowEventArgs {
public static event Action<WorkflowEventArgs> workflowevent;

[HttpGet("event")]
[SwaggerResponse(200, contentTypes: new[]{"text/event-stream"})]
public IActionResult Message(string owner, string repo, [FromQuery] string filter, [FromQuery] long? runid)
{
var mfilter = new WorkflowPattern(filter ?? (owner + "/" + repo), RegexOptions.IgnoreCase);
Expand Down Expand Up @@ -7556,6 +7569,7 @@ public IActionResult Message(string owner, string repo, [FromQuery] string filte
}

[HttpGet("event2")]
[SwaggerResponse(200, contentTypes: new[]{"text/event-stream"})]
public IActionResult LiveUpdateEvents([FromQuery] string owner, [FromQuery] string repo, [FromQuery] long? runid)
{
var requestAborted = HttpContext.RequestAborted;
Expand Down
Loading
Loading