qa: Refactor gitlab use
This commit is contained in:
parent
c31f76e9c3
commit
d4cad767eb
@ -195,41 +195,48 @@ func GitLab_GetMyToken(c *gin.Context) {
|
||||
}
|
||||
|
||||
type GitLabIssue struct {
|
||||
Title string
|
||||
Description string
|
||||
Id int64 `json:"id,omitempty"`
|
||||
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)
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("title", "[QA] "+issue.Title)
|
||||
params.Set("description", issue.Description)
|
||||
|
||||
enc, err := json.Marshal(issue)
|
||||
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 {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
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) {
|
||||
@ -269,7 +276,7 @@ func GitLab_ExportQA(c *gin.Context) {
|
||||
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 {
|
||||
for i, comment := range comments {
|
||||
@ -282,17 +289,23 @@ func GitLab_ExportQA(c *gin.Context) {
|
||||
|
||||
// Format the issue
|
||||
issue := GitLabIssue{
|
||||
Title: query.Subject,
|
||||
Title: "[QA] " + query.Subject,
|
||||
Description: description,
|
||||
}
|
||||
|
||||
// Create the issue on GitLab
|
||||
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 {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
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)
|
||||
}
|
||||
|
31
qa/api/qa.go
31
qa/api/qa.go
@ -75,17 +75,44 @@ func qaCommentHandler(c *gin.Context) {
|
||||
c.Next()
|
||||
}
|
||||
|
||||
type QAQuery struct {
|
||||
*fic.QAQuery
|
||||
ForgeLink string `json:"forge_link,omitempty"`
|
||||
}
|
||||
|
||||
func getExerciceQA(c *gin.Context) {
|
||||
exercice := c.MustGet("exercice").(*fic.Exercice)
|
||||
|
||||
qa, err := exercice.GetQAQueries()
|
||||
qas, err := exercice.GetQAQueries()
|
||||
if err != nil {
|
||||
log.Println("Unable to GetQAQueries: ", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to list QA entries: %s", err.Error())})
|
||||
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) {
|
||||
|
@ -22,7 +22,7 @@
|
||||
Row,
|
||||
} from 'sveltestrap';
|
||||
|
||||
import { auth, version } from '$lib/stores/auth';
|
||||
import { auth, gitlab, version } from '$lib/stores/auth';
|
||||
|
||||
export let activemenu = "";
|
||||
$: {
|
||||
@ -33,17 +33,6 @@
|
||||
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>
|
||||
|
||||
<Navbar color="dark" dark expand="xs">
|
||||
@ -101,17 +90,17 @@
|
||||
{/if}
|
||||
</Nav>
|
||||
<Nav class="ms-auto text-light" navbar>
|
||||
{#await gitlab then gl}
|
||||
{#if $gitlab}
|
||||
<Icon name="gitlab" />
|
||||
{:catch err}
|
||||
{:else}
|
||||
<Button
|
||||
color="warning"
|
||||
size="sm"
|
||||
href="auth/gitlab?next=/"
|
||||
href={"auth/gitlab?next=" + encodeURIComponent($page.url.pathname) }
|
||||
>
|
||||
<Icon name="gitlab" /> Connexion GitLab
|
||||
</Button>
|
||||
{/await}
|
||||
{/if}
|
||||
<NavItem class="ms-2 text-truncate">
|
||||
v{$version.version}
|
||||
{#if $auth}– Logged as {$auth.name} (team #{$auth.id_team}){/if}
|
||||
|
@ -4,6 +4,7 @@
|
||||
import {
|
||||
Alert,
|
||||
Button,
|
||||
ButtonGroup,
|
||||
Card,
|
||||
CardBody,
|
||||
CardHeader,
|
||||
@ -16,7 +17,7 @@
|
||||
import BadgeState from '$lib/components/BadgeState.svelte';
|
||||
import DateFormat from '$lib/components/DateFormat.svelte';
|
||||
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 { viewIdx } from '$lib/stores/todo';
|
||||
|
||||
@ -150,16 +151,25 @@
|
||||
})
|
||||
}
|
||||
|
||||
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);
|
||||
let exportingToGitlab = false;
|
||||
async function exportgitlab() {
|
||||
exportingToGitlab = true;
|
||||
try {
|
||||
const data = await query.export2Gitlab();
|
||||
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>
|
||||
|
||||
{#if query}
|
||||
@ -169,14 +179,35 @@
|
||||
<h4 class="card-title fw-bold mb-0">{query.subject}</h4>
|
||||
<div>
|
||||
{#if $auth && !query.solved}
|
||||
{#await gitlab then gl}
|
||||
<Button
|
||||
on:click={() => query.export2Gitlab()}
|
||||
>
|
||||
<Icon name="gitlab" />
|
||||
Exporter vers GitLab
|
||||
</Button>
|
||||
{/await}
|
||||
<ButtonGroup>
|
||||
{#if query.forge_link}
|
||||
<Button
|
||||
color="warning"
|
||||
outline
|
||||
size="sm"
|
||||
href="{query.forge_link}"
|
||||
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 $auth && !query.solved && $viewIdx[query.id_exercice]}
|
||||
<Button on:click={solveQA} color="success">
|
||||
|
@ -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_team = id_team;
|
||||
this.id_exercice = id_exercice;
|
||||
@ -31,6 +31,7 @@ export class QAQuery {
|
||||
this.solved = solved;
|
||||
this.closed = closed;
|
||||
this.exported = exported;
|
||||
this.forge_link = forge_link;
|
||||
}
|
||||
|
||||
async delete() {
|
||||
|
@ -27,3 +27,30 @@ export const auth = derived(
|
||||
version,
|
||||
$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();
|
||||
|
@ -9,13 +9,16 @@
|
||||
|
||||
import Header from '$lib/components/Header.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 { view } from '$lib/stores/todo';
|
||||
|
||||
version.refresh();
|
||||
setInterval(version.refresh, 30000);
|
||||
|
||||
gitlab.refresh();
|
||||
setInterval(gitlab.refresh, 300000);
|
||||
|
||||
view.refresh().catch((err) => {
|
||||
ToastsStore.addErrorToast({
|
||||
msg: err,
|
||||
|
Loading…
x
Reference in New Issue
Block a user