From dbf1d707b00e80dafa754bc9407c2dd01edbdf7b Mon Sep 17 00:00:00 2001 From: ganeshkumarsv <53483484+ganeshkumarsv@users.noreply.github.com> Date: Mon, 25 Oct 2021 20:07:37 -0400 Subject: [PATCH 01/14] Add a method to upload Sarif results to Github Add a method to upload Code Scanning Sarif results to Github --- github/code-scanning.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/github/code-scanning.go b/github/code-scanning.go index 50264a0c3ba..f05606911ad 100644 --- a/github/code-scanning.go +++ b/github/code-scanning.go @@ -171,3 +171,19 @@ func (s *CodeScanningService) GetAlert(ctx context.Context, owner, repo string, return a, resp, nil } + +func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, sarif string) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/code-scanning/sarifs", owner, repo) + + req, err := s.client.NewRequest("POST", u, sarif) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, "{}") + if err != nil { + return resp, err + } + + return resp, nil +} From c743d4c37150f401f94178bb2685f81b57c5ea13 Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Thu, 28 Oct 2021 15:23:15 -0400 Subject: [PATCH 02/14] adding sarif struct to push the response --- github/code-scanning.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/github/code-scanning.go b/github/code-scanning.go index f05606911ad..3864cf09caa 100644 --- a/github/code-scanning.go +++ b/github/code-scanning.go @@ -119,6 +119,12 @@ type AlertListOptions struct { ListOptions } +type SarifObject struct { + CommitSHA *string `json:"commit_sha,omitempty"` + Ref *string `json:"ref,omitempty"` + Sarif *string `json:"sarif,omitempty"` +} + // ListAlertsForRepo lists code scanning alerts for a repository. // // Lists all open code scanning alerts for the default branch (usually master) and protected branches in a repository. @@ -179,8 +185,8 @@ func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo strin if err != nil { return nil, err } - - resp, err := s.client.Do(ctx, req, "{}") + sarifObj := new(SarifObject) + resp, err := s.client.Do(ctx, req, sarifObj) if err != nil { return resp, err } From 1496951e77b01753bfb305d61675d2c1252ceae7 Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Thu, 28 Oct 2021 15:42:48 -0400 Subject: [PATCH 03/14] fix upload sarif --- github/code-scanning.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/github/code-scanning.go b/github/code-scanning.go index 3864cf09caa..ef08c5ca157 100644 --- a/github/code-scanning.go +++ b/github/code-scanning.go @@ -178,15 +178,19 @@ func (s *CodeScanningService) GetAlert(ctx context.Context, owner, repo string, return a, resp, nil } -func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, sarif string) (*Response, error) { +func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, commitSHA string, ref string, sarif string) (*Response, error) { u := fmt.Sprintf("repos/%v/%v/code-scanning/sarifs", owner, repo) - - req, err := s.client.NewRequest("POST", u, sarif) + sarifObj := &SarifObject{ + CommitSHA: &commitSHA, + Ref: &ref, + Sarif: &sarif, + } + req, err := s.client.NewRequest("POST", u, sarifObj) if err != nil { return nil, err } - sarifObj := new(SarifObject) - resp, err := s.client.Do(ctx, req, sarifObj) + sarifInterface := new(SarifObject) + resp, err := s.client.Do(ctx, req, sarifInterface) if err != nil { return resp, err } From 3aec3e5c08003b2d35a6cdd722da7cd6c1354851 Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Thu, 28 Oct 2021 16:17:51 -0400 Subject: [PATCH 04/14] adding docs for uploading sarif --- github/code-scanning.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/github/code-scanning.go b/github/code-scanning.go index ef08c5ca157..dec34012d54 100644 --- a/github/code-scanning.go +++ b/github/code-scanning.go @@ -119,10 +119,14 @@ type AlertListOptions struct { ListOptions } -type SarifObject struct { - CommitSHA *string `json:"commit_sha,omitempty"` - Ref *string `json:"ref,omitempty"` - Sarif *string `json:"sarif,omitempty"` +// AnalysisResults specifies the results of a code scanning job. +// +// GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data + +type AnalysisResults struct { + CommitSHA *string `json:"commit_sha,omitempty"` + Ref *string `json:"ref,omitempty"` + Sarif *string `json:"sarif,omitempty"` } // ListAlertsForRepo lists code scanning alerts for a repository. @@ -178,18 +182,25 @@ func (s *CodeScanningService) GetAlert(ctx context.Context, owner, repo string, return a, resp, nil } +// UploadSarif uploads the result of code scanning job to GitHub. +// +// For the parameter sarif, you must first compress your SARIF file using gzip and then translate the contents of the file into a Base64 encoding string +// You must use an access token with the security_events scope to use this endpoint. GitHub Apps must have the security_events +// write permission to use this endpoint. +// +// GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, commitSHA string, ref string, sarif string) (*Response, error) { u := fmt.Sprintf("repos/%v/%v/code-scanning/sarifs", owner, repo) - sarifObj := &SarifObject{ + sarifObj := &AnalysisResults{ CommitSHA: &commitSHA, - Ref: &ref, - Sarif: &sarif, + Ref: &ref, + Sarif: &sarif, } req, err := s.client.NewRequest("POST", u, sarifObj) if err != nil { return nil, err } - sarifInterface := new(SarifObject) + sarifInterface := new(AnalysisResults) resp, err := s.client.Do(ctx, req, sarifInterface) if err != nil { return resp, err From 05dc42e8c00ffc205caa552c267cbb834d4288e7 Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Thu, 28 Oct 2021 16:20:37 -0400 Subject: [PATCH 05/14] fix variables for uploading sarif --- github/code-scanning.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/github/code-scanning.go b/github/code-scanning.go index dec34012d54..652e547e7c6 100644 --- a/github/code-scanning.go +++ b/github/code-scanning.go @@ -191,17 +191,17 @@ func (s *CodeScanningService) GetAlert(ctx context.Context, owner, repo string, // GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, commitSHA string, ref string, sarif string) (*Response, error) { u := fmt.Sprintf("repos/%v/%v/code-scanning/sarifs", owner, repo) - sarifObj := &AnalysisResults{ + results := &AnalysisResults{ CommitSHA: &commitSHA, Ref: &ref, Sarif: &sarif, } - req, err := s.client.NewRequest("POST", u, sarifObj) + req, err := s.client.NewRequest("POST", u, results) if err != nil { return nil, err } - sarifInterface := new(AnalysisResults) - resp, err := s.client.Do(ctx, req, sarifInterface) + analysis := new(AnalysisResults) + resp, err := s.client.Do(ctx, req, analysis) if err != nil { return resp, err } From d051ca3d8fef24c2061a337ad2b4d5408f163a4d Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Thu, 28 Oct 2021 16:30:02 -0400 Subject: [PATCH 06/14] adding accessors for upload sarif method --- github/github-accessors.go | 24 ++++++++++++++++++++++++ github/github-accessors_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/github/github-accessors.go b/github/github-accessors.go index 1d2db4ecadb..8817fe440ba 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -284,6 +284,30 @@ func (a *Alert) GetURL() string { return *a.URL } +// GetCommitSHA returns the CommitSHA field if it's non-nil, zero value otherwise. +func (a *AnalysisResults) GetCommitSHA() string { + if a == nil || a.CommitSHA == nil { + return "" + } + return *a.CommitSHA +} + +// GetRef returns the Ref field if it's non-nil, zero value otherwise. +func (a *AnalysisResults) GetRef() string { + if a == nil || a.Ref == nil { + return "" + } + return *a.Ref +} + +// GetSarif returns the Sarif field if it's non-nil, zero value otherwise. +func (a *AnalysisResults) GetSarif() string { + if a == nil || a.Sarif == nil { + return "" + } + return *a.Sarif +} + // GetVerifiablePasswordAuthentication returns the VerifiablePasswordAuthentication field if it's non-nil, zero value otherwise. func (a *APIMeta) GetVerifiablePasswordAuthentication() bool { if a == nil || a.VerifiablePasswordAuthentication == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index f6af9991366..85cd5422492 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -308,6 +308,36 @@ func TestAlert_GetURL(tt *testing.T) { a.GetURL() } +func TestAnalysisResults_GetCommitSHA(tt *testing.T) { + var zeroValue string + a := &AnalysisResults{CommitSHA: &zeroValue} + a.GetCommitSHA() + a = &AnalysisResults{} + a.GetCommitSHA() + a = nil + a.GetCommitSHA() +} + +func TestAnalysisResults_GetRef(tt *testing.T) { + var zeroValue string + a := &AnalysisResults{Ref: &zeroValue} + a.GetRef() + a = &AnalysisResults{} + a.GetRef() + a = nil + a.GetRef() +} + +func TestAnalysisResults_GetSarif(tt *testing.T) { + var zeroValue string + a := &AnalysisResults{Sarif: &zeroValue} + a.GetSarif() + a = &AnalysisResults{} + a.GetSarif() + a = nil + a.GetSarif() +} + func TestAPIMeta_GetVerifiablePasswordAuthentication(tt *testing.T) { var zeroValue bool a := &APIMeta{VerifiablePasswordAuthentication: &zeroValue} From 73eb8f3e9d2da066f678cdd244ed0b5acd3a25fd Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Thu, 28 Oct 2021 17:12:05 -0400 Subject: [PATCH 07/14] adding test case for Upload Sarif --- github/code-scanning_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/github/code-scanning_test.go b/github/code-scanning_test.go index 6ef8d84aef0..b5f5830cce0 100644 --- a/github/code-scanning_test.go +++ b/github/code-scanning_test.go @@ -7,8 +7,10 @@ package github import ( "context" + "encoding/json" "fmt" "net/http" + "reflect" "testing" "time" @@ -53,6 +55,38 @@ func TestActionsService_Alert_ID(t *testing.T) { } } +func TestActionsService_UploadSarif(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/code-scanning/sarifs", func(w http.ResponseWriter, r *http.Request) { + v := new(AnalysisResults) + json.NewDecoder(r.Body).Decode(v) + testMethod(t, r, "POST") + want := &AnalysisResults{CommitSHA: String("abc"), Ref: String("ref/head/main"), Sarif: String("abc")} + if !reflect.DeepEqual(v, want) { + t.Errorf("Request body = %+v, want %+v", v, want) + } + + fmt.Fprint(w, `{"commit_sha":"abc","ref":"ref/head/main","sarif":"abc"}`) + }) + + ctx := context.Background() + _, err := client.CodeScanning.UploadSarif(ctx, "o", "r", "abc", "ref/head/main", "abc") + if err != nil { + t.Errorf("CodeScanning.UploadSarif returned error: %v", err) + } + const methodName = "UploadSarif" + testBadOptions(t, methodName, func() (err error) { + _, err = client.CodeScanning.UploadSarif(ctx, "\n", "\n", "abc", "ref/head/main", "abc") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.CodeScanning.UploadSarif(ctx, "o", "r", "abc", "ref/head/main", "abc") + }) +} + func TestActionsService_ListAlertsForRepo(t *testing.T) { client, mux, _, teardown := setup() defer teardown() From 8fe1023010e6ea379adc290a98ebfcae11d26e6f Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Sat, 30 Oct 2021 07:15:37 -0400 Subject: [PATCH 08/14] replace reflect with cmp --- github/code-scanning_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/github/code-scanning_test.go b/github/code-scanning_test.go index b5f5830cce0..e6bc5b05291 100644 --- a/github/code-scanning_test.go +++ b/github/code-scanning_test.go @@ -10,7 +10,6 @@ import ( "encoding/json" "fmt" "net/http" - "reflect" "testing" "time" @@ -64,7 +63,7 @@ func TestActionsService_UploadSarif(t *testing.T) { json.NewDecoder(r.Body).Decode(v) testMethod(t, r, "POST") want := &AnalysisResults{CommitSHA: String("abc"), Ref: String("ref/head/main"), Sarif: String("abc")} - if !reflect.DeepEqual(v, want) { + if !cmp.Equal(v, want) { t.Errorf("Request body = %+v, want %+v", v, want) } From 94a6a586bd0a76452def4f67da344323328ab907 Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Mon, 1 Nov 2021 12:50:11 -0400 Subject: [PATCH 09/14] addressing code review comments --- github/code-scanning.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/github/code-scanning.go b/github/code-scanning.go index 652e547e7c6..931152cb997 100644 --- a/github/code-scanning.go +++ b/github/code-scanning.go @@ -119,11 +119,10 @@ type AlertListOptions struct { ListOptions } -// AnalysisResults specifies the results of a code scanning job. +// SarifAnalysis specifies the results of a code scanning job. // // GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data - -type AnalysisResults struct { +type SarifAnalysis struct { CommitSHA *string `json:"commit_sha,omitempty"` Ref *string `json:"ref,omitempty"` Sarif *string `json:"sarif,omitempty"` @@ -184,23 +183,20 @@ func (s *CodeScanningService) GetAlert(ctx context.Context, owner, repo string, // UploadSarif uploads the result of code scanning job to GitHub. // -// For the parameter sarif, you must first compress your SARIF file using gzip and then translate the contents of the file into a Base64 encoding string +// For the parameter sarif, you must first compress your SARIF file using gzip and then translate the contents of the file into a Base64 encoding string. // You must use an access token with the security_events scope to use this endpoint. GitHub Apps must have the security_events // write permission to use this endpoint. // // GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data -func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, commitSHA string, ref string, sarif string) (*Response, error) { +func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, sarif *SarifAnalysis) (*Response, error) { u := fmt.Sprintf("repos/%v/%v/code-scanning/sarifs", owner, repo) - results := &AnalysisResults{ - CommitSHA: &commitSHA, - Ref: &ref, - Sarif: &sarif, - } - req, err := s.client.NewRequest("POST", u, results) + + req, err := s.client.NewRequest("POST", u, sarif) if err != nil { return nil, err } - analysis := new(AnalysisResults) + + analysis := new(SarifAnalysis) resp, err := s.client.Do(ctx, req, analysis) if err != nil { return resp, err From afd88007369dea210d6b9ea71e6a551be6a5d84c Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Tue, 2 Nov 2021 11:54:29 -0400 Subject: [PATCH 10/14] address code review comments --- github/code-scanning.go | 26 +++++++++++++++++--------- github/code-scanning_test.go | 12 +++++++----- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/github/code-scanning.go b/github/code-scanning.go index 931152cb997..1e240ec4422 100644 --- a/github/code-scanning.go +++ b/github/code-scanning.go @@ -123,9 +123,17 @@ type AlertListOptions struct { // // GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data type SarifAnalysis struct { - CommitSHA *string `json:"commit_sha,omitempty"` - Ref *string `json:"ref,omitempty"` - Sarif *string `json:"sarif,omitempty"` + CommitSHA *string `json:"commit_sha,omitempty"` + Ref *string `json:"ref,omitempty"` + Sarif *string `json:"sarif,omitempty"` + CheckoutURI *string `json:"checkout_uri,omitempty"` + StartedAt *string `json:"started_at,omitempty"` + ToolName *string `json:"tool_name,omitempty"` +} + +type SarifID struct { + ID *string `json:"id,omitempty"` + URL *string `json:"url,omitempty"` } // ListAlertsForRepo lists code scanning alerts for a repository. @@ -188,19 +196,19 @@ func (s *CodeScanningService) GetAlert(ctx context.Context, owner, repo string, // write permission to use this endpoint. // // GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data -func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, sarif *SarifAnalysis) (*Response, error) { +func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, sarif *SarifAnalysis) (*SarifID, *Response, error) { u := fmt.Sprintf("repos/%v/%v/code-scanning/sarifs", owner, repo) req, err := s.client.NewRequest("POST", u, sarif) if err != nil { - return nil, err + return nil, nil, err } - analysis := new(SarifAnalysis) - resp, err := s.client.Do(ctx, req, analysis) + sarifID := new(SarifID) + resp, err := s.client.Do(ctx, req, sarifID) if err != nil { - return resp, err + return nil, resp, err } - return resp, nil + return sarifID, resp, nil } diff --git a/github/code-scanning_test.go b/github/code-scanning_test.go index e6bc5b05291..0c949e9528b 100644 --- a/github/code-scanning_test.go +++ b/github/code-scanning_test.go @@ -59,10 +59,10 @@ func TestActionsService_UploadSarif(t *testing.T) { defer teardown() mux.HandleFunc("/repos/o/r/code-scanning/sarifs", func(w http.ResponseWriter, r *http.Request) { - v := new(AnalysisResults) + v := new(SarifAnalysis) json.NewDecoder(r.Body).Decode(v) testMethod(t, r, "POST") - want := &AnalysisResults{CommitSHA: String("abc"), Ref: String("ref/head/main"), Sarif: String("abc")} + want := &SarifAnalysis{CommitSHA: String("abc"), Ref: String("ref/head/main"), Sarif: String("abc"), CheckoutURI: String("uri"), StartedAt: String("123"), ToolName: String("codeql-cli")} if !cmp.Equal(v, want) { t.Errorf("Request body = %+v, want %+v", v, want) } @@ -71,18 +71,20 @@ func TestActionsService_UploadSarif(t *testing.T) { }) ctx := context.Background() - _, err := client.CodeScanning.UploadSarif(ctx, "o", "r", "abc", "ref/head/main", "abc") + sarifAnalysis := &SarifAnalysis{CommitSHA: String("abc"), Ref: String("ref/head/main"), Sarif: String("abc"), CheckoutURI: String("uri"), StartedAt: String("123"), ToolName: String("codeql-cli")} + _, _, err := client.CodeScanning.UploadSarif(ctx, "o", "r", sarifAnalysis) if err != nil { t.Errorf("CodeScanning.UploadSarif returned error: %v", err) } const methodName = "UploadSarif" testBadOptions(t, methodName, func() (err error) { - _, err = client.CodeScanning.UploadSarif(ctx, "\n", "\n", "abc", "ref/head/main", "abc") + _, _, err = client.CodeScanning.UploadSarif(ctx, "\n", "\n", sarifAnalysis) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - return client.CodeScanning.UploadSarif(ctx, "o", "r", "abc", "ref/head/main", "abc") + _, resp, err := client.CodeScanning.UploadSarif(ctx, "o", "r", sarifAnalysis) + return resp, err }) } From f7288a7f3a2bee92bf56ecd7785ace7fce69febd Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Tue, 2 Nov 2021 13:05:45 -0400 Subject: [PATCH 11/14] add go-doc style and address review comments --- github/code-scanning.go | 5 ++++- github/code-scanning_test.go | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/github/code-scanning.go b/github/code-scanning.go index 1e240ec4422..544f11ca94c 100644 --- a/github/code-scanning.go +++ b/github/code-scanning.go @@ -127,10 +127,13 @@ type SarifAnalysis struct { Ref *string `json:"ref,omitempty"` Sarif *string `json:"sarif,omitempty"` CheckoutURI *string `json:"checkout_uri,omitempty"` - StartedAt *string `json:"started_at,omitempty"` + StartedAt *Timestamp `json:"started_at,omitempty"` ToolName *string `json:"tool_name,omitempty"` } +// SarifID specifies the status of sarif upload. +// +// GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data type SarifID struct { ID *string `json:"id,omitempty"` URL *string `json:"url,omitempty"` diff --git a/github/code-scanning_test.go b/github/code-scanning_test.go index 0c949e9528b..6dada610baf 100644 --- a/github/code-scanning_test.go +++ b/github/code-scanning_test.go @@ -62,7 +62,7 @@ func TestActionsService_UploadSarif(t *testing.T) { v := new(SarifAnalysis) json.NewDecoder(r.Body).Decode(v) testMethod(t, r, "POST") - want := &SarifAnalysis{CommitSHA: String("abc"), Ref: String("ref/head/main"), Sarif: String("abc"), CheckoutURI: String("uri"), StartedAt: String("123"), ToolName: String("codeql-cli")} + want := &SarifAnalysis{CommitSHA: String("abc"), Ref: String("ref/head/main"), Sarif: String("abc"), CheckoutURI: String("uri"), StartedAt: &Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}, ToolName: String("codeql-cli")} if !cmp.Equal(v, want) { t.Errorf("Request body = %+v, want %+v", v, want) } @@ -71,12 +71,13 @@ func TestActionsService_UploadSarif(t *testing.T) { }) ctx := context.Background() - sarifAnalysis := &SarifAnalysis{CommitSHA: String("abc"), Ref: String("ref/head/main"), Sarif: String("abc"), CheckoutURI: String("uri"), StartedAt: String("123"), ToolName: String("codeql-cli")} + sarifAnalysis := &SarifAnalysis{CommitSHA: String("abc"), Ref: String("ref/head/main"), Sarif: String("abc"), CheckoutURI: String("uri"), StartedAt: &Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)}, ToolName: String("codeql-cli")} _, _, err := client.CodeScanning.UploadSarif(ctx, "o", "r", sarifAnalysis) if err != nil { t.Errorf("CodeScanning.UploadSarif returned error: %v", err) } const methodName = "UploadSarif" + testBadOptions(t, methodName, func() (err error) { _, _, err = client.CodeScanning.UploadSarif(ctx, "\n", "\n", sarifAnalysis) return err From f642f4e52d559b68dd7f8436209f67d2c757feb9 Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Tue, 2 Nov 2021 13:06:43 -0400 Subject: [PATCH 12/14] go fmt --- github/code-scanning.go | 12 ++++++------ github/code-scanning_test.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/github/code-scanning.go b/github/code-scanning.go index 544f11ca94c..c49da37fffb 100644 --- a/github/code-scanning.go +++ b/github/code-scanning.go @@ -123,19 +123,19 @@ type AlertListOptions struct { // // GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data type SarifAnalysis struct { - CommitSHA *string `json:"commit_sha,omitempty"` - Ref *string `json:"ref,omitempty"` - Sarif *string `json:"sarif,omitempty"` - CheckoutURI *string `json:"checkout_uri,omitempty"` + CommitSHA *string `json:"commit_sha,omitempty"` + Ref *string `json:"ref,omitempty"` + Sarif *string `json:"sarif,omitempty"` + CheckoutURI *string `json:"checkout_uri,omitempty"` StartedAt *Timestamp `json:"started_at,omitempty"` - ToolName *string `json:"tool_name,omitempty"` + ToolName *string `json:"tool_name,omitempty"` } // SarifID specifies the status of sarif upload. // // GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data type SarifID struct { - ID *string `json:"id,omitempty"` + ID *string `json:"id,omitempty"` URL *string `json:"url,omitempty"` } diff --git a/github/code-scanning_test.go b/github/code-scanning_test.go index 6dada610baf..e6755a4c56d 100644 --- a/github/code-scanning_test.go +++ b/github/code-scanning_test.go @@ -77,7 +77,7 @@ func TestActionsService_UploadSarif(t *testing.T) { t.Errorf("CodeScanning.UploadSarif returned error: %v", err) } const methodName = "UploadSarif" - + testBadOptions(t, methodName, func() (err error) { _, _, err = client.CodeScanning.UploadSarif(ctx, "\n", "\n", sarifAnalysis) return err From 86e6e4dc2fc3a83317297e1361259a2cc22b0821 Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Tue, 2 Nov 2021 13:10:17 -0400 Subject: [PATCH 13/14] change accessors after modifying the structs --- github/github-accessors.go | 88 ++++++++++++++++++------- github/github-accessors_test.go | 110 +++++++++++++++++++++++--------- 2 files changed, 144 insertions(+), 54 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 8817fe440ba..9be9c1e6688 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -284,30 +284,6 @@ func (a *Alert) GetURL() string { return *a.URL } -// GetCommitSHA returns the CommitSHA field if it's non-nil, zero value otherwise. -func (a *AnalysisResults) GetCommitSHA() string { - if a == nil || a.CommitSHA == nil { - return "" - } - return *a.CommitSHA -} - -// GetRef returns the Ref field if it's non-nil, zero value otherwise. -func (a *AnalysisResults) GetRef() string { - if a == nil || a.Ref == nil { - return "" - } - return *a.Ref -} - -// GetSarif returns the Sarif field if it's non-nil, zero value otherwise. -func (a *AnalysisResults) GetSarif() string { - if a == nil || a.Sarif == nil { - return "" - } - return *a.Sarif -} - // GetVerifiablePasswordAuthentication returns the VerifiablePasswordAuthentication field if it's non-nil, zero value otherwise. func (a *APIMeta) GetVerifiablePasswordAuthentication() bool { if a == nil || a.VerifiablePasswordAuthentication == nil { @@ -15172,6 +15148,70 @@ func (r *RunnerLabels) GetType() string { return *r.Type } +// GetCheckoutURI returns the CheckoutURI field if it's non-nil, zero value otherwise. +func (s *SarifAnalysis) GetCheckoutURI() string { + if s == nil || s.CheckoutURI == nil { + return "" + } + return *s.CheckoutURI +} + +// GetCommitSHA returns the CommitSHA field if it's non-nil, zero value otherwise. +func (s *SarifAnalysis) GetCommitSHA() string { + if s == nil || s.CommitSHA == nil { + return "" + } + return *s.CommitSHA +} + +// GetRef returns the Ref field if it's non-nil, zero value otherwise. +func (s *SarifAnalysis) GetRef() string { + if s == nil || s.Ref == nil { + return "" + } + return *s.Ref +} + +// GetSarif returns the Sarif field if it's non-nil, zero value otherwise. +func (s *SarifAnalysis) GetSarif() string { + if s == nil || s.Sarif == nil { + return "" + } + return *s.Sarif +} + +// GetStartedAt returns the StartedAt field if it's non-nil, zero value otherwise. +func (s *SarifAnalysis) GetStartedAt() Timestamp { + if s == nil || s.StartedAt == nil { + return Timestamp{} + } + return *s.StartedAt +} + +// GetToolName returns the ToolName field if it's non-nil, zero value otherwise. +func (s *SarifAnalysis) GetToolName() string { + if s == nil || s.ToolName == nil { + return "" + } + return *s.ToolName +} + +// GetID returns the ID field if it's non-nil, zero value otherwise. +func (s *SarifID) GetID() string { + if s == nil || s.ID == nil { + return "" + } + return *s.ID +} + +// GetURL returns the URL field if it's non-nil, zero value otherwise. +func (s *SarifID) GetURL() string { + if s == nil || s.URL == nil { + return "" + } + return *s.URL +} + // GetActive returns the Active field if it's non-nil, zero value otherwise. func (s *SCIMUserAttributes) GetActive() bool { if s == nil || s.Active == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 85cd5422492..ff89369d352 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -308,36 +308,6 @@ func TestAlert_GetURL(tt *testing.T) { a.GetURL() } -func TestAnalysisResults_GetCommitSHA(tt *testing.T) { - var zeroValue string - a := &AnalysisResults{CommitSHA: &zeroValue} - a.GetCommitSHA() - a = &AnalysisResults{} - a.GetCommitSHA() - a = nil - a.GetCommitSHA() -} - -func TestAnalysisResults_GetRef(tt *testing.T) { - var zeroValue string - a := &AnalysisResults{Ref: &zeroValue} - a.GetRef() - a = &AnalysisResults{} - a.GetRef() - a = nil - a.GetRef() -} - -func TestAnalysisResults_GetSarif(tt *testing.T) { - var zeroValue string - a := &AnalysisResults{Sarif: &zeroValue} - a.GetSarif() - a = &AnalysisResults{} - a.GetSarif() - a = nil - a.GetSarif() -} - func TestAPIMeta_GetVerifiablePasswordAuthentication(tt *testing.T) { var zeroValue bool a := &APIMeta{VerifiablePasswordAuthentication: &zeroValue} @@ -17739,6 +17709,86 @@ func TestRunnerLabels_GetType(tt *testing.T) { r.GetType() } +func TestSarifAnalysis_GetCheckoutURI(tt *testing.T) { + var zeroValue string + s := &SarifAnalysis{CheckoutURI: &zeroValue} + s.GetCheckoutURI() + s = &SarifAnalysis{} + s.GetCheckoutURI() + s = nil + s.GetCheckoutURI() +} + +func TestSarifAnalysis_GetCommitSHA(tt *testing.T) { + var zeroValue string + s := &SarifAnalysis{CommitSHA: &zeroValue} + s.GetCommitSHA() + s = &SarifAnalysis{} + s.GetCommitSHA() + s = nil + s.GetCommitSHA() +} + +func TestSarifAnalysis_GetRef(tt *testing.T) { + var zeroValue string + s := &SarifAnalysis{Ref: &zeroValue} + s.GetRef() + s = &SarifAnalysis{} + s.GetRef() + s = nil + s.GetRef() +} + +func TestSarifAnalysis_GetSarif(tt *testing.T) { + var zeroValue string + s := &SarifAnalysis{Sarif: &zeroValue} + s.GetSarif() + s = &SarifAnalysis{} + s.GetSarif() + s = nil + s.GetSarif() +} + +func TestSarifAnalysis_GetStartedAt(tt *testing.T) { + var zeroValue Timestamp + s := &SarifAnalysis{StartedAt: &zeroValue} + s.GetStartedAt() + s = &SarifAnalysis{} + s.GetStartedAt() + s = nil + s.GetStartedAt() +} + +func TestSarifAnalysis_GetToolName(tt *testing.T) { + var zeroValue string + s := &SarifAnalysis{ToolName: &zeroValue} + s.GetToolName() + s = &SarifAnalysis{} + s.GetToolName() + s = nil + s.GetToolName() +} + +func TestSarifID_GetID(tt *testing.T) { + var zeroValue string + s := &SarifID{ID: &zeroValue} + s.GetID() + s = &SarifID{} + s.GetID() + s = nil + s.GetID() +} + +func TestSarifID_GetURL(tt *testing.T) { + var zeroValue string + s := &SarifID{URL: &zeroValue} + s.GetURL() + s = &SarifID{} + s.GetURL() + s = nil + s.GetURL() +} + func TestSCIMUserAttributes_GetActive(tt *testing.T) { var zeroValue bool s := &SCIMUserAttributes{Active: &zeroValue} From 504ad4465a8af7c1e9807322ac8ddc8f64b91fbb Mon Sep 17 00:00:00 2001 From: GaneshKumar Date: Tue, 2 Nov 2021 21:18:29 -0400 Subject: [PATCH 14/14] address review comments --- github/code-scanning.go | 2 +- github/code-scanning_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/github/code-scanning.go b/github/code-scanning.go index c49da37fffb..4508c3390d8 100644 --- a/github/code-scanning.go +++ b/github/code-scanning.go @@ -131,7 +131,7 @@ type SarifAnalysis struct { ToolName *string `json:"tool_name,omitempty"` } -// SarifID specifies the status of sarif upload. +// SarifID identifies a sarif analysis upload. // // GitHub API docs: https://docs.github.com/en/rest/reference/code-scanning#upload-an-analysis-as-sarif-data type SarifID struct { diff --git a/github/code-scanning_test.go b/github/code-scanning_test.go index e6755a4c56d..647bfa05feb 100644 --- a/github/code-scanning_test.go +++ b/github/code-scanning_test.go @@ -76,8 +76,8 @@ func TestActionsService_UploadSarif(t *testing.T) { if err != nil { t.Errorf("CodeScanning.UploadSarif returned error: %v", err) } - const methodName = "UploadSarif" + const methodName = "UploadSarif" testBadOptions(t, methodName, func() (err error) { _, _, err = client.CodeScanning.UploadSarif(ctx, "\n", "\n", sarifAnalysis) return err