ronantakizawa/codereview-bench huggingface dataset:
Repository: google/go-github
File: github/orgs_personal_access_tokens.go (Go)

Code before:
package github

import (
	"context"
	"fmt"
	"net/http"
)

// Approves or denies a pending request to access organization resources via a fine-grained personal access token.
// Only GitHub Apps can call this API, using the `organization_personal_access_token_requests: write` permission.
// `action` can be one of `approve` or `deny`.
//
// GitHub API docs: https://docs.github.com/en/rest/orgs/personal-access-tokens?apiVersion=2022-11-28#review-a-request-to-access-organization-resources-with-a-fine-grained-personal-access-token
func (s *OrganizationsService) ReviewPersonalAccessTokenRequest(ctx context.Context, org, requestID, action, reason string) (*Response, error) {
	u := fmt.Sprintf("orgs/%v/personal-access-token-requests/%v", org, requestID)
	body := struct {
		Action string `json:"action"`
		Reason string `json:"reason,omitempty"`
	}{
		Action: action,
		Reason: reason,
	}

	req, err := s.client.NewRequest(http.MethodPost, u, &body)
	if err != nil {
		return nil, err
	}

	return s.client.Do(ctx, req, nil)
}


Review comment (suggestion):
```suggestion
// ReviewPersonalAccessTokenRequest approves or denies a pending request to access organization resources via a fine-grained personal access token.
```

Code after:
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	"context"
	"fmt"
	"net/http"
)

// ReviewPersonalAccessTokenRequestOptions specifies the parameters to the ReviewPersonalAccessTokenRequest method.
type ReviewPersonalAccessTokenRequestOptions struct {
	Action string  `json:"action"`
	Reason *string `json:"reason,omitempty"`
}

// ReviewPersonalAccessTokenRequest approves or denies a pending request to access organization resources via a fine-grained personal access token.
// Only GitHub Apps can call this API, using the `organization_personal_access_token_requests: write` permission.
// `action` can be one of `approve` or `deny`.
//
// GitHub API docs: https://docs.github.com/en/rest/orgs/personal-access-tokens?apiVersion=2022-11-28#review-a-request-to-access-organization-resources-with-a-fine-grained-personal-access-token
func (s *OrganizationsService) ReviewPersonalAccessTokenRequest(ctx context.Context, org string, requestID int64, opts ReviewPersonalAccessTokenRequestOptions) (*Response, error) {
	u := fmt.Sprintf("orgs/%v/personal-access-token-requests/%v", org, requestID)

	req, err := s.client.NewRequest(http.MethodPost, u, &opts)
	if err != nil {
		return nil, err
	}

	return s.client.Do(ctx, req, nil)
}
Repository: google/go-github
File: github/orgs_credential_authorizations.go (Go)

Code before:
package github

import (
	"context"
	"fmt"
	"net/http"
)

// CredentialAuthorization represents a credential authorized through SAML SSO
type CredentialAuthorization struct {
	// User login that owns the underlying credential.
	Login *string `json:"login"`

	// Unique identifier for the credential.
	CredentialID *int64 `json:"credential_id"`

	// Human-readable description of the credential type.
	CredentialType *string `json:"credential_type"`

	// Last eight characters of the credential.
	// Only included in responses with credential_type of personal access token.
	TokenLastEight *string `json:"token_last_eight"`

	// Date when the credential was authorized for use.
	CredentialAuthorizedAt *Timestamp `json:"credential_authorized_at"`

	// Date when the credential was last accessed.
	// May be null if it was never accessed.
	CredentialAccessedAt *Timestamp `json:"credential_accessed_at"`

	// List of oauth scopes the token has been granted.

Review comment (security):
```suggestion
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github
```

Code after:
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	"context"
	"fmt"
	"net/http"
)

// CredentialAuthorization represents a credential authorized through SAML SSO.
type CredentialAuthorization struct {
	// User login that owns the underlying credential.
	Login *string `json:"login,omitempty"`

	// Unique identifier for the credential.
	CredentialID *int64 `json:"credential_id,omitempty"`

	// Human-readable description of the credential type.
	CredentialType *string `json:"credential_type,omitempty"`

	// Last eight characters of the credential.
	// Only included in responses with credential_type of personal access token.
	TokenLastEight *string `json:"token_last_eight,omitempty"`

	// Date when the credential was authorized for use.
	CredentialAuthorizedAt *Timestamp `json:"credential_authorized_at,omitempty"`
Repository: google/go-github
File: github/orgs_credential_authorizations_test.go (Go)

Code before:
package github

import (
	"context"
	"fmt"
	"net/http"
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
)

func TestOrganizationsService_ListCredentialAuthorizations(t *testing.T) {
	client, mux, _, teardown := setup()
	defer teardown()

	mux.HandleFunc("/orgs/o/credential-authorizations", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, `[
			{
				"login": "l",
				"credential_id": 1,
				"credential_type": "t",
				"credential_authorized_at": "2017-01-21T00:00:00Z",
				"credential_accessed_at": "2017-01-21T00:00:00Z",
				"authorized_credential_id": 1
			}
		]`)
	})

	ctx := context.Background()

Review comment (security):
```suggestion
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github
```

Code after:
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	"context"
	"fmt"
	"net/http"
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
)

func TestOrganizationsService_ListCredentialAuthorizations(t *testing.T) {
	client, mux, _, teardown := setup()
	defer teardown()

	mux.HandleFunc("/orgs/o/credential-authorizations", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		testFormValues(t, r, values{"per_page": "2", "page": "2"})
		fmt.Fprint(w, `[
			{
				"login": "l",
				"credential_id": 1,
				"credential_type": "t",
				"credential_authorized_at": "2017-01-21T00:00:00Z",
				"credential_accessed_at": "2017-01-21T00:00:00Z",
Repository: google/go-github
File: github/orgs_credential_authorizations.go (Go)

Code before:
package github

import (
	"context"
	"fmt"
	"net/http"
)

// CredentialAuthorization represents a credential authorized through SAML SSO
type CredentialAuthorization struct {
	// User login that owns the underlying credential.
	Login *string `json:"login"`

	// Unique identifier for the credential.
	CredentialID *int64 `json:"credential_id"`

	// Human-readable description of the credential type.
	CredentialType *string `json:"credential_type"`

	// Last eight characters of the credential.
	// Only included in responses with credential_type of personal access token.
	TokenLastEight *string `json:"token_last_eight"`

	// Date when the credential was authorized for use.
	CredentialAuthorizedAt *Timestamp `json:"credential_authorized_at"`

	// Date when the credential was last accessed.
	// May be null if it was never accessed.
	CredentialAccessedAt *Timestamp `json:"credential_accessed_at"`

	// List of oauth scopes the token has been granted.
	Scopes []string `json:"scopes"`

	// Unique string to distinguish the credential.

Review comment (security):
```suggestion
// CredentialAuthorization represents a credential authorized through SAML SSO.
```

Code after:
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	"context"
	"fmt"
	"net/http"
)

// CredentialAuthorization represents a credential authorized through SAML SSO.
type CredentialAuthorization struct {
	// User login that owns the underlying credential.
	Login *string `json:"login,omitempty"`

	// Unique identifier for the credential.
	CredentialID *int64 `json:"credential_id,omitempty"`

	// Human-readable description of the credential type.
	CredentialType *string `json:"credential_type,omitempty"`

	// Last eight characters of the credential.
	// Only included in responses with credential_type of personal access token.
	TokenLastEight *string `json:"token_last_eight,omitempty"`

	// Date when the credential was authorized for use.
	CredentialAuthorizedAt *Timestamp `json:"credential_authorized_at,omitempty"`

	// Date when the credential was last accessed.
	// May be null if it was never accessed.
	CredentialAccessedAt *Timestamp `json:"credential_accessed_at,omitempty"`
Repository: google/go-github
File: github/security_advisories.go (Go)

Code before:
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	"context"
	"fmt"
)

type SecurityAdvisoriesService service

// RequestCVE request a CVE for a repository security advisory.
//
// GitHub API docs: https://docs.github.com/en/rest/security-advisories/repository-advisories#request-a-cve-for-a-repository-security-advisory
func (s *SecurityAdvisoriesService) RequestCVE(ctx context.Context, owner, repo, ghsaID string) (*Response, error) {
	url := fmt.Sprintf("repos/%v/%v/security-advisories/%v/cve", owner, repo, ghsaID)

	req, err := s.client.NewRequest("POST", url, nil)
	if err != nil {
		return nil, err
	}

	resp, err := s.client.Do(ctx, req, nil)

	if err != nil {
		_, ok := err.(*AcceptedError)
		if ok {
			return resp, nil
		}

		return resp, err
	}

	return resp, nil
}


Review comment (security):
```suggestion
// RequestCVE requests a Common Vulnerabilities and Exposures (CVE) for a repository security advisory.
// The ghsaID is the GitHub Security Advisory identifier of the advisory.
```

Code after:
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	"context"
	"fmt"
)

type SecurityAdvisoriesService service

// RequestCVE requests a Common Vulnerabilities and Exposures (CVE) for a repository security advisory.
// The ghsaID is the GitHub Security Advisory identifier of the advisory.
//
// GitHub API docs: https://docs.github.com/en/rest/security-advisories/repository-advisories#request-a-cve-for-a-repository-security-advisory
func (s *SecurityAdvisoriesService) RequestCVE(ctx context.Context, owner, repo, ghsaID string) (*Response, error) {
	url := fmt.Sprintf("repos/%v/%v/security-advisories/%v/cve", owner, repo, ghsaID)

	req, err := s.client.NewRequest("POST", url, nil)
	if err != nil {
		return nil, err
	}

	resp, err := s.client.Do(ctx, req, nil)
	if err != nil {
		if _, ok := err.(*AcceptedError); ok {
			return resp, nil
		}

		return resp, err
	}

	return resp, nil
}
Repository: google/go-github
File: github/security_advisories.go (Go)

Code before:

package github

import (
	"context"
	"fmt"
)

type SecurityAdvisoriesService service

// RequestCVE request a CVE for a repository security advisory.
//
// GitHub API docs: https://docs.github.com/en/rest/security-advisories/repository-advisories#request-a-cve-for-a-repository-security-advisory
func (s *SecurityAdvisoriesService) RequestCVE(ctx context.Context, owner, repo, ghsaID string) (*Response, error) {
	url := fmt.Sprintf("repos/%v/%v/security-advisories/%v/cve", owner, repo, ghsaID)

	req, err := s.client.NewRequest("POST", url, nil)
	if err != nil {
		return nil, err
	}

	resp, err := s.client.Do(ctx, req, nil)

	if err != nil {
		_, ok := err.(*AcceptedError)
		if ok {
			return resp, nil
		}

		return resp, err
	}

	return resp, nil
}


Review comment (suggestion):
Please remove the blank line that is separating the potential cause of an error with the handling of that error.

```suggestion
	if err != nil {
		if _, ok := err.(*AcceptedError); ok {
```

Code after:

package github

import (
	"context"
	"fmt"
)

type SecurityAdvisoriesService service

// RequestCVE requests a Common Vulnerabilities and Exposures (CVE) for a repository security advisory.
// The ghsaID is the GitHub Security Advisory identifier of the advisory.
//
// GitHub API docs: https://docs.github.com/en/rest/security-advisories/repository-advisories#request-a-cve-for-a-repository-security-advisory
func (s *SecurityAdvisoriesService) RequestCVE(ctx context.Context, owner, repo, ghsaID string) (*Response, error) {
	url := fmt.Sprintf("repos/%v/%v/security-advisories/%v/cve", owner, repo, ghsaID)

	req, err := s.client.NewRequest("POST", url, nil)
	if err != nil {
		return nil, err
	}

	resp, err := s.client.Do(ctx, req, nil)
	if err != nil {
		if _, ok := err.(*AcceptedError); ok {
			return resp, nil
		}

		return resp, err
	}

	return resp, nil
}
Repository: google/go-github
File: github/security_advisories_test.go (Go)

Code before:
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	"context"
	"net/http"
	"testing"
)

func TestSecurityAdvisoriesService_RequestCVE(t *testing.T) {
	client, mux, _, teardown := setup()
	defer teardown()

	mux.HandleFunc("/repos/o/r/security-advisories/ghsa_id/cve", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "POST")
		w.WriteHeader(http.StatusAccepted)
	})

	ctx := context.Background()
	_, err := client.SecurityAdvisories.RequestCVE(ctx, "o", "r", "ghsa_id")
	if err != nil {
		t.Errorf("SecurityAdvisoriesService.RequestCVE returned error: %v", err)
	}

	const methodName = "RequestCVE"
	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
		resp, err := client.SecurityAdvisories.RequestCVE(ctx, "o", "r", "ghsa_id")
		if err == nil {
			t.Errorf("testNewRequestAndDoFailure %v should have return err", methodName)
		}
		return resp, err
	})
}


Review comment (security):
```suggestion
	const methodName = "RequestCVE"
	testBadOptions(t, methodName, func() (err error) {
		_, err := client.SecurityAdvisories.RequestCVE(ctx, "\n", "\n", "\n")
		return err
	})

```

Code after:
func TestSecurityAdvisoriesService_RequestCVE(t *testing.T) {
	client, mux, _, teardown := setup()
	defer teardown()

	mux.HandleFunc("/repos/o/r/security-advisories/ghsa_id_ok/cve", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "POST")
		w.WriteHeader(http.StatusOK)
	})

	mux.HandleFunc("/repos/o/r/security-advisories/ghsa_id_accepted/cve", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "POST")
		w.WriteHeader(http.StatusAccepted)
	})

	ctx := context.Background()
	_, err := client.SecurityAdvisories.RequestCVE(ctx, "o", "r", "ghsa_id_ok")
	if err != nil {
		t.Errorf("SecurityAdvisoriesService.RequestCVE returned error: %v", err)
	}

	_, err = client.SecurityAdvisories.RequestCVE(ctx, "o", "r", "ghsa_id_accepted")
	if err != nil {
		t.Errorf("SecurityAdvisoriesService.RequestCVE returned error: %v", err)
	}

	const methodName = "RequestCVE"
	testBadOptions(t, methodName, func() (err error) {
		_, err = client.SecurityAdvisories.RequestCVE(ctx, "\n", "\n", "\n")
		return err
	})

	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
		resp, err := client.SecurityAdvisories.RequestCVE(ctx, "o", "r", "ghsa_id")
		if err == nil {
			t.Errorf("testNewRequestAndDoFailure %v should have return err", methodName)
		}
		return resp, err
	})
}
Repository: google/go-github
File: github/security_advisories.go (Go)

Code before:
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	"context"
	"fmt"
)

type SecurityAdvisoriesService service

// RequestCVE requests a Common Vulnerabilities and Exposures (CVE) for a repository security advisory.
//
// GitHub API docs: https://docs.github.com/en/rest/security-advisories/repository-advisories#request-a-cve-for-a-repository-security-advisory
func (s *SecurityAdvisoriesService) RequestCVE(ctx context.Context, owner, repo, ghsaID string) (*Response, error) {
	url := fmt.Sprintf("repos/%v/%v/security-advisories/%v/cve", owner, repo, ghsaID)

	req, err := s.client.NewRequest("POST", url, nil)
	if err != nil {
		return nil, err
	}

	resp, err := s.client.Do(ctx, req, nil)

	if err != nil {
		if _, ok := err.(*AcceptedError); ok {
			return resp, nil
		}

		return resp, err
	}

	return resp, nil
}


Review comment (refactor):
Please remove this blank line that is separating the potential source of an error with the handling of the same error.

Code after:
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	"context"
	"fmt"
)

type SecurityAdvisoriesService service

// RequestCVE requests a Common Vulnerabilities and Exposures (CVE) for a repository security advisory.
// The ghsaID is the GitHub Security Advisory identifier of the advisory.
//
// GitHub API docs: https://docs.github.com/en/rest/security-advisories/repository-advisories#request-a-cve-for-a-repository-security-advisory
func (s *SecurityAdvisoriesService) RequestCVE(ctx context.Context, owner, repo, ghsaID string) (*Response, error) {
	url := fmt.Sprintf("repos/%v/%v/security-advisories/%v/cve", owner, repo, ghsaID)

	req, err := s.client.NewRequest("POST", url, nil)
	if err != nil {
		return nil, err
	}

	resp, err := s.client.Do(ctx, req, nil)
	if err != nil {
		if _, ok := err.(*AcceptedError); ok {
			return resp, nil
		}

		return resp, err
	}

	return resp, nil
}
Repository: google/go-github
File: github/event_test.go (Go)

Code before:
	}()

	name := "UserEvent"
	body := json.RawMessage("[") // bogus JSON
	e := &Event{Type: &name, RawPayload: &body}
	e.Payload()
}

func TestPayload_NoPanic(t *testing.T) {
	name := "UserEvent"
	body := json.RawMessage("{}")
	e := &Event{Type: &name, RawPayload: &body}
	e.Payload()
}

func TestEmptyEvent_NoPanic(t *testing.T) {
	e := &Event{}
	e.ParsePayload()
}

func TestEvent_Marshal(t *testing.T) {
	testJSONMarshal(t, &Event{}, "{}")

	l := make(map[string]interface{})
	l["key"] = "value"

	jsonMsg, _ := json.Marshal(&l)

	u := &Event{
		Type:       String("t"),
		Public:     Bool(false),
		RawPayload: (*json.RawMessage)(&jsonMsg),
		Repo: &Repository{
			ID:   Int64(1),
			URL:  String("s"),
			Name: String("n"),
		},
		Actor: &User{
			Login:     String("l"),
			ID:        Int64(1),
			NodeID:    String("n"),
			URL:       String("u"),
			ReposURL:  String("r"),
			EventsURL: String("e"),
			AvatarURL: String("a"),
		},
		Org: &Organization{
			BillingEmail:                         String("be"),
			Blog:                                 String("b"),
			Company:                              String("c"),
			Email:                                String("e"),

Review comment (suggestion):
Also, please:

```suggestion
  var nilEvent *Event
  nilEvent.ParsePayload()
}
```

Code after:
	}()

	name := "UserEvent"
	body := json.RawMessage("[") // bogus JSON
	e := &Event{Type: &name, RawPayload: &body}
	e.Payload()
}

func TestPayload_NoPanic(t *testing.T) {
	name := "UserEvent"
	body := json.RawMessage("{}")
	e := &Event{Type: &name, RawPayload: &body}
	e.Payload()
}

func TestEmptyEvent_NoPanic(t *testing.T) {
	e := &Event{}
	if _, err := e.ParsePayload(); err == nil {
		t.Error("ParsePayload unexpectedly succeeded on empty event")
	}

	e = nil
	if _, err := e.ParsePayload(); err == nil {
		t.Error("ParsePayload unexpectedly succeeded on nil event")
	}
}

func TestEvent_Marshal(t *testing.T) {
	testJSONMarshal(t, &Event{}, "{}")

	l := make(map[string]interface{})
	l["key"] = "value"

	jsonMsg, _ := json.Marshal(&l)

	u := &Event{
		Type:       String("t"),
		Public:     Bool(false),
		RawPayload: (*json.RawMessage)(&jsonMsg),
		Repo: &Repository{
			ID:   Int64(1),
			URL:  String("s"),
			Name: String("n"),
		},
		Actor: &User{
			Login:     String("l"),
			ID:        Int64(1),
			NodeID:    String("n"),
			URL:       String("u"),
			ReposURL:  String("r"),
			EventsURL: String("e"),
Repository: google/go-github
File: github/messages.go (Go)

Code before:
		Type:       &eventType,
		RawPayload: (*json.RawMessage)(&payload),
	}
	return event.ParsePayload()
}

// WebhookTypes returns a list of all the known GitHub event type strings
// supported by go-github.
func MessageTypes() []string {
	types := make([]string, 0, len(eventTypeMapping))
	for t := range eventTypeMapping {
		types = append(types, t)
	}
	return types
}

// EventForType returns an empty struct matching the specified GitHub event type.
// If messageType does not match any known event types, it returns nil.
func EventForType(messageType string) interface{} {
	prototype := eventTypeMapping[messageType]
	if prototype != nil {
		// return a _copy_ of the pointed-to-object.  Unfortunately, for this
		// we need to use reflection.
		return reflect.New(reflect.TypeOf(prototype).Elem()).Interface()
	}
	return prototype
}


Review comment (suggestion):
Let's please return the happy path at the end, which is idiomatic Go style:

```suggestion
	if prototype == nil {
		return nil
	}
	// return a _copy_ of the pointed-to-object.  Unfortunately, for this
	// we need to use reflection.
	return reflect.New(reflect.TypeOf(prototype).Elem()).Interface()
```

Code after:
		return nil, fmt.Errorf("unknown X-Github-Event in message: %v", messageType)
	}

	event := Event{
		Type:       &eventType,
		RawPayload: (*json.RawMessage)(&payload),
	}
	return event.ParsePayload()
}

// WebhookTypes returns a sorted list of all the known GitHub event type strings
// supported by go-github.
func MessageTypes() []string {
	types := make([]string, 0, len(eventTypeMapping))
	for t := range eventTypeMapping {
		types = append(types, t)
	}
	sort.Strings(types)
	return types
}

// EventForType returns an empty struct matching the specified GitHub event type.
// If messageType does not match any known event types, it returns nil.
func EventForType(messageType string) interface{} {
	prototype := eventTypeMapping[messageType]
	if prototype == nil {
		return nil
	}
	// return a _copy_ of the pointed-to-object.  Unfortunately, for this we
	// need to use reflection.  If we store the actual objects in the map,
	// we still need to use reflection to convert from `any` to the actual
	// type, so this was deemed the lesser of two evils. (#2865)
	return reflect.New(reflect.TypeOf(prototype).Elem()).Interface()
}