github.com/google/go-github/v33@v33.0.0/github/git_refs_test.go (about)

     1  // Copyright 2013 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  	"net/http"
    13  	"reflect"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  func TestGitService_GetRef_singleRef(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	mux.HandleFunc("/repos/o/r/git/ref/heads/b", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "GET")
    24  		fmt.Fprint(w, `
    25  		  {
    26  		    "ref": "refs/heads/b",
    27  		    "url": "https://api.github.com/repos/o/r/git/refs/heads/b",
    28  		    "object": {
    29  		      "type": "commit",
    30  		      "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
    31  		      "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
    32  		    }
    33  		  }`)
    34  	})
    35  
    36  	ref, _, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b")
    37  	if err != nil {
    38  		t.Fatalf("Git.GetRef returned error: %v", err)
    39  	}
    40  
    41  	want := &Reference{
    42  		Ref: String("refs/heads/b"),
    43  		URL: String("https://api.github.com/repos/o/r/git/refs/heads/b"),
    44  		Object: &GitObject{
    45  			Type: String("commit"),
    46  			SHA:  String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
    47  			URL:  String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"),
    48  		},
    49  	}
    50  	if !reflect.DeepEqual(ref, want) {
    51  		t.Errorf("Git.GetRef returned %+v, want %+v", ref, want)
    52  	}
    53  
    54  	// without 'refs/' prefix
    55  	if _, _, err := client.Git.GetRef(context.Background(), "o", "r", "heads/b"); err != nil {
    56  		t.Errorf("Git.GetRef returned error: %v", err)
    57  	}
    58  }
    59  
    60  func TestGitService_GetRef_noRefs(t *testing.T) {
    61  	client, mux, _, teardown := setup()
    62  	defer teardown()
    63  
    64  	mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) {
    65  		testMethod(t, r, "GET")
    66  		w.WriteHeader(http.StatusNotFound)
    67  	})
    68  
    69  	ref, resp, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b")
    70  	if err == nil {
    71  		t.Errorf("Expected HTTP 404 response")
    72  	}
    73  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
    74  		t.Errorf("Git.GetRef returned status %d, want %d", got, want)
    75  	}
    76  	if ref != nil {
    77  		t.Errorf("Git.GetRef return %+v, want nil", ref)
    78  	}
    79  }
    80  
    81  func TestGitService_ListMatchingRefs_singleRef(t *testing.T) {
    82  	client, mux, _, teardown := setup()
    83  	defer teardown()
    84  
    85  	mux.HandleFunc("/repos/o/r/git/matching-refs/heads/b", func(w http.ResponseWriter, r *http.Request) {
    86  		testMethod(t, r, "GET")
    87  		fmt.Fprint(w, `
    88  		  [
    89  		    {
    90  		      "ref": "refs/heads/b",
    91  		      "url": "https://api.github.com/repos/o/r/git/refs/heads/b",
    92  		      "object": {
    93  		        "type": "commit",
    94  		        "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
    95  		        "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
    96  		      }
    97  		    }
    98  		  ]`)
    99  	})
   100  
   101  	opts := &ReferenceListOptions{Ref: "refs/heads/b"}
   102  	refs, _, err := client.Git.ListMatchingRefs(context.Background(), "o", "r", opts)
   103  	if err != nil {
   104  		t.Fatalf("Git.ListMatchingRefs returned error: %v", err)
   105  	}
   106  
   107  	ref := refs[0]
   108  	want := &Reference{
   109  		Ref: String("refs/heads/b"),
   110  		URL: String("https://api.github.com/repos/o/r/git/refs/heads/b"),
   111  		Object: &GitObject{
   112  			Type: String("commit"),
   113  			SHA:  String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
   114  			URL:  String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"),
   115  		},
   116  	}
   117  	if !reflect.DeepEqual(ref, want) {
   118  		t.Errorf("Git.ListMatchingRefs returned %+v, want %+v", ref, want)
   119  	}
   120  
   121  	// without 'refs/' prefix
   122  	opts = &ReferenceListOptions{Ref: "heads/b"}
   123  	if _, _, err := client.Git.ListMatchingRefs(context.Background(), "o", "r", opts); err != nil {
   124  		t.Errorf("Git.ListMatchingRefs returned error: %v", err)
   125  	}
   126  }
   127  
   128  func TestGitService_ListMatchingRefs_multipleRefs(t *testing.T) {
   129  	client, mux, _, teardown := setup()
   130  	defer teardown()
   131  
   132  	mux.HandleFunc("/repos/o/r/git/matching-refs/heads/b", func(w http.ResponseWriter, r *http.Request) {
   133  		testMethod(t, r, "GET")
   134  		fmt.Fprint(w, `
   135  		  [
   136  		    {
   137  			    "ref": "refs/heads/booger",
   138  			    "url": "https://api.github.com/repos/o/r/git/refs/heads/booger",
   139  			    "object": {
   140  			      "type": "commit",
   141  			      "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
   142  			      "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
   143  			    }
   144  		  	},
   145  		    {
   146  		      "ref": "refs/heads/bandsaw",
   147  		      "url": "https://api.github.com/repos/o/r/git/refs/heads/bandsaw",
   148  		      "object": {
   149  		        "type": "commit",
   150  		        "sha": "612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac",
   151  		        "url": "https://api.github.com/repos/o/r/git/commits/612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac"
   152  		      }
   153  		    }
   154  		  ]
   155  		`)
   156  	})
   157  
   158  	opts := &ReferenceListOptions{Ref: "refs/heads/b"}
   159  	refs, _, err := client.Git.ListMatchingRefs(context.Background(), "o", "r", opts)
   160  	if err != nil {
   161  		t.Errorf("Git.ListMatchingRefs returned error: %v", err)
   162  	}
   163  
   164  	want := &Reference{
   165  		Ref: String("refs/heads/booger"),
   166  		URL: String("https://api.github.com/repos/o/r/git/refs/heads/booger"),
   167  		Object: &GitObject{
   168  			Type: String("commit"),
   169  			SHA:  String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
   170  			URL:  String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"),
   171  		},
   172  	}
   173  	if !reflect.DeepEqual(refs[0], want) {
   174  		t.Errorf("Git.ListMatchingRefs returned %+v, want %+v", refs[0], want)
   175  	}
   176  }
   177  
   178  func TestGitService_ListMatchingRefs_noRefs(t *testing.T) {
   179  	client, mux, _, teardown := setup()
   180  	defer teardown()
   181  
   182  	mux.HandleFunc("/repos/o/r/git/matching-refs/heads/b", func(w http.ResponseWriter, r *http.Request) {
   183  		testMethod(t, r, "GET")
   184  		fmt.Fprint(w, "[]")
   185  	})
   186  
   187  	opts := &ReferenceListOptions{Ref: "refs/heads/b"}
   188  	refs, _, err := client.Git.ListMatchingRefs(context.Background(), "o", "r", opts)
   189  	if err != nil {
   190  		t.Errorf("Git.ListMatchingRefs returned error: %v", err)
   191  	}
   192  
   193  	if len(refs) != 0 {
   194  		t.Errorf("Git.ListMatchingRefs returned %+v, want an empty slice", refs)
   195  	}
   196  }
   197  
   198  func TestGitService_ListMatchingRefs_allRefs(t *testing.T) {
   199  	client, mux, _, teardown := setup()
   200  	defer teardown()
   201  
   202  	mux.HandleFunc("/repos/o/r/git/matching-refs/", func(w http.ResponseWriter, r *http.Request) {
   203  		testMethod(t, r, "GET")
   204  		fmt.Fprint(w, `
   205  		  [
   206  		    {
   207  		      "ref": "refs/heads/branchA",
   208  		      "url": "https://api.github.com/repos/o/r/git/refs/heads/branchA",
   209  		      "object": {
   210  		        "type": "commit",
   211  		        "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
   212  		        "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
   213  		      }
   214  		    },
   215  		    {
   216  		      "ref": "refs/heads/branchB",
   217  		      "url": "https://api.github.com/repos/o/r/git/refs/heads/branchB",
   218  		      "object": {
   219  		        "type": "commit",
   220  		        "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
   221  		        "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
   222  		      }
   223  		    }
   224  		  ]`)
   225  	})
   226  
   227  	refs, _, err := client.Git.ListMatchingRefs(context.Background(), "o", "r", nil)
   228  	if err != nil {
   229  		t.Errorf("Git.ListMatchingRefs returned error: %v", err)
   230  	}
   231  
   232  	want := []*Reference{
   233  		{
   234  			Ref: String("refs/heads/branchA"),
   235  			URL: String("https://api.github.com/repos/o/r/git/refs/heads/branchA"),
   236  			Object: &GitObject{
   237  				Type: String("commit"),
   238  				SHA:  String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
   239  				URL:  String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"),
   240  			},
   241  		},
   242  		{
   243  			Ref: String("refs/heads/branchB"),
   244  			URL: String("https://api.github.com/repos/o/r/git/refs/heads/branchB"),
   245  			Object: &GitObject{
   246  				Type: String("commit"),
   247  				SHA:  String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
   248  				URL:  String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"),
   249  			},
   250  		},
   251  	}
   252  	if !reflect.DeepEqual(refs, want) {
   253  		t.Errorf("Git.ListMatchingRefs returned %+v, want %+v", refs, want)
   254  	}
   255  }
   256  
   257  func TestGitService_ListMatchingRefs_options(t *testing.T) {
   258  	client, mux, _, teardown := setup()
   259  	defer teardown()
   260  
   261  	mux.HandleFunc("/repos/o/r/git/matching-refs/t", func(w http.ResponseWriter, r *http.Request) {
   262  		testMethod(t, r, "GET")
   263  		testFormValues(t, r, values{"page": "2"})
   264  		fmt.Fprint(w, `[{"ref": "r"}]`)
   265  	})
   266  
   267  	opts := &ReferenceListOptions{Ref: "t", ListOptions: ListOptions{Page: 2}}
   268  	refs, _, err := client.Git.ListMatchingRefs(context.Background(), "o", "r", opts)
   269  	if err != nil {
   270  		t.Errorf("Git.ListMatchingRefs returned error: %v", err)
   271  	}
   272  
   273  	want := []*Reference{{Ref: String("r")}}
   274  	if !reflect.DeepEqual(refs, want) {
   275  		t.Errorf("Git.ListMatchingRefs returned %+v, want %+v", refs, want)
   276  	}
   277  }
   278  
   279  func TestGitService_CreateRef(t *testing.T) {
   280  	client, mux, _, teardown := setup()
   281  	defer teardown()
   282  
   283  	args := &createRefRequest{
   284  		Ref: String("refs/heads/b"),
   285  		SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
   286  	}
   287  
   288  	mux.HandleFunc("/repos/o/r/git/refs", func(w http.ResponseWriter, r *http.Request) {
   289  		v := new(createRefRequest)
   290  		json.NewDecoder(r.Body).Decode(v)
   291  
   292  		testMethod(t, r, "POST")
   293  		if !reflect.DeepEqual(v, args) {
   294  			t.Errorf("Request body = %+v, want %+v", v, args)
   295  		}
   296  		fmt.Fprint(w, `
   297  		  {
   298  		    "ref": "refs/heads/b",
   299  		    "url": "https://api.github.com/repos/o/r/git/refs/heads/b",
   300  		    "object": {
   301  		      "type": "commit",
   302  		      "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
   303  		      "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
   304  		    }
   305  		  }`)
   306  	})
   307  
   308  	ref, _, err := client.Git.CreateRef(context.Background(), "o", "r", &Reference{
   309  		Ref: String("refs/heads/b"),
   310  		Object: &GitObject{
   311  			SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
   312  		},
   313  	})
   314  	if err != nil {
   315  		t.Errorf("Git.CreateRef returned error: %v", err)
   316  	}
   317  
   318  	want := &Reference{
   319  		Ref: String("refs/heads/b"),
   320  		URL: String("https://api.github.com/repos/o/r/git/refs/heads/b"),
   321  		Object: &GitObject{
   322  			Type: String("commit"),
   323  			SHA:  String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
   324  			URL:  String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"),
   325  		},
   326  	}
   327  	if !reflect.DeepEqual(ref, want) {
   328  		t.Errorf("Git.CreateRef returned %+v, want %+v", ref, want)
   329  	}
   330  
   331  	// without 'refs/' prefix
   332  	_, _, err = client.Git.CreateRef(context.Background(), "o", "r", &Reference{
   333  		Ref: String("heads/b"),
   334  		Object: &GitObject{
   335  			SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
   336  		},
   337  	})
   338  	if err != nil {
   339  		t.Errorf("Git.CreateRef returned error: %v", err)
   340  	}
   341  }
   342  
   343  func TestGitService_UpdateRef(t *testing.T) {
   344  	client, mux, _, teardown := setup()
   345  	defer teardown()
   346  
   347  	args := &updateRefRequest{
   348  		SHA:   String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
   349  		Force: Bool(true),
   350  	}
   351  
   352  	mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) {
   353  		v := new(updateRefRequest)
   354  		json.NewDecoder(r.Body).Decode(v)
   355  
   356  		testMethod(t, r, "PATCH")
   357  		if !reflect.DeepEqual(v, args) {
   358  			t.Errorf("Request body = %+v, want %+v", v, args)
   359  		}
   360  		fmt.Fprint(w, `
   361  		  {
   362  		    "ref": "refs/heads/b",
   363  		    "url": "https://api.github.com/repos/o/r/git/refs/heads/b",
   364  		    "object": {
   365  		      "type": "commit",
   366  		      "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
   367  		      "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
   368  		    }
   369  		  }`)
   370  	})
   371  
   372  	ref, _, err := client.Git.UpdateRef(context.Background(), "o", "r", &Reference{
   373  		Ref:    String("refs/heads/b"),
   374  		Object: &GitObject{SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd")},
   375  	}, true)
   376  	if err != nil {
   377  		t.Errorf("Git.UpdateRef returned error: %v", err)
   378  	}
   379  
   380  	want := &Reference{
   381  		Ref: String("refs/heads/b"),
   382  		URL: String("https://api.github.com/repos/o/r/git/refs/heads/b"),
   383  		Object: &GitObject{
   384  			Type: String("commit"),
   385  			SHA:  String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
   386  			URL:  String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"),
   387  		},
   388  	}
   389  	if !reflect.DeepEqual(ref, want) {
   390  		t.Errorf("Git.UpdateRef returned %+v, want %+v", ref, want)
   391  	}
   392  
   393  	// without 'refs/' prefix
   394  	_, _, err = client.Git.UpdateRef(context.Background(), "o", "r", &Reference{
   395  		Ref:    String("heads/b"),
   396  		Object: &GitObject{SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd")},
   397  	}, true)
   398  	if err != nil {
   399  		t.Errorf("Git.UpdateRef returned error: %v", err)
   400  	}
   401  }
   402  
   403  func TestGitService_DeleteRef(t *testing.T) {
   404  	client, mux, _, teardown := setup()
   405  	defer teardown()
   406  
   407  	mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) {
   408  		testMethod(t, r, "DELETE")
   409  	})
   410  
   411  	_, err := client.Git.DeleteRef(context.Background(), "o", "r", "refs/heads/b")
   412  	if err != nil {
   413  		t.Errorf("Git.DeleteRef returned error: %v", err)
   414  	}
   415  
   416  	// without 'refs/' prefix
   417  	if _, err := client.Git.DeleteRef(context.Background(), "o", "r", "heads/b"); err != nil {
   418  		t.Errorf("Git.DeleteRef returned error: %v", err)
   419  	}
   420  }
   421  
   422  func TestGitService_GetRef_pathEscape(t *testing.T) {
   423  	client, mux, _, teardown := setup()
   424  	defer teardown()
   425  
   426  	mux.HandleFunc("/repos/o/r/git/ref/heads/b", func(w http.ResponseWriter, r *http.Request) {
   427  		testMethod(t, r, "GET")
   428  		if strings.Contains(r.URL.RawPath, "%2F") {
   429  			t.Errorf("RawPath still contains escaped / as %%2F: %v", r.URL.RawPath)
   430  		}
   431  		fmt.Fprint(w, `
   432  		  {
   433  		    "ref": "refs/heads/b",
   434  		    "url": "https://api.github.com/repos/o/r/git/refs/heads/b",
   435  		    "object": {
   436  		      "type": "commit",
   437  		      "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
   438  		      "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
   439  		    }
   440  		  }`)
   441  	})
   442  
   443  	_, _, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b")
   444  	if err != nil {
   445  		t.Fatalf("Git.GetRef returned error: %v", err)
   446  	}
   447  }