// parsePullRequestEvent uses the given environment variables
// to parse and build githubEvent struct.
// Currently, we support 2 kinds of event as below:
// - PullRequestEvent
//   https://pkg.go.dev/github.com/google/go-github/v36/github#PullRequestEvent
// - IssueCommentEvent
//   https://pkg.go.dev/github.com/google/go-github/v36/github#IssueCommentEvent
parsePullRequestEvent uses the given environment variables
to parse and build githubEvent struct.
// parsePullRequestEvent uses the given environment variables // to parse and build githubEvent struct . // Currently we support 2 kinds of event as below : // - PullRequestEvent // https : // pkg . go . dev / github . com / google / go - github / v36 / github#PullRequestEvent // - IssueCommentEvent // https : // pkg . go . dev / github . com / google / go - github / v36 / github#IssueCommentEvent
func parseGitHubEvent(ctx context.Context, client *github.Client) (*githubEvent, error) {
	const (
		pullRequestEventName = "pull_request"
		commentEventName     = "issue_comment"
	)
	eventName := os.Getenv("GITHUB_EVENT_NAME")
	if eventName != pullRequestEventName && eventName != commentEventName {
		return nil, fmt.Errorf("unexpected event %s, only %q and %q event are supported", eventName, pullRequestEventName, commentEventName)
	}
	eventPath := os.Getenv("GITHUB_EVENT_PATH")
	payload, err := os.ReadFile(eventPath)
	if err != nil {
		return nil, fmt.Errorf("failed to read event payload: %v", err)
	}
	event, err := github.ParseWebHook(eventName, payload)
	if err != nil {
		return nil, fmt.Errorf("failed to parse event payload: %v", err)
	}
	switch e := event.(type) {
	case *github.PullRequestEvent:
		return &githubEvent{
			Owner:       e.Repo.Owner.GetLogin(),
			Repo:        e.Repo.GetName(),
			RepoRemote:  e.Repo.GetSSHURL(),
			PRNumber:    e.GetNumber(),
			PRMergeable: e.PullRequest.Mergeable,
			PRClosed:    !e.PullRequest.GetClosedAt().IsZero(),
			HeadBranch:  e.PullRequest.Head.GetRef(),
			HeadCommit:  e.PullRequest.Head.GetSHA(),
			BaseBranch:  e.PullRequest.Base.GetRef(),
			SenderLogin: e.Sender.GetLogin(),
		}, nil
	case *github.IssueCommentEvent:
		var (
			owner = e.Repo.Owner.GetLogin()
			repo  = e.Repo.GetName()
			prNum = e.Issue.GetNumber()
		)
		pr, err := getPullRequest(ctx, client, owner, repo, prNum)
		if err != nil {
			return nil, err
		}
		return &githubEvent{
			Owner:       owner,
			Repo:        repo,
			RepoRemote:  e.Repo.GetSSHURL(),
			PRNumber:    prNum,
			PRMergeable: pr.Mergeable,
			PRClosed:    !pr.GetClosedAt().IsZero(),
			HeadBranch:  pr.Head.GetRef(),
			HeadCommit:  pr.Head.GetSHA(),
			BaseBranch:  pr.Base.GetRef(),
			SenderLogin: e.Sender.GetLogin(),
			IsComment:   true,
			CommentURL:  e.Comment.GetHTMLURL(),
		}, nil
	default:
		return nil, fmt.Errorf("got an unexpected event type, got: %t", e)
	}
}
func parseGitHubEvent ( ctx context . Context , client * github . Client ) ( * githubEvent , error ) { const ( pullRequestEventName = " " commentEventName = " " ) eventName := os . Getenv ( " " ) if eventName != pullRequestEventName && eventName != commentEventName { return nil , fmt . Errorf ( " " , eventName , pullRequestEventName , commentEventName ) } eventPath := os . Getenv ( " " ) payload , err := os . ReadFile ( eventPath ) if err != nil { return nil , fmt . Errorf ( " " , err ) } event , err := github . ParseWebHook ( eventName , payload ) if err != nil { return nil , fmt . Errorf ( " " , err ) } switch e := event . ( type ) { case * github . PullRequestEvent : return & githubEvent { Owner : e . Repo . Owner . GetLogin ( ) , Repo : e . Repo . GetName ( ) , RepoRemote : e . Repo . GetSSHURL ( ) , PRNumber : e . GetNumber ( ) , PRMergeable : e . PullRequest . Mergeable , PRClosed : ! e . PullRequest . GetClosedAt ( ) . IsZero ( ) , HeadBranch : e . PullRequest . Head . GetRef ( ) , HeadCommit : e . PullRequest . Head . GetSHA ( ) , BaseBranch : e . PullRequest . Base . GetRef ( ) , SenderLogin : e . Sender . GetLogin ( ) , } , nil case * github . IssueCommentEvent : var ( owner = e . Repo . Owner . GetLogin ( ) repo = e . Repo . GetName ( ) prNum = e . Issue . GetNumber ( ) ) pr , err := getPullRequest ( ctx , client , owner , repo , prNum ) if err != nil { return nil , err } return & githubEvent { Owner : owner , Repo : repo , RepoRemote : e . Repo . GetSSHURL ( ) , PRNumber : prNum , PRMergeable : pr . Mergeable , PRClosed : ! pr . GetClosedAt ( ) . IsZero ( ) , HeadBranch : pr . Head . GetRef ( ) , HeadCommit : pr . Head . GetSHA ( ) , BaseBranch : pr . Base . GetRef ( ) , SenderLogin : e . Sender . GetLogin ( ) , IsComment : true , CommentURL : e . Comment . GetHTMLURL ( ) , } , nil default : return nil , fmt . Errorf ( " " , e ) } }
parsePullRequestEvent uses the given environment variables
to parse and build githubEvent struct.
parsePullRequestEvent uses the given environment variables to parse and build githubEvent struct .