kmnskd
kmnskd

Reputation: 11

Teamcity kotlin dsl can't get the email address

I need to send an email to trigger, but teamcity can't get the email address in the configuration environment or system environment, so I try to request teamcity api in teamcity kotlin dsl for getting the email address, so I import java.net.URL and java.net.URL.net.HttpURLConnection, but can't get the email address and not throw any error, i don't know if the code can be written like this. The code is as follows.

HttpRequest.kt

package utils

import com.google.gson.Gson
import com.google.gson.JsonObject
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL


class HttpRequest {
    fun get(host: String, username: String): JsonObject? {
        val uri = "$host/app/rest/users/username:$username"
        val url = URL(uri)
        val connection = url.openConnection() as HttpURLConnection

        try {
            connection.requestMethod = "GET"
            connection.setRequestProperty("Content-Type", "application/json")
            connection.setRequestProperty("Accept", "application/json")
            connection.setRequestProperty("Authorization", "Bearer $token")

            val responseCode = connection.responseCode
            if (responseCode == HttpURLConnection.HTTP_OK) {
                val inputStream = connection.inputStream
                val reader = BufferedReader(InputStreamReader(inputStream))
                val response = StringBuilder()
                var line: String?
                while (reader.readLine().also { line = it } != null) {
                    response.append(line)
                }
                val gson = Gson()
                val jsonObject = gson.fromJson(response.toString(), JsonObject::class.java)
                return jsonObject
            } else {
                return null
            }
        } catch(e: Exception) {
            e.printStackTrace()
            return null
        } finally {
            connection.disconnect()
        }
    }


    fun getEmail(host: String, username: String): String {
        val httpRequest: HttpRequest = HttpRequest()
        val jsonResponse = httpRequest.get(host, username)
        val email = jsonResponse?.get("email")?.asString
        return email.toString()
    }
}

buildType.kt

package buildType

import jetbrains.buildServer.configs.kotlin.BuildType
import jetbrains.buildServer.configs.kotlin.buildFeatures.notifications
import jetbrains.buildServer.configs.kotlin.buildSteps.script
import jetbrains.buildServer.configs.kotlin.triggers.schedule
import utils.HttpRequest
import vcs.GitMain


object BuildTest : BuildType({
    val httpRequest: HttpRequest = HttpRequest()
    val build_email = httpRequest.getEmail("http://127.0.0.1:8111", "%teamcity.build.triggeredBy.username%")
    name = "BuildStep"

    vcs {
        root(GitMain)
    }

    params {
        text("env.TeamCityServerUrl", "%teamcity.serverUrl%")
        text("env.triggeredusername", "%teamcity.build.triggeredBy.username%")
        
        text("build_email", build_email)
    }

    steps {
        script {
            id = "simpleRunner"
            scriptContent = """
                echo %env.triggeredusername%
                cd calculator-service
            """.trimIndent()
        }
    }

    triggers {

        schedule {
            schedulingPolicy = cron {
                minutes = "00"
                hours = "17"
            }
            triggerBuild = always()
            withPendingChangesOnly = false
        }
    }

    features {
        notifications {
            notifierSettings = emailNotifier {
                email = "%env.build_email%"
            }
            buildFinishedSuccessfully = true
            buildFailed = true
        }
    }
})

enter image description here

I can get email address with debug the function getEmail at local IDE. enter image description here

Upvotes: 0

Views: 62

Answers (0)

Related Questions