16
16
17
17
package com.google.firebase.dataconnect.gradle.ci
18
18
19
- import com.google.firebase.dataconnect.gradle.plugin.nextAlphanumericString
20
- import java.io.ByteArrayInputStream
21
19
import 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
27
20
import org.gradle.api.logging.Logger
28
- import org.gradle.internal.impldep.org.apache.commons.io.output.ByteArrayOutputStream
29
21
import org.gradle.process.ExecOperations
30
22
31
23
class PostCommentForJobResults (
@@ -41,12 +33,16 @@ class PostCommentForJobResults(
41
33
val githubRunNumber : String ,
42
34
val githubRunAttempt : String ,
43
35
val workDirectory : File ,
44
- val execOperations : ExecOperations ,
36
+ execOperations : ExecOperations ,
45
37
val logger : Logger
46
38
) {
47
39
40
+ private val githubClient = GithubClient (execOperations, workDirectory, githubRepository, logger)
41
+
48
42
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(" }" )
50
46
logger.info(" githubIssue={}" , githubIssue)
51
47
logger.info(" githubRepository={}" , githubRepository)
52
48
logger.info(" githubEventName={}" , githubEventName)
@@ -64,32 +60,13 @@ class PostCommentForJobResults(
64
60
val issueUrl = " $githubRepositoryHtmlUrl /issues/$githubIssue "
65
61
logger.info(" Posting the following comment to GitHub Issue {}:" , issueUrl)
66
62
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)
87
65
}
88
66
89
67
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)
93
70
add(" Posting from Pull Request $githubRepositoryHtmlUrl /pull/$prNumber (${prInfo.title} )" )
94
71
}
95
72
@@ -120,38 +97,9 @@ class PostCommentForJobResults(
120
97
logger.info(" Extracting PR number from githubRef: {}" , githubRef)
121
98
val prNumber: Int? =
122
99
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 )
124
101
return prNumber
125
102
}
126
103
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
-
154
104
data class JobResult (val jobId : String , val result : String )
155
-
156
- @Serializable private data class GitHubPrInfo (val title : String , val body : String )
157
105
}
0 commit comments