Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This is a C# based repository that produces several CLIs that are used by custom
- `cmd/gei/`, `cmd/ado2gh/`, `cmd/bbs2gh/`: Go CLI entry points
- `pkg/scriptgen/`: PowerShell script generation (ported from C#)
- `pkg/github/`: GitHub API client (REST + GraphQL)
- `pkg/ado/`: Azure DevOps API client
- `pkg/storage/`: Cloud storage clients (Azure Blob, AWS S3, GitHub-owned multipart)
- `pkg/archive/`: Archive upload orchestration
- `pkg/logger/`, `pkg/env/`: Shared Go packages
Expand All @@ -42,24 +43,24 @@ This is a C# based repository that produces several CLIs that are used by custom

## Go Port Sync Requirements

**Current state:** The `gei` CLI is fully ported to Go, including `migrate-repo`, `migrate-org`, and all alert migration commands. The GitHub API client, shared commands, and cloud storage clients are also ported.
**Current state:** `gei` and `ado2gh` are fully ported to Go. This includes the ADO API client, all ado2gh commands (migrate-repo, generate-script, inventory-report, etc.), and all gei commands. The GitHub API client, shared commands, and cloud storage clients are also ported.

**When making C# changes, check if the Go port needs updating:**

| C# Area | Go Equivalent | Sync Required? |
|----------|--------------|----------------|
| `src/gei/Commands/` (any command) | `cmd/gei/` | **Yes** — all gei commands are ported |
| `src/ado2gh/Commands/` (any command) | `cmd/ado2gh/` | **Yes** — all ado2gh commands are ported |
| `GenerateScriptCommandHandler.cs` (any CLI) | `cmd/{cli}/generate_script.go` + `pkg/scriptgen/generator.go` | **Yes** — scripts must be identical |
| `src/Octoshift/Services/GithubApi.cs` | `pkg/github/client.go` | **Yes** — API behavior must match |
| `src/Octoshift/Services/GithubClient.cs` | `pkg/github/client.go` | **Yes** — HTTP/auth behavior must match |
| `src/Octoshift/Services/AdoApi.cs` | `pkg/ado/client.go` | **Yes** — API behavior must match |
| Shared commands in `src/Octoshift/Commands/` | `internal/sharedcmd/` | **Yes** — command behavior must match |
| `src/Octoshift/Services/AzureApi.cs` | `pkg/storage/azure/client.go` | **Yes** — upload behavior must match |
| `src/Octoshift/Services/AwsApi.cs` | `pkg/storage/aws/client.go` | **Yes** — upload behavior must match |
| `src/Octoshift/Services/HttpDownloadService.cs` | `pkg/storage/ghowned/client.go` | **Yes** — multipart upload must match |
| `src/Octoshift/Services/ArchiveUploader.cs` | `pkg/archive/uploader.go` | **Yes** — orchestration must match |
| ADO API client (`src/Octoshift/Services/AdoApi.cs`) | Not yet ported | No |
| BBS API client (`src/Octoshift/Services/BbsApi.cs`) | Not yet ported | No |
| `ado2gh` commands | Not yet ported | No |
| `bbs2gh` commands | Not yet ported | No |

**Testing:** Run `go test ./...` to verify Go changes. Run `golangci-lint run` to check for lint issues.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ MigrationBackup/
cmd/gei/gei
cmd/ado2gh/ado2gh
cmd/bbs2gh/bbs2gh
gh-gei/
gh-ado2gh/
gh-bbs2gh/

# Go coverage reports
coverage/
Expand Down
175 changes: 175 additions & 0 deletions cmd/ado2gh/add_team_to_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package main

import (
"context"

"github.com/github/gh-gei/internal/cmdutil"
"github.com/github/gh-gei/pkg/env"
"github.com/github/gh-gei/pkg/github"
"github.com/github/gh-gei/pkg/logger"
"github.com/spf13/cobra"
)

// ---------------------------------------------------------------------------
// Consumer-defined interfaces
// ---------------------------------------------------------------------------

// addTeamToRepoGitHub defines the GitHub API methods needed by add-team-to-repo.
type addTeamToRepoGitHub interface {
GetTeamSlug(ctx context.Context, org, teamName string) (string, error)
AddTeamToRepo(ctx context.Context, org, teamSlug, repo, role string) error
}

// addTeamToRepoEnvProvider provides environment variable fallbacks.
type addTeamToRepoEnvProvider interface {
TargetGitHubPAT() string
}

// ---------------------------------------------------------------------------
// Args struct
// ---------------------------------------------------------------------------

type addTeamToRepoArgs struct {
githubOrg string
githubRepo string
team string
role string
githubPAT string
targetAPIURL string
}

// ---------------------------------------------------------------------------
// Command constructor (testable)
// ---------------------------------------------------------------------------

func newAddTeamToRepoCmd(
gh addTeamToRepoGitHub,
envProv addTeamToRepoEnvProvider,
log *logger.Logger,
) *cobra.Command {
var a addTeamToRepoArgs

cmd := &cobra.Command{
Use: "add-team-to-repo",
Short: "Adds a team to a repo with a specific role/permission",
Long: "Adds a team to a repo with a specific role/permission\n" +
"Note: Expects GH_PAT env variable or --github-pat option to be set.",
RunE: func(cmd *cobra.Command, _ []string) error {
return runAddTeamToRepo(cmd.Context(), gh, envProv, log, a)
},
}

cmd.Flags().StringVar(&a.githubOrg, "github-org", "", "GitHub organization name (REQUIRED)")
cmd.Flags().StringVar(&a.githubRepo, "github-repo", "", "GitHub repository name (REQUIRED)")
cmd.Flags().StringVar(&a.team, "team", "", "Team name (REQUIRED)")
cmd.Flags().StringVar(&a.role, "role", "", "Role/permission: pull, push, admin, maintain, triage (REQUIRED)")
cmd.Flags().StringVar(&a.githubPAT, "github-pat", "", "GitHub personal access token (falls back to GH_PAT env)")
cmd.Flags().StringVar(&a.targetAPIURL, "target-api-url", "", "API URL for the target GitHub instance")

return cmd
}

// ---------------------------------------------------------------------------
// Production command constructor
// ---------------------------------------------------------------------------

func newAddTeamToRepoCmdLive() *cobra.Command {
var a addTeamToRepoArgs

cmd := &cobra.Command{
Use: "add-team-to-repo",
Short: "Adds a team to a repo with a specific role/permission",
Long: "Adds a team to a repo with a specific role/permission\n" +
"Note: Expects GH_PAT env variable or --github-pat option to be set.",
RunE: func(cmd *cobra.Command, _ []string) error {
log := getLogger(cmd)
envProv := &addTeamToRepoEnvAdapter{prov: env.New()}

githubPAT := a.githubPAT
if githubPAT == "" {
githubPAT = envProv.TargetGitHubPAT()
}

apiURL := a.targetAPIURL
if apiURL == "" {
apiURL = "https://api.github.com"
}

gh := github.NewClient(githubPAT,
github.WithAPIURL(apiURL),
github.WithLogger(log),
github.WithVersion(version),
)

return runAddTeamToRepo(cmd.Context(), gh, envProv, log, a)
},
}

cmd.Flags().StringVar(&a.githubOrg, "github-org", "", "GitHub organization name (REQUIRED)")
cmd.Flags().StringVar(&a.githubRepo, "github-repo", "", "GitHub repository name (REQUIRED)")
cmd.Flags().StringVar(&a.team, "team", "", "Team name (REQUIRED)")
cmd.Flags().StringVar(&a.role, "role", "", "Role/permission: pull, push, admin, maintain, triage (REQUIRED)")
cmd.Flags().StringVar(&a.githubPAT, "github-pat", "", "GitHub personal access token (falls back to GH_PAT env)")
cmd.Flags().StringVar(&a.targetAPIURL, "target-api-url", "", "API URL for the target GitHub instance")

return cmd
}

type addTeamToRepoEnvAdapter struct {
prov *env.Provider
}

func (a *addTeamToRepoEnvAdapter) TargetGitHubPAT() string { return a.prov.TargetGitHubPAT() }

// ---------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------

func validateAddTeamToRepoArgs(a *addTeamToRepoArgs) error {
if err := cmdutil.ValidateRequired(a.githubOrg, "--github-org"); err != nil {
return err
}
if err := cmdutil.ValidateRequired(a.githubRepo, "--github-repo"); err != nil {
return err
}
if err := cmdutil.ValidateRequired(a.team, "--team"); err != nil {
return err
}
if err := cmdutil.ValidateRequired(a.role, "--role"); err != nil {
return err
}
if err := cmdutil.ValidateOneOf(a.role, "--role", "pull", "push", "admin", "maintain", "triage"); err != nil {
return err
}
return nil
}

// ---------------------------------------------------------------------------
// Runner
// ---------------------------------------------------------------------------

func runAddTeamToRepo(
ctx context.Context,
gh addTeamToRepoGitHub,
envProv addTeamToRepoEnvProvider,
log *logger.Logger,
a addTeamToRepoArgs,
) error {
if err := validateAddTeamToRepoArgs(&a); err != nil {
return err
}

log.Info("Adding team to repo...")

teamSlug, err := gh.GetTeamSlug(ctx, a.githubOrg, a.team)
if err != nil {
return err
}

if err := gh.AddTeamToRepo(ctx, a.githubOrg, teamSlug, a.githubRepo, a.role); err != nil {
return err
}

log.Success("Successfully added team to repo")
return nil
}
82 changes: 82 additions & 0 deletions cmd/ado2gh/add_team_to_repo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"bytes"
"context"
"testing"

"github.com/github/gh-gei/pkg/logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// ---------------------------------------------------------------------------
// Mock implementations
// ---------------------------------------------------------------------------

type mockAddTeamToRepoGitHub struct {
getTeamSlugFn func(ctx context.Context, org, teamName string) (string, error)
addTeamToRepoFn func(ctx context.Context, org, teamSlug, repo, role string) error
}

func (m *mockAddTeamToRepoGitHub) GetTeamSlug(ctx context.Context, org, teamName string) (string, error) {
return m.getTeamSlugFn(ctx, org, teamName)
}

func (m *mockAddTeamToRepoGitHub) AddTeamToRepo(ctx context.Context, org, teamSlug, repo, role string) error {
return m.addTeamToRepoFn(ctx, org, teamSlug, repo, role)
}

type mockAddTeamToRepoEnv struct {
targetPAT string
}

func (m *mockAddTeamToRepoEnv) TargetGitHubPAT() string { return m.targetPAT }

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

func TestAddTeamToRepo_HappyPath(t *testing.T) {
var buf bytes.Buffer
log := logger.New(false, &buf)

var capturedOrg, capturedTeamSlug, capturedRepo, capturedRole string

gh := &mockAddTeamToRepoGitHub{
getTeamSlugFn: func(_ context.Context, org, teamName string) (string, error) {
assert.Equal(t, "my-org", org)
assert.Equal(t, "my-team", teamName)
return "foo-slug", nil
},
addTeamToRepoFn: func(_ context.Context, org, teamSlug, repo, role string) error {
capturedOrg = org
capturedTeamSlug = teamSlug
capturedRepo = repo
capturedRole = role
return nil
},
}

cmd := newAddTeamToRepoCmd(gh, &mockAddTeamToRepoEnv{targetPAT: "gh-token"}, log)
cmd.SetOut(&buf)
cmd.SetErr(&buf)
cmd.SetArgs([]string{
"--github-org", "my-org",
"--github-repo", "my-repo",
"--team", "my-team",
"--role", "push",
})

err := cmd.Execute()
require.NoError(t, err)

assert.Equal(t, "my-org", capturedOrg)
assert.Equal(t, "foo-slug", capturedTeamSlug)
assert.Equal(t, "my-repo", capturedRepo)
assert.Equal(t, "push", capturedRole)

output := buf.String()
assert.Contains(t, output, "Adding team to repo...")
assert.Contains(t, output, "Successfully added team to repo")
}
Loading
Loading