qa: Refactor gitlab use

This commit is contained in:
nemunaire 2023-11-26 12:57:04 +01:00
parent c31f76e9c3
commit d4cad767eb
7 changed files with 145 additions and 54 deletions

View File

@ -195,41 +195,48 @@ func GitLab_GetMyToken(c *gin.Context) {
} }
type GitLabIssue struct { type GitLabIssue struct {
Title string Id int64 `json:"id,omitempty"`
Description string IId int64 `json:"iid,omitempty"`
ProjectId int64 `json:"project_id,omitempty"`
Title string `json:"title"`
Description string `json:"description"`
WebURL string `json:"web_url"`
} }
func gitlab_newIssue(ctx context.Context, token *oauth2.Token, exerciceid string, issue *GitLabIssue) error { func gitlab_newIssue(ctx context.Context, token *oauth2.Token, exerciceid string, issue *GitLabIssue) (*GitLabIssue, error) {
client := gitlaboauth2Config.Client(ctx, token) client := gitlaboauth2Config.Client(ctx, token)
params := url.Values{}
params.Set("title", "[QA] "+issue.Title)
params.Set("description", issue.Description)
enc, err := json.Marshal(issue) enc, err := json.Marshal(issue)
if err != nil { if err != nil {
return err return nil, err
} }
req, err := http.NewRequest("POST", gitlabBaseURL+"/api/v4/projects/"+url.QueryEscape(exerciceid)+"/issues?"+params.Encode(), bytes.NewBuffer(enc)) req, err := http.NewRequest("POST", gitlabBaseURL+"/api/v4/projects/"+url.QueryEscape(exerciceid)+"/issues", bytes.NewBuffer(enc))
if err != nil { if err != nil {
return err return nil, err
} }
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated { if resp.StatusCode != http.StatusCreated {
str, _ := io.ReadAll(resp.Body) str, _ := io.ReadAll(resp.Body)
return fmt.Errorf("Bad status code from the API: %d %s", resp.StatusCode, string(str)) return nil, fmt.Errorf("Bad status code from the API: %d %s", resp.StatusCode, string(str))
} }
return nil var issueCreated GitLabIssue
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&issueCreated)
if err != nil {
return nil, fmt.Errorf("Unable to decode issue JSON: %s", err.Error())
}
return &issueCreated, nil
} }
func GitLab_getExerciceId(exercice *fic.Exercice) (string, error) { func GitLab_getExerciceId(exercice *fic.Exercice) (string, error) {
@ -269,7 +276,7 @@ func GitLab_ExportQA(c *gin.Context) {
return return
} }
description := "<" + oidcRedirectURL + path.Join(c.MustGet("baseurl").(string), "exercices", fmt.Sprintf("%d", exercice.Id), fmt.Sprintf("%d", query.Id)) + ">" description := "<" + oidcRedirectURL + path.Join(path.Clean("/"+c.MustGet("baseurl").(string)), "exercices", fmt.Sprintf("%d", exercice.Id), fmt.Sprintf("%d", query.Id)) + ">"
if len(comments) > 0 { if len(comments) > 0 {
for i, comment := range comments { for i, comment := range comments {
@ -282,17 +289,23 @@ func GitLab_ExportQA(c *gin.Context) {
// Format the issue // Format the issue
issue := GitLabIssue{ issue := GitLabIssue{
Title: query.Subject, Title: "[QA] " + query.Subject,
Description: description, Description: description,
} }
// Create the issue on GitLab // Create the issue on GitLab
oauth2Token := c.MustGet("gitlab-token").(*oauth2.Token) oauth2Token := c.MustGet("gitlab-token").(*oauth2.Token)
err = gitlab_newIssue(c.Request.Context(), oauth2Token, gitlabid, &issue) iid, err := gitlab_newIssue(c.Request.Context(), oauth2Token, gitlabid, &issue)
if err != nil { if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return return
} }
c.JSON(http.StatusOK, gitlabid) query.Exported = &iid.IId
_, err = query.Update()
if err != nil {
log.Println("Unable to update QAquery in GitLab_ExportQA:", err.Error())
}
c.JSON(http.StatusOK, iid)
} }

View File

@ -75,17 +75,44 @@ func qaCommentHandler(c *gin.Context) {
c.Next() c.Next()
} }
type QAQuery struct {
*fic.QAQuery
ForgeLink string `json:"forge_link,omitempty"`
}
func getExerciceQA(c *gin.Context) { func getExerciceQA(c *gin.Context) {
exercice := c.MustGet("exercice").(*fic.Exercice) exercice := c.MustGet("exercice").(*fic.Exercice)
qa, err := exercice.GetQAQueries() qas, err := exercice.GetQAQueries()
if err != nil { if err != nil {
log.Println("Unable to GetQAQueries: ", err.Error()) log.Println("Unable to GetQAQueries: ", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to list QA entries: %s", err.Error())}) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to list QA entries: %s", err.Error())})
return return
} }
c.JSON(http.StatusOK, qa) forgelink, err := GitLab_getExerciceId(exercice)
if err != nil {
log.Println("Unable to make request to admin:", err.Error())
c.JSON(http.StatusOK, qas)
return
}
var res []*QAQuery
for _, qa := range qas {
if qa.Exported != nil {
res = append(res, &QAQuery{
qa,
gitlabBaseURL + "/" + forgelink + fmt.Sprintf("/-/issues/%d", *qa.Exported),
})
} else {
res = append(res, &QAQuery{
qa,
"",
})
}
}
c.JSON(http.StatusOK, res)
} }
func exportQA(c *gin.Context) { func exportQA(c *gin.Context) {

View File

@ -22,7 +22,7 @@
Row, Row,
} from 'sveltestrap'; } from 'sveltestrap';
import { auth, version } from '$lib/stores/auth'; import { auth, gitlab, version } from '$lib/stores/auth';
export let activemenu = ""; export let activemenu = "";
$: { $: {
@ -33,17 +33,6 @@
activemenu = ""; activemenu = "";
} }
} }
async function rungitlab() {
const res = await fetch('api/gitlab/token');
if (res.status === 200) {
return res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
const gitlab = rungitlab();
</script> </script>
<Navbar color="dark" dark expand="xs"> <Navbar color="dark" dark expand="xs">
@ -101,17 +90,17 @@
{/if} {/if}
</Nav> </Nav>
<Nav class="ms-auto text-light" navbar> <Nav class="ms-auto text-light" navbar>
{#await gitlab then gl} {#if $gitlab}
<Icon name="gitlab" /> <Icon name="gitlab" />
{:catch err} {:else}
<Button <Button
color="warning" color="warning"
size="sm" size="sm"
href="auth/gitlab?next=/" href={"auth/gitlab?next=" + encodeURIComponent($page.url.pathname) }
> >
<Icon name="gitlab" /> Connexion GitLab <Icon name="gitlab" /> Connexion GitLab
</Button> </Button>
{/await} {/if}
<NavItem class="ms-2 text-truncate"> <NavItem class="ms-2 text-truncate">
v{$version.version} v{$version.version}
{#if $auth}&ndash; Logged as {$auth.name} (team #{$auth.id_team}){/if} {#if $auth}&ndash; Logged as {$auth.name} (team #{$auth.id_team}){/if}

View File

@ -4,6 +4,7 @@
import { import {
Alert, Alert,
Button, Button,
ButtonGroup,
Card, Card,
CardBody, CardBody,
CardHeader, CardHeader,
@ -16,7 +17,7 @@
import BadgeState from '$lib/components/BadgeState.svelte'; import BadgeState from '$lib/components/BadgeState.svelte';
import DateFormat from '$lib/components/DateFormat.svelte'; import DateFormat from '$lib/components/DateFormat.svelte';
import { getQAComments, QAComment } from '$lib/qa'; import { getQAComments, QAComment } from '$lib/qa';
import { auth } from '$lib/stores/auth'; import { auth, gitlab } from '$lib/stores/auth';
import { ToastsStore } from '$lib/stores/toasts'; import { ToastsStore } from '$lib/stores/toasts';
import { viewIdx } from '$lib/stores/todo'; import { viewIdx } from '$lib/stores/todo';
@ -150,16 +151,25 @@
}) })
} }
async function rungitlab() { let exportingToGitlab = false;
const res = await fetch('api/gitlab/token'); async function exportgitlab() {
if (res.status === 200) { exportingToGitlab = true;
return res.json(); try {
} else { const data = await query.export2Gitlab();
throw new Error((await res.json()).errmsg); query.forge_link = data.web_url;
query.exported = data.iid;
ToastsStore.addToast({
color: "success",
title: "Retour exporté vers Gitlab !",
msg: "Ce retour a bien été exporté vers Gitlab sous la référence #" + query.exported,
});
} catch (err) {
ToastsStore.addErrorToast({
msg: err,
});
} }
exportingToGitlab = false;
} }
const gitlab = rungitlab();
</script> </script>
{#if query} {#if query}
@ -169,14 +179,35 @@
<h4 class="card-title fw-bold mb-0">{query.subject}</h4> <h4 class="card-title fw-bold mb-0">{query.subject}</h4>
<div> <div>
{#if $auth && !query.solved} {#if $auth && !query.solved}
{#await gitlab then gl} <ButtonGroup>
<Button {#if query.forge_link}
on:click={() => query.export2Gitlab()} <Button
> color="warning"
<Icon name="gitlab" /> outline
Exporter vers GitLab size="sm"
</Button> href="{query.forge_link}"
{/await} target="_blank"
>
<Icon name="gitlab" />
</Button>
{/if}
{#if $gitlab}
<Button
color="warning"
outline
size="sm"
disabled={exportingToGitlab || query.exported}
on:click={exportgitlab}
>
{#if exportingToGitlab}
<Spinner size="sm" />
{:else if !query.exported}
<Icon name="gitlab" />
{/if}
Exporter vers GitLab
</Button>
{/if}
</ButtonGroup>
{/if} {/if}
{#if $auth && !query.solved && $viewIdx[query.id_exercice]} {#if $auth && !query.solved && $viewIdx[query.id_exercice]}
<Button on:click={solveQA} color="success"> <Button on:click={solveQA} color="success">

View File

@ -20,7 +20,7 @@ export class QAQuery {
} }
} }
update({ id, id_exercice, id_team, user, creation, state, subject, solved, closed, exported }) { update({ id, id_exercice, id_team, user, creation, state, subject, solved, closed, exported, forge_link }) {
this.id = id; this.id = id;
this.id_team = id_team; this.id_team = id_team;
this.id_exercice = id_exercice; this.id_exercice = id_exercice;
@ -31,6 +31,7 @@ export class QAQuery {
this.solved = solved; this.solved = solved;
this.closed = closed; this.closed = closed;
this.exported = exported; this.exported = exported;
this.forge_link = forge_link;
} }
async delete() { async delete() {

View File

@ -27,3 +27,30 @@ export const auth = derived(
version, version,
$version => $version.auth, $version => $version.auth,
); );
function createGitlabStore() {
const { subscribe, set, update } = writable(null);
return {
subscribe,
set,
update,
refresh: async () => {
const res = await fetch('api/gitlab/token', {headers: {'Accept': 'application/json'}});
if (res.status === 200) {
const token = await res.json();
update((m) => token);
return token;
} else {
update((m) => null);
return null;
}
},
};
}
export const gitlab = createGitlabStore();

View File

@ -9,13 +9,16 @@
import Header from '$lib/components/Header.svelte'; import Header from '$lib/components/Header.svelte';
import Toaster from '$lib/components/Toaster.svelte'; import Toaster from '$lib/components/Toaster.svelte';
import { version } from '$lib/stores/auth'; import { gitlab, version } from '$lib/stores/auth';
import { ToastsStore } from '$lib/stores/toasts'; import { ToastsStore } from '$lib/stores/toasts';
import { view } from '$lib/stores/todo'; import { view } from '$lib/stores/todo';
version.refresh(); version.refresh();
setInterval(version.refresh, 30000); setInterval(version.refresh, 30000);
gitlab.refresh();
setInterval(gitlab.refresh, 300000);
view.refresh().catch((err) => { view.refresh().catch((err) => {
ToastsStore.addErrorToast({ ToastsStore.addErrorToast({
msg: err, msg: err,