@@ -30,8 +30,8 @@ import (
3030
3131 "github.com/google/go-cmp/cmp"
3232 "github.com/google/go-cmp/cmp/cmpopts"
33- "github.com/google/go-github/v69/github"
3433 "github.com/googleapis/librarian/internal/config"
34+ "github.com/googleapis/librarian/internal/github"
3535 "gopkg.in/yaml.v3"
3636)
3737
@@ -180,6 +180,7 @@ func TestCleanAndCopy(t *testing.T) {
180180 cmd := exec .Command (
181181 "go" ,
182182 "run" ,
183+ "-tags" , "e2etest" ,
183184 "github.com/googleapis/librarian/cmd/librarian" ,
184185 "generate" ,
185186 fmt .Sprintf ("--api=%s" , apiToGenerate ),
@@ -406,15 +407,24 @@ func TestReleaseInit(t *testing.T) {
406407 updatedState string
407408 wantChangelog string
408409 libraryID string
410+ push bool
409411 wantErr bool
410412 }{
411413 {
412- name : "runs successfully" ,
414+ name : "runs successfully without push " ,
413415 initialRepoStateDir : "testdata/e2e/release/init/repo_init" ,
414416 updatedState : "testdata/e2e/release/init/updated-state.yaml" ,
415417 wantChangelog : "testdata/e2e/release/init/CHANGELOG.md" ,
416418 libraryID : "go-google-cloud-pubsub-v1" ,
417419 },
420+ {
421+ name : "runs successfully with push" ,
422+ initialRepoStateDir : "testdata/e2e/release/init/repo_init" ,
423+ updatedState : "testdata/e2e/release/init/updated-state.yaml" ,
424+ wantChangelog : "testdata/e2e/release/init/CHANGELOG.md" ,
425+ libraryID : "go-google-cloud-pubsub-v1" ,
426+ push : true , // Enable --push for this case
427+ },
418428 } {
419429 t .Run (test .name , func (t * testing.T ) {
420430 workRoot := t .TempDir ()
@@ -423,6 +433,17 @@ func TestReleaseInit(t *testing.T) {
423433 if err := initRepo (t , repo , test .initialRepoStateDir ); err != nil {
424434 t .Fatalf ("prepare test error = %v" , err )
425435 }
436+
437+ if test .push {
438+ // Create a local bare repository to act as the remote for the push.
439+ bareRepoDir := filepath .Join (t .TempDir (), "remote.git" )
440+ if err := os .MkdirAll (bareRepoDir , 0755 ); err != nil {
441+ t .Fatalf ("Failed to create bare repo dir: %v" , err )
442+ }
443+ runGit (t , bareRepoDir , "init" , "--bare" )
444+ runGit (t , repo , "remote" , "set-url" , "origin" , bareRepoDir )
445+ }
446+
426447 runGit (t , repo , "tag" , "go-google-cloud-pubsub-v1-1.0.0" )
427448 // Add a new commit to simulate a change.
428449 newFilePath := filepath .Join (repo , "google-cloud-pubsub/v1" , "new-file.txt" )
@@ -469,16 +490,58 @@ END_COMMIT_OVERRIDE
469490 runGit (t , repo , "commit" , "-m" , commitMsg )
470491 runGit (t , repo , "log" , "--oneline" , "go-google-cloud-pubsub-v1-1.0.0..HEAD" , "--" , "google-cloud-pubsub/v1" )
471492
472- cmd := exec .Command (
473- "go" ,
493+ // Setup mock GitHub server for --push case
494+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
495+ if r .Header .Get ("Authorization" ) != "Bearer fake-token" {
496+ t .Errorf ("missing or wrong authorization header: got %q" , r .Header .Get ("Authorization" ))
497+ }
498+
499+ // Mock endpoint for POST /repos/{owner}/{repo}/pulls
500+ if r .Method == "POST" && strings .HasSuffix (r .URL .Path , "/pulls" ) {
501+ var newPR github.NewPullRequest
502+ if err := json .NewDecoder (r .Body ).Decode (& newPR ); err != nil {
503+ t .Fatalf ("failed to decode request body: %v" , err )
504+ }
505+ if ! strings .Contains (* newPR .Title , "chore: librarian release pull request" ) {
506+ t .Errorf ("unexpected PR title: got %q" , * newPR .Title )
507+ }
508+ if * newPR .Base != "main" { // Assuming default branch
509+ t .Errorf ("unexpected PR base: got %q" , * newPR .Base )
510+ }
511+ w .WriteHeader (http .StatusCreated )
512+ fmt .Fprint (w , `{"number": 123, "html_url": "https://github.com/googleapis/librarian/pull/123"}` )
513+ return
514+ }
515+
516+ // Mock endpoint for POST /repos/{owner}/{repo}/issues/{number}/labels
517+ if r .Method == "POST" && strings .Contains (r .URL .Path , "/issues/123/labels" ) {
518+ w .WriteHeader (http .StatusOK )
519+ fmt .Fprint (w , `[]` )
520+ return
521+ }
522+ t .Fatalf ("unexpected request: %s %s" , r .Method , r .URL .Path )
523+ }))
524+ defer server .Close ()
525+
526+ cmdArgs := []string {
474527 "run" ,
528+ "-tags" , "e2etest" ,
475529 "github.com/googleapis/librarian/cmd/librarian" ,
476530 "release" ,
477531 "init" ,
478532 fmt .Sprintf ("--repo=%s" , repo ),
479533 fmt .Sprintf ("--output=%s" , workRoot ),
480534 fmt .Sprintf ("--library=%s" , test .libraryID ),
481- )
535+ }
536+ if test .push {
537+ cmdArgs = append (cmdArgs , "--push" )
538+ t .Logf ("zle: server.URL: %s" , server .URL )
539+ }
540+
541+ cmd := exec .Command ("go" , cmdArgs ... )
542+ cmd .Env = os .Environ ()
543+ cmd .Env = append (cmd .Env , "LIBRARIAN_GITHUB_TOKEN=fake-token" )
544+ cmd .Env = append (cmd .Env , "LIBRARIAN_GITHUB_BASE_URL=" + server .URL )
482545 cmd .Stderr = os .Stderr
483546 cmd .Stdout = os .Stdout
484547 err := cmd .Run ()
0 commit comments