@@ -8,14 +8,21 @@ import (
88
99 "github.com/jenkins-x/go-scm/scm"
1010 "github.com/jenkins-x/go-scm/scm/driver/github"
11- "github.com/mitchellh/go-homedir"
1211 "golang.org/x/oauth2"
1312 "gopkg.in/yaml.v2"
1413
1514 "github.com/bigkevmcd/promote/pkg/cache"
1615 "github.com/bigkevmcd/promote/pkg/util"
1716)
1817
18+ // PromoteService is the main driver for promoting files between two
19+ // repositories.
20+ //
21+ // It uses a GitCache to checkout the code to, and will copy the environment
22+ // configuration for the `fromEnv` to the `toEnv` in a named branch.
23+ //
24+ // The Git repositories are looked up in the mapping, and a pull request is
25+ // opened in the toEnv's project,
1926func PromoteService (cache cache.GitCache , token , service , fromEnv , toEnv , newBranchName string , mapping map [string ]string ) error {
2027 ctx := context .Background ()
2128 fromURL , ok := mapping [fromEnv ]
@@ -47,20 +54,9 @@ func PromoteService(cache cache.GitCache, token, service, fromEnv, toEnv, newBra
4754 return fmt .Errorf ("failed to commit and push branch for environment %v: %s" , toEnv , err )
4855 }
4956
50- prInput , err := makePullRequestInput ( fromEnv , fromURL , toEnv , toURL , newBranchName )
57+ pr , err := createPullRequest ( ctx , fromEnv , fromURL , toEnv , toURL , token , newBranchName )
5158 if err != nil {
52- // TODO: improve this
53- return err
54- }
55- user , repo , err := util .ExtractUserAndRepo (toURL )
56- if err != nil {
57- // TODO: improve this
58- return err
59- }
60-
61- pr , _ , err := createClient (token ).PullRequests .Create (ctx , fmt .Sprintf ("%s/%s" , user , repo ), prInput )
62- if err != nil {
63- // TODO: improve this
59+ // TODO: improve this error message
6460 return err
6561 }
6662 log .Printf ("created PR %d" , pr .Number )
@@ -70,12 +66,7 @@ func PromoteService(cache cache.GitCache, token, service, fromEnv, toEnv, newBra
7066// LoadMappingFromFile takes a filename with a YAML mapping of environment name
7167// to git repository URLs.
7268func LoadMappingFromFile (fname string ) (map [string ]string , error ) {
73- expanded , err := homedir .Expand (fname )
74- if err != nil {
75- return nil , fmt .Errorf ("failed to expand mapping filename: %w" , err )
76- }
77-
78- f , err := os .Open (expanded )
69+ f , err := os .Open (fname )
7970 if err != nil {
8071 return nil , err
8172 }
@@ -99,7 +90,10 @@ func createClient(token string) *scm.Client {
9990 return client
10091}
10192
102- func makePullRequestInput (fromEnv , fromURL , toEnv , toURL , branchName string ) (* scm.PullRequestInput , error ) {
93+ // TODO: OptionFuncs for Base and Title?
94+ // TODO: For the Head, should this try and determine whether or not this is a
95+ // fork ("user" of both repoURLs) and if so, simplify the Head?
96+ func makePullRequestInput (fromEnv , fromURL , toEnv , branchName string ) (* scm.PullRequestInput , error ) {
10397 title := fmt .Sprintf ("promotion from %s to %s" , fromEnv , toEnv )
10498 fromUser , _ , err := util .ExtractUserAndRepo (fromURL )
10599 if err != nil {
@@ -112,3 +106,21 @@ func makePullRequestInput(fromEnv, fromURL, toEnv, toURL, branchName string) (*s
112106 Body : "this is a test body" ,
113107 }, nil
114108}
109+
110+ func createPullRequest (ctx context.Context , fromEnv , fromURL , toEnv , toURL , token , newBranchName string ) (* scm.PullRequest , error ) {
111+ prInput , err := makePullRequestInput (fromEnv , fromURL , toEnv , newBranchName )
112+ if err != nil {
113+ // TODO: improve this error message
114+ return nil , err
115+ }
116+
117+ user , repo , err := util .ExtractUserAndRepo (toURL )
118+ if err != nil {
119+ // TODO: improve this error message
120+ return nil , err
121+ }
122+ // TODO: come up with a better way of generating the repo URL (this
123+ // only works for GitHub)
124+ pr , _ , err := createClient (token ).PullRequests .Create (ctx , fmt .Sprintf ("%s/%s" , user , repo ), prInput )
125+ return pr , err
126+ }
0 commit comments