11package cmd
22
33import (
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
1720type (
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://%[email protected] /%s/%s" ,
globalConfig .
GithubTestOrg .
Token ,
org ,
testRepo ),
97+ URL :
fmt .
Sprintf (
"https://%[email protected] /%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://%[email protected] /%s/%s" ,
globalConfig .
GithubTestOrg .
Token ,
org ,
target )}
109+ logger .Info ("Pushing changes..." )
110+ remote .
Config ().
URLs = []
string {
fmt .
Sprintf (
"https://%[email protected] /%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}
0 commit comments