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 {
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)
}