|
| 1 | +import * as fs from 'fs'; |
| 2 | + |
| 3 | +export const GitHub = { |
| 4 | + writeOutput(name: string, value: any): void { |
| 5 | + if (typeof process.env.GITHUB_OUTPUT == 'string' && process.env.GITHUB_OUTPUT.length > 0) { |
| 6 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `${name}=${value}\n`); |
| 7 | + } |
| 8 | + console.log(`Output ${name}`, value); |
| 9 | + }, |
| 10 | + |
| 11 | + downloadPreviousArtifact(branch: string, targetDir: string, artifactName: string): void { |
| 12 | + fs.mkdirSync(targetDir, { recursive: true }); |
| 13 | + |
| 14 | + // if (workflow == null) { |
| 15 | + // println("Skipping previous artifact '$artifactName' download for branch '$branch' - not running in CI") |
| 16 | + // return |
| 17 | + // } |
| 18 | + console.log(`Trying to download previous artifact '${artifactName}' for branch '${branch}'`) |
| 19 | + |
| 20 | + // val run = workflow!!.listRuns() |
| 21 | + // .firstOrNull { it.headBranch == branch && it.conclusion == GHWorkflowRun.Conclusion.SUCCESS } |
| 22 | + // if (run == null) { |
| 23 | + // println("Couldn't find any successful run workflow ${workflow!!.name}") |
| 24 | + // return |
| 25 | + // } |
| 26 | + |
| 27 | + // val artifact = run.listArtifacts().firstOrNull { it.name == artifactName } |
| 28 | + // if (artifact == null) { |
| 29 | + // println("Couldn't find any artifact matching $artifactName") |
| 30 | + // return |
| 31 | + // } |
| 32 | + |
| 33 | + // println("Downloading artifact ${artifact.archiveDownloadUrl} and extracting to $targetDir") |
| 34 | + // artifact.download { |
| 35 | + // val zipStream = ZipInputStream(it) |
| 36 | + // var entry: ZipEntry? |
| 37 | + // // while there are entries I process them |
| 38 | + // while (true) { |
| 39 | + // entry = zipStream.nextEntry |
| 40 | + // if (entry == null) { |
| 41 | + // break |
| 42 | + // } |
| 43 | + // if (entry.isDirectory) { |
| 44 | + // Path.of(entry.name).createDirectories() |
| 45 | + // } else { |
| 46 | + // println("Extracting ${entry.name}") |
| 47 | + // val outFile = FileOutputStream(targetDir.resolve(entry.name).toFile()) |
| 48 | + // while (zipStream.available() > 0) { |
| 49 | + // val c = zipStream.read() |
| 50 | + // if (c > 0) { |
| 51 | + // outFile.write(c) |
| 52 | + // } else { |
| 53 | + // break |
| 54 | + // } |
| 55 | + // } |
| 56 | + // outFile.close() |
| 57 | + // } |
| 58 | + // } |
| 59 | + // } |
| 60 | + }, |
| 61 | + |
| 62 | + // fun addOrUpdateComment(commentBuilder: PrCommentBuilder) { |
| 63 | + // if (pullRequest == null) { |
| 64 | + // val file = File("out/comment.html") |
| 65 | + // println("No PR available (not running in CI?): writing built comment to ${file.absolutePath}") |
| 66 | + // file.writeText(commentBuilder.body) |
| 67 | + // } else { |
| 68 | + // val comments = pullRequest!!.comments |
| 69 | + // // Trying to fetch `github!!.myself` throws (in CI only): Exception in thread "main" org.kohsuke.github.HttpException: |
| 70 | + // // {"message":"Resource not accessible by integration","documentation_url":"https://docs.github.com/rest/reference/users#get-the-authenticated-user"} |
| 71 | + // // Let's make this conditional on some env variable that's unlikely to be set. |
| 72 | + // // Do not use "CI" because that's commonly set during local development and testing. |
| 73 | + // val author = if (env.containsKey("GITHUB_ACTION")) "github-actions[bot]" else github!!.myself.login |
| 74 | + // val comment = comments.firstOrNull { |
| 75 | + // it.user.login.equals(author) && |
| 76 | + // it.body.startsWith(commentBuilder.title, ignoreCase = true) |
| 77 | + // } |
| 78 | + // if (comment != null) { |
| 79 | + // println("Updating PR comment ${comment.htmlUrl} body") |
| 80 | + // comment.update(commentBuilder.body) |
| 81 | + // } else { |
| 82 | + // println("Adding new PR comment to ${pullRequest!!.htmlUrl}") |
| 83 | + // pullRequest!!.comment(commentBuilder.body) |
| 84 | + // } |
| 85 | + // } |
| 86 | + // } |
| 87 | +} |
0 commit comments