Skip to content

Commit def010f

Browse files
committed
Add GitHub Copilot CLI shell plugin
Signed-off-by: Eddú Meléndez <eddu.melendez@gmail.com>
1 parent 17d2904 commit def010f

4 files changed

Lines changed: 125 additions & 0 deletions

File tree

plugins/copilot/auth_token.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package copilot
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/importer"
6+
"github.com/1Password/shell-plugins/sdk/provision"
7+
"github.com/1Password/shell-plugins/sdk/schema"
8+
"github.com/1Password/shell-plugins/sdk/schema/credname"
9+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
10+
)
11+
12+
func AuthToken() schema.CredentialType {
13+
return schema.CredentialType{
14+
Name: credname.AuthToken,
15+
DocsURL: sdk.URL("https://docs.github.com/en/copilot/how-tos/copilot-cli/set-up-copilot-cli/authenticate-copilot-cli"),
16+
ManagementURL: sdk.URL("https://github.com/settings/personal-access-tokens/new"),
17+
Fields: []schema.CredentialField{
18+
{
19+
Name: fieldname.Token,
20+
MarkdownDescription: "Token used to authenticate to GitHub Copilot.",
21+
Secret: true,
22+
},
23+
},
24+
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
25+
Importer: importer.TryEnvVarPair(defaultEnvVarMapping),
26+
}
27+
}
28+
29+
var defaultEnvVarMapping = map[string]sdk.FieldName{
30+
"COPILOT_GITHUB_TOKEN": fieldname.Token,
31+
}

plugins/copilot/auth_token_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package copilot
2+
3+
import (
4+
"testing"
5+
6+
"github.com/1Password/shell-plugins/sdk"
7+
"github.com/1Password/shell-plugins/sdk/plugintest"
8+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
9+
)
10+
11+
const exampleCopilotToken = "github_pat_OYXGsaLFxgNy9msXs44LFNzg3wh0VsXRGycViVc0iKPOqczc1QKlB3ZVVrm5ESukqKR8nE3jzPBEXAMPLE"
12+
13+
func TestAuthTokenProvisioner(t *testing.T) {
14+
plugintest.TestProvisioner(t, AuthToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{
15+
"default": {
16+
ItemFields: map[sdk.FieldName]string{
17+
fieldname.Token: exampleCopilotToken,
18+
},
19+
ExpectedOutput: sdk.ProvisionOutput{
20+
Environment: map[string]string{
21+
"COPILOT_GITHUB_TOKEN": exampleCopilotToken,
22+
},
23+
},
24+
},
25+
})
26+
}
27+
28+
func TestAuthTokenImporter(t *testing.T) {
29+
plugintest.TestImporter(t, AuthToken().Importer, map[string]plugintest.ImportCase{
30+
"COPILOT_GITHUB_TOKEN": {
31+
Environment: map[string]string{
32+
"COPILOT_GITHUB_TOKEN": exampleCopilotToken,
33+
},
34+
ExpectedCandidates: []sdk.ImportCandidate{
35+
{
36+
Fields: map[sdk.FieldName]string{
37+
fieldname.Token: exampleCopilotToken,
38+
},
39+
},
40+
},
41+
},
42+
"GITHUB_TOKEN ignored": {
43+
Environment: map[string]string{
44+
"COPILOT_GITHUB_TOKEN": "",
45+
"GITHUB_TOKEN": exampleCopilotToken,
46+
},
47+
ExpectedCandidates: []sdk.ImportCandidate{},
48+
},
49+
})
50+
}

plugins/copilot/copilot.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package copilot
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/needsauth"
6+
"github.com/1Password/shell-plugins/sdk/schema"
7+
"github.com/1Password/shell-plugins/sdk/schema/credname"
8+
)
9+
10+
func CopilotCLI() schema.Executable {
11+
return schema.Executable{
12+
Name: "GitHub Copilot CLI",
13+
Runs: []string{"copilot"},
14+
DocsURL: sdk.URL("https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-command-reference"),
15+
NeedsAuth: needsauth.NotForHelpOrVersion(),
16+
Uses: []schema.CredentialUsage{
17+
{
18+
Name: credname.AuthToken,
19+
},
20+
},
21+
}
22+
}

plugins/copilot/plugin.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package copilot
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/schema"
6+
)
7+
8+
func New() schema.Plugin {
9+
return schema.Plugin{
10+
Name: "copilot",
11+
Platform: schema.PlatformInfo{
12+
Name: "GitHub Copilot",
13+
Homepage: sdk.URL("https://github.com/features/copilot/cli"),
14+
},
15+
Credentials: []schema.CredentialType{
16+
AuthToken(),
17+
},
18+
Executables: []schema.Executable{
19+
CopilotCLI(),
20+
},
21+
}
22+
}

0 commit comments

Comments
 (0)