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

Set assignees for issue #63

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion kinta-lib/src/main/kotlin/com/dailymotion/kinta/GitTool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ interface GitTool {
base: String? = null,
title: String? = null,
body: String? = null,
)
): String?

fun deleteRef(
token: String? = null,
owner: String? = null,
repo: String? = null,
ref: String
)

fun setAssignees(
token: String? = null,
owner: String? = null,
repo: String? = null,
issue: String,
assignees: List<String>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package com.dailymotion.kinta.integration.github

import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.coroutines.await
import com.dailymotion.kinta.*
import com.dailymotion.kinta.GetPullRequestWithBase
import com.dailymotion.kinta.GetPullRequestWithHead
import com.dailymotion.kinta.GetRefs
import com.dailymotion.kinta.GitTool
import com.dailymotion.kinta.KintaEnv
import com.dailymotion.kinta.Logger
import com.dailymotion.kinta.Project
import com.dailymotion.kinta.integration.git.model.BranchInfo
import com.dailymotion.kinta.integration.git.model.PullRequestInfo
import com.dailymotion.kinta.integration.github.internal.GithubOauthClient
Expand All @@ -14,7 +20,12 @@ import kotlinx.coroutines.runBlocking
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
Expand All @@ -23,7 +34,7 @@ import okio.ByteString.Companion.encodeUtf8
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.transport.URIish
import java.io.File
import java.util.*
import java.util.Base64


@Suppress("NAME_SHADOWING")
Expand All @@ -38,7 +49,7 @@ object GithubIntegration : GitTool {
base: String?,
title: String?,
body: String?,
) {
): String? {
val token = token ?: retrieveToken()
val owner = owner ?: repository().owner
val repo = repo ?: repository().name
Expand Down Expand Up @@ -73,12 +84,14 @@ object GithubIntegration : GitTool {
throw Exception(response.body()?.string() ?: "")
}

response.body()?.charStream()?.let {
return response.body()?.charStream()?.let {
try {
val htmlUrl = json.parseToJsonElement(it.readText()).jsonObject["html_url"]?.jsonPrimitive?.content
Logger.i("-> $htmlUrl")
htmlUrl
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}
Expand Down Expand Up @@ -370,4 +383,33 @@ object GithubIntegration : GitTool {
"cannot delete secret $name: ${response.body()?.byteStream()?.reader()?.readText()}"
}
}

override fun setAssignees(
token: String?,
owner: String?,
repo: String?,
issue: String,
assignees: List<String>
) {
val token = token ?: retrieveToken()
val owner = owner ?: repository().owner
val repo = repo ?: repository().name

val jsonObject = JsonObject(
mapOf("assignees" to JsonArray(assignees.map { JsonPrimitive(it) }))
)

val body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString())

val request = Request.Builder()
.url("https://api.github.com/repos/$owner/$repo/issues/$issue/assignees")
.post(body)
.build()

Logger.i("Assigning...")
val response = httpClient(token).newCall(request).execute()
if (!response.isSuccessful) {
throw Exception(response.body()?.string() ?: "")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.dailymotion.kinta.integration.git.model.BranchInfo
import com.dailymotion.kinta.integration.git.model.PullRequestInfo
import com.dailymotion.kinta.integration.gitlab.internal.GitlabService
import com.dailymotion.kinta.integration.gitlab.internal.MergeRequestBody
import com.dailymotion.kinta.integration.gitlab.internal.UpdateMergeRequestBody
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -61,7 +62,7 @@ object GitlabIntegration : GitTool {
base: String?,
title: String?,
body: String?,
) {
): String? {
val token_ = token ?: retrieveToken()
val owner_ = owner ?: repository().owner
val repo_ = repo ?: repository().name
Expand All @@ -88,13 +89,16 @@ object GitlabIntegration : GitTool {
}

response.body()?.charStream()?.let {
try {
return try {
val htmlUrl = json.parseToJsonElement(it.readText()).jsonObject["web_url"]?.jsonPrimitive?.content
Logger.i("-> $htmlUrl")
htmlUrl
} catch (e: Exception) {
e.printStackTrace()
null
}
}
return null
}

/**
Expand Down Expand Up @@ -197,4 +201,28 @@ object GitlabIntegration : GitTool {
val repoName = s[1].removeSuffix(".git")
return Repository(s[0], repoName)
}

override fun setAssignees(
token: String?,
owner: String?,
repo: String?,
issue: String,
assignees: List<String>
) {
val token_ = token ?: retrieveToken()
val owner_ = owner ?: repository().owner
val repo_ = repo ?: repository().name

Logger.i("Updating assignees...")
val response = service(token_).updatePullRequest(
projectId = "$owner_/$repo_",
mergeRequestId = issue,
mergeRequestBody = UpdateMergeRequestBody(
assignee_ids = assignees
)
).execute()
if (!response.isSuccessful) {
throw Exception(response.body()?.string() ?: "")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package com.dailymotion.kinta.integration.gitlab.internal

import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.*
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Path
import retrofit2.http.Query

interface GitlabService {
@POST("projects/{projectId}/merge_requests")
Expand All @@ -20,4 +26,7 @@ interface GitlabService {
@DELETE("projects/{projectId}/repository/branches/{branch}")
fun deleteBranch(@Path("projectId") projectId: String, @Path("branch") branch: String): Call<ResponseBody>

@PUT("projects/{projectId}/merge_requests/{mergeRequestId}")
fun updatePullRequest(@Path("projectId") projectId: String, @Path("mergeRequestId") mergeRequestId: String, @Body mergeRequestBody: UpdateMergeRequestBody): Call<ResponseBody>

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ data class MergeRequestBody(
val target_branch: String,
val title: String,
val description: String,
)

@Serializable
data class UpdateMergeRequestBody(
val assignee_ids: List<String>,
)