-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: can create pull requests now :D
- Loading branch information
Showing
13 changed files
with
312 additions
and
108 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,76 @@ | ||
import io.github.cdimascio.dotenv.dotenv | ||
|
||
import io.ktor.serialization.kotlinx.json.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.engine.* | ||
import io.ktor.server.netty.* | ||
import io.ktor.server.plugins.contentnegotiation.* | ||
import jdk.nashorn.internal.parser.Token | ||
import kotlinx.coroutines.* | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.json.Json | ||
import structures.HttpProvider | ||
import structures.api.Response | ||
import structures.api.account.OrgAccount | ||
import structures.api.account.TokenData | ||
import java.util.* | ||
import structures.wrapped.Base | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
object Globals { | ||
val eventEmitter = EventEmitter<Base>() | ||
val serializer = Json { | ||
ignoreUnknownKeys = true | ||
prettyPrint = true | ||
explicitNulls = false | ||
} | ||
} | ||
|
||
object Client : CoroutineScope { | ||
|
||
class Client : CoroutineScope { | ||
val api = HttpProvider(this) | ||
private var parentJob = Job() | ||
val eventEmitter = EventEmitter() | ||
val json = Json { ignoreUnknownKeys = true } | ||
lateinit var application : structures.api.application.Application | ||
lateinit var orgAccount: OrgAccount | ||
lateinit var tokenData : TokenData | ||
override val coroutineContext: CoroutineContext | ||
get() = Dispatchers.Default + parentJob | ||
private val server = embeddedServer( | ||
Netty, | ||
port = 8000, | ||
host = "localhost", | ||
parentCoroutineContext = coroutineContext | ||
) { | ||
configureRouting() | ||
install(ContentNegotiation) | ||
} | ||
init { | ||
dotenv { | ||
systemProperties = true | ||
} | ||
} | ||
|
||
inline fun <reified T : Response> on( | ||
inline fun <reified T : Base> on( | ||
eventName: String, | ||
crossinline fn: (T) -> Unit | ||
crossinline fn: suspend (T) -> Unit | ||
) { | ||
launch { | ||
eventEmitter.events.collect { | ||
Globals.eventEmitter.events.collect { | ||
when (eventName) { | ||
"pull_request" -> { | ||
fn(json.decodeFromString(it)) | ||
fn(it as T) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
suspend fun loginAsync() { | ||
verifyWithJwt() | ||
verifyWithInstallationApp() | ||
getInstallationToken() | ||
|
||
} | ||
private suspend fun verifyWithJwt() { | ||
application = HttpProvider.loginIntoApplicationAsync().await() | ||
} | ||
private suspend fun verifyWithInstallationApp() { | ||
orgAccount = HttpProvider.loginIntoOrgAsync().await() | ||
|
||
} | ||
private suspend fun getInstallationToken() { | ||
tokenData = HttpProvider.getInstallationToken(orgAccount).await() | ||
coroutineScope { | ||
withContext(Dispatchers.Default) { | ||
application = api.loginIntoApplicationAsync().await() | ||
orgAccount = api.loginIntoOrgAsync().await() | ||
tokenData = api.getInstallationTokenAsync(orgAccount).await() | ||
} | ||
} | ||
} | ||
/** | ||
* This should start last in order to prevent blocking of thread and listeners | ||
*/ | ||
fun startWebhookListener() { | ||
server.start(wait = true) | ||
embeddedServer( | ||
Netty, | ||
port = 8000, | ||
host = "localhost", | ||
parentCoroutineContext = coroutineContext | ||
) { | ||
configureRouting(this@Client) | ||
install(ContentNegotiation) { | ||
json(Globals.serializer) | ||
} | ||
}.start(wait = true).also { println("Started server") } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import structures.api.Response | ||
import structures.wrapped.Base | ||
|
||
class EventEmitter { | ||
private val _events = MutableSharedFlow<String>() // private mutable shared flow | ||
class EventEmitter<T : Base> { | ||
private val _events = MutableSharedFlow<T>() // private mutable shared flow | ||
val events = _events.asSharedFlow() // publicly exposed as read-only shared flow | ||
|
||
suspend fun produceEvent(event: String) { | ||
suspend fun produceEvent(event: T) { | ||
_events.emit(event) // suspends until all subscribers receive it | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
|
||
import kotlinx.coroutines.* | ||
import structures.api.PullRequests | ||
import structures.api.application.PullRequestAction | ||
import structures.options.PullRequestCreateOptions | ||
import structures.wrapped.PullRequestsManager | ||
|
||
fun main() = runBlocking { | ||
|
||
|
||
Client.loginAsync() | ||
println(Client.orgAccount) | ||
Client.on<PullRequests>("pull_request") { pr_event -> | ||
println(pr_event) | ||
val sernClient = Client() | ||
sernClient.loginAsync() | ||
|
||
sernClient.on<PullRequestsManager>("pull_request") { pr_event -> | ||
when(pr_event.action) { | ||
PullRequestAction.Opened -> { | ||
println("new pull request opened") | ||
} | ||
else -> Unit | ||
} | ||
} | ||
Client.startWebhookListener() | ||
sernClient.startWebhookListener() | ||
} | ||
|
||
|
||
|
||
|
Oops, something went wrong.