Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jira data center #122

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions jira/src/main/kotlin/gropius/sync/jira/JiraDataService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ class JiraDataService(
* @return the IMSUser for the Jira user
*/
suspend fun mapUser(imsProject: IMSProject, user: JsonElement): User {
val encodedAccountId =
jsonNodeMapper.jsonNodeToDeterministicString(objectMapper.valueToTree<JsonNode>(user.jsonObject["accountId"]!!.jsonPrimitive.content))
val encodedAccountId = if (user.jsonObject["accountId"] != null) jsonNodeMapper.jsonNodeToDeterministicString(
objectMapper.valueToTree<JsonNode>(user.jsonObject["accountId"]!!.jsonPrimitive.content)
)
else jsonNodeMapper.jsonNodeToDeterministicString(objectMapper.valueToTree<JsonNode>(user.jsonObject["key"]!!.jsonPrimitive.content))
val foundImsUser =
imsProject.ims().value.users().firstOrNull { it.templatedFields["jira_id"] == encodedAccountId }
if (foundImsUser != null) {
Expand Down Expand Up @@ -221,7 +223,36 @@ class JiraDataService(
val cloudId =
token.cloudIds?.filter { URI(it.url + "/rest/api/2") == URI(imsConfig.rootUrl.toString()) }?.map { it.id }
?.firstOrNull()
if (cloudId != null) {
if (token.type == "PAT") {
try {
val res = client.request(imsConfig.rootUrl.toString()) {
method = requestMethod
url {
appendPathSegments("/rest/api/2/")
urlBuilder(this)
}
headers {
append(
HttpHeaders.Authorization, "Bearer ${token.token}"
)
}
if (body != null) {
contentType(ContentType.parse(JSON_CHARSET_MIME_TYPE))
setBody(body)
}
}
logger.info("Response Code for request with token token is ${res.status}(${res.status.isSuccess()}): $body is ${res.bodyAsText()}")
return if (res.status.isSuccess()) {
logger.trace("Response for {} {}", res.request.url, res.bodyAsText())
Optional.of(res)
} else {
Optional.empty()
}
} catch (e: ClientRequestException) {
e.printStackTrace()
return Optional.empty()
}
} else if (cloudId != null) {
try {
val res = client.request("https://api.atlassian.com/ex/jira/") {
method = requestMethod
Expand Down
9 changes: 7 additions & 2 deletions jira/src/main/kotlin/gropius/sync/jira/JiraTokenManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ data class JiraCloudId(val id: String, val url: String, val name: String, val sc
* Response of the getIMSToken login endpoint
* @param token Token if available
* @param isImsUserKnown True if the user exists and just has no token
* @param cloudIds List of cloud ids
* @param type Type of the token
*/
@Serializable
data class JiraTokenResponse(
override val token: String?, override val isImsUserKnown: Boolean, val cloudIds: List<JiraCloudId>? = null
override val token: String?,
override val isImsUserKnown: Boolean,
val cloudIds: List<JiraCloudId>? = null,
val type: String? = null
) : BaseResponseType

@Component
Expand All @@ -30,6 +35,6 @@ class JiraTokenManager(
neoOperations: ReactiveNeo4jOperations, syncConfigurationProperties: SyncConfigurationProperties
) : TokenManager<JiraTokenResponse>(neoOperations, syncConfigurationProperties) {
override suspend fun parseHttpBody(response: HttpResponse): JiraTokenResponse? {
return response.body()
return response.body<JiraTokenResponse>()
}
}
10 changes: 5 additions & 5 deletions jira/src/main/kotlin/gropius/sync/jira/model/IssueData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ class JiraTimelineItem(val id: String, val created: String, val author: JsonObje
issue: Issue
): Pair<List<TimelineItem>, TimelineItemConversionInformation> {
val jiraService = (service as JiraDataService)
if (data.fieldId == "summary") {
val fieldId = data.fieldId ?: data.field
if (fieldId == "summary") {
return gropiusSummary(timelineItemConversionInformation, imsProject, service, jiraService)
} else if (data.fieldId == "resolution") {
} else if (fieldId == "resolution") {
return gropiusState(
timelineItemConversionInformation, imsProject, service, jiraService
)
} else if (data.fieldId == "labels") {
} else if (fieldId == "labels") {
return gropiusLabels(
timelineItemConversionInformation, imsProject, service, jiraService
)
Expand Down Expand Up @@ -360,8 +361,7 @@ data class IssueData(
}

override suspend fun fillImsIssueTemplatedFields(
templatedFields: MutableMap<String, String>,
service: SyncDataService
templatedFields: MutableMap<String, String>, service: SyncDataService
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ data class JiraComment(
val updateAuthor: JsonObject,
val created: String,
val updated: String,
val jsdPublic: Boolean
val jsdPublic: Boolean? = null
) {}

/**
Expand Down
7 changes: 5 additions & 2 deletions sync/src/main/kotlin/gropius/sync/TokenManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier
Expand All @@ -34,6 +35,7 @@ interface BaseResponseType {
val isImsUserKnown: Boolean
}

@Serializable
data class LinkImsUserQuery(val imsUserIds: List<String>)

/**
Expand Down Expand Up @@ -203,7 +205,8 @@ abstract class TokenManager<ResponseType : BaseResponseType>(
} else {
logger.trace("User ${user.rawId} had no token")
}
}
} else
logger.trace("User $user does not allow sync from $owner")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brackets pls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
TODO("Error Message for no working users")
}
Expand Down Expand Up @@ -234,7 +237,7 @@ abstract class TokenManager<ResponseType : BaseResponseType>(
* @param user The user AFTER BEING SAVED TO DB (valid, non-null rawId)
*/
suspend fun advertiseIMSUser(user: IMSUser) {
client.post(syncConfigurationProperties.loginServiceBase.toString()) {
client.put(syncConfigurationProperties.loginServiceBase.toString()) {
url {
appendPathSegments("auth", "api", "sync", "link-ims-users")
}
Expand Down