google / google/go-github

Use enums for the action field in GitHub Webhooks

Open
#3,127 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
11.3k
Forks
2.5k
Avg merge
1d 22h
Merged PRs (30d)
49

Description

## Issue Description:

Many GitHub webhooks contain the action field, which is enumerated. For example, in the [GitHub documentation for webhook payloads and events](https://docs.github.com/en/webhooks/webhook-events-and-payloads#check_run), the check_run event includes an action property that can take on values such as "completed", "created", "requested_action", and "rerequested". However, in the library, these fields are currently represented as free-form strings. In my opinion, making these enums would be more user-friendly.

## Proposed Solution

A rudimentary example of how this can be done is as follows ([go play ground link](https://goplay.tools/snippet/gBqRSMGO0Px))

```go
package main

import (
"encoding/json"
"fmt"
)

type ChecRunAction string

const (
CheckRunCreated ChecRunAction = "created"
CheckRunRequested ChecRunAction = "requested"
CheckRunRerequested ChecRunAction = "rerequested"
CheckRunRequestedAction ChecRunAction = "requested_action"
)

func main() {
jsonStr := `{"hi": "created"}`

var v struct {
A ChecRunAction `json:"hi"`
}
json.Unmarshal([]byte(jsonStr), &v)
fmt.Println(v)
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.