@@ -6,18 +6,19 @@ import (
66 "encoding/json"
77 "errors"
88 "fmt"
9- "io"
10- "log"
11- "net"
12- "net/url"
13- "os"
14-
159 "github.com/MakeNowJust/heredoc"
1610 "github.com/dependabot/cli/internal/infra"
1711 "github.com/dependabot/cli/internal/model"
1812 "github.com/dependabot/cli/internal/server"
1913 "github.com/spf13/cobra"
2014 "gopkg.in/yaml.v3"
15+ "io"
16+ "log"
17+ "net"
18+ "net/url"
19+ "os"
20+ "regexp"
21+ "strings"
2122)
2223
2324var updateCmd = NewUpdateCommand ()
@@ -55,6 +56,8 @@ func NewUpdateCommand() *cobra.Command {
5556 Short : "Perform an update job" ,
5657 Example : heredoc .Doc (`
5758 $ dependabot update go_modules dependabot/cli
59+ $ dependabot update go_modules git@github.com:dependabot/cli.git
60+ $ dependabot update go_modules https://github.com/dependabot/cli.git
5861 $ dependabot update -f input.yml
5962 ` ),
6063 RunE : func (cmd * cobra.Command , args []string ) error {
@@ -208,6 +211,34 @@ func readArguments(cmd *cobra.Command, flags *UpdateFlags) (*model.Input, error)
208211 return nil , errors .New ("requires a repo argument" )
209212 }
210213
214+ var hostname , apiEndpoint string
215+ // if the repo is a git URL, extract the hostname
216+ // accepts org/repo, git@host:org/repo.git, and https://host/org/repo.git
217+ if u , err := url .Parse (repo ); err == nil {
218+ hostname = u .Hostname ()
219+ apiEndpoint = fmt .Sprintf ("%s://%s/api/v3" , u .Scheme , hostname )
220+ repo = u .Path
221+ } else {
222+ // at this point, it may be org/repo or username@host:org/repo.git
223+ re := regexp .MustCompile (`^(?:(?:[a-zA-Z0-9._%+-]+@|https?://)?([^:/]+):)?([^/]+)/([^/]+)(?:\.git)?$` )
224+ matches := re .FindStringSubmatch (repo )
225+ if len (matches ) < 4 {
226+ return nil , fmt .Errorf ("invalid repo format: %s" , repo )
227+ }
228+ if matches [1 ] != "" {
229+ hostname = matches [1 ]
230+ apiEndpoint = fmt .Sprintf ("https://%s/api/v3" , hostname )
231+ }
232+ repo = fmt .Sprintf ("%s/%s" , matches [2 ], matches [3 ])
233+ }
234+ repo = strings .TrimPrefix (strings .TrimSuffix (repo , ".git" ), "/" )
235+ if hostname == "" {
236+ hostname = "github.com"
237+ apiEndpoint = "https://api.github.com"
238+ }
239+
240+ log .Println ("Using hostname:" , hostname , "api endpoint:" , apiEndpoint )
241+
211242 allowed := []model.Allowed {{UpdateType : "all" }}
212243 if len (flags .dependencies ) > 0 {
213244 allowed = allowed [:0 ]
@@ -238,8 +269,8 @@ func readArguments(cmd *cobra.Command, flags *UpdateFlags) (*model.Input, error)
238269 Directory : flags .directory ,
239270 Commit : flags .commit ,
240271 Branch : flags .branch ,
241- Hostname : nil ,
242- APIEndpoint : nil ,
272+ Hostname : & hostname ,
273+ APIEndpoint : & apiEndpoint ,
243274 },
244275 UpdateSubdependencies : false ,
245276 UpdatingAPullRequest : false ,
@@ -315,17 +346,21 @@ func processInput(input *model.Input, flags *UpdateFlags) {
315346
316347 if hasLocalToken && ! isGitSourceInCreds {
317348 log .Println ("Inserting $LOCAL_GITHUB_ACCESS_TOKEN into credentials" )
349+ host := "github.com"
350+ if input .Job .Source .Hostname != nil && * input .Job .Source .Hostname != "" {
351+ host = * input .Job .Source .Hostname
352+ }
318353 input .Credentials = append (input .Credentials , model.Credential {
319354 "type" : "git_source" ,
320- "host" : "github.com" ,
355+ "host" : host ,
321356 "username" : "x-access-token" ,
322357 "password" : "$LOCAL_GITHUB_ACCESS_TOKEN" ,
323358 })
324359 if len (input .Job .CredentialsMetadata ) > 0 {
325360 // Add the metadata since the next section will be skipped.
326361 input .Job .CredentialsMetadata = append (input .Job .CredentialsMetadata , map [string ]any {
327362 "type" : "git_source" ,
328- "host" : "github.com" ,
363+ "host" : host ,
329364 })
330365 }
331366 }
0 commit comments