Skip to content

Commit 362406f

Browse files
authored
Poetry native support improvement (jfrog#3247)
1 parent 605ffde commit 362406f

File tree

4 files changed

+236
-19
lines changed

4 files changed

+236
-19
lines changed

buildtools/cli.go

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"fmt"
66
"os"
77
"os/exec"
8+
"path/filepath"
89
"sort"
910
"strconv"
1011
"strings"
1112

13+
"github.com/BurntSushi/toml"
1214
"github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/container/strategies"
13-
1415
"github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/python"
1516
"github.com/jfrog/jfrog-cli-artifactory/artifactory/commands/setup"
1617
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
@@ -1161,6 +1162,49 @@ func getCommandName(orgArgs []string) (string, []string) {
11611162
return "", cmdArgs
11621163
}
11631164

1165+
// getPrimarySourceFromToml reads pyproject.toml and returns the name of the primary source
1166+
// Returns (sourceName, isPrimary) where isPrimary indicates if source has priority='primary'
1167+
func getPrimarySourceFromToml() (string, bool) {
1168+
type PyProjectSource struct {
1169+
Name string `toml:"name"`
1170+
URL string `toml:"url"`
1171+
Priority string `toml:"priority"`
1172+
}
1173+
1174+
type PyProject struct {
1175+
Tool struct {
1176+
Poetry struct {
1177+
Source []PyProjectSource `toml:"source"`
1178+
} `toml:"poetry"`
1179+
} `toml:"tool"`
1180+
}
1181+
1182+
tomlPath := filepath.Join(".", "pyproject.toml")
1183+
tomlData, err := os.ReadFile(tomlPath)
1184+
if err != nil {
1185+
return "", false
1186+
}
1187+
1188+
var pyproject PyProject
1189+
if err := toml.Unmarshal(tomlData, &pyproject); err != nil {
1190+
return "", false
1191+
}
1192+
1193+
// Look for primary source
1194+
for _, source := range pyproject.Tool.Poetry.Source {
1195+
if source.Priority == "primary" {
1196+
return source.Name, true
1197+
}
1198+
}
1199+
1200+
// If no primary, use first source
1201+
if len(pyproject.Tool.Poetry.Source) > 0 {
1202+
return pyproject.Tool.Poetry.Source[0].Name, false
1203+
}
1204+
1205+
return "", false
1206+
}
1207+
11641208
func NpmInstallCmd(c *cli.Context) error {
11651209
if show, err := cliutils.ShowGenericCmdHelpIfNeeded(c, c.Args(), "npminstallhelp"); show || err != nil {
11661210
return err
@@ -1353,6 +1397,34 @@ func pythonCmd(c *cli.Context, projectType project.ProjectType) error {
13531397
}
13541398
cmdName, poetryArgs := getCommandName(filteredArgs)
13551399

1400+
// Extract --repository flag for artifact collection (if publishing)
1401+
deployerRepo := ""
1402+
for i, arg := range poetryArgs {
1403+
if strings.HasPrefix(arg, "--repository=") {
1404+
deployerRepo = strings.TrimPrefix(arg, "--repository=")
1405+
} else if (arg == "--repository" || arg == "-r") && i+1 < len(poetryArgs) {
1406+
deployerRepo = poetryArgs[i+1]
1407+
}
1408+
}
1409+
1410+
// Auto-add repository flag if not provided and we're publishing
1411+
if cmdName == "publish" && deployerRepo == "" {
1412+
// Try to get primary source from pyproject.toml (same as native Poetry behavior)
1413+
if repoName, isPrimary := getPrimarySourceFromToml(); repoName != "" {
1414+
if isPrimary {
1415+
log.Info(fmt.Sprintf("No --repository flag specified. Using '%s' from pyproject.toml (priority='primary')", repoName))
1416+
} else {
1417+
log.Info(fmt.Sprintf("No --repository flag specified. Using '%s' from pyproject.toml (first source)", repoName))
1418+
}
1419+
poetryArgs = append([]string{"-r", repoName}, poetryArgs...)
1420+
deployerRepo = repoName
1421+
} else {
1422+
log.Warn("No repository specified and no sources found in pyproject.toml. Poetry will attempt to publish to PyPI.")
1423+
}
1424+
} else if cmdName == "publish" && deployerRepo != "" {
1425+
log.Info(fmt.Sprintf("Publishing to repository: %s (from --repository flag)", deployerRepo))
1426+
}
1427+
13561428
// Execute native poetry command directly (similar to Maven FlexPack)
13571429
log.Info(fmt.Sprintf("Running Poetry %s.", cmdName))
13581430
poetryCmd := exec.Command("poetry", append([]string{cmdName}, poetryArgs...)...)
@@ -1369,7 +1441,7 @@ func pythonCmd(c *cli.Context, projectType project.ProjectType) error {
13691441
workingDir, err := os.Getwd()
13701442
if err != nil {
13711443
log.Warn("Failed to get working directory, skipping build info collection: " + err.Error())
1372-
} else if err := buildinfo.GetPoetryBuildInfo(workingDir, buildConfiguration); err != nil {
1444+
} else if err := buildinfo.GetPoetryBuildInfo(workingDir, buildConfiguration, deployerRepo); err != nil {
13731445
log.Warn("Failed to collect Poetry build info: " + err.Error())
13741446
} else {
13751447
buildNumber, err := buildConfiguration.GetBuildNumber()

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@ replace (
1010
)
1111

1212
require (
13+
github.com/BurntSushi/toml v1.5.0
1314
github.com/agnivade/levenshtein v1.2.1
1415
github.com/buger/jsonparser v1.1.1
1516
github.com/docker/docker v28.5.2+incompatible
1617
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
1718
github.com/jfrog/archiver/v3 v3.6.1
18-
github.com/jfrog/build-info-go v1.12.5-0.20251209031413-f5f0e93dc8db
19+
github.com/jfrog/build-info-go v1.12.5-0.20251209120002-025bda5ff78b
1920
github.com/jfrog/gofrog v1.7.6
2021
github.com/jfrog/jfrog-cli-application v1.0.2-0.20251208114900-b3cc968c8e3d
21-
github.com/jfrog/jfrog-cli-artifactory v0.8.1-0.20251209060234-c05dd4957894
22+
github.com/jfrog/jfrog-cli-artifactory v0.8.1-0.20251209121625-98f7b22a08c1
2223
github.com/jfrog/jfrog-cli-core/v2 v2.60.1-0.20251125083543-e689762c4ff0
2324
github.com/jfrog/jfrog-cli-evidence v0.8.3-0.20251204144808-73fa744851c0
2425
github.com/jfrog/jfrog-cli-platform-services v1.10.1-0.20251205121610-171eb9b0000e
2526
github.com/jfrog/jfrog-cli-security v1.24.0
2627
github.com/jfrog/jfrog-client-go v1.55.1-0.20251209032636-8966184db185
2728
github.com/jszwec/csvutil v1.10.0
2829
github.com/manifoldco/promptui v0.9.0
30+
github.com/spf13/viper v1.21.0
2931
github.com/stretchr/testify v1.11.1
3032
github.com/testcontainers/testcontainers-go v0.35.0
3133
github.com/urfave/cli v1.22.17
@@ -48,7 +50,6 @@ require (
4850
dario.cat/mergo v1.0.2 // indirect
4951
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 // indirect
5052
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
51-
github.com/BurntSushi/toml v1.5.0 // indirect
5253
github.com/CycloneDX/cyclonedx-go v0.9.3 // indirect
5354
github.com/GoogleCloudPlatform/grpc-gcp-go/grpcgcp v1.5.3 // indirect
5455
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.30.0 // indirect
@@ -214,7 +215,6 @@ require (
214215
github.com/spf13/afero v1.15.0 // indirect
215216
github.com/spf13/cast v1.10.0 // indirect
216217
github.com/spf13/pflag v1.0.10 // indirect
217-
github.com/spf13/viper v1.21.0 // indirect
218218
github.com/spiffe/go-spiffe/v2 v2.6.0 // indirect
219219
github.com/stretchr/objx v0.5.3 // indirect
220220
github.com/subosito/gotenv v1.6.0 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,8 +1175,8 @@ github.com/jellydator/ttlcache/v3 v3.4.0 h1:YS4P125qQS0tNhtL6aeYkheEaB/m8HCqdMMP
11751175
github.com/jellydator/ttlcache/v3 v3.4.0/go.mod h1:Hw9EgjymziQD3yGsQdf1FqFdpp7YjFMd4Srg5EJlgD4=
11761176
github.com/jfrog/archiver/v3 v3.6.1 h1:LOxnkw9pOn45DzCbZNFV6K0+6dCsQ0L8mR3ZcujO5eI=
11771177
github.com/jfrog/archiver/v3 v3.6.1/go.mod h1:VgR+3WZS4N+i9FaDwLZbq+jeU4B4zctXL+gL4EMzfLw=
1178-
github.com/jfrog/build-info-go v1.12.5-0.20251209031413-f5f0e93dc8db h1:5q4hUqZVl7Xt+R+ono5lDH1/lkvV1spnfDtp0VtJqlo=
1179-
github.com/jfrog/build-info-go v1.12.5-0.20251209031413-f5f0e93dc8db/go.mod h1:9W4U440fdTHwW1HiB/R0VQvz/5q8ZHsms9MWcq+JrdY=
1178+
github.com/jfrog/build-info-go v1.12.5-0.20251209120002-025bda5ff78b h1:wf8u5g84GW8ZYsM59UGk+1vvcUOCaP75NiGZeOatkC8=
1179+
github.com/jfrog/build-info-go v1.12.5-0.20251209120002-025bda5ff78b/go.mod h1:9W4U440fdTHwW1HiB/R0VQvz/5q8ZHsms9MWcq+JrdY=
11801180
github.com/jfrog/froggit-go v1.20.6 h1:Xp7+LlEh0m1KGrQstb+u0aGfjRUtv1eh9xQBV3571jQ=
11811181
github.com/jfrog/froggit-go v1.20.6/go.mod h1:obSG1SlsWjktkuqmKtpq7MNTTL63e0ot+ucTnlOMV88=
11821182
github.com/jfrog/go-mockhttp v0.3.1 h1:/wac8v4GMZx62viZmv4wazB5GNKs+GxawuS1u3maJH8=
@@ -1187,8 +1187,8 @@ github.com/jfrog/jfrog-apps-config v1.0.1 h1:mtv6k7g8A8BVhlHGlSveapqf4mJfonwvXYL
11871187
github.com/jfrog/jfrog-apps-config v1.0.1/go.mod h1:8AIIr1oY9JuH5dylz2S6f8Ym2MaadPLR6noCBO4C22w=
11881188
github.com/jfrog/jfrog-cli-application v1.0.2-0.20251208114900-b3cc968c8e3d h1:0o6tj4nPP9uCscyfPbKBUcCaIYof42irwii6XsBB8zM=
11891189
github.com/jfrog/jfrog-cli-application v1.0.2-0.20251208114900-b3cc968c8e3d/go.mod h1:xum2HquWO5uExa/A7MQs3TgJJVEeoqTR+6Z4mfBr1Xw=
1190-
github.com/jfrog/jfrog-cli-artifactory v0.8.1-0.20251209060234-c05dd4957894 h1:PCtFq128LZCCrMtQ/tzZViCDxvzwEvnjHQDzAA8ZVAw=
1191-
github.com/jfrog/jfrog-cli-artifactory v0.8.1-0.20251209060234-c05dd4957894/go.mod h1:Jb2MTLteycwR+95MfcfLvguo/JcRqILdDpbdnMWveVA=
1190+
github.com/jfrog/jfrog-cli-artifactory v0.8.1-0.20251209121625-98f7b22a08c1 h1:vT9QWrwW6pJPcHewSVsDWWoCHq+Nt3UqnEaJjyMqFMU=
1191+
github.com/jfrog/jfrog-cli-artifactory v0.8.1-0.20251209121625-98f7b22a08c1/go.mod h1:b/Sf+FOjWwoQOZ00r+fXMbDqpts8L0q1lMNgS5cofAs=
11921192
github.com/jfrog/jfrog-cli-core/v2 v2.60.1-0.20251125083543-e689762c4ff0 h1:EsasTBE5i2MyCESS/icZxKIlObpGiOyW9K67MAaEWco=
11931193
github.com/jfrog/jfrog-cli-core/v2 v2.60.1-0.20251125083543-e689762c4ff0/go.mod h1:d9aADumiyjCBvZLffp8wldvP9XFHxcvk2PoOSUYms2g=
11941194
github.com/jfrog/jfrog-cli-evidence v0.8.3-0.20251204144808-73fa744851c0 h1:8S1vE1PeVtrzWkKL0N39cX6XLLNV0It+f6xjRKjw7Ug=

0 commit comments

Comments
 (0)