Refactor submission retrieval

This commit is contained in:
nemunaire 2022-09-05 04:40:49 +02:00
commit eacaedeb03
7 changed files with 175 additions and 113 deletions

View file

@ -1,21 +1,14 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/drone/drone-go/drone"
"github.com/gin-gonic/gin"
"golang.org/x/oauth2"
@ -25,35 +18,11 @@ var (
droneToken = ""
droneConfig *http.Client
droneEndpoint string
s3_endpoint string
s3_region = "us"
s3_bucket string
s3_access_key string
s3_secret_key string
s3_path_style bool
)
func init() {
flag.StringVar(&droneToken, "drone-token", droneToken, "Token for Drone Oauth")
flag.StringVar(&droneEndpoint, "drone-endpoint", droneEndpoint, "Drone Endpoint")
s3_endpoint, _ = os.LookupEnv("S3_ENDPOINT")
if region, ok := os.LookupEnv("S3_REGION"); ok {
s3_region = region
}
s3_bucket, _ = os.LookupEnv("S3_BUCKET")
s3_access_key, _ = os.LookupEnv("S3_ACCESS_KEY")
s3_secret_key, _ = os.LookupEnv("S3_SECRET_KEY")
if path_style, ok := os.LookupEnv("S3_PATH_STYLE"); ok {
s3_path_style = path_style == "1" || path_style == "ON" || path_style == "on" || path_style == "TRUE" || path_style == "true" || path_style == "yes" || path_style == "YES"
}
flag.StringVar(&s3_endpoint, "s3-endpoint", s3_endpoint, "When using S3 backend, endpoint to use")
flag.StringVar(&s3_region, "s3-region", s3_region, "When using S3 backend, region to use")
flag.StringVar(&s3_bucket, "s3-bucket", s3_bucket, "When using S3 backend, bucket to use")
flag.StringVar(&s3_access_key, "s3-access-key", s3_access_key, "When using S3 backend, Access Key")
flag.StringVar(&s3_secret_key, "s3-secret-key", s3_secret_key, "When using S3 backend, Secret Key")
flag.BoolVar(&s3_path_style, "s3-path-style", s3_path_style, "When using S3 backend, force path style (when using minio)")
}
func initializeDroneOauth() {
@ -68,15 +37,6 @@ func initializeDroneOauth() {
}
}
func s3NewSession() (*session.Session, error) {
return session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(s3_access_key, s3_secret_key, ""),
Endpoint: aws.String(s3_endpoint),
Region: aws.String(s3_region),
S3ForcePathStyle: &s3_path_style,
})
}
func declareAPIAuthRepositoriesRoutes(router *gin.RouterGroup) {
router.GET("/repositories", func(c *gin.Context) {
var u *User
@ -196,10 +156,10 @@ func declareAPIAuthRepositoriesRoutes(router *gin.RouterGroup) {
now := time.Now()
/*if repo.LastCheck != nil && !repo.LastCheck.Before(now.Add(-5*time.Minute)) {
if repo.LastCheck != nil && !repo.LastCheck.Before(now.Add(-5*time.Minute)) {
c.AbortWithStatusJSON(http.StatusPaymentRequired, gin.H{"errmsg": "Please wait between two pulls."})
return
}*/
}
client := drone.NewClient(droneEndpoint, droneConfig)
result, err := client.BuildCreate("srs", "atsebay.t-worker", "", "master", map[string]string{
@ -225,6 +185,11 @@ func declareAPIAuthRepositoriesRoutes(router *gin.RouterGroup) {
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."})
@ -239,49 +204,6 @@ func declareAPIAuthRepositoriesRoutes(router *gin.RouterGroup) {
}
c.JSON(http.StatusOK, result)
})
repositoriesRoutes.GET("/submission", func(c *gin.Context) {
var u *User
if user, ok := c.Get("user"); ok {
u = user.(*User)
} else {
u = c.MustGet("LoggedUser").(*User)
}
repo := c.MustGet("repository").(*Repository)
work, err := getWork(int(repo.IdWork))
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to find related work."})
return
}
s, err := s3NewSession()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Something goes wrong."})
return
}
log.Println(path.Join(fmt.Sprintf("%d", work.Id), fmt.Sprintf("rendu-%s.metadata", u.Login)))
result, err := s3.New(s).GetObject(&s3.GetObjectInput{
Bucket: aws.String(s3_bucket),
Key: aws.String(path.Join(fmt.Sprintf("%d", work.Id), fmt.Sprintf("rendu-%s.metadata", u.Login))),
})
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Submission not found."})
return
}
tmp := map[string]interface{}{}
err = json.NewDecoder(result.Body).Decode(&tmp)
if err != nil {
log.Println("Unable to decode JSON metadata:", err.Error())
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Submission not found."})
return
}
c.JSON(http.StatusOK, tmp)
})
}