import * as core from '@actions/core' ; import * as github from '@actions/github' ; import type { Release , ReleaseAsset } from '@octokit/webhooks-types' ; import type { SemVer } from 'semver' ; import { runAndBuffer } from './shell' ; const getRepoParams = ( ) => ( { owner : core . getInput ( 'repository_owner' ) || github . context . repo . owner , repo : core . getInput ( 'repository_name' ) || github . context . repo . repo , } as const ) ; const octokit = github . getOctokit ( core . getInput ( 'github_token' ) ) ; const findRelease = async ( filter ) => { const releasesPages = octokit . paginate . iterator ( 'GET /repos/{owner}/{repo}/releases' , { ... getRepoParams ( ) , } ) ; for await ( const response of releasesPages ) { for ( const release of response . data ) { if ( filter ( release ) ) { return release ; } } } return undefined ; } ; const getChangelog = async ( ) => ( await runAndBuffer ( `yarn --silent conventional-changelog -p angular` ) ) . split ( '\n' ) . slice ( 1 ) . join ( '\n' ) . trim ( ) ; export const getSnapshotRelease = async ( commitSha ) => { const body = await getChangelog ( ) ; const release = await findRelease ( ( release ) => release . name === 'Snapshot' ) ; if ( release ) { return ( await octokit . request ( 'PATCH /repos/{owner}/{repo}/releases/{release_id}' , { ... getRepoParams ( ) , release_id : release . id , draft : true , body , tag_name : `snapshot-${ commitSha }` , target_commitish : commitSha , } ) ) . data ; } return ( await octokit . request ( 'POST /repos/{owner}/{repo}/releases' , { ... getRepoParams ( ) , draft : true , name : 'Snapshot' , body , tag_name : `snapshot-${ commitSha }` , target_commitish : commitSha , } ) ) . data ; } ; export const getTaggedRelease = async ( version , commitSha ) => { const body = await getChangelog ( ) ; const release = await findRelease ( ( release ) => release . tag_name === version . version ) ; if ( release ) { return ( await octokit . request ( 'PATCH /repos/{owner}/{repo}/releases/{release_id}' , { ... getRepoParams ( ) , release_id : release . id , draft : true , body , tag_name : version . version , target_commitish : commitSha , } ) ) . data ; } return ( await octokit . request ( 'POST /repos/{owner}/{repo}/releases' , { ... getRepoParams ( ) , draft : true , name : version . version , body , tag_name : version . version , target_commitish : commitSha , } ) ) . data ; } ; export const getReleaseAssets = async ( releaseId ) => octokit . paginate ( 'GET /repos/{owner}/{repo}/releases/{release_id}/assets' , { ... getRepoParams ( ) , release_id : releaseId , } ) ; export const overrideAsset = async ( release , assets , name , size , data ) => { const asset = assets . find ( ( asset ) => asset . name === name ) ; if ( asset ) { core . info ( `deleting existing asset ${ name }` ) ; await octokit . request ( 'DELETE /repos/{owner}/{repo}/releases/assets/{asset_id}' , { ... getRepoParams ( ) , asset_id : asset . id , } ) ; } core . info ( `uploading asset ${ name }` ) ; await octokit . request ( { method : 'POST' , url : release . upload_url , headers : { 'content-type' : 'application/octet-stream' , 'content-length' : size , } , name , label : name , data , } ) ; } ;