github.com/google/go-github/v49@v49.1.0/github/actions_workflow_runs_test.go (about)

     1  // Copyright 2020 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  	"net/url"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/google/go-cmp/cmp"
    18  )
    19  
    20  func TestActionsService_ListWorkflowRunsByID(t *testing.T) {
    21  	client, mux, _, teardown := setup()
    22  	defer teardown()
    23  
    24  	mux.HandleFunc("/repos/o/r/actions/workflows/29679449/runs", func(w http.ResponseWriter, r *http.Request) {
    25  		testMethod(t, r, "GET")
    26  		testFormValues(t, r, values{"per_page": "2", "page": "2"})
    27  		fmt.Fprint(w, `{"total_count":4,"workflow_runs":[{"id":399444496,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`)
    28  	})
    29  
    30  	opts := &ListWorkflowRunsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}}
    31  	ctx := context.Background()
    32  	runs, _, err := client.Actions.ListWorkflowRunsByID(ctx, "o", "r", 29679449, opts)
    33  	if err != nil {
    34  		t.Errorf("Actions.ListWorkFlowRunsByID returned error: %v", err)
    35  	}
    36  
    37  	want := &WorkflowRuns{
    38  		TotalCount: Int(4),
    39  		WorkflowRuns: []*WorkflowRun{
    40  			{ID: Int64(399444496), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
    41  			{ID: Int64(399444497), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
    42  		},
    43  	}
    44  	if !cmp.Equal(runs, want) {
    45  		t.Errorf("Actions.ListWorkflowRunsByID returned %+v, want %+v", runs, want)
    46  	}
    47  
    48  	const methodName = "ListWorkflowRunsByID"
    49  	testBadOptions(t, methodName, func() (err error) {
    50  		_, _, err = client.Actions.ListWorkflowRunsByID(ctx, "\n", "\n", 29679449, opts)
    51  		return err
    52  	})
    53  
    54  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    55  		got, resp, err := client.Actions.ListWorkflowRunsByID(ctx, "o", "r", 29679449, opts)
    56  		if got != nil {
    57  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    58  		}
    59  		return resp, err
    60  	})
    61  }
    62  
    63  func TestActionsService_ListWorkflowRunsFileName(t *testing.T) {
    64  	client, mux, _, teardown := setup()
    65  	defer teardown()
    66  
    67  	mux.HandleFunc("/repos/o/r/actions/workflows/29679449/runs", func(w http.ResponseWriter, r *http.Request) {
    68  		testMethod(t, r, "GET")
    69  		testFormValues(t, r, values{"per_page": "2", "page": "2"})
    70  		fmt.Fprint(w, `{"total_count":4,"workflow_runs":[{"id":399444496,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`)
    71  	})
    72  
    73  	opts := &ListWorkflowRunsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}}
    74  	ctx := context.Background()
    75  	runs, _, err := client.Actions.ListWorkflowRunsByFileName(ctx, "o", "r", "29679449", opts)
    76  	if err != nil {
    77  		t.Errorf("Actions.ListWorkFlowRunsByFileName returned error: %v", err)
    78  	}
    79  
    80  	want := &WorkflowRuns{
    81  		TotalCount: Int(4),
    82  		WorkflowRuns: []*WorkflowRun{
    83  			{ID: Int64(399444496), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
    84  			{ID: Int64(399444497), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
    85  		},
    86  	}
    87  	if !cmp.Equal(runs, want) {
    88  		t.Errorf("Actions.ListWorkflowRunsByFileName returned %+v, want %+v", runs, want)
    89  	}
    90  
    91  	const methodName = "ListWorkflowRunsByFileName"
    92  	testBadOptions(t, methodName, func() (err error) {
    93  		_, _, err = client.Actions.ListWorkflowRunsByFileName(ctx, "\n", "\n", "29679449", opts)
    94  		return err
    95  	})
    96  
    97  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    98  		got, resp, err := client.Actions.ListWorkflowRunsByFileName(ctx, "o", "r", "29679449", opts)
    99  		if got != nil {
   100  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   101  		}
   102  		return resp, err
   103  	})
   104  }
   105  
   106  func TestActionsService_GetWorkflowRunByID(t *testing.T) {
   107  	client, mux, _, teardown := setup()
   108  	defer teardown()
   109  
   110  	mux.HandleFunc("/repos/o/r/actions/runs/29679449", func(w http.ResponseWriter, r *http.Request) {
   111  		testMethod(t, r, "GET")
   112  		fmt.Fprint(w, `{"id":399444496,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}}`)
   113  	})
   114  
   115  	ctx := context.Background()
   116  	runs, _, err := client.Actions.GetWorkflowRunByID(ctx, "o", "r", 29679449)
   117  	if err != nil {
   118  		t.Errorf("Actions.GetWorkflowRunByID returned error: %v", err)
   119  	}
   120  
   121  	want := &WorkflowRun{
   122  		ID:        Int64(399444496),
   123  		RunNumber: Int(296),
   124  		CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
   125  		UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
   126  	}
   127  
   128  	if !cmp.Equal(runs, want) {
   129  		t.Errorf("Actions.GetWorkflowRunByID returned %+v, want %+v", runs, want)
   130  	}
   131  
   132  	const methodName = "GetWorkflowRunByID"
   133  	testBadOptions(t, methodName, func() (err error) {
   134  		_, _, err = client.Actions.GetWorkflowRunByID(ctx, "\n", "\n", 29679449)
   135  		return err
   136  	})
   137  
   138  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   139  		got, resp, err := client.Actions.GetWorkflowRunByID(ctx, "o", "r", 29679449)
   140  		if got != nil {
   141  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   142  		}
   143  		return resp, err
   144  	})
   145  }
   146  
   147  func TestActionsService_GetWorkflowRunAttempt(t *testing.T) {
   148  	client, mux, _, teardown := setup()
   149  	defer teardown()
   150  
   151  	mux.HandleFunc("/repos/o/r/actions/runs/29679449/attempts/3", func(w http.ResponseWriter, r *http.Request) {
   152  		testMethod(t, r, "GET")
   153  		testFormValues(t, r, values{"exclude_pull_requests": "true"})
   154  		fmt.Fprint(w, `{"id":399444496,"run_number":296,"run_attempt":3,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}}`)
   155  	})
   156  
   157  	opts := &WorkflowRunAttemptOptions{ExcludePullRequests: Bool(true)}
   158  	ctx := context.Background()
   159  	runs, _, err := client.Actions.GetWorkflowRunAttempt(ctx, "o", "r", 29679449, 3, opts)
   160  	if err != nil {
   161  		t.Errorf("Actions.GetWorkflowRunAttempt returned error: %v", err)
   162  	}
   163  
   164  	want := &WorkflowRun{
   165  		ID:         Int64(399444496),
   166  		RunNumber:  Int(296),
   167  		RunAttempt: Int(3),
   168  		CreatedAt:  &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
   169  		UpdatedAt:  &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
   170  	}
   171  
   172  	if !cmp.Equal(runs, want) {
   173  		t.Errorf("Actions.GetWorkflowRunAttempt returned %+v, want %+v", runs, want)
   174  	}
   175  
   176  	const methodName = "GetWorkflowRunAttempt"
   177  	testBadOptions(t, methodName, func() (err error) {
   178  		_, _, err = client.Actions.GetWorkflowRunAttempt(ctx, "\n", "\n", 29679449, 3, opts)
   179  		return err
   180  	})
   181  
   182  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   183  		got, resp, err := client.Actions.GetWorkflowRunAttempt(ctx, "o", "r", 29679449, 3, opts)
   184  		if got != nil {
   185  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   186  		}
   187  		return resp, err
   188  	})
   189  }
   190  
   191  func TestActionsService_RerunWorkflowRunByID(t *testing.T) {
   192  	client, mux, _, teardown := setup()
   193  	defer teardown()
   194  
   195  	mux.HandleFunc("/repos/o/r/actions/runs/3434/rerun", func(w http.ResponseWriter, r *http.Request) {
   196  		testMethod(t, r, "POST")
   197  		w.WriteHeader(http.StatusCreated)
   198  	})
   199  
   200  	ctx := context.Background()
   201  	resp, err := client.Actions.RerunWorkflowByID(ctx, "o", "r", 3434)
   202  	if err != nil {
   203  		t.Errorf("Actions.RerunWorkflowByID returned error: %v", err)
   204  	}
   205  	if resp.StatusCode != http.StatusCreated {
   206  		t.Errorf("Actions.RerunWorkflowRunByID returned status: %d, want %d", resp.StatusCode, http.StatusCreated)
   207  	}
   208  
   209  	const methodName = "RerunWorkflowByID"
   210  	testBadOptions(t, methodName, func() (err error) {
   211  		_, err = client.Actions.RerunWorkflowByID(ctx, "\n", "\n", 3434)
   212  		return err
   213  	})
   214  
   215  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   216  		return client.Actions.RerunWorkflowByID(ctx, "o", "r", 3434)
   217  	})
   218  }
   219  
   220  func TestActionsService_RerunFailedJobsByID(t *testing.T) {
   221  	client, mux, _, teardown := setup()
   222  	defer teardown()
   223  
   224  	mux.HandleFunc("/repos/o/r/actions/runs/3434/rerun-failed-jobs", func(w http.ResponseWriter, r *http.Request) {
   225  		testMethod(t, r, "POST")
   226  		w.WriteHeader(http.StatusCreated)
   227  	})
   228  
   229  	ctx := context.Background()
   230  	resp, err := client.Actions.RerunFailedJobsByID(ctx, "o", "r", 3434)
   231  	if err != nil {
   232  		t.Errorf("Actions.RerunFailedJobsByID returned error: %v", err)
   233  	}
   234  	if resp.StatusCode != http.StatusCreated {
   235  		t.Errorf("Actions.RerunFailedJobsByID returned status: %d, want %d", resp.StatusCode, http.StatusCreated)
   236  	}
   237  
   238  	const methodName = "RerunFailedJobsByID"
   239  	testBadOptions(t, methodName, func() (err error) {
   240  		_, err = client.Actions.RerunFailedJobsByID(ctx, "\n", "\n", 3434)
   241  		return err
   242  	})
   243  
   244  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   245  		return client.Actions.RerunFailedJobsByID(ctx, "o", "r", 3434)
   246  	})
   247  }
   248  
   249  func TestActionsService_RerunJobByID(t *testing.T) {
   250  	client, mux, _, teardown := setup()
   251  	defer teardown()
   252  
   253  	mux.HandleFunc("/repos/o/r/actions/jobs/3434/rerun", func(w http.ResponseWriter, r *http.Request) {
   254  		testMethod(t, r, "POST")
   255  		w.WriteHeader(http.StatusCreated)
   256  	})
   257  
   258  	ctx := context.Background()
   259  	resp, err := client.Actions.RerunJobByID(ctx, "o", "r", 3434)
   260  	if err != nil {
   261  		t.Errorf("Actions.RerunJobByID returned error: %v", err)
   262  	}
   263  	if resp.StatusCode != http.StatusCreated {
   264  		t.Errorf("Actions.RerunJobByID returned status: %d, want %d", resp.StatusCode, http.StatusCreated)
   265  	}
   266  
   267  	const methodName = "RerunJobByID"
   268  	testBadOptions(t, methodName, func() (err error) {
   269  		_, err = client.Actions.RerunJobByID(ctx, "\n", "\n", 3434)
   270  		return err
   271  	})
   272  
   273  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   274  		return client.Actions.RerunJobByID(ctx, "o", "r", 3434)
   275  	})
   276  }
   277  
   278  func TestActionsService_CancelWorkflowRunByID(t *testing.T) {
   279  	client, mux, _, teardown := setup()
   280  	defer teardown()
   281  
   282  	mux.HandleFunc("/repos/o/r/actions/runs/3434/cancel", func(w http.ResponseWriter, r *http.Request) {
   283  		testMethod(t, r, "POST")
   284  		w.WriteHeader(http.StatusAccepted)
   285  	})
   286  
   287  	ctx := context.Background()
   288  	resp, err := client.Actions.CancelWorkflowRunByID(ctx, "o", "r", 3434)
   289  	if _, ok := err.(*AcceptedError); !ok {
   290  		t.Errorf("Actions.CancelWorkflowRunByID returned error: %v (want AcceptedError)", err)
   291  	}
   292  	if resp.StatusCode != http.StatusAccepted {
   293  		t.Errorf("Actions.CancelWorkflowRunByID returned status: %d, want %d", resp.StatusCode, http.StatusAccepted)
   294  	}
   295  
   296  	const methodName = "CancelWorkflowRunByID"
   297  	testBadOptions(t, methodName, func() (err error) {
   298  		_, err = client.Actions.CancelWorkflowRunByID(ctx, "\n", "\n", 3434)
   299  		return err
   300  	})
   301  
   302  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   303  		return client.Actions.CancelWorkflowRunByID(ctx, "o", "r", 3434)
   304  	})
   305  }
   306  
   307  func TestActionsService_GetWorkflowRunLogs(t *testing.T) {
   308  	client, mux, _, teardown := setup()
   309  	defer teardown()
   310  
   311  	mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
   312  		testMethod(t, r, "GET")
   313  		http.Redirect(w, r, "http://github.com/a", http.StatusFound)
   314  	})
   315  
   316  	ctx := context.Background()
   317  	url, resp, err := client.Actions.GetWorkflowRunLogs(ctx, "o", "r", 399444496, true)
   318  	if err != nil {
   319  		t.Errorf("Actions.GetWorkflowRunLogs returned error: %v", err)
   320  	}
   321  	if resp.StatusCode != http.StatusFound {
   322  		t.Errorf("Actions.GetWorkflowRunLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound)
   323  	}
   324  	want := "http://github.com/a"
   325  	if url.String() != want {
   326  		t.Errorf("Actions.GetWorkflowRunLogs returned %+v, want %+v", url.String(), want)
   327  	}
   328  
   329  	const methodName = "GetWorkflowRunLogs"
   330  	testBadOptions(t, methodName, func() (err error) {
   331  		_, _, err = client.Actions.GetWorkflowRunLogs(ctx, "\n", "\n", 399444496, true)
   332  		return err
   333  	})
   334  }
   335  
   336  func TestActionsService_GetWorkflowRunLogs_StatusMovedPermanently_dontFollowRedirects(t *testing.T) {
   337  	client, mux, _, teardown := setup()
   338  	defer teardown()
   339  
   340  	mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
   341  		testMethod(t, r, "GET")
   342  		http.Redirect(w, r, "http://github.com/a", http.StatusMovedPermanently)
   343  	})
   344  
   345  	ctx := context.Background()
   346  	_, resp, _ := client.Actions.GetWorkflowRunLogs(ctx, "o", "r", 399444496, false)
   347  	if resp.StatusCode != http.StatusMovedPermanently {
   348  		t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusMovedPermanently)
   349  	}
   350  }
   351  
   352  func TestActionsService_GetWorkflowRunLogs_StatusMovedPermanently_followRedirects(t *testing.T) {
   353  	client, mux, serverURL, teardown := setup()
   354  	defer teardown()
   355  
   356  	// Mock a redirect link, which leads to an archive link
   357  	mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
   358  		testMethod(t, r, "GET")
   359  		redirectURL, _ := url.Parse(serverURL + baseURLPath + "/redirect")
   360  		http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently)
   361  	})
   362  
   363  	mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
   364  		testMethod(t, r, "GET")
   365  		http.Redirect(w, r, "http://github.com/a", http.StatusFound)
   366  	})
   367  
   368  	ctx := context.Background()
   369  	url, resp, err := client.Actions.GetWorkflowRunLogs(ctx, "o", "r", 399444496, true)
   370  	if err != nil {
   371  		t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err)
   372  	}
   373  
   374  	if resp.StatusCode != http.StatusFound {
   375  		t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound)
   376  	}
   377  
   378  	want := "http://github.com/a"
   379  	if url.String() != want {
   380  		t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want)
   381  	}
   382  
   383  	const methodName = "GetWorkflowRunLogs"
   384  	testBadOptions(t, methodName, func() (err error) {
   385  		_, _, err = client.Actions.GetWorkflowRunLogs(ctx, "\n", "\n", 399444496, true)
   386  		return err
   387  	})
   388  }
   389  
   390  func TestActionService_ListRepositoryWorkflowRuns(t *testing.T) {
   391  	client, mux, _, teardown := setup()
   392  	defer teardown()
   393  
   394  	mux.HandleFunc("/repos/o/r/actions/runs", func(w http.ResponseWriter, r *http.Request) {
   395  		testMethod(t, r, "GET")
   396  		testFormValues(t, r, values{"per_page": "2", "page": "2"})
   397  		fmt.Fprint(w, `{"total_count":2,
   398  		"workflow_runs":[
   399  			{"id":298499444,"run_number":301,"created_at":"2020-04-11T11:14:54Z","updated_at":"2020-04-11T11:14:54Z"},
   400  			{"id":298499445,"run_number":302,"created_at":"2020-04-11T11:14:54Z","updated_at":"2020-04-11T11:14:54Z"}]}`)
   401  	})
   402  
   403  	opts := &ListWorkflowRunsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}}
   404  	ctx := context.Background()
   405  	runs, _, err := client.Actions.ListRepositoryWorkflowRuns(ctx, "o", "r", opts)
   406  	if err != nil {
   407  		t.Errorf("Actions.ListRepositoryWorkflowRuns returned error: %v", err)
   408  	}
   409  
   410  	expected := &WorkflowRuns{
   411  		TotalCount: Int(2),
   412  		WorkflowRuns: []*WorkflowRun{
   413  			{ID: Int64(298499444), RunNumber: Int(301), CreatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}},
   414  			{ID: Int64(298499445), RunNumber: Int(302), CreatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}},
   415  		},
   416  	}
   417  
   418  	if !cmp.Equal(runs, expected) {
   419  		t.Errorf("Actions.ListRepositoryWorkflowRuns returned %+v, want %+v", runs, expected)
   420  	}
   421  
   422  	const methodName = "ListRepositoryWorkflowRuns"
   423  	testBadOptions(t, methodName, func() (err error) {
   424  		_, _, err = client.Actions.ListRepositoryWorkflowRuns(ctx, "\n", "\n", opts)
   425  
   426  		return err
   427  	})
   428  
   429  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   430  		got, resp, err := client.Actions.ListRepositoryWorkflowRuns(ctx, "o", "r", opts)
   431  
   432  		if got != nil {
   433  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   434  		}
   435  		return resp, err
   436  	})
   437  }
   438  
   439  func TestActionService_DeleteWorkflowRun(t *testing.T) {
   440  	client, mux, _, teardown := setup()
   441  	defer teardown()
   442  
   443  	mux.HandleFunc("/repos/o/r/actions/runs/399444496", func(w http.ResponseWriter, r *http.Request) {
   444  		testMethod(t, r, "DELETE")
   445  
   446  		w.WriteHeader(http.StatusNoContent)
   447  	})
   448  
   449  	ctx := context.Background()
   450  	if _, err := client.Actions.DeleteWorkflowRun(ctx, "o", "r", 399444496); err != nil {
   451  		t.Errorf("DeleteWorkflowRun returned error: %v", err)
   452  	}
   453  
   454  	const methodName = "DeleteWorkflowRun"
   455  	testBadOptions(t, methodName, func() (err error) {
   456  		_, err = client.Actions.DeleteWorkflowRun(ctx, "\n", "\n", 399444496)
   457  		return err
   458  	})
   459  
   460  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   461  		return client.Actions.DeleteWorkflowRun(ctx, "o", "r", 399444496)
   462  	})
   463  }
   464  
   465  func TestActionService_DeleteWorkflowRunLogs(t *testing.T) {
   466  	client, mux, _, teardown := setup()
   467  	defer teardown()
   468  
   469  	mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) {
   470  		testMethod(t, r, "DELETE")
   471  
   472  		w.WriteHeader(http.StatusNoContent)
   473  	})
   474  
   475  	ctx := context.Background()
   476  	if _, err := client.Actions.DeleteWorkflowRunLogs(ctx, "o", "r", 399444496); err != nil {
   477  		t.Errorf("DeleteWorkflowRunLogs returned error: %v", err)
   478  	}
   479  
   480  	const methodName = "DeleteWorkflowRunLogs"
   481  	testBadOptions(t, methodName, func() (err error) {
   482  		_, err = client.Actions.DeleteWorkflowRunLogs(ctx, "\n", "\n", 399444496)
   483  		return err
   484  	})
   485  
   486  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   487  		return client.Actions.DeleteWorkflowRunLogs(ctx, "o", "r", 399444496)
   488  	})
   489  }
   490  
   491  func TestActionsService_GetWorkflowRunUsageByID(t *testing.T) {
   492  	client, mux, _, teardown := setup()
   493  	defer teardown()
   494  
   495  	mux.HandleFunc("/repos/o/r/actions/runs/29679449/timing", func(w http.ResponseWriter, r *http.Request) {
   496  		testMethod(t, r, "GET")
   497  		fmt.Fprint(w, `{"billable":{"UBUNTU":{"total_ms":180000,"jobs":1,"job_runs":[{"job_id":1,"duration_ms":60000}]},"MACOS":{"total_ms":240000,"jobs":2,"job_runs":[{"job_id":2,"duration_ms":30000},{"job_id":3,"duration_ms":10000}]},"WINDOWS":{"total_ms":300000,"jobs":2}},"run_duration_ms":500000}`)
   498  	})
   499  
   500  	ctx := context.Background()
   501  	workflowRunUsage, _, err := client.Actions.GetWorkflowRunUsageByID(ctx, "o", "r", 29679449)
   502  	if err != nil {
   503  		t.Errorf("Actions.GetWorkflowRunUsageByID returned error: %v", err)
   504  	}
   505  
   506  	want := &WorkflowRunUsage{
   507  		Billable: &WorkflowRunBillMap{
   508  			"UBUNTU": &WorkflowRunBill{
   509  				TotalMS: Int64(180000),
   510  				Jobs:    Int(1),
   511  				JobRuns: []*WorkflowRunJobRun{
   512  					{
   513  						JobID:      Int(1),
   514  						DurationMS: Int64(60000),
   515  					},
   516  				},
   517  			},
   518  			"MACOS": &WorkflowRunBill{
   519  				TotalMS: Int64(240000),
   520  				Jobs:    Int(2),
   521  				JobRuns: []*WorkflowRunJobRun{
   522  					{
   523  						JobID:      Int(2),
   524  						DurationMS: Int64(30000),
   525  					},
   526  					{
   527  						JobID:      Int(3),
   528  						DurationMS: Int64(10000),
   529  					},
   530  				},
   531  			},
   532  			"WINDOWS": &WorkflowRunBill{
   533  				TotalMS: Int64(300000),
   534  				Jobs:    Int(2),
   535  			},
   536  		},
   537  		RunDurationMS: Int64(500000),
   538  	}
   539  
   540  	if !cmp.Equal(workflowRunUsage, want) {
   541  		t.Errorf("Actions.GetWorkflowRunUsageByID returned %+v, want %+v", workflowRunUsage, want)
   542  	}
   543  
   544  	const methodName = "GetWorkflowRunUsageByID"
   545  	testBadOptions(t, methodName, func() (err error) {
   546  		_, _, err = client.Actions.GetWorkflowRunUsageByID(ctx, "\n", "\n", 29679449)
   547  		return err
   548  	})
   549  
   550  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   551  		got, resp, err := client.Actions.GetWorkflowRunUsageByID(ctx, "o", "r", 29679449)
   552  		if got != nil {
   553  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   554  		}
   555  		return resp, err
   556  	})
   557  }
   558  
   559  func TestWorkflowRun_Marshal(t *testing.T) {
   560  	testJSONMarshal(t, &WorkflowRun{}, "{}")
   561  
   562  	u := &WorkflowRun{
   563  		ID:         Int64(1),
   564  		Name:       String("n"),
   565  		NodeID:     String("nid"),
   566  		HeadBranch: String("hb"),
   567  		HeadSHA:    String("hs"),
   568  		RunNumber:  Int(1),
   569  		RunAttempt: Int(1),
   570  		Event:      String("e"),
   571  		Status:     String("s"),
   572  		Conclusion: String("c"),
   573  		WorkflowID: Int64(1),
   574  		URL:        String("u"),
   575  		HTMLURL:    String("h"),
   576  		PullRequests: []*PullRequest{
   577  			{
   578  				URL:    String("u"),
   579  				ID:     Int64(1),
   580  				Number: Int(1),
   581  				Head: &PullRequestBranch{
   582  					Ref: String("r"),
   583  					SHA: String("s"),
   584  					Repo: &Repository{
   585  						ID:   Int64(1),
   586  						URL:  String("s"),
   587  						Name: String("n"),
   588  					},
   589  				},
   590  				Base: &PullRequestBranch{
   591  					Ref: String("r"),
   592  					SHA: String("s"),
   593  					Repo: &Repository{
   594  						ID:   Int64(1),
   595  						URL:  String("u"),
   596  						Name: String("n"),
   597  					},
   598  				},
   599  			},
   600  		},
   601  		CreatedAt:          &Timestamp{referenceTime},
   602  		UpdatedAt:          &Timestamp{referenceTime},
   603  		RunStartedAt:       &Timestamp{referenceTime},
   604  		JobsURL:            String("j"),
   605  		LogsURL:            String("l"),
   606  		CheckSuiteURL:      String("c"),
   607  		ArtifactsURL:       String("a"),
   608  		CancelURL:          String("c"),
   609  		RerunURL:           String("r"),
   610  		PreviousAttemptURL: String("p"),
   611  		HeadCommit: &HeadCommit{
   612  			Message: String("m"),
   613  			Author: &CommitAuthor{
   614  				Name:  String("n"),
   615  				Email: String("e"),
   616  				Login: String("l"),
   617  			},
   618  			URL:       String("u"),
   619  			Distinct:  Bool(false),
   620  			SHA:       String("s"),
   621  			ID:        String("i"),
   622  			TreeID:    String("tid"),
   623  			Timestamp: &Timestamp{referenceTime},
   624  			Committer: &CommitAuthor{
   625  				Name:  String("n"),
   626  				Email: String("e"),
   627  				Login: String("l"),
   628  			},
   629  		},
   630  		WorkflowURL: String("w"),
   631  		Repository: &Repository{
   632  			ID:   Int64(1),
   633  			URL:  String("u"),
   634  			Name: String("n"),
   635  		},
   636  		HeadRepository: &Repository{
   637  			ID:   Int64(1),
   638  			URL:  String("u"),
   639  			Name: String("n"),
   640  		},
   641  		Actor: &User{
   642  			Login:           String("l"),
   643  			ID:              Int64(1),
   644  			AvatarURL:       String("a"),
   645  			GravatarID:      String("g"),
   646  			Name:            String("n"),
   647  			Company:         String("c"),
   648  			Blog:            String("b"),
   649  			Location:        String("l"),
   650  			Email:           String("e"),
   651  			Hireable:        Bool(true),
   652  			Bio:             String("b"),
   653  			TwitterUsername: String("t"),
   654  			PublicRepos:     Int(1),
   655  			Followers:       Int(1),
   656  			Following:       Int(1),
   657  			CreatedAt:       &Timestamp{referenceTime},
   658  			SuspendedAt:     &Timestamp{referenceTime},
   659  			URL:             String("u"),
   660  		},
   661  	}
   662  
   663  	want := `{
   664  		"id": 1,
   665  		"name": "n",
   666  		"node_id": "nid",
   667  		"head_branch": "hb",
   668  		"head_sha": "hs",
   669  		"run_number": 1,
   670  		"run_attempt": 1,
   671  		"event": "e",
   672  		"status": "s",
   673  		"conclusion": "c",
   674  		"workflow_id": 1,
   675  		"url": "u",
   676  		"html_url": "h",
   677  		"pull_requests": [
   678  			{
   679  				"id":1,
   680  				"number":1,
   681  				"url":"u",
   682  				"head":{
   683  					"ref":"r",
   684  					"sha":"s",
   685  					"repo": {
   686  						"id":1,
   687  						"name":"n",
   688  						"url":"s"
   689  						}
   690  					},
   691  					"base": {
   692  						"ref":"r",
   693  						"sha":"s",
   694  						"repo": {
   695  							"id":1,
   696  							"name":"n",
   697  							"url":"u"
   698  						}
   699  					}
   700  			}
   701  		],
   702  		"created_at": ` + referenceTimeStr + `,
   703  		"updated_at": ` + referenceTimeStr + `,
   704  		"run_started_at": ` + referenceTimeStr + `,
   705  		"jobs_url": "j",
   706  		"logs_url": "l",
   707  		"check_suite_url": "c",
   708  		"artifacts_url": "a",
   709  		"cancel_url": "c",
   710  		"rerun_url": "r",
   711  		"previous_attempt_url": "p",
   712  		"head_commit": {
   713  			"message": "m",
   714  			"author": {
   715  				"name": "n",
   716  				"email": "e",
   717  				"username": "l"
   718  			},
   719  			"url": "u",
   720  			"distinct": false,
   721  			"sha": "s",
   722  			"id": "i",
   723  			"tree_id": "tid",
   724  			"timestamp": ` + referenceTimeStr + `,
   725  			"committer": {
   726  				"name": "n",
   727  				"email": "e",
   728  				"username": "l"
   729  			}
   730  		},
   731  		"workflow_url": "w",
   732  		"repository": {
   733  			"id": 1,
   734  			"url": "u",
   735  			"name": "n"
   736  		},
   737  		"head_repository": {
   738  			"id": 1,
   739  			"url": "u",
   740  			"name": "n"
   741  		},
   742  		"actor": {
   743  			"login": "l",
   744  			"id": 1,
   745  			"avatar_url": "a",
   746  			"gravatar_id": "g",
   747  			"name": "n",
   748  			"company": "c",
   749  			"blog": "b",
   750  			"location": "l",
   751  			"email": "e",
   752  			"hireable": true,
   753  			"bio": "b",
   754  			"twitter_username": "t",
   755  			"public_repos": 1,
   756  			"followers": 1,
   757  			"following": 1,
   758  			"created_at": ` + referenceTimeStr + `,
   759  			"suspended_at": ` + referenceTimeStr + `,
   760  			"url": "u"
   761  		}
   762  	}`
   763  
   764  	testJSONMarshal(t, u, want)
   765  }
   766  
   767  func TestWorkflowRuns_Marshal(t *testing.T) {
   768  	testJSONMarshal(t, &WorkflowRuns{}, "{}")
   769  
   770  	u := &WorkflowRuns{
   771  		TotalCount: Int(1),
   772  		WorkflowRuns: []*WorkflowRun{
   773  			{
   774  				ID:         Int64(1),
   775  				Name:       String("n"),
   776  				NodeID:     String("nid"),
   777  				HeadBranch: String("hb"),
   778  				HeadSHA:    String("hs"),
   779  				RunNumber:  Int(1),
   780  				RunAttempt: Int(1),
   781  				Event:      String("e"),
   782  				Status:     String("s"),
   783  				Conclusion: String("c"),
   784  				WorkflowID: Int64(1),
   785  				URL:        String("u"),
   786  				HTMLURL:    String("h"),
   787  				PullRequests: []*PullRequest{
   788  					{
   789  						URL:    String("u"),
   790  						ID:     Int64(1),
   791  						Number: Int(1),
   792  						Head: &PullRequestBranch{
   793  							Ref: String("r"),
   794  							SHA: String("s"),
   795  							Repo: &Repository{
   796  								ID:   Int64(1),
   797  								URL:  String("s"),
   798  								Name: String("n"),
   799  							},
   800  						},
   801  						Base: &PullRequestBranch{
   802  							Ref: String("r"),
   803  							SHA: String("s"),
   804  							Repo: &Repository{
   805  								ID:   Int64(1),
   806  								URL:  String("u"),
   807  								Name: String("n"),
   808  							},
   809  						},
   810  					},
   811  				},
   812  				CreatedAt:          &Timestamp{referenceTime},
   813  				UpdatedAt:          &Timestamp{referenceTime},
   814  				RunStartedAt:       &Timestamp{referenceTime},
   815  				JobsURL:            String("j"),
   816  				LogsURL:            String("l"),
   817  				CheckSuiteURL:      String("c"),
   818  				ArtifactsURL:       String("a"),
   819  				CancelURL:          String("c"),
   820  				RerunURL:           String("r"),
   821  				PreviousAttemptURL: String("p"),
   822  				HeadCommit: &HeadCommit{
   823  					Message: String("m"),
   824  					Author: &CommitAuthor{
   825  						Name:  String("n"),
   826  						Email: String("e"),
   827  						Login: String("l"),
   828  					},
   829  					URL:       String("u"),
   830  					Distinct:  Bool(false),
   831  					SHA:       String("s"),
   832  					ID:        String("i"),
   833  					TreeID:    String("tid"),
   834  					Timestamp: &Timestamp{referenceTime},
   835  					Committer: &CommitAuthor{
   836  						Name:  String("n"),
   837  						Email: String("e"),
   838  						Login: String("l"),
   839  					},
   840  				},
   841  				WorkflowURL: String("w"),
   842  				Repository: &Repository{
   843  					ID:   Int64(1),
   844  					URL:  String("u"),
   845  					Name: String("n"),
   846  				},
   847  				HeadRepository: &Repository{
   848  					ID:   Int64(1),
   849  					URL:  String("u"),
   850  					Name: String("n"),
   851  				},
   852  				Actor: &User{
   853  					Login:           String("l"),
   854  					ID:              Int64(1),
   855  					AvatarURL:       String("a"),
   856  					GravatarID:      String("g"),
   857  					Name:            String("n"),
   858  					Company:         String("c"),
   859  					Blog:            String("b"),
   860  					Location:        String("l"),
   861  					Email:           String("e"),
   862  					Hireable:        Bool(true),
   863  					Bio:             String("b"),
   864  					TwitterUsername: String("t"),
   865  					PublicRepos:     Int(1),
   866  					Followers:       Int(1),
   867  					Following:       Int(1),
   868  					CreatedAt:       &Timestamp{referenceTime},
   869  					SuspendedAt:     &Timestamp{referenceTime},
   870  					URL:             String("u"),
   871  				},
   872  			},
   873  		},
   874  	}
   875  
   876  	want := `{
   877  		"total_count": 1,
   878  		"workflow_runs": [
   879  			{
   880  				"id": 1,
   881  				"name": "n",
   882  				"node_id": "nid",
   883  				"head_branch": "hb",
   884  				"head_sha": "hs",
   885  				"run_number": 1,
   886  				"run_attempt": 1,
   887  				"event": "e",
   888  				"status": "s",
   889  				"conclusion": "c",
   890  				"workflow_id": 1,
   891  				"url": "u",
   892  				"html_url": "h",
   893  				"pull_requests": [
   894  					{
   895  						"id":1,
   896  						"number":1,
   897  						"url":"u",
   898  						"head":{
   899  							"ref":"r",
   900  							"sha":"s",
   901  							"repo": {
   902  								"id":1,
   903  								"name":"n",
   904  								"url":"s"
   905  								}
   906  							},
   907  							"base": {
   908  								"ref":"r",
   909  								"sha":"s",
   910  								"repo": {
   911  									"id":1,
   912  									"name":"n",
   913  									"url":"u"
   914  								}
   915  							}
   916  					}
   917  				],
   918  				"created_at": ` + referenceTimeStr + `,
   919  				"updated_at": ` + referenceTimeStr + `,
   920  				"run_started_at": ` + referenceTimeStr + `,
   921  				"jobs_url": "j",
   922  				"logs_url": "l",
   923  				"check_suite_url": "c",
   924  				"artifacts_url": "a",
   925  				"cancel_url": "c",
   926  				"rerun_url": "r",
   927  				"previous_attempt_url": "p",
   928  				"head_commit": {
   929  					"message": "m",
   930  					"author": {
   931  						"name": "n",
   932  						"email": "e",
   933  						"username": "l"
   934  					},
   935  					"url": "u",
   936  					"distinct": false,
   937  					"sha": "s",
   938  					"id": "i",
   939  					"tree_id": "tid",
   940  					"timestamp": ` + referenceTimeStr + `,
   941  					"committer": {
   942  						"name": "n",
   943  						"email": "e",
   944  						"username": "l"
   945  					}
   946  				},
   947  				"workflow_url": "w",
   948  				"repository": {
   949  					"id": 1,
   950  					"url": "u",
   951  					"name": "n"
   952  				},
   953  				"head_repository": {
   954  					"id": 1,
   955  					"url": "u",
   956  					"name": "n"
   957  				},
   958  				"actor": {
   959  					"login": "l",
   960  					"id": 1,
   961  					"avatar_url": "a",
   962  					"gravatar_id": "g",
   963  					"name": "n",
   964  					"company": "c",
   965  					"blog": "b",
   966  					"location": "l",
   967  					"email": "e",
   968  					"hireable": true,
   969  					"bio": "b",
   970  					"twitter_username": "t",
   971  					"public_repos": 1,
   972  					"followers": 1,
   973  					"following": 1,
   974  					"created_at": ` + referenceTimeStr + `,
   975  					"suspended_at": ` + referenceTimeStr + `,
   976  					"url": "u"
   977  				}
   978  			}
   979  		]
   980  	}`
   981  
   982  	testJSONMarshal(t, u, want)
   983  }
   984  
   985  func TestWorkflowRunBill_Marshal(t *testing.T) {
   986  	testJSONMarshal(t, &WorkflowRunBill{}, "{}")
   987  
   988  	u := &WorkflowRunBill{
   989  		TotalMS: Int64(1),
   990  		Jobs:    Int(1),
   991  	}
   992  
   993  	want := `{
   994  		"total_ms": 1,
   995  		"jobs": 1
   996  	}`
   997  
   998  	testJSONMarshal(t, u, want)
   999  }
  1000  
  1001  func TestWorkflowRunBillMap_Marshal(t *testing.T) {
  1002  	testJSONMarshal(t, &WorkflowRunBillMap{}, "{}")
  1003  
  1004  	u := &WorkflowRunBillMap{
  1005  		"UBUNTU": &WorkflowRunBill{
  1006  			TotalMS: Int64(1),
  1007  			Jobs:    Int(1),
  1008  		},
  1009  		"MACOS": &WorkflowRunBill{
  1010  			TotalMS: Int64(1),
  1011  			Jobs:    Int(1),
  1012  		},
  1013  		"WINDOWS": &WorkflowRunBill{
  1014  			TotalMS: Int64(1),
  1015  			Jobs:    Int(1),
  1016  		},
  1017  	}
  1018  
  1019  	want := `{
  1020  		"UBUNTU": {
  1021  			"total_ms": 1,
  1022  			"jobs": 1
  1023  		},
  1024  		"MACOS": {
  1025  			"total_ms": 1,
  1026  			"jobs": 1
  1027  		},
  1028  		"WINDOWS": {
  1029  			"total_ms": 1,
  1030  			"jobs": 1
  1031  		}
  1032  	}`
  1033  
  1034  	testJSONMarshal(t, u, want)
  1035  }
  1036  
  1037  func TestWorkflowRunUsage_Marshal(t *testing.T) {
  1038  	testJSONMarshal(t, &WorkflowRunUsage{}, "{}")
  1039  
  1040  	u := &WorkflowRunUsage{
  1041  		Billable: &WorkflowRunBillMap{
  1042  			"UBUNTU": &WorkflowRunBill{
  1043  				TotalMS: Int64(1),
  1044  				Jobs:    Int(1),
  1045  			},
  1046  			"MACOS": &WorkflowRunBill{
  1047  				TotalMS: Int64(1),
  1048  				Jobs:    Int(1),
  1049  			},
  1050  			"WINDOWS": &WorkflowRunBill{
  1051  				TotalMS: Int64(1),
  1052  				Jobs:    Int(1),
  1053  			},
  1054  		},
  1055  		RunDurationMS: Int64(1),
  1056  	}
  1057  
  1058  	want := `{
  1059  		"billable": {
  1060  			"UBUNTU": {
  1061  				"total_ms": 1,
  1062  				"jobs": 1
  1063  			},
  1064  			"MACOS": {
  1065  				"total_ms": 1,
  1066  				"jobs": 1
  1067  			},
  1068  			"WINDOWS": {
  1069  				"total_ms": 1,
  1070  				"jobs": 1
  1071  			}
  1072  		},
  1073  		"run_duration_ms": 1
  1074  	}`
  1075  
  1076  	testJSONMarshal(t, u, want)
  1077  }
  1078  
  1079  func TestActionService_PendingDeployments(t *testing.T) {
  1080  	client, mux, _, teardown := setup()
  1081  	defer teardown()
  1082  
  1083  	input := &PendingDeploymentsRequest{EnvironmentIDs: []int64{3, 4}, State: "approved", Comment: ""}
  1084  
  1085  	mux.HandleFunc("/repos/o/r/actions/runs/399444496/pending_deployments", func(w http.ResponseWriter, r *http.Request) {
  1086  		v := new(PendingDeploymentsRequest)
  1087  		json.NewDecoder(r.Body).Decode(v)
  1088  
  1089  		testMethod(t, r, "POST")
  1090  		if !cmp.Equal(v, input) {
  1091  			t.Errorf("Request body = %+v, want %+v", v, input)
  1092  		}
  1093  
  1094  		fmt.Fprint(w, `[{"id":1}, {"id":2}]`)
  1095  	})
  1096  
  1097  	ctx := context.Background()
  1098  	deployments, _, err := client.Actions.PendingDeployments(ctx, "o", "r", 399444496, input)
  1099  	if err != nil {
  1100  		t.Errorf("Actions.PendingDeployments returned error: %v", err)
  1101  	}
  1102  
  1103  	want := []*Deployment{{ID: Int64(1)}, {ID: Int64(2)}}
  1104  	if !cmp.Equal(deployments, want) {
  1105  		t.Errorf("Actions.PendingDeployments returned %+v, want %+v", deployments, want)
  1106  	}
  1107  
  1108  	const methodName = "PendingDeployments"
  1109  	testBadOptions(t, methodName, func() (err error) {
  1110  		_, _, err = client.Actions.PendingDeployments(ctx, "\n", "\n", 399444496, input)
  1111  		return err
  1112  	})
  1113  
  1114  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
  1115  		got, resp, err := client.Actions.PendingDeployments(ctx, "o", "r", 399444496, input)
  1116  		if got != nil {
  1117  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
  1118  		}
  1119  		return resp, err
  1120  	})
  1121  }