Can display extraction logs

This commit is contained in:
nemunaire 2022-09-05 04:58:48 +02:00
parent eacaedeb03
commit 0b16676929
3 changed files with 84 additions and 19 deletions

View File

@ -205,6 +205,35 @@ func declareAPIAuthRepositoriesRoutes(router *gin.RouterGroup) {
c.JSON(http.StatusOK, result)
})
repositoriesRoutes.GET("/state-logs", func(c *gin.Context) {
repo := c.MustGet("repository").(*Repository)
tmp := strings.Split(repo.DroneRef, "/")
if len(tmp) < 3 {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "No build number. Please try pulling your work."})
return
}
nbuild, err := strconv.Atoi(tmp[2])
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Bad build number. Please retry pulling your work."})
return
}
client := drone.NewClient(droneEndpoint, droneConfig)
result, err := client.Logs(tmp[0], tmp[1], nbuild, 1, 2)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Unable to retrieve logs."})
return
}
if len(result) > 7 {
c.JSON(http.StatusOK, result[7:])
} else {
c.JSON(http.StatusOK, result)
}
})
}
func repositoryHandler(c *gin.Context) {

View File

@ -12,11 +12,27 @@
let repo_work = null;
let repo_used = null;
let remote_repos = [];
let repo_pull_state = null;
let show_logs = null;
function updatePullState(repo) {
repo_pull_state = repo.getBuildState();
repo_pull_state.then((state) => {
if (state.status == "pending" || state.status == "running") {
setTimeout(() => updatePullState(repo), state.status == "pending" ? 1000 : 2500);
}
})
}
function showLogs(repo) {
show_logs = repo.getBuildLogs()
}
function refresh_repo_work() {
repo_work = getRepositories(work.id);
repo_work.then((repos) => {
repo_used = repos[0];
updatePullState(repos[0])
}, () => {
repo_used = new WorkRepository({id_work: work.id});
remote_repos = getRemoteRepositories();
@ -52,25 +68,27 @@
<div>
Dépôt lié&nbsp;: <strong style="font-family: monospace">{repo.uri}</strong><br>
Dernière récupération&nbsp;: <strong>{#if repo.last_check}<DateFormat date={new Date(repo.last_check)} dateStyle="medium" timeStyle="medium" />{:else}-{/if}</strong>
{#await repo.getBuildState()}
<div class="spinner-grow spinner-grow-sm mx-1" role="status"></div>
{:then state}
{#if state.status == "pending" || state.status == "running"}
<div
class="spinner-grow spinner-grow-sm mx-1"
class:text-secondary={state.status == "pending"}
class:text-warning={state.status == "running"}
title="La récupération est en cours"
role="status"
></div>
{:else if state.status == "success"}
<i class="bi bi-check-circle-fill text-success mx-1" title="La récupération s'est bien passée"></i>
{:else if state.status == "failure"}
<i class="bi bi-exclamation-circle-fill text-danger mx-1" title="La récupération ne s'est pas bien passée"></i>
{:else}
{state.status}
{/if}
{/await}
{#if repo_pull_state}
{#await repo_pull_state}
<div class="spinner-grow spinner-grow-sm mx-1" role="status"></div>
{:then state}
{#if state.status == "pending" || state.status == "running"}
<div
class="spinner-grow spinner-grow-sm mx-1"
class:text-secondary={state.status == "pending"}
class:text-warning={state.status == "running"}
title="La récupération est en cours"
role="status"
></div>
{:else if state.status == "success"}
<i class="bi bi-check-circle-fill text-success mx-1" title="La récupération s'est bien passée"></i>
{:else if state.status == "failure"}
<i class="bi bi-exclamation-circle-fill text-danger mx-1" title="La récupération ne s'est pas bien passée" style="cursor: pointer" on:click={() => showLogs(repo)}></i>
{:else}
{state.status}
{/if}
{/await}
{/if}
</div>
</div>
<div class="d-flex flex-column justify-content-center">
@ -92,6 +110,13 @@
</button>
</div>
</div>
{#if show_logs}
{#await show_logs then logs}
<div class="card-body d-flex justify-content-between bg-dark text-light">
<pre class="pb-2">{#each logs as l (l.pos)}{l.out}{/each}</pre>
</div>
{/await}
{/if}
</div>
{/each}
{:catch}

View File

@ -36,6 +36,17 @@ export class WorkRepository {
}
}
async getBuildLogs() {
const res = await fetch(`api/repositories/${this.id}/state-logs`, {
headers: {'Accept': 'application/json'}
});
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
async retrieveWork() {
const res = await fetch(`api/repositories/${this.id}/trigger`, {
method: 'POST',