Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions plugins/sonarqube/cli_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package sonarqube

import (
"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 CLIToken() schema.CredentialType {
return schema.CredentialType{
Name: credname.CLIToken,
DocsURL: sdk.URL("https://docs.sonarsource.com/sonarqube-cli/using-sonarqube-cli/environment-variables"),
ManagementURL: sdk.URL("https://sonarcloud.io/account/security"),
Fields: []schema.CredentialField{
{
Name: fieldname.Token,
MarkdownDescription: "User token used to authenticate to SonarQube CLI.",
Secret: true,
},
{
Name: fieldname.Organization,
MarkdownDescription: "SonarQube Cloud organization key. Use together with the token to authenticate to SonarQube Cloud.",
Secret: false,
Optional: true,
},
{
Name: fieldname.URL,
MarkdownDescription: "Server URL. Use together with the token to authenticate to a self-hosted SonarQube instance or a specific SonarQube Cloud region.",
Secret: false,
Optional: true,
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
),
}
}

var defaultEnvVarMapping = map[string]sdk.FieldName{
"SONARQUBE_CLI_TOKEN": fieldname.Token,
"SONARQUBE_CLI_ORG": fieldname.Organization,
"SONARQUBE_CLI_SERVER": fieldname.URL,
}
93 changes: 93 additions & 0 deletions plugins/sonarqube/cli_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package sonarqube

import (
"testing"

"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/plugintest"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)

func TestCLITokenImporter(t *testing.T) {
plugintest.TestImporter(t, CLIToken().Importer, map[string]plugintest.ImportCase{
"token only": {
Environment: map[string]string{
"SONARQUBE_CLI_TOKEN": "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
},
},
},
},
"token with organization": {
Environment: map[string]string{
"SONARQUBE_CLI_TOKEN": "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
"SONARQUBE_CLI_ORG": "my-org",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
fieldname.Organization: "my-org",
},
},
},
},
"token with server": {
Environment: map[string]string{
"SONARQUBE_CLI_TOKEN": "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
"SONARQUBE_CLI_SERVER": "https://sonarqube.example.com",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Token: "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
fieldname.URL: "https://sonarqube.example.com",
},
},
},
},
})
}

func TestCLITokenProvisioner(t *testing.T) {
plugintest.TestProvisioner(t, CLIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{
"token only": {
ItemFields: map[sdk.FieldName]string{
fieldname.Token: "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"SONARQUBE_CLI_TOKEN": "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
},
},
},
"token with organization": {
ItemFields: map[sdk.FieldName]string{
fieldname.Token: "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
fieldname.Organization: "my-org",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"SONARQUBE_CLI_TOKEN": "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
"SONARQUBE_CLI_ORG": "my-org",
},
},
},
"token with server": {
ItemFields: map[sdk.FieldName]string{
fieldname.Token: "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
fieldname.URL: "https://sonarqube.example.com",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"SONARQUBE_CLI_TOKEN": "sqp_a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
"SONARQUBE_CLI_SERVER": "https://sonarqube.example.com",
},
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/sonarqube/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sonarqube

import (
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/schema"
)

func New() schema.Plugin {
return schema.Plugin{
Name: "sonarqube",
Platform: schema.PlatformInfo{
Name: "SonarQube CLI",
Homepage: sdk.URL("https://docs.sonarsource.com/sonarqube-cli/"),
},
Comment on lines +11 to +14

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't apply IMO

Credentials: []schema.CredentialType{
CLIToken(),
},
Executables: []schema.Executable{
SonarScannerCLI(),
},
}
}
30 changes: 30 additions & 0 deletions plugins/sonarqube/sonar_scanner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sonarqube

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 SonarScannerCLI() schema.Executable {
return schema.Executable{
Name: "SonarQube CLI",
Runs: []string{"sonar"},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

partially fixed

DocsURL: sdk.URL("https://docs.sonarsource.com/sonarqube-cli/"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.NotForExactArgs("auth", "login"),
needsauth.NotForExactArgs("auth", "logout"),
needsauth.NotWhenContainsArgs("config", "telemetry"),
needsauth.NotWhenContainsArgs("system"),
needsauth.NotWhenContainsArgs("self-update"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.CLIToken,
},
},
}
}