Skip to content

Commit a0ebd6f

Browse files
authored
Merge pull request #25 from digipost/catch-504-apollo
Catch og skip 504-exceptions from the Apollo GraphQL client
2 parents cba4ea2 + 2f6fd7b commit a0ebd6f

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

src/main/kotlin/no/digipost/github/monitoring/GithubGraphql.kt

+32-25
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
package no.digipost.github.monitoring
22

3-
import com.apollographql.apollo3.ApolloCall
43
import com.apollographql.apollo3.ApolloClient
5-
import com.apollographql.apollo3.api.ApolloResponse
64
import com.apollographql.apollo3.api.Optional
75
import com.apollographql.apollo3.exception.ApolloHttpException
86
import com.github.graphql.client.GetVulnerabilityAlertsForRepoQuery
97
import com.github.graphql.client.QueryRepositoriesQuery
8+
import java.io.IOException
109
import kotlinx.coroutines.channels.Channel
11-
import kotlinx.coroutines.flow.Flow
1210
import kotlinx.coroutines.flow.catch
13-
import kotlinx.coroutines.flow.filterNotNull
1411
import kotlinx.coroutines.flow.first
15-
import kotlinx.coroutines.flow.map
12+
import kotlinx.coroutines.flow.flow
1613
import kotlinx.coroutines.launch
1714
import kotlinx.coroutines.runBlocking
1815
import okhttp3.internal.immutableListOf
1916
import okhttp3.internal.toImmutableList
2017
import org.slf4j.Logger
2118
import org.slf4j.LoggerFactory
22-
import java.io.IOException
2319

2420
data class Repos(val all: List<Repository>) {
2521
fun getUniqueCVEs(): Map<String, Vulnerability> {
@@ -113,29 +109,40 @@ private suspend fun getVulnerabilitiesForRepo(
113109

114110
while (hasNext) {
115111

116-
val response = apolloClient.query(GetVulnerabilityAlertsForRepoQuery(name, GITHUB_OWNER, after = Optional.present(cursor))).toFlow()
117-
.catch { ex -> logger.error("Noe gikk galt i henting av sårbarheter fra Github", ex) }
118-
.first()
112+
try {
113+
val response = apolloClient
114+
.query(GetVulnerabilityAlertsForRepoQuery(name, GITHUB_OWNER, after = Optional.present(cursor)))
115+
.toFlow().first();
119116

120-
val vulnerabilityAlerts = response.data?.repository?.vulnerabilityAlerts?.nodes ?: emptyList()
121-
val vulnerabilities = vulnerabilityAlerts.mapNotNull {
122-
it?.let {
123-
Vulnerability(
124-
it.securityVulnerability!!.severity,
125-
it.createdAt.toString().substring(0, 10),
126-
it.securityVulnerability.`package`.name,
127-
it.securityVulnerability.advisory.cvss.score,
128-
it.securityVulnerability.advisory.identifiers.firstOrNull { identifier -> "CVE" == identifier.type }?.value
129-
?: "ukjent CVE"
130-
)
131-
}
132-
}.toImmutableList()
117+
val vulnerabilityAlerts = response.data?.repository?.vulnerabilityAlerts?.nodes ?: emptyList()
118+
val vulnerabilities = vulnerabilityAlerts.mapNotNull {
119+
it?.let {
120+
Vulnerability(
121+
it.securityVulnerability!!.severity,
122+
it.createdAt.toString().substring(0, 10),
123+
it.securityVulnerability.`package`.name,
124+
it.securityVulnerability.advisory.cvss.score,
125+
it.securityVulnerability.advisory.identifiers.firstOrNull { identifier -> "CVE" == identifier.type }?.value
126+
?: "ukjent CVE"
127+
)
128+
}
129+
}.toImmutableList()
130+
131+
allVulnerabilities = allVulnerabilities + vulnerabilities
133132

134-
allVulnerabilities = allVulnerabilities + vulnerabilities
133+
hasNext = response.data?.repository?.vulnerabilityAlerts?.pageInfo?.hasNextPage ?: false
135134

136-
hasNext = response.data?.repository?.vulnerabilityAlerts?.pageInfo?.hasNextPage ?: false
135+
cursor = response.data?.repository?.vulnerabilityAlerts?.pageInfo?.endCursor
136+
} catch (e: Exception) {
137+
if (e is ApolloHttpException && e.statusCode == 504) {
138+
logger.warn("Fikk 504 fra ApolloGraphQL for {}, skipper", name)
139+
continue
140+
} else {
141+
logger.error("Noe gikk galt i henting av sårbarheter fra Github", e)
142+
throw e
143+
}
144+
}
137145

138-
cursor = response.data?.repository?.vulnerabilityAlerts?.pageInfo?.endCursor
139146
}
140147

141148
return allVulnerabilities

0 commit comments

Comments
 (0)