Skip to content

Commit 3189bd9

Browse files
committed
temp commit
1 parent a76bfc3 commit 3189bd9

File tree

10 files changed

+128
-1
lines changed

10 files changed

+128
-1
lines changed

backend/middleware/basic.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func HttpBasicApiAuth() gin.HandlerFunc {
5959
} else {
6060
setDefaultOrganisationId(c)
6161
c.Set(ACCESS_LEVEL_KEY, jobToken.Type)
62+
c.Set(JOB_TOKEN_KEY, jobToken.Value)
6263
}
6364
} else if token == os.Getenv("BEARER_AUTH_TOKEN") {
6465
setDefaultOrganisationId(c)

backend/middleware/jwt.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,4 @@ func CORSMiddleware() gin.HandlerFunc {
309309

310310
const ORGANISATION_ID_KEY = "organisation_ID"
311311
const ACCESS_LEVEL_KEY = "access_level"
312+
const JOB_TOKEN_KEY = "job_token"

backend/migrations/20240729155442.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- Create "job_artefacts" table
2+
CREATE TABLE "public"."job_artefacts" (
3+
"id" bigserial NOT NULL,
4+
"created_at" timestamptz NULL,
5+
"updated_at" timestamptz NULL,
6+
"deleted_at" timestamptz NULL,
7+
"job_token_id" bigint NULL,
8+
"contents" bytea NULL,
9+
PRIMARY KEY ("id"),
10+
CONSTRAINT "fk_job_artefacts_job_token" FOREIGN KEY ("job_token_id") REFERENCES "public"."job_tokens" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
11+
);
12+
-- Create index "idx_job_artefacts_deleted_at" to table: "job_artefacts"
13+
CREATE INDEX "idx_job_artefacts_deleted_at" ON "public"."job_artefacts" ("deleted_at");

backend/migrations/20240729155926.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Modify "job_artefacts" table
2+
ALTER TABLE "public"."job_artefacts" ADD COLUMN "size" bigint NULL, ADD COLUMN "content_type" text NULL;

backend/migrations/20240729160028.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Modify "job_artefacts" table
2+
ALTER TABLE "public"."job_artefacts" ADD COLUMN "filename" text NULL;

backend/migrations/atlas.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:r0O5Hy6gJdMDgkCW9ocd4ytiCyPghNcXKTAT3tuppZ8=
1+
h1:A7OaxVcBVM26DpHZ8tH+NLsfDZxrkpsFUxFJbK3Am68=
22
20231227132525.sql h1:43xn7XC0GoJsCnXIMczGXWis9d504FAWi4F1gViTIcw=
33
20240115170600.sql h1:IW8fF/8vc40+eWqP/xDK+R4K9jHJ9QBSGO6rN9LtfSA=
44
20240116123649.sql h1:R1JlUIgxxF6Cyob9HdtMqiKmx/BfnsctTl5rvOqssQw=
@@ -27,3 +27,6 @@ h1:r0O5Hy6gJdMDgkCW9ocd4ytiCyPghNcXKTAT3tuppZ8=
2727
20240704192835.sql h1:F84HKuE3qX8hYqDCU2K9NDG4WQaSfmnk2P4OCje54qg=
2828
20240705144450.sql h1:AAB4GbML2Jea1MSv/+E0DLMMCI9GLqwptpvBScvU19s=
2929
20240709165155.sql h1:HWDuhwXD+esr9uaKHsqwltPfUEq5uLdcEzpnZ1biKgU=
30+
20240729155442.sql h1:s7PCALP3SgPz5y9Ya7HkDzYjeN86Q5NniW2YIkOMBrQ=
31+
20240729155926.sql h1:8vsDrpy/R1UDI+meIp6KoDfhS60t+ngu8aPB+uonFZ4=
32+
20240729160028.sql h1:snkkxhA2aEQhqBmIhN8l+nPlBhrPOZiPP+dnyhobwD8=

backend/models/artefact.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package models
2+
3+
import (
4+
"gorm.io/gorm"
5+
)
6+
7+
type JobArtefact struct {
8+
gorm.Model
9+
JobTokenID uint
10+
JobToken JobToken
11+
Filename string
12+
Contents []byte `gorm:"type:bytea"`
13+
Size int64
14+
ContentType string
15+
}

backend/models/storage.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,17 @@ func (db *Database) GetJobToken(tenantId any) (*JobToken, error) {
11261126
return token, nil
11271127
}
11281128

1129+
func (db *Database) DeleteJobTokenArtefacts(jobTokenId uint) error {
1130+
artefact := JobArtefact{}
1131+
result := db.GormDB.Where("job_token_id = ?", jobTokenId).Delete(&artefact)
1132+
if result.Error != nil {
1133+
return result.Error
1134+
}
1135+
log.Printf("DeleteJobTokenArtefacts %v has been deleted successfully\n", jobTokenId)
1136+
return nil
1137+
1138+
}
1139+
11291140
func (db *Database) CreateGithubAppInstallation(installationId int64, githubAppId int64, login string, accountId int, repoFullName string) (*GithubAppInstallation, error) {
11301141
installation := &GithubAppInstallation{
11311142
GithubInstallationId: installationId,

ee/backend/controllers/artefacts.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package controllers
2+
3+
import (
4+
"github.com/diggerhq/digger/backend/middleware"
5+
"github.com/diggerhq/digger/backend/models"
6+
"github.com/gin-gonic/gin"
7+
"io"
8+
"log"
9+
"net/http"
10+
)
11+
12+
func SetJobArtefact(c *gin.Context) {
13+
jobTokenValue, exists := c.Get(middleware.JOB_TOKEN_KEY)
14+
if !exists {
15+
c.String(http.StatusBadRequest, "missing value: bearer job token")
16+
return
17+
}
18+
19+
jobToken, err := models.DB.GetJobToken(jobTokenValue)
20+
if err != nil {
21+
c.String(http.StatusBadRequest, "could not find job token")
22+
return
23+
}
24+
25+
file, err := c.FormFile("file")
26+
if err != nil {
27+
c.JSON(http.StatusBadRequest, gin.H{"error": "No file is received"})
28+
return
29+
}
30+
31+
// Open the file
32+
src, err := file.Open()
33+
if err != nil {
34+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error opening file"})
35+
return
36+
}
37+
defer src.Close()
38+
39+
// Read the content
40+
content, err := io.ReadAll(src)
41+
if err != nil {
42+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error reading file content"})
43+
return
44+
}
45+
46+
// Deleting existing artefacts
47+
err = models.DB.DeleteJobTokenArtefacts(jobToken.ID)
48+
if err != nil {
49+
log.Printf("could not delete artefacts: %v", err)
50+
c.JSON(http.StatusInternalServerError, "could not delete existing artefacts")
51+
return
52+
}
53+
54+
log.Printf("contents of the file is: %v", string(content))
55+
// Create a new File record
56+
artefactRecord := models.JobArtefact{
57+
JobTokenID: jobToken.ID,
58+
Filename: file.Filename,
59+
Contents: content,
60+
Size: file.Size,
61+
ContentType: file.Header.Get("Content-Type"),
62+
}
63+
64+
// Save the file to the database
65+
if result := models.DB.GormDB.Create(&artefactRecord); result.Error != nil {
66+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error saving file to database"})
67+
return
68+
}
69+
70+
c.JSON(http.StatusOK, gin.H{"message": "File uploaded successfully", "id": artefactRecord.ID})
71+
72+
}
73+
74+
func DownloadJobArtefact(c *gin.Context) {
75+
76+
}

ee/backend/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func main() {
7979
policiesGroup.GET("/:policyid/details", web.PolicyDetailsPage)
8080
policiesGroup.POST("/:policyid/details", web.PolicyDetailsUpdatePage)
8181

82+
jobArtefactsGroup := r.Group("/job_artefacts")
83+
jobArtefactsGroup.Use(middleware.GetApiMiddleware())
84+
jobArtefactsGroup.PUT("/", controllers.SetJobArtefact)
8285
port := config.GetPort()
8386
r.Run(fmt.Sprintf(":%d", port))
8487
}

0 commit comments

Comments
 (0)