<commit_before>package main

import (
	"database/sql"
	"io/ioutil"
	"net/http"
	"strconv"

	"github.com/gorilla/mux"

	"github.com/ovh/cds/engine/api/cache"
	"github.com/ovh/cds/engine/api/context"
	"github.com/ovh/cds/engine/api/pipeline"
	"github.com/ovh/cds/engine/log"
	"github.com/ovh/cds/sdk"
)

func addStageHandler(w http.ResponseWriter, r *http.Request, db *sql.DB, c *context.Context) {

	// Get project name in URL
	vars := mux.Vars(r)
	projectKey := vars["key"]
	pipelineKey := vars["permPipelineKey"]

	// Get body
	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		log.Warning("addStageHandler> cannot read body: %s", err)
		WriteError(w, r, sdk.ErrWrongRequest)
		return
	}

	stageData, err := sdk.NewStage("").FromJSON(data)
	if err != nil {
		log.Warning("addStageHandler> cannot unmarshal body: %s", err)
		WriteError(w, r, sdk.ErrWrongRequest)
		return
	}

	// Check if pipeline exist
	pipelineData, err := pipeline.LoadPipeline(db, projectKey, pipelineKey, false)
	if err != nil {
		WriteError(w, r, err)
		return
	}

	if err := pipeline.LoadPipelineStage(db, pipelineData); err != nil {
		log.Warning("addStageHandler> Cannot load pipeline stages: %s", err)
		WriteError(w, r, err)
		return
	}

	stageData.BuildOrder = len(pipelineData.Stages) + 1
	stageData.PipelineID = pipelineData.ID

	tx, err := db.Begin()
	if err != nil {
		log.Warning("addStageHandler> Cannot start transaction: %s", err)
		WriteError(w, r, err)
	}
	defer tx.Rollback()

	if err := pipeline.InsertStage(db, stageData); err != nil {
		log.Warning("addStageHandler> Cannot insert stage: %s", err)
		WriteError(w, r, err)
		return
	}

	if err := pipeline.UpdatePipelineLastModified(tx, pipelineData); err != nil {
		log.Warning("addStageHandler> Cannot update pipeline last modified date: %s", err)
		WriteError(w, r, err)
		return
	}

	if err := tx.Commit(); err != nil {
		log.Warning("addStageHandler> Cannot commit transaction: %s", err)
		WriteError(w, r, err)
	}

	if err := pipeline.LoadPipelineStage(db, pipelineData); err != nil {
		log.Warning("addStageHandler> Cannot load pipeline stages: %s", err)
		WriteError(w, r, err)
		return
	}

	k := cache.Key("application", projectKey, "*")
	cache.DeleteAll(k)
	cache.Delete(cache.Key("pipeline", projectKey, pipelineKey))

	WriteJSON(w, r, stageData, http.StatusCreated)
}

func getStageHandler(w http.ResponseWriter, r *http.Request, db *sql.DB, c *context.Context) {
	// Get project name in URL
	vars := mux.Vars(r)
	projectKey := vars["key"]
	pipelineKey := vars["permPipelineKey"]
	stageIDString := vars["stageID"]
<commit_msg>fix: return pipeline + list of stages<commit_after>package main

import (
	"database/sql"
	"io/ioutil"
	"net/http"
	"strconv"

	"github.com/gorilla/mux"

	"github.com/ovh/cds/engine/api/cache"
	"github.com/ovh/cds/engine/api/context"
	"github.com/ovh/cds/engine/api/pipeline"
	"github.com/ovh/cds/engine/log"
	"github.com/ovh/cds/sdk"
)

func addStageHandler(w http.ResponseWriter, r *http.Request, db *sql.DB, c *context.Context) {

	// Get project name in URL
	vars := mux.Vars(r)
	projectKey := vars["key"]
	pipelineKey := vars["permPipelineKey"]

	// Get body
	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		log.Warning("addStageHandler> cannot read body: %s", err)
		WriteError(w, r, sdk.ErrWrongRequest)
		return
	}

	stageData, err := sdk.NewStage("").FromJSON(data)
	if err != nil {
		log.Warning("addStageHandler> cannot unmarshal body: %s", err)
		WriteError(w, r, sdk.ErrWrongRequest)
		return
	}

	// Check if pipeline exist
	pipelineData, err := pipeline.LoadPipeline(db, projectKey, pipelineKey, false)
	if err != nil {
		WriteError(w, r, err)
		return
	}

	if err := pipeline.LoadPipelineStage(db, pipelineData); err != nil {
		log.Warning("addStageHandler> Cannot load pipeline stages: %s", err)
		WriteError(w, r, err)
		return
	}

	stageData.BuildOrder = len(pipelineData.Stages) + 1
	stageData.PipelineID = pipelineData.ID

	tx, err := db.Begin()
	if err != nil {
		log.Warning("addStageHandler> Cannot start transaction: %s", err)
		WriteError(w, r, err)
	}
	defer tx.Rollback()

	if err := pipeline.InsertStage(db, stageData); err != nil {
		log.Warning("addStageHandler> Cannot insert stage: %s", err)
		WriteError(w, r, err)
		return
	}

	if err := pipeline.UpdatePipelineLastModified(tx, pipelineData); err != nil {
		log.Warning("addStageHandler> Cannot update pipeline last modified date: %s", err)
		WriteError(w, r, err)
		return
	}

	if err := tx.Commit(); err != nil {
		log.Warning("addStageHandler> Cannot commit transaction: %s", err)
		WriteError(w, r, err)
	}

	if err := pipeline.LoadPipelineStage(db, pipelineData); err != nil {
		log.Warning("addStageHandler> Cannot load pipeline stages: %s", err)
		WriteError(w, r, err)
		return
	}

	k := cache.Key("application", projectKey, "*")
	cache.DeleteAll(k)
	cache.Delete(cache.Key("pipeline", projectKey, pipelineKey))

	WriteJSON(w, r, pipelineData, http.StatusCreated)
}

func getStageHandler(w http.ResponseWriter, r *http.Request, db *sql.DB, c *context.Context) {
	// Get project name in URL
	vars := mux.Vars(r)
	projectKey := vars["key"]
	pipelineKey := vars["permPipelineKey"]
	stageIDString := vars["stageID"]

{'fraction_non_alphanumeric': 0.15008658841639408, 'fraction_numerical': 0.00038483740619588223, 'mean_word_length': 4.40894901144641, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5858038', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1859, 'n_words': 465}