1616
1717package com.google.firebase.dataconnect.gradle.ci
1818
19- import com.google.firebase.dataconnect.gradle.plugin.nextAlphanumericString
20- import java.io.ByteArrayInputStream
2119import java.io.File
22- import kotlin.random.Random
23- import kotlinx.serialization.ExperimentalSerializationApi
24- import kotlinx.serialization.Serializable
25- import kotlinx.serialization.json.Json
26- import kotlinx.serialization.json.decodeFromStream
2720import org.gradle.api.logging.Logger
28- import org.gradle.internal.impldep.org.apache.commons.io.output.ByteArrayOutputStream
2921import org.gradle.process.ExecOperations
3022
3123class PostCommentForJobResults (
@@ -41,12 +33,16 @@ class PostCommentForJobResults(
4133 val githubRunNumber : String ,
4234 val githubRunAttempt : String ,
4335 val workDirectory : File ,
44- val execOperations : ExecOperations ,
36+ execOperations : ExecOperations ,
4537 val logger : Logger
4638) {
4739
40+ private val githubClient = GithubClient (execOperations, workDirectory, githubRepository, logger)
41+
4842 fun run () {
49- logger.info(" jobResults={}" , jobResults)
43+ logger.info(" jobResults=[{}]{" , jobResults.size)
44+ jobResults.forEach { logger.info(" {}: {}" , it.jobId, it.result) }
45+ logger.info(" }" )
5046 logger.info(" githubIssue={}" , githubIssue)
5147 logger.info(" githubRepository={}" , githubRepository)
5248 logger.info(" githubEventName={}" , githubEventName)
@@ -64,32 +60,13 @@ class PostCommentForJobResults(
6460 val issueUrl = " $githubRepositoryHtmlUrl /issues/$githubIssue "
6561 logger.info(" Posting the following comment to GitHub Issue {}:" , issueUrl)
6662 messageLines.forEach { logger.info(" > {}" , it) }
67- postCommentToGithubIssue(messageLines)
68- }
69-
70- private fun postCommentToGithubIssue (messageLines : List <String >) {
71- val tempFile = File (workDirectory, Random .nextAlphanumericString(30 ))
72- logger.info(" Writing GitHub Issue comment into text file: {}" , tempFile.absolutePath)
73- workDirectory.mkdirs()
74- tempFile.writeText(messageLines.joinToString(" \n " ))
75-
76- execOperations.exec { execSpec ->
77- execSpec.executable(" gh" )
78- execSpec.args(" issue" )
79- execSpec.args(" comment" )
80- execSpec.args(githubIssue.toString())
81- execSpec.args(" --body-file" )
82- execSpec.args(tempFile.absolutePath)
83- execSpec.args(" -R" )
84- execSpec.args(githubRepository)
85- logger.info(" Running command: {}" , execSpec.commandLine.joinToString(" " ))
86- }
63+ val commentUrl = githubClient.postComment(githubIssue, messageLines)
64+ logger.lifecycle(" Comment posted successfully: {}" , commentUrl)
8765 }
8866
8967 private fun calculateMessageLines (): List <String > = buildList {
90- val prNumber: Int? = parseGithubPrNumberFromGithubRef()
91- val prInfo: GitHubPrInfo ? = if (prNumber == = null ) null else fetchGithubPrInfo(prNumber)
92- if (prInfo != = null ) {
68+ parseGithubPrNumberFromGithubRef()?.let { prNumber ->
69+ val prInfo = githubClient.fetchIssueInfo(prNumber)
9370 add(" Posting from Pull Request $githubRepositoryHtmlUrl /pull/$prNumber (${prInfo.title} )" )
9471 }
9572
@@ -120,38 +97,9 @@ class PostCommentForJobResults(
12097 logger.info(" Extracting PR number from githubRef: {}" , githubRef)
12198 val prNumber: Int? =
12299 Regex (" refs/pull/([0-9]+)/merge" ).matchEntire(githubRef)?.groupValues?.get(1 )?.toInt()
123- logger.info(" Extracted PR number from githubRef: {}" , githubRef)
100+ logger.info(" Extracted PR number from githubRef {} : {}" , githubRef, prNumber )
124101 return prNumber
125102 }
126103
127- private fun fetchGithubPrInfo (prNumber : Int ): GitHubPrInfo {
128- logger.info(" Fetching information from GitHub about PR #{}" , prNumber)
129- val byteArrayOutputStream = ByteArrayOutputStream ()
130- execOperations.exec { execSpec ->
131- execSpec.standardOutput = byteArrayOutputStream
132- execSpec.executable(" gh" )
133- execSpec.args(" issue" )
134- execSpec.args(" view" )
135- execSpec.args(prNumber.toString())
136- execSpec.args(" --json" )
137- execSpec.args(" title,body" )
138- execSpec.args(" -R" )
139- execSpec.args(githubRepository)
140- logger.info(" Running command: {}" , execSpec.commandLine.joinToString(" " ))
141- }
142-
143- val jsonParser = Json { ignoreUnknownKeys = true }
144- @OptIn(ExperimentalSerializationApi ::class )
145- val githubPrInfo =
146- jsonParser.decodeFromStream<GitHubPrInfo >(
147- ByteArrayInputStream (byteArrayOutputStream.toByteArray())
148- )
149-
150- logger.info(" Fetched information from GitHub about PR #{}: {}" , prNumber, githubPrInfo)
151- return githubPrInfo
152- }
153-
154104 data class JobResult (val jobId : String , val result : String )
155-
156- @Serializable private data class GitHubPrInfo (val title : String , val body : String )
157105}
0 commit comments