2024-04-02 20:58:16 +03:00
import { Octokit } from "@octokit/rest" ;
2022-10-07 19:50:46 +03:00
import assert from "assert" ;
import ado from "azure-devops-node-api" ;
import fetch from "node-fetch" ;
2019-06-04 01:24:35 +03:00
2024-03-08 21:51:28 +03:00
/ * *
* @ param { string } name
* /
function mustGetEnv ( name ) {
const value = process . env [ name ] ;
assert ( value , ` No ${ name } specified ` ) ;
return value ;
}
const REQUESTING _USER = mustGetEnv ( "REQUESTING_USER" ) ;
const SOURCE _ISSUE = + mustGetEnv ( "SOURCE_ISSUE" ) ;
const BUILD _BUILDID = + mustGetEnv ( "BUILD_BUILDID" ) ;
const DISTINCT _ID = mustGetEnv ( "DISTINCT_ID" ) ;
2024-03-08 21:57:32 +03:00
const STATUS _COMMENT = + mustGetEnv ( "STATUS_COMMENT" ) ;
2024-03-08 21:51:28 +03:00
const gh = new Octokit ( {
auth : process . argv [ 2 ] ,
} ) ;
2019-06-04 01:24:35 +03:00
async function main ( ) {
// The pipelines API does _not_ make getting the direct URL to a specific file _within_ an artifact trivial
const cli = new ado . WebApi ( "https://typescript.visualstudio.com/defaultcollection" , ado . getHandlerFromToken ( "" ) ) ; // Empty token, anon auth
const build = await cli . getBuildApi ( ) ;
2024-03-08 21:51:28 +03:00
const artifact = await build . getArtifact ( "typescript" , BUILD _BUILDID , "tgz" ) ;
2022-10-07 19:50:46 +03:00
assert ( artifact . resource ? . url ) ;
2019-06-04 01:24:35 +03:00
const updatedUrl = new URL ( artifact . resource . url ) ;
updatedUrl . search = ` artifactName=tgz&fileId= ${ artifact . resource . data } &fileName=manifest ` ;
const resp = await ( await fetch ( ` ${ updatedUrl } ` ) ) . json ( ) ;
2022-10-07 19:50:46 +03:00
const file = /** @type {any} */ ( resp ) . items [ 0 ] ;
2019-06-04 01:24:35 +03:00
const tgzUrl = new URL ( artifact . resource . url ) ;
tgzUrl . search = ` artifactName=tgz&fileId= ${ file . blob . id } &fileName= ${ file . path } ` ;
const link = "" + tgzUrl ;
2019-09-20 22:20:10 +03:00
// Please keep the strings "an installable tgz" and "packed" in this message, as well as the devDependencies section,
// so that the playgrounds deployment process can find these comments.
2024-03-08 21:51:28 +03:00
const comment = ` Hey @ ${ REQUESTING _USER } , I've packed this into [an installable tgz]( ${ link } ). You can install it for testing by referencing it in your \` package.json \` like so:
2019-06-04 01:24:35 +03:00
\ ` \` \`
{
"devDependencies" : {
"typescript" : "${link}"
}
}
\ ` \` \`
and then running \ ` npm install \` .
2024-03-08 21:51:28 +03:00
` ;
2019-09-20 22:20:10 +03:00
2019-11-01 04:50:00 +03:00
// Temporarily disable until we get access controls set up right
2020-07-01 18:18:15 +03:00
// Send a ping to https://github.com/microsoft/typescript-make-monaco-builds#pull-request-builds
2024-03-08 21:51:28 +03:00
await gh . request ( "POST /repos/microsoft/typescript-make-monaco-builds/dispatches" , { event _type : ` ${ SOURCE _ISSUE } ` , headers : { Accept : "application/vnd.github.everest-preview+json" } } ) ;
return comment ;
2019-06-04 01:24:35 +03:00
}
2024-03-08 21:51:28 +03:00
let newComment ;
let emoji ;
try {
newComment = await main ( ) ;
emoji = "✅" ;
}
catch ( e ) {
2019-06-04 01:24:35 +03:00
console . error ( e ) ;
2024-03-08 21:51:28 +03:00
newComment = ` Hey @ ${ REQUESTING _USER } , something went wrong when looking for the build artifact. ([You can check the log here](https://typescript.visualstudio.com/TypeScript/_build/index?buildId= ${ BUILD _BUILDID } &_a=summary)). ` ;
emoji = "❌" ;
}
const resultsComment = await gh . issues . createComment ( {
issue _number : SOURCE _ISSUE ,
owner : "microsoft" ,
repo : "TypeScript" ,
body : newComment ,
2019-09-20 22:20:10 +03:00
} ) ;
2024-03-08 21:51:28 +03:00
const toReplace = ` <!--result- ${ DISTINCT _ID } --> ` ;
let posted = false ;
for ( let i = 0 ; i < 5 ; i ++ ) {
// Get status comment contents
const statusComment = await gh . rest . issues . getComment ( {
2024-03-08 21:57:32 +03:00
comment _id : STATUS _COMMENT ,
2024-03-08 21:51:28 +03:00
owner : "microsoft" ,
repo : "TypeScript" ,
} ) ;
const oldComment = statusComment . data . body ;
if ( ! oldComment ? . includes ( toReplace ) ) {
posted = true ;
break ;
}
const newComment = oldComment . replace ( toReplace , ` [ ${ emoji } Results]( ${ resultsComment . data . html _url } ) ` ) ;
// Update status comment
await gh . rest . issues . updateComment ( {
2024-03-08 21:57:32 +03:00
comment _id : STATUS _COMMENT ,
2024-03-08 21:51:28 +03:00
owner : "microsoft" ,
repo : "TypeScript" ,
body : newComment ,
} ) ;
// Repeat; someone may have edited the comment at the same time.
await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
}
if ( ! posted ) {
throw new Error ( "Failed to update status comment" ) ;
}