Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit 3e418ee

Browse files
committed
using new err pkg
1 parent 730e295 commit 3e418ee

File tree

7 files changed

+306
-194
lines changed

7 files changed

+306
-194
lines changed

cmd/hiring_send.go

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
package cmd
22

33
import (
4+
"context"
5+
"errors"
46
"fmt"
57
"os"
68

79
"github.com/google/go-github/github"
10+
"github.com/hashicorp/errwrap"
811
"github.com/hellofresh/github-cli/pkg/config"
12+
gh "github.com/hellofresh/github-cli/pkg/github"
13+
"github.com/hellofresh/github-cli/pkg/log"
914
"github.com/hellofresh/github-cli/pkg/repo"
10-
"github.com/pkg/errors"
11-
log "github.com/sirupsen/logrus"
1215
"github.com/spf13/cobra"
1316
"gopkg.in/src-d/go-git.v4"
1417
"gopkg.in/src-d/go-git.v4/storage/memory"
1518
)
1619

1720
type (
1821
// HiringSendOpts are the flags for the send a hiring test command
19-
HiringSendOpts struct {
20-
Org string
21-
}
22+
HiringSendOpts struct{}
2223
)
2324

2425
// NewHiringSendCmd creates a new send hiring test command
25-
func NewHiringSendCmd() *cobra.Command {
26-
opts := &HiringSendOpts{}
26+
func NewHiringSendCmd(ctx context.Context) *cobra.Command {
2727
cmd := &cobra.Command{
28-
Use: "send [username] [repo]",
29-
Short: "Creates a new hellofresh hiring test",
30-
Long: `Creates a new hellofresh hiring test based on the rules defined on your .github.toml`,
31-
PreRunE: setupConnection,
28+
Use: "send [username] [repo]",
29+
Short: "Creates a new hellofresh hiring test",
30+
Long: `Creates a new hellofresh hiring test based on the rules defined on your .github.toml`,
3231
RunE: func(cmd *cobra.Command, args []string) error {
33-
return RunCreateTestRepo(args[0], args[1], opts)
32+
return RunCreateTestRepo(ctx, args[0], args[1])
3433
},
3534
Args: func(cmd *cobra.Command, args []string) error {
3635
if len(args) < 1 || args[0] == "" {
@@ -45,77 +44,79 @@ func NewHiringSendCmd() *cobra.Command {
4544
},
4645
}
4746

48-
cmd.Flags().StringVarP(&opts.Org, "organization", "o", "", "Github's organization")
49-
5047
return cmd
5148
}
5249

5350
// RunCreateTestRepo runs the command to create a new hiring test repository
54-
func RunCreateTestRepo(candidate string, testRepo string, opts *HiringSendOpts) error {
51+
func RunCreateTestRepo(ctx context.Context, candidate string, testRepo string) error {
5552
var err error
5653

57-
org := opts.Org
58-
if org == "" {
59-
org = globalConfig.GithubTestOrg.Organization
54+
logger := log.WithContext(ctx)
55+
cfg := config.WithContext(ctx)
56+
githubClient := gh.WithContext(ctx)
57+
if githubClient == nil {
58+
return errors.New("failed to get github client")
6059
}
60+
61+
org := cfg.Github.Organization
6162
if org == "" {
62-
return errors.New("Please provide an organization")
63+
return errors.New("please provide an organization")
6364
}
6465

6566
target := fmt.Sprintf("%s-%s", candidate, testRepo)
6667

6768
creator := repo.NewGithub(githubClient)
6869

69-
log.Info("Creating repository...")
70-
_, err = creator.CreateRepo(org, &github.Repository{
70+
logger.Info("Creating repository...")
71+
_, err = creator.CreateRepo(ctx, org, &github.Repository{
7172
Name: github.String(target),
7273
Private: github.Bool(true),
7374
HasIssues: github.Bool(false),
7475
HasPages: github.Bool(false),
7576
HasWiki: github.Bool(false),
7677
})
7778
if err != nil {
78-
return errors.Wrap(err, "Could not create github repo for candidate")
79+
return errwrap.Wrapf("could not create github repo for candidate: {{err}}", err)
7980
}
8081

81-
log.Info("Adding collaborators to repository...")
82+
logger.Info("Adding collaborators to repository...")
8283
collaboratorsOpts := []*config.Collaborator{
8384
&config.Collaborator{
8485
Username: candidate,
8586
Permission: "push",
8687
},
8788
}
88-
err = creator.AddCollaborators(target, org, collaboratorsOpts)
89+
err = creator.AddCollaborators(ctx, target, org, collaboratorsOpts)
8990
if err != nil {
90-
return errors.Wrap(err, "Could not add collaborators to repository")
91+
return errwrap.Wrapf("could not add collaborators to repository: {{err}}", err)
9192
}
9293

93-
log.Info("Cloning repository...")
94+
logger.Info("Cloning repository...")
9495
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
9596
Progress: os.Stdout,
96-
URL: fmt.Sprintf("https://%s@github.com/%s/%s", globalConfig.GithubTestOrg.Token, org, testRepo),
97+
URL: fmt.Sprintf("https://%s@github.com/%s/%s", cfg.GithubTestOrg.Token, org, testRepo),
9798
})
9899
if err != nil {
99-
return errors.Wrap(err, "Error cloning to repository")
100+
return errwrap.Wrapf("error cloning to repository: {{err}}", err)
100101
}
101102

102-
log.Info("Changing remote...")
103+
logger.Info("Changing remote...")
103104
remote, err := r.Remote(git.DefaultRemoteName)
104105
if err != nil {
105-
return errors.Wrap(err, "Error changing remote for repository")
106+
return errwrap.Wrapf("error changing remote for repository: {{err}}", err)
106107
}
107108

108-
log.Info("Pushing changes...")
109-
remote.Config().URLs = []string{fmt.Sprintf("https://%s@github.com/%s/%s", globalConfig.GithubTestOrg.Token, org, target)}
109+
logger.Info("Pushing changes...")
110+
remote.Config().URLs = []string{fmt.Sprintf("https://%s@github.com/%s/%s", cfg.GithubTestOrg.Token, org, target)}
110111
err = remote.Push(&git.PushOptions{
111112
RemoteName: git.DefaultRemoteName,
112113
Progress: os.Stdout,
113114
})
114115
if err != nil {
115-
return errors.Wrap(err, "Error pushing to repository")
116+
return errwrap.Wrapf("error pushing to repository: {{err}}", err)
116117
}
117118

118-
log.Infof("Done! Hiring test for %s is created", candidate)
119+
logger.Infof("Done! Hiring test for %s is created", candidate)
119120

120121
return nil
121122
}

cmd/hiring_unseat.go

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package cmd
22

33
import (
44
"context"
5+
"errors"
56
"math"
67
"time"
78

89
"github.com/google/go-github/github"
9-
"github.com/pkg/errors"
10-
log "github.com/sirupsen/logrus"
10+
"github.com/hashicorp/errwrap"
11+
"github.com/hellofresh/github-cli/pkg/config"
12+
gh "github.com/hellofresh/github-cli/pkg/github"
13+
"github.com/hellofresh/github-cli/pkg/log"
14+
"github.com/sirupsen/logrus"
1115
"github.com/spf13/cobra"
1216
)
1317

@@ -19,96 +23,102 @@ const (
1923
type (
2024
// UnseatOpts are the flags for the unseat command
2125
UnseatOpts struct {
22-
Org string
2326
Page int
2427
ReposPerPage int
2528
}
2629
)
2730

2831
// NewHiringUnseat creates a new hiring unseat command
29-
func NewHiringUnseat() *cobra.Command {
32+
func NewHiringUnseat(ctx context.Context) *cobra.Command {
3033
opts := &UnseatOpts{}
3134

3235
cmd := &cobra.Command{
33-
Use: "unseat",
34-
Short: "Removes external collaborators from repositories",
35-
Long: `Removes external (people not in the organization) collaborators from repositories`,
36-
PreRunE: setupConnection,
36+
Use: "unseat",
37+
Short: "Removes external collaborators from repositories",
38+
Long: `Removes external (people not in the organization) collaborators from repositories`,
3739
RunE: func(cmd *cobra.Command, args []string) error {
38-
return RunUnseat(opts)
40+
return RunUnseat(ctx, opts)
3941
},
4042
}
4143

42-
cmd.Flags().StringVarP(&opts.Org, "organization", "o", "", "Github's organization")
4344
cmd.Flags().IntVar(&opts.ReposPerPage, "page-size", 50, "How many repositories should we get per page? (max 100)")
4445
cmd.Flags().IntVar(&opts.Page, "page", 1, "Starting page for repositories")
4546

4647
return cmd
4748
}
4849

4950
// RunUnseat runs the command to create a new hiring test repository
50-
func RunUnseat(opts *UnseatOpts) error {
51+
func RunUnseat(ctx context.Context, opts *UnseatOpts) error {
5152
var unseatedCollaborators int
52-
ctx := context.Background()
5353

54-
org := opts.Org
55-
if org == "" {
56-
org = globalConfig.GithubTestOrg.Organization
54+
logger := log.WithContext(ctx)
55+
cfg := config.WithContext(ctx)
56+
githubClient := gh.WithContext(ctx)
57+
if githubClient == nil {
58+
return errors.New("failed to get github client")
5759
}
60+
61+
org := cfg.Github.Organization
5862
if org == "" {
59-
return errors.New("Please provide an organization")
63+
return errors.New("please provide an organization")
6064
}
6165

62-
log.Info("Fetching repositories...")
63-
allRepos, err := fetchAllRepos(org, opts.ReposPerPage, opts.Page)
66+
logger.Info("Fetching repositories...")
67+
allRepos, err := fetchAllRepos(ctx, org, opts.ReposPerPage, opts.Page)
6468
if err != nil {
65-
return errors.Wrap(err, "Could not retrieve repositories")
69+
return errwrap.Wrapf("could not retrieve repositories: {{err}}", err)
6670
}
67-
log.Infof("%d repositories fetched!", len(allRepos))
71+
logger.Infof("%d repositories fetched!", len(allRepos))
6872

69-
log.Info("Removing outside colaborators...")
73+
logger.Info("Removing outside colaborators...")
7074
for _, repo := range allRepos {
7175
if isRepoInactive(repo) {
7276
continue
7377
}
7478

7579
repoName := *repo.Name
76-
log.WithField("repo", repoName).Debug("Fetching outside collaborators")
80+
logger.WithField("repo", repoName).Debug("Fetching outside collaborators")
7781
outsideCollaborators, _, err := githubClient.Repositories.ListCollaborators(ctx, org, repoName, &github.ListCollaboratorsOptions{
7882
Affiliation: "outside",
7983
})
8084
if err != nil {
81-
return errors.Wrap(err, "Could not retrieve outside collaborators")
85+
return errwrap.Wrapf("could not retrieve outside collaborators: {{err}}", err)
8286
}
8387

8488
for _, collaborator := range outsideCollaborators {
85-
log.WithFields(log.Fields{
89+
logger.WithFields(logrus.Fields{
8690
"repo": repoName,
8791
"collaborator": collaborator.GetLogin(),
8892
}).Info("Deleting outside collaborators")
8993
_, err := githubClient.Repositories.RemoveCollaborator(ctx, org, repoName, collaborator.GetLogin())
9094
if err != nil {
91-
return errors.Wrap(err, "Could not unseat outside collaborator")
95+
return errwrap.Wrapf("could not unseat outside collaborator: {{err}}", err)
9296
}
9397

9498
unseatedCollaborators++
9599
}
96100
}
97101

98-
log.Infof("Done! %d outside collaborators unseated", unseatedCollaborators)
102+
logger.Infof("Done! %d outside collaborators unseated", unseatedCollaborators)
99103
return nil
100104
}
101105

102-
func fetchAllRepos(owner string, reposPerPage int, page int) ([]*github.Repository, error) {
106+
func fetchAllRepos(ctx context.Context, owner string, reposPerPage int, page int) ([]*github.Repository, error) {
103107
var allRepos []*github.Repository
104108

109+
logger := log.WithContext(ctx)
110+
githubClient := gh.WithContext(ctx)
111+
if githubClient == nil {
112+
return nil, errors.New("failed to get github client")
113+
}
114+
105115
opt := &github.RepositoryListByOrgOptions{
106116
ListOptions: github.ListOptions{PerPage: reposPerPage, Page: page},
107117
}
108118

109119
for {
110-
log.Debugf("Fetching repositories page [%d]", opt.Page)
111-
repos, resp, err := githubClient.Repositories.ListByOrg(context.Background(), owner, opt)
120+
logger.Debugf("Fetching repositories page [%d]", opt.Page)
121+
repos, resp, err := githubClient.Repositories.ListByOrg(ctx, owner, opt)
112122
if err != nil {
113123
return allRepos, err
114124
}

0 commit comments

Comments
 (0)