Skip to content

Commit

Permalink
readding artifactory add functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
amdurh05 committed Nov 21, 2024
1 parent 2b68e57 commit f7f3445
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions internal/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package internal

import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/hex"
Expand Down Expand Up @@ -66,10 +67,82 @@ func (l *Lock) AddResource(paths []string, algo string, tags []string, filename
if err != nil {
return err
}

// If cache URL is provided, handles Artifactory upload
if cacheURL != "" {
token := os.Getenv("GRABIT_ARTIFACTORY_TOKEN")
if token == "" {
return fmt.Errorf("GRABIT_ARTIFACTORY_TOKEN environment variable is not set")
}
}
// Get file again for upload
path, err := GetUrltoTempFile(paths[0], context.Background())
if err != nil {
return fmt.Errorf("failed to get file for cache: %s", err)
}
defer os.Remove(path)

// Upload to Artifactory using hash as filename
err = uploadToArtifactory(path, cacheURL, r.Integrity)
if err != nil {
return fmt.Errorf("failed to upload to cache: %v", err)
}

l.conf.Resource = append(l.conf.Resource, *r)
return nil
}

func uploadToArtifactory(filePath, cacheURL, integrity string) error {
token := os.Getenv("GRABIT_ARTIFACTORY_TOKEN")
if token == "" {
return fmt.Errorf("GRABIT_ARTIFACTORY_TOKEN environment variable is not set")
}

// Read file
fileContent, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}

// Get hash from integrity string
h := strings.TrimPrefix(integrity, "sha256-")
h = strings.TrimRight(h, "=")
padding := len(h) % 4
if padding != 0 {
h += strings.Repeat("=", 4-padding)
}

hashBytes, err := base64.StdEncoding.DecodeString(h)
if err != nil {
return fmt.Errorf("failed to decode hash: %v", err)
}

hexHash := hex.EncodeToString(hashBytes)
artifactoryURL := fmt.Sprintf("%s/%s", cacheURL, hexHash)

// Create upload request
req, err := http.NewRequest("PUT", artifactoryURL, bytes.NewReader(fileContent))
if err != nil {
return fmt.Errorf("failed to create request: %v", err)
}

req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/octet-stream")

// Do upload
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to upload: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
return fmt.Errorf("upload failed with status: %d", resp.StatusCode)
}

return nil
}

func (l *Lock) DeleteResource(path string) {
newStatements := []Resource{}
for _, r := range l.conf.Resource {
Expand Down

0 comments on commit f7f3445

Please sign in to comment.