From aedd05e96e94164f7413b0a28bcfa815867835ca Mon Sep 17 00:00:00 2001 From: Micah Benn Date: Fri, 19 Dec 2025 00:59:51 -0600 Subject: [PATCH] Add Buildkite plugin --- plugins/buildkite/api_token.go | 96 +++++++++++++++++++++++++ plugins/buildkite/api_token_test.go | 91 +++++++++++++++++++++++ plugins/buildkite/bk.go | 26 +++++++ plugins/buildkite/plugin.go | 22 ++++++ plugins/buildkite/test-fixtures/bk.yaml | 5 ++ 5 files changed, 240 insertions(+) create mode 100644 plugins/buildkite/api_token.go create mode 100644 plugins/buildkite/api_token_test.go create mode 100644 plugins/buildkite/bk.go create mode 100644 plugins/buildkite/plugin.go create mode 100644 plugins/buildkite/test-fixtures/bk.yaml diff --git a/plugins/buildkite/api_token.go b/plugins/buildkite/api_token.go new file mode 100644 index 00000000..f2311552 --- /dev/null +++ b/plugins/buildkite/api_token.go @@ -0,0 +1,96 @@ +package buildkite + +import ( + "context" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/importer" + "github.com/1Password/shell-plugins/sdk/provision" + "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://buildkite.com/docs/platform/cli"), + ManagementURL: sdk.URL("https://buildkite.com/user/api-access-tokens"), + Fields: []schema.CredentialField{ + { + Name: fieldname.Organization, + MarkdownDescription: "Organization slug for your Buildkite account.", + Secret: false, + Composition: &schema.ValueComposition{ + Charset: schema.Charset{ + Uppercase: true, + Lowercase: true, + Digits: true, + Symbols: false, + }, + }, + }, + { + Name: fieldname.Token, + MarkdownDescription: "API Token used to authenticate with Buildkite.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 45, + Prefix: "bkua_", + Charset: schema.Charset{ + Lowercase: true, + Digits: true, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + TryBuildkiteConfigFile(), + )} +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "BUILDKITE_ORGANIZATION_SLUG": fieldname.Organization, + "BUILDKITE_API_TOKEN": fieldname.Token, +} + +// Check if the platform stores the API Token in a local config file, and if so, +// implement the function below to add support for importing it. +func TryBuildkiteConfigFile() sdk.Importer { + return importer.TryFile("~/.config/bk.yaml", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { + var config Config + if err := contents.ToYAML(&config); err != nil { + out.AddError(err) + return + } + + if len(config.Organizations) == 0 { + return + } + + for organizationSlug, organization := range config.Organizations { + + if organizationSlug == "" || organization.Token == "" { + return + } + + out.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.Token: organization.Token, + fieldname.Organization: organizationSlug, + }, + NameHint: importer.SanitizeNameHint(organizationSlug), + }) + } + }) +} + +type Config struct { + Organizations map[string]Organization `yaml:"organizations"` +} + +type Organization struct { + Token string `yaml:"api_token"` +} diff --git a/plugins/buildkite/api_token_test.go b/plugins/buildkite/api_token_test.go new file mode 100644 index 00000000..01940548 --- /dev/null +++ b/plugins/buildkite/api_token_test.go @@ -0,0 +1,91 @@ +package buildkite + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestAPITokenProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.Organization: "example", + fieldname.Token: "bkua_abcdefghijklmnopqrstuvwxyz1234567890abcd", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "BUILDKITE_ORGANIZATION_SLUG": "example", + "BUILDKITE_API_TOKEN": "bkua_abcdefghijklmnopqrstuvwxyz1234567890abcd", + }, + }, + }, + }) +} + +func TestAPITokenImporter(t *testing.T) { + plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ + "BUILDKITE_ORGANIZATION_SLUG": "example", + "BUILDKITE_API_TOKEN": "bkua_abcdefghijklmnopqrstuvwxyz1234567890abcd", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Organization: "example", + fieldname.Token: "bkua_abcdefghijklmnopqrstuvwxyz1234567890abcd", + }, + }, + }, + }, + "config file default path": { + Files: map[string]string{ + "~/.config/bk.yaml": plugintest.LoadFixture(t, "bk.yaml"), + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Organization: "example", + fieldname.Token: "bkua_abcdefghijklmnopqrstuvwxyz1234567890abcd", + }, + NameHint: "example", + }, + { + Fields: map[sdk.FieldName]string{ + fieldname.Organization: "example2", + fieldname.Token: "bkua_abcdefghijklmnopqrstuvwxyz1234567890defg", + }, + NameHint: "example2", + }, + }, + }, + }) +} + +func TestAPIKeyNeedsAuth(t *testing.T) { + plugintest.TestNeedsAuth(t, BuildkiteCLI().NeedsAuth, map[string]plugintest.NeedsAuthCase{ + "no for --help": { + Args: []string{"--help"}, + ExpectedNeedsAuth: false, + }, + "no for --version": { + Args: []string{"--version"}, + ExpectedNeedsAuth: false, + }, + "no for configure": { + Args: []string{"configure"}, + ExpectedNeedsAuth: false, + }, + "no for without args": { + Args: []string{}, + ExpectedNeedsAuth: false, + }, + "yes for all other commands": { + Args: []string{"example"}, + ExpectedNeedsAuth: true, + }, + }) +} diff --git a/plugins/buildkite/bk.go b/plugins/buildkite/bk.go new file mode 100644 index 00000000..c542b871 --- /dev/null +++ b/plugins/buildkite/bk.go @@ -0,0 +1,26 @@ +package buildkite + +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 BuildkiteCLI() schema.Executable { + return schema.Executable{ + Name: "Buildkite CLI", + Runs: []string{"bk"}, + DocsURL: sdk.URL("https://buildkite.com/docs/cli"), + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.NotWithoutArgs(), + needsauth.NotForExactArgs("configure"), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.APIToken, + }, + }, + } +} diff --git a/plugins/buildkite/plugin.go b/plugins/buildkite/plugin.go new file mode 100644 index 00000000..5c835393 --- /dev/null +++ b/plugins/buildkite/plugin.go @@ -0,0 +1,22 @@ +package buildkite + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "buildkite", + Platform: schema.PlatformInfo{ + Name: "Buildkite", + Homepage: sdk.URL("https://buildkite.com"), + }, + Credentials: []schema.CredentialType{ + APIToken(), + }, + Executables: []schema.Executable{ + BuildkiteCLI(), + }, + } +} diff --git a/plugins/buildkite/test-fixtures/bk.yaml b/plugins/buildkite/test-fixtures/bk.yaml new file mode 100644 index 00000000..baa1a22f --- /dev/null +++ b/plugins/buildkite/test-fixtures/bk.yaml @@ -0,0 +1,5 @@ +organizations: + example: + api_token: bkua_abcdefghijklmnopqrstuvwxyz1234567890abcd + example2: + api_token: bkua_abcdefghijklmnopqrstuvwxyz1234567890defg