Repository navigation
Add plugin for Python poetry build tool
#725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
80b9e8e
Initial import of `poetry` plugin
acourtneybrown 4612d60
Merge remote-tracking branch 'origin/main' into add-poetry
acourtneybrown 1a32c25
Merge remote-tracking branch 'origin/main' into add-poetry
acourtneybrown d0d1a8f
Add `repository` field to import & provision
acourtneybrown 585441d
Replace `-` with `_` in token env var.
acourtneybrown 58ba9d6
Add test recommended in PR review
acourtneybrown 81f2553
Add handling of env var import containing underscore
acourtneybrown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package poetry | ||
|
|
||
| import ( | ||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/importer" | ||
| "github.com/1Password/shell-plugins/sdk/schema" | ||
| "github.com/1Password/shell-plugins/sdk/schema/credname" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| func APIToken() schema.CredentialType { | ||
| return schema.CredentialType{ | ||
| Name: credname.APIToken, | ||
| DocsURL: sdk.URL("https://pypi.org/help/#apitoken"), | ||
| ManagementURL: sdk.URL("https://pypi.org/manage/account/#api-tokens"), | ||
| Fields: []schema.CredentialField{ | ||
| { | ||
| Name: fieldname.Token, | ||
| MarkdownDescription: "API token used to authenticate to PyPI.", | ||
| Secret: true, | ||
| Composition: &schema.ValueComposition{ | ||
| Prefix: "pypi-", | ||
| Charset: schema.Charset{ | ||
| Uppercase: true, | ||
| Lowercase: true, | ||
| Digits: true, | ||
| Specific: []rune{'-', '_'}, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| Name: fieldname.Repository, | ||
| MarkdownDescription: "Poetry repository name", | ||
| }, | ||
| }, | ||
| DefaultProvisioner: PoetryProvisioner(), | ||
| Importer: importer.TryAll( | ||
| TryPoetryEnvVars(), | ||
| importer.LinuxOnly(TryPoetryConfigFile("~/.config/pypoetry/auth.toml")), | ||
| importer.MacOnly(TryPoetryConfigFile("~/Library/Application Support/pypoetry/auth.toml")), | ||
| ), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package poetry | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/plugintest" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| func TestAPITokenImporter(t *testing.T) { | ||
| plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ | ||
| "POETRY_PYPI_TOKEN_PYPI environment variable": { | ||
| Environment: map[string]string{ | ||
| "POETRY_PYPI_TOKEN_PYPI": "pypi-AgEIcHlwaS5vcmc", | ||
| }, | ||
| ExpectedCandidates: []sdk.ImportCandidate{ | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-AgEIcHlwaS5vcmc", | ||
| fieldname.Repository: "pypi", | ||
| }, | ||
| NameHint: "pypi", | ||
| }, | ||
| }, | ||
| }, | ||
| "POETRY_PYPI_TOKEN_TESTPYPI environment variable": { | ||
| Environment: map[string]string{ | ||
| "POETRY_PYPI_TOKEN_TESTPYPI": "pypi-flit123abc", | ||
| }, | ||
| ExpectedCandidates: []sdk.ImportCandidate{ | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-flit123abc", | ||
| fieldname.Repository: "testpypi", | ||
| }, | ||
| NameHint: "testpypi", | ||
| }, | ||
| }, | ||
| }, | ||
| "environment variable with dash": { | ||
| Environment: map[string]string{ | ||
| "POETRY_PYPI_TOKEN_MY_REPO": "pypi-flit123abc", | ||
| }, | ||
| ExpectedCandidates: []sdk.ImportCandidate{ | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-flit123abc", | ||
| fieldname.Repository: "my_repo", | ||
| }, | ||
| NameHint: "my_repo", | ||
| }, | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-flit123abc", | ||
| fieldname.Repository: "my-repo", | ||
| }, | ||
| NameHint: "my-repo", | ||
| }, | ||
| }, | ||
| }, | ||
| "config file macos": { | ||
| OS: "darwin", | ||
| Files: map[string]string{ | ||
| "~/Library/Application Support/pypoetry/auth.toml": plugintest.LoadFixture(t, "auth.toml"), | ||
| }, | ||
| ExpectedCandidates: []sdk.ImportCandidate{ | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-fake_token_value", | ||
| fieldname.Repository: "pypi", | ||
| }, | ||
| NameHint: "pypi", | ||
| }, | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-another_fake_token_value", | ||
| fieldname.Repository: "testpypi", | ||
| }, | ||
| NameHint: "testpypi", | ||
| }, | ||
| }, | ||
| }, | ||
| "config file linux": { | ||
| OS: "linux", | ||
| Files: map[string]string{ | ||
| "~/.config/pypoetry/auth.toml": plugintest.LoadFixture(t, "auth.toml"), | ||
| }, | ||
| ExpectedCandidates: []sdk.ImportCandidate{ | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-fake_token_value", | ||
| fieldname.Repository: "pypi", | ||
| }, | ||
| NameHint: "pypi", | ||
| }, | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-another_fake_token_value", | ||
| fieldname.Repository: "testpypi", | ||
| }, | ||
| NameHint: "testpypi", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestAPITokenProvisioner(t *testing.T) { | ||
| plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ | ||
| "default provisioner sets Poetry env vars": { | ||
| ItemFields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-AgEIcHlwaS5vcmc", | ||
| fieldname.Repository: "mysecretpypi", | ||
| }, | ||
| ExpectedOutput: sdk.ProvisionOutput{ | ||
| Environment: map[string]string{ | ||
| "POETRY_PYPI_TOKEN_MYSECRETPYPI": "pypi-AgEIcHlwaS5vcmc", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package poetry | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/importer" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| func TryPoetryEnvVars() sdk.Importer { | ||
| return func(ctx context.Context, in sdk.ImportInput, out *sdk.ImportOutput) { | ||
| prefix := "POETRY_PYPI_TOKEN_" | ||
|
|
||
| for _, envVar := range os.Environ() { | ||
| // Split into key and value | ||
| key := envVar | ||
| value := "" | ||
| if before, after, ok := strings.Cut(envVar, "="); ok { | ||
| key = before | ||
| value = after | ||
| } | ||
|
|
||
| if after, ok := strings.CutPrefix(key, prefix); ok { | ||
| repo := strings.ToLower(after) | ||
| attempt := out.NewAttempt(importer.SourceEnvVars(key)) | ||
| attempt.AddCandidate(sdk.ImportCandidate{ | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: value, | ||
| fieldname.Repository: repo, | ||
| }, | ||
| NameHint: importer.SanitizeNameHint(repo), | ||
| }) | ||
| repoWithDashes := strings.ReplaceAll(repo, "_", "-") | ||
| if repoWithDashes != repo { | ||
| attempt.AddCandidate(sdk.ImportCandidate{ | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: value, | ||
| fieldname.Repository: repoWithDashes, | ||
| }, | ||
| NameHint: importer.SanitizeNameHint(repoWithDashes), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TryPoetryConfigFile returns an importer that scans the poetry CLI configuration file for API tokens. | ||
| func TryPoetryConfigFile(path string) sdk.Importer { | ||
| return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { | ||
| var config map[string]interface{} | ||
| if err := contents.ToTOML(&config); err != nil { | ||
| out.AddError(err) | ||
| return | ||
| } | ||
|
|
||
| pypiToken := config["pypi-token"].(map[string]interface{}) | ||
|
|
||
| for repo, token := range pypiToken { | ||
| tokenStr := token.(string) | ||
| if tokenStr == "" { | ||
| continue | ||
| } | ||
|
|
||
| out.AddCandidate(sdk.ImportCandidate{ | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.Token: tokenStr, | ||
| fieldname.Repository: repo, | ||
| }, | ||
| NameHint: importer.SanitizeNameHint(repo), | ||
| }) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package poetry | ||
|
|
||
| import ( | ||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/schema" | ||
| ) | ||
|
|
||
| func New() schema.Plugin { | ||
| return schema.Plugin{ | ||
| Name: "poetry", | ||
| Platform: schema.PlatformInfo{ | ||
| Name: "Poetry", | ||
| Homepage: sdk.URL("https://python-poetry.org"), | ||
| }, | ||
| Credentials: []schema.CredentialType{ | ||
| APIToken(), | ||
| }, | ||
| Executables: []schema.Executable{ | ||
| PoetryCLI(), | ||
| }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package poetry | ||
|
|
||
| import ( | ||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/needsauth" | ||
| "github.com/1Password/shell-plugins/sdk/schema" | ||
| "github.com/1Password/shell-plugins/sdk/schema/credname" | ||
| ) | ||
|
|
||
| func PoetryCLI() schema.Executable { | ||
| return schema.Executable{ | ||
| Name: "Poetry", | ||
| Runs: []string{"poetry"}, | ||
| DocsURL: sdk.URL("https://python-poetry.org/docs/"), | ||
| NeedsAuth: needsauth.IfAll( | ||
| needsauth.NotForHelpOrVersion(), | ||
| needsauth.ForCommand("publish"), | ||
| ), | ||
| Uses: []schema.CredentialUsage{ | ||
| { | ||
| Name: credname.APIToken, | ||
| Provisioner: PoetryProvisioner(), | ||
| }, | ||
| }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package poetry | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/plugintest" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| func TestPoetryCLIProvisioner(t *testing.T) { | ||
| plugintest.TestProvisioner(t, PoetryCLI().Uses[0].Provisioner, map[string]plugintest.ProvisionCase{ | ||
| "sets Poetry env vars": { | ||
| ItemFields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-AgEIcHlwaS5vcmc", | ||
| fieldname.Repository: "pypi", | ||
| }, | ||
| ExpectedOutput: sdk.ProvisionOutput{ | ||
| Environment: map[string]string{ | ||
| "POETRY_PYPI_TOKEN_PYPI": "pypi-AgEIcHlwaS5vcmc", | ||
| }, | ||
| }, | ||
| }, | ||
| "repository name with a dash": { | ||
| ItemFields: map[sdk.FieldName]string{ | ||
| fieldname.Token: "pypi-AgEIcHlwaS5vcmc", | ||
| fieldname.Repository: "my-repo", | ||
| }, | ||
| ExpectedOutput: sdk.ProvisionOutput{ | ||
| Environment: map[string]string{ | ||
| "POETRY_PYPI_TOKEN_MY_REPO": "pypi-AgEIcHlwaS5vcmc", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestPoetryCLINeedsAuth(t *testing.T) { | ||
| plugintest.TestNeedsAuth(t, PoetryCLI().NeedsAuth, map[string]plugintest.NeedsAuthCase{ | ||
| "requires auth for publish": { | ||
| Args: []string{"publish"}, | ||
| ExpectedNeedsAuth: true, | ||
| }, | ||
| "skips auth for help": { | ||
| Args: []string{"--help"}, | ||
| ExpectedNeedsAuth: false, | ||
| }, | ||
| "skips auth for version": { | ||
| Args: []string{"--version"}, | ||
| ExpectedNeedsAuth: false, | ||
| }, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package poetry | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| // poetryTokenEnvVarPrefix is the prefix of the environment variable that the poetry CLI uses to | ||
| // provision the API token for a package repository, e.g. "pypi" or "testpypi". | ||
| const poetryTokenEnvVarPrefix = "POETRY_PYPI_TOKEN_" | ||
|
|
||
| // poetryTokenEnvVar returns the name of the environment variable that the poetry CLI uses to | ||
| // provision the API token for the package repository with the given name, e.g. "pypi" or "testpypi". | ||
| func poetryTokenEnvVar(repositoryName string) string { | ||
| return fmt.Sprintf("%s%s", poetryTokenEnvVarPrefix, strings.ToUpper(strings.ReplaceAll(repositoryName, "-", "_"))) | ||
| } | ||
|
|
||
| type poetryProvisioner struct{} | ||
|
|
||
| // PoetryProvisioner provisions the API token for the supported package repositories via the | ||
| // environment variables that the poetry CLI understands. | ||
| func PoetryProvisioner() sdk.Provisioner { | ||
| return poetryProvisioner{} | ||
| } | ||
|
|
||
| func (p poetryProvisioner) Description() string { | ||
| return "Provision Poetry API token via POETRY_PYPI_TOKEN_* environment variables" | ||
| } | ||
|
|
||
| func (p poetryProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { | ||
| token := in.ItemFields[fieldname.Token] | ||
| repo := in.ItemFields[fieldname.Repository] | ||
| out.AddEnvVar(poetryTokenEnvVar(repo), token) | ||
| } | ||
|
|
||
| func (p poetryProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { | ||
| // Environment variables are automatically cleaned up when the process ends. | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.