@@ -400,3 +400,74 @@ func TestCreatePullRequest(t *testing.T) {
400400 })
401401 }
402402}
403+
404+ func TestAddLabelsToIssue (t * testing.T ) {
405+ t .Parallel ()
406+ for _ , test := range []struct {
407+ name string
408+ handler http.HandlerFunc
409+ issueNum int
410+ labels []string
411+ wantErr bool
412+ wantErrSubstr string
413+ }{
414+ {
415+ name : "add labels to an issue" ,
416+ handler : func (w http.ResponseWriter , r * http.Request ) {
417+ if r .Method != http .MethodPost {
418+ t .Errorf ("unexpected method: got %s, want %s" , r .Method , http .MethodPost )
419+ }
420+ wantPath := "/repos/owner/repo/issues/7/labels"
421+ if r .URL .Path != wantPath {
422+ t .Errorf ("unexpected path: got %s, want %s" , r .URL .Path , wantPath )
423+ }
424+ var labels []string
425+ if err := json .NewDecoder (r .Body ).Decode (& labels ); err != nil {
426+ t .Fatalf ("failed to decode request body: %v" , err )
427+ }
428+ expectedBody := []string {"new-label" , "another-label" }
429+ if strings .Join (labels , "," ) != strings .Join (expectedBody , "," ) {
430+ t .Errorf ("unexpected body: got %q, want %q" , labels , expectedBody )
431+ }
432+ },
433+ issueNum : 7 ,
434+ labels : []string {"new-label" , "another-label" },
435+ },
436+ {
437+ name : "GitHub API error" ,
438+ handler : func (w http.ResponseWriter , r * http.Request ) { w .WriteHeader (http .StatusInternalServerError ) },
439+ wantErr : true ,
440+ wantErrSubstr : "500" ,
441+ },
442+ } {
443+ t .Run (test .name , func (t * testing.T ) {
444+ t .Parallel ()
445+ server := httptest .NewServer (test .handler )
446+ defer server .Close ()
447+
448+ repo := & Repository {Owner : "owner" , Name : "repo" }
449+ client , err := newClientWithHTTP ("fake-token" , repo , server .Client ())
450+ if err != nil {
451+ t .Fatalf ("newClientWithHTTP() error = %v" , err )
452+ }
453+ client .BaseURL , _ = url .Parse (server .URL + "/" )
454+
455+ err = client .AddLabelsToIssue (context .Background (), repo , test .issueNum , test .labels )
456+
457+ if test .wantErr {
458+ if err == nil {
459+ t .Errorf ("AddLabelsToIssue() should return an error" )
460+ }
461+ if ! strings .Contains (err .Error (), test .wantErrSubstr ) {
462+ t .Errorf ("AddLabelsToIssue() err = %v, want error containing %q" , err , test .wantErrSubstr )
463+ }
464+
465+ return
466+ }
467+
468+ if err != nil {
469+ t .Errorf ("AddLabelsToIssue() err = %v, want nil" , err )
470+ }
471+ })
472+ }
473+ }
0 commit comments