Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Feb 14, 2024
1 parent 7e7d062 commit e602bf1
Show file tree
Hide file tree
Showing 10 changed files with 1,891 additions and 986 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pluginUntilBuild=235.*
# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
platformType=IC
#platformVersion=LATEST-EAP-SNAPSHOT
platformVersion=2023.3.3
platformVersion=2023.3.4

# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
Expand Down
78 changes: 57 additions & 21 deletions src/main/kotlin/com/dsoftware/ghmanager/api/GetRunLogRequest.kt
Original file line number Diff line number Diff line change
@@ -1,44 +1,80 @@
package com.dsoftware.ghmanager.api

import com.dsoftware.ghmanager.api.model.Job
import com.intellij.openapi.diagnostic.logger
import com.jetbrains.rd.util.first
import org.jetbrains.plugins.github.api.GithubApiRequest
import org.jetbrains.plugins.github.api.GithubApiResponse
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.text.SimpleDateFormat
import java.util.Date
import java.util.TimeZone


class GetJobLogRequest(url: String) : GithubApiRequest.Get<JobLog>(url) {
class GetJobLogRequest(private val job: Job) : GithubApiRequest.Get<JobLog>(job.url + "/logs") {
override fun extractResult(response: GithubApiResponse): JobLog {
LOG.debug("extracting result for $url")
return response.handleBody {
extractJobLogFromStream(it)
}
}

companion object {
private val LOG = logger<GetJobLogRequest>()
fun extractJobLogFromStream(inputStream: InputStream): JobLog {
val content = HashMap<Int, String>()
var stepNumber = 1
try {
val reader = BufferedReader(InputStreamReader(inputStream))
val stepLog = StringBuilder()
for (line in reader.lines()) {
if (line.contains("##[group]Run ")) {
content[stepNumber] = stepLog.toString()
stepLog.clear()
stepNumber++
} else {
stepLog.append(line + "\n")
}
fun extractJobLogFromStream(inputStream: InputStream): JobLog {
val dateTimePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSX"
val formatter = SimpleDateFormat(dateTimePattern)
val stepsPeriodMap: Map<Int, Pair<Date?, Date?>> = job.steps?.associate { step ->
val start = if(step.startedAt != null) formatter.parse(step.startedAt) else null
val completed = if(step.completedAt != null) formatter.parse(step.completedAt) else null
step.number to (start to completed)
} ?: emptyMap()
val contentBuilders = HashMap<Int, StringBuilder>()

formatter.timeZone = TimeZone.getTimeZone("UTC")
fun findStep(datetimeStr: String): Int {
val time = formatter.parse(datetimeStr)
var lo = 0
var hi = job.steps!!.size - 1
while (lo <= hi) {
val mid = (lo + hi) / 2
val midVal = stepsPeriodMap[mid]
if (midVal?.second != null && midVal.second!!.before(time)) {
lo = mid + 1
} else if (midVal?.first != null && midVal.first!!.after(time)) {
hi = mid - 1
} else {
return mid
}
}
return -1
}
try {
val reader = BufferedReader(InputStreamReader(inputStream))
val lines = reader.lines()

var lineNum = 0
var currStep = 0
for (line in lines) {
++lineNum
if (line.length < 29) {
contentBuilders.getOrDefault(currStep, StringBuilder()).append(line + "\n")
continue
}
} catch (e: IOException) {
LOG.warn(e.message)
throw e
val dateStr = line.substring(0, 28)
currStep = findStep(dateStr)
contentBuilders.getOrDefault(currStep, StringBuilder()).append(line + "\n")
}
return content
} catch (e: IOException) {
LOG.warn(e.message)
throw e
}
return contentBuilders.map { (k, v) -> k to v.toString() }.toMap()
}

companion object {
private val LOG = logger<GetJobLogRequest>()

}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/com/dsoftware/ghmanager/api/GithubApi.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dsoftware.ghmanager.api

import com.dsoftware.ghmanager.api.model.Job
import com.dsoftware.ghmanager.api.model.WorkflowRunJobs
import com.dsoftware.ghmanager.api.model.WorkflowRuns
import com.dsoftware.ghmanager.api.model.WorkflowTypes
Expand All @@ -24,7 +25,7 @@ typealias WorkflowRunLog = Map<String, JobLog>

object GithubApi : GithubApiRequests.Entity("/repos") {
private val LOG = logger<GithubApi>()
fun getJobLog(url: String) = GetJobLogRequest(url).withOperationName("Get Job log $url")
fun getJobLog(job: Job) = GetJobLogRequest(job).withOperationName("Get Job log ${job.id}")

fun postUrl(name: String, url: String, data: Any = Object()) =
GithubApiRequest.Post.Json(url, data, Object::class.java, null).withOperationName(name)
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/com/dsoftware/ghmanager/api/model/JobModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ data class JobStep(
val name: String,
val number: Int,
/* The time that the step started, in ISO 8601 format. */
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX", timezone = "UTC")
val startedAt: Date? = null,
val startedAt: String? = null,
/* The time that the job finished, in ISO 8601 format. */
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX", timezone = "UTC")
val completedAt: Date? = null
val completedAt: String? = null
)
5 changes: 3 additions & 2 deletions src/main/kotlin/com/dsoftware/ghmanager/data/DataProviders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.dsoftware.ghmanager.data
import com.dsoftware.ghmanager.api.WorkflowRunLog
import com.dsoftware.ghmanager.api.GithubApi
import com.dsoftware.ghmanager.api.JobLog
import com.dsoftware.ghmanager.api.model.Job
import com.dsoftware.ghmanager.api.model.WorkflowRunJobs
import com.intellij.openapi.Disposable
import com.intellij.openapi.diagnostic.thisLogger
Expand Down Expand Up @@ -65,11 +66,11 @@ open class DataProvider<T>(
class JobLogDataProvider(
progressManager: ProgressManager,
requestExecutor: GithubApiRequestExecutor,
jobLogUrl: String
job: Job
) : DataProvider<JobLog>(
progressManager,
requestExecutor,
GithubApi.getJobLog(jobLogUrl),
GithubApi.getJobLog(job),
emptyMap()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.dsoftware.ghmanager.Constants.LOG_MSG_JOB_IN_PROGRESS
import com.dsoftware.ghmanager.Constants.LOG_MSG_MISSING
import com.dsoftware.ghmanager.Constants.LOG_MSG_PICK_JOB
import com.dsoftware.ghmanager.api.JobLog
import com.dsoftware.ghmanager.api.WorkflowRunLog
import com.dsoftware.ghmanager.api.model.Job
import com.dsoftware.ghmanager.api.model.JobStep
import com.intellij.collaboration.ui.SingleValueModel
Expand Down Expand Up @@ -90,7 +89,7 @@ class LogLoadingModelListener(
logModel.value = when {
logsLoadingModel.result == null -> null
jobSelection == null -> LOG_MSG_PICK_JOB
logs == null && jobSelection.status == "in_progress" -> LOG_MSG_JOB_IN_PROGRESS
jobSelection.status == "in_progress" -> LOG_MSG_JOB_IN_PROGRESS
logs == null -> LOG_MSG_MISSING + jobSelection.name
else -> stepsAsLog(logs, jobSelection)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,9 @@ class SingleRunDataLoader(
.maximumSize(200)
.build<String, DataProvider<*>>()


// fun getLogsDataProvider(workflowRun: WorkflowRun): WorkflowRunLogsDataProvider {
// return cache.get(workflowRun.logsUrl) {
// WorkflowRunLogsDataProvider(progressManager, requestExecutor, workflowRun.logsUrl)
// } as WorkflowRunLogsDataProvider
// }

fun getJobLogDataProvider(job: Job): JobLogDataProvider {
return cache.get("${job.url}/logs") {
JobLogDataProvider(progressManager, requestExecutor, "${job.url}/logs")
JobLogDataProvider(progressManager, requestExecutor, job)
} as JobLogDataProvider
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/kotlin/com/dsoftware/ghmanager/TestGetJobLogRequest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.dsoftware.ghmanager

import com.dsoftware.ghmanager.api.model.Job
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import com.intellij.testFramework.fixtures.BasePlatformTestCase

class TestGetJobLogRequest : BasePlatformTestCase() {
private val mapper = ObjectMapper().registerKotlinModule()

fun testGetJobLogRequest() {
val logContent = TestGetJobLogRequest::class.java.getResource("wf-run-single-job.log")?.readText()
val jobJson = TestGetJobLogRequest::class.java.getResource("wf-run-single-job.json")?.readText()
val obj: List<Job> = mapper.readValue(json)
}
}
92 changes: 92 additions & 0 deletions src/test/resources/job.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"id": 21454796844,
"run_id": 7863783013,
"workflow_name": "Push on master",
"head_branch": "master",
"run_url": "https://api.github.com/repos/cunla/fakeredis-py/actions/runs/7863783013",
"run_attempt": 1,
"node_id": "CR_kwDOHLrRQs8AAAAE_s44LA",
"head_sha": "a0576c489ba7cad8cad4ba7e14a7fe30ef9959a1",
"url": "https://api.github.com/repos/cunla/fakeredis-py/actions/jobs/21454796844",
"html_url": "https://github.com/cunla/fakeredis-py/actions/runs/7863783013/job/21454796844",
"status": "completed",
"conclusion": "success",
"created_at": "2024-02-11T18:09:46Z",
"started_at": "2024-02-11T18:09:52Z",
"completed_at": "2024-02-11T18:11:49Z",
"name": "Analyze (python)",
"steps": [
{
"name": "Set up job",
"status": "completed",
"conclusion": "success",
"number": 1,
"started_at": "2024-02-11T18:09:51.000Z",
"completed_at": "2024-02-11T18:09:55.000Z"
},
{
"name": "Checkout repository",
"status": "completed",
"conclusion": "success",
"number": 2,
"started_at": "2024-02-11T18:09:55.000Z",
"completed_at": "2024-02-11T18:09:56.000Z"
},
{
"name": "Initialize CodeQL",
"status": "completed",
"conclusion": "success",
"number": 3,
"started_at": "2024-02-11T18:09:56.000Z",
"completed_at": "2024-02-11T18:10:09.000Z"
},
{
"name": "Perform CodeQL Analysis",
"status": "completed",
"conclusion": "success",
"number": 4,
"started_at": "2024-02-11T18:10:09.000Z",
"completed_at": "2024-02-11T18:11:46.000Z"
},
{
"name": "Post Perform CodeQL Analysis",
"status": "completed",
"conclusion": "success",
"number": 6,
"started_at": "2024-02-11T18:11:47.000Z",
"completed_at": "2024-02-11T18:11:47.000Z"
},
{
"name": "Post Initialize CodeQL",
"status": "completed",
"conclusion": "success",
"number": 7,
"started_at": "2024-02-11T18:11:47.000Z",
"completed_at": "2024-02-11T18:11:47.000Z"
},
{
"name": "Post Checkout repository",
"status": "completed",
"conclusion": "success",
"number": 8,
"started_at": "2024-02-11T18:11:49.000Z",
"completed_at": "2024-02-11T18:11:49.000Z"
},
{
"name": "Complete job",
"status": "completed",
"conclusion": "success",
"number": 9,
"started_at": "2024-02-11T18:11:47.000Z",
"completed_at": "2024-02-11T18:11:47.000Z"
}
],
"check_run_url": "https://api.github.com/repos/cunla/fakeredis-py/check-runs/21454796844",
"labels": [
"ubuntu-latest"
],
"runner_id": 9,
"runner_name": "GitHub Actions 9",
"runner_group_id": 2,
"runner_group_name": "GitHub Actions"
}
Loading

0 comments on commit e602bf1

Please sign in to comment.