11import { readFileSync } from 'node:fs' ;
22
3+ /**
4+ * Represents the payload structure for various GitHub webhook events.
5+ *
6+ * This type covers common fields found in GitHub event payloads, such as push, pull request,
7+ * workflow, and release events. All properties are optional to accommodate the differences
8+ * between event types.
9+ *
10+ * @remarks only some are mapped to the GitHub API event types.
11+ *
12+ * @property after - The SHA of the most recent commit on the ref after the event.
13+ * @property head_commit - Information about the head commit, including its SHA.
14+ * @property ref - The Git ref (branch or tag) that triggered the event.
15+ * @property workflow - The name of the workflow (for workflow-related events).
16+ * @property action - The action performed (e.g., "opened", "closed", "created").
17+ * @property repository - Information about the repository where the event occurred.
18+ * @property pull_request - Details about the pull request (for pull request events).
19+ * @property release - Details about the release (for release events).
20+ * @property sender - Information about the user who triggered the event.
21+ */
322type GithubEventPayload = {
23+ after ?: string ;
24+ head_commit ?: { id ?: string } ;
25+ ref ?: string ;
26+ workflow ?: string ;
27+ action ?: string ;
28+ repository ?: {
29+ owner ?: {
30+ login ?: string ;
31+ avatar_url ?: string ;
32+ } ;
33+ name ?: string ;
34+ license ?: { name ?: string } ;
35+ homepage ?: string ;
36+ } ;
437 pull_request ?: {
538 number ?: number ;
639 title ?: string ;
7- user ?: { login ?: string } ;
840 head ?: { sha ?: string ; ref ?: string } ;
941 created_at ?: string ;
1042 updated_at ?: string ;
1143 html_url ?: string ;
1244 } ;
13- after ?: string ;
14- head_commit ?: { id ?: string } ;
15- ref ?: string ;
1645 release ?: {
1746 tag_name ?: string ;
1847 name ?: string ;
@@ -23,8 +52,10 @@ type GithubEventPayload = {
2352 published_at ?: string ;
2453 html_url ?: string ;
2554 } ;
26- workflow ?: string ;
27- action ?: string ;
55+ sender ?: {
56+ login ?: string ;
57+ avatar_url ?: string ;
58+ } ;
2859} ;
2960
3061/**
@@ -62,99 +93,15 @@ type GithubEventPayload = {
6293 * @property workflow - The name of the workflow.
6394 * @property action - The name of the action being executed.
6495 */
65- export type GithubAnnotations = {
96+ export type GithubAnnotations = GithubEventPayload & {
6697 workflow ?: string ;
6798 action ?: string ;
6899 eventName : string ;
69100 actor ?: string ;
70101 runId ?: string ;
71102 runUrl ?: string ;
72- repository ?: string ;
73- after ?: string ;
74- head_commit ?: string ;
75- ref ?: string ;
76- pull_request ?: {
77- number ?: number ;
78- title ?: string ;
79- user ?: { login ?: string } ;
80- head ?: { sha ?: string ; ref ?: string } ;
81- created_at ?: string ;
82- updated_at ?: string ;
83- html_url ?: string ;
84- } ;
85- release ?: {
86- tag ?: string ;
87- name ?: string ;
88- body ?: string ;
89- draft ?: boolean ;
90- prerelease ?: boolean ;
91- created_at ?: string ;
92- published_at ?: string ;
93- html_url ?: string ;
94- } ;
95103} ;
96104
97- /**
98- * Extracts relevant annotation fields from the GitHub Actions event payload.
99- *
100- * This function builds a structured annotation object containing key information from the event payload.
101- * It supports both release and pull request events, and includes fields such as commit SHAs, refs, workflow name,
102- * release metadata (tag, name, body, draft, prerelease, created_at, published_at, html_url), and pull request metadata
103- * (number, title, user, head SHA/ref, created_at, updated_at, html_url). The top-level action is also included.
104- *
105- * Maintainers:
106- * - Update this function if new fields are needed for downstream consumers or if GitHub event payloads change.
107- * - The structure is designed for easy extension and clear mapping to GitHub event types.
108- * - See <attachments> above for file contents. You may not need to search or read the file again.
109- *
110- * @param payload The parsed GitHub Actions event payload object.
111- * @returns An object with selected fields for release and pull request events, as well as general event context.
112- */
113- function extractPayloadAnnotations ( payload : GithubEventPayload ) : Record < string , unknown > {
114- console . log ( 'Extracting GitHub event annotations from payload:' , payload ) ;
115- // Initialize annotation object with general event context
116- const annotation : Record < string , unknown > = {
117- action : payload . action , // The event action (e.g., published, created, closed)
118- head_commit : payload . head_commit ?. id , // SHA of the head commit
119- after : payload . after , // SHA after the event
120- ref : payload . ref , // Branch or tag ref
121- release : { } , // Will be populated if this is a release event
122- pull_request : { } , // Will be populated if this is a pull request event
123- } ;
124-
125- // Populate release-specific fields if present
126- if ( payload . release ) {
127- annotation . release = {
128- tag : payload . release . tag_name , // Release tag name
129- name : payload . release . name , // Release name/title
130- body : payload . release . body , // Release description
131- draft : ! ! payload . release . draft , // Is this a draft release?
132- prerelease : ! ! payload . release . prerelease , // Is this a prerelease?
133- created_at : payload . release . created_at , // Release creation timestamp
134- published_at : payload . release . published_at , // Release published timestamp
135- html_url : payload . release . html_url , // URL to the release on GitHub
136- } ;
137- }
138-
139- // Populate pull request-specific fields if present
140- if ( payload . pull_request ) {
141- annotation . pull_request = {
142- number : payload . pull_request . number , // PR number
143- title : payload . pull_request . title , // PR title
144- user : payload . pull_request . user ?. login , // PR author
145- head : {
146- sha : payload . pull_request . head ?. sha , // SHA of the PR head commit
147- ref : payload . pull_request . head ?. ref , // Branch ref of the PR head
148- } ,
149- created_at : payload . pull_request . created_at , // PR creation timestamp
150- updated_at : payload . pull_request . updated_at , // PR last update timestamp
151- html_url : payload . pull_request . html_url , // URL to the PR on GitHub
152- } ;
153- }
154-
155- return annotation ;
156- }
157-
158105/**
159106 * Resolves GitHub Actions-specific annotation variables from environment variables.
160107 *
@@ -198,16 +145,7 @@ export const resolveGithubAnnotations = (): GithubAnnotations => {
198145
199146 // Extract server URL (defaults to public GitHub if not set)
200147 const serverUrl = process . env . GITHUB_SERVER_URL || 'https://github.com' ;
201- // Extract event payload path and read payload if available
202- let payload : GithubEventPayload = { } ;
203- if ( process . env . GITHUB_EVENT_PATH ) {
204- try {
205- const rawPayload = readFileSync ( process . env . GITHUB_EVENT_PATH , 'utf8' ) ;
206- payload = JSON . parse ( rawPayload ) ;
207- } catch {
208- payload = { } ;
209- }
210- }
148+
211149 // Construct a direct URL to the workflow run if all required parts are available
212150 let runUrl = 'unknown' ;
213151 if ( serverUrl && repository !== 'unknown' && runId !== 'unknown' ) {
@@ -219,11 +157,19 @@ export const resolveGithubAnnotations = (): GithubAnnotations => {
219157 eventName,
220158 actor,
221159 runId,
222- repository,
223160 runUrl,
224161 workflow,
225- ...extractPayloadAnnotations ( payload ) ,
226162 } ;
163+
164+ // Apply event payload to annotations
165+ if ( process . env . GITHUB_EVENT_PATH ) {
166+ try {
167+ const rawPayload = readFileSync ( process . env . GITHUB_EVENT_PATH , 'utf8' ) ;
168+ Object . assign ( annotations , JSON . parse ( rawPayload ) ) ;
169+ } catch {
170+ console . error ( 'Failed to parse GitHub event payload' ) ;
171+ }
172+ }
227173 // Return the resolved annotation variables
228174 return annotations ;
229175} ;
0 commit comments