google / go-github

Go library for accessing the GitHub v3 API

Home Page:https://pkg.go.dev/github.com/google/go-github/v62/github

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use enums for the action field in GitHub Webhooks

prnvbn opened this issue · comments

Issue Description:

Many GitHub webhooks contain the action field, which is enumerated. For example, in the GitHub documentation for webhook payloads and events, 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)

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)
}

If you agree, I can make this change

That seems safe to me.

nice, will send a PR your way!

@gmlewis, please could you review the PR: #3136?