11import { debug , getBooleanInput , getInput , setFailed , warning } from '@actions/core' ;
22import { context , getOctokit } from '@actions/github' ;
3+ import { type operations } from '@octokit/openapi-types' ;
4+ import { type Api } from '@octokit/plugin-rest-endpoint-methods' ;
35import { Base64 } from 'js-base64' ;
46import { existsSync , readFileSync } from 'node:fs' ;
57import { resolve } from 'node:path' ;
68
7- import { createSummary , createTable } from './comment.mjs ' ;
8- import { countStatuses , diffLocks , parseLock , STATUS } from './utils.mjs ' ;
9+ import { createSummary , createTable } from './comment' ;
10+ import { countStatuses , diffLocks , parseLock , STATUS } from './utils' ;
911
10- async function getCommentId ( octokit , oktokitParams , issueNumber , commentHeader ) {
12+ async function getCommentId (
13+ octokit : Api ,
14+ oktokitParams : { owner : string ; repo : string } ,
15+ issueNumber : number ,
16+ commentHeader : string
17+ ) {
1118 const currentComments = await octokit . rest . issues . listComments ( {
1219 ...oktokitParams ,
1320 issue_number : issueNumber ,
@@ -19,11 +26,11 @@ async function getCommentId(octokit, oktokitParams, issueNumber, commentHeader)
1926 }
2027
2128 return currentComments . data
22- . filter ( ( { user, body } ) => user . login === 'github-actions[bot]' && body . startsWith ( commentHeader ) )
29+ . filter ( ( { user, body } ) => user ? .login === 'github-actions[bot]' && body ? .startsWith ( commentHeader ) )
2330 . map ( ( { id } ) => id ) [ 0 ] ;
2431}
2532
26- function getBasePathFromInput ( input ) {
33+ function getBasePathFromInput ( input : string ) {
2734 return input . lastIndexOf ( '/' ) ? input . substring ( 0 , input . lastIndexOf ( '/' ) ) : '' ;
2835}
2936
@@ -38,14 +45,20 @@ async function run() {
3845
3946 const { owner, repo, number } = context . issue ;
4047
41- if ( ! number ) {
48+ if ( ! number || ! context . payload . pull_request ) {
4249 throw Error ( '💥 Cannot find the PR data in the workflow context, aborting!' ) ;
4350 }
4451
4552 const { ref } = context . payload . pull_request . base ;
46- const { default_branch } = context . payload . repository ;
53+ const contextRepo = context . payload ? .repository ;
4754
48- const baseBranch = ref || default_branch ;
55+ if ( ! contextRepo ) {
56+ throw Error ( '💥 Cannot find the repository data in the workflow context, aborting!' ) ;
57+ }
58+
59+ const { default_branch } = contextRepo ;
60+
61+ const baseBranch = ref ?? default_branch ;
4962 debug ( 'Base branch: ' + baseBranch ) ;
5063
5164 const lockPath = resolve ( process . cwd ( ) , inputPath ) ;
@@ -56,6 +69,9 @@ async function run() {
5669
5770 const content = readFileSync ( lockPath , { encoding : 'utf8' } ) ;
5871 const updatedLock = parseLock ( content ) ;
72+ if ( updatedLock . type === 'error' ) {
73+ warning ( 'Unsupported Yarn lock version! Please report this issue in the action repository.' ) ;
74+ }
5975
6076 const oktokitParams = { owner, repo } ;
6177 debug ( 'Oktokit params: ' + JSON . stringify ( oktokitParams ) ) ;
@@ -73,7 +89,10 @@ async function run() {
7389 throw Error ( '💥 Cannot fetch repository base branch tree, aborting!' ) ;
7490 }
7591
76- const baseLockSHA = baseTree . data . tree . filter ( file => file . path === 'yarn.lock' ) [ 0 ] . sha ;
92+ const baseLockSHA = baseTree . data . tree . filter (
93+ ( file : operations [ 'repos/get-content' ] [ 'responses' ] [ '200' ] [ 'content' ] [ 'application/vnd.github.object' ] ) =>
94+ file . path === 'yarn.lock'
95+ ) [ 0 ] . sha ;
7796
7897 debug ( 'Base lockfile SHA: ' + baseLockSHA ) ;
7998
@@ -155,7 +174,11 @@ async function run() {
155174 }
156175 }
157176 } catch ( error ) {
158- setFailed ( error . message ) ;
177+ if ( error instanceof Error ) {
178+ setFailed ( error . message ) ;
179+ } else if ( typeof error === 'string' ) {
180+ setFailed ( error ) ;
181+ }
159182 }
160183}
161184
0 commit comments