diff --git a/CHANGELOG.md b/CHANGELOG.md index 26d8e4f6..c1146451 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ --> ## Master - Update Kotlin to 1.4.10 [@gianluz] [#140](https://github.com/danger/kotlin/pull/140) +- Migrate from moshi to kotlinx serialization [@gianluz] [#141] (https://github.com/danger/kotlin/pull/141) # 0.7.1 diff --git a/build.gradle b/build.gradle index f4c86fa2..f486ec94 100644 --- a/build.gradle +++ b/build.gradle @@ -6,6 +6,7 @@ buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" + classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion" classpath 'com.github.jengelman.gradle.plugins:shadow:5.0.0' classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.21.2" } @@ -25,6 +26,9 @@ allprojects { repositories { mavenCentral() jcenter() + maven { + url = "https://kotlin.bintray.com/kotlinx/" + } } } diff --git a/danger-kotlin-library/build.gradle b/danger-kotlin-library/build.gradle index fdf695ac..2c6c4f77 100644 --- a/danger-kotlin-library/build.gradle +++ b/danger-kotlin-library/build.gradle @@ -1,10 +1,12 @@ plugins { id 'org.jetbrains.kotlin.jvm' + id 'org.jetbrains.kotlin.plugin.serialization' id 'maven-publish' } apply from: file('dependencies.gradle') apply from: file('version.gradle') +apply plugin: 'kotlinx-serialization' shadowJar { baseName = 'danger-kotlin' diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/BitBucketServer.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/BitBucketServer.kt index 422710f1..2b8d543a 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/BitBucketServer.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/BitBucketServer.kt @@ -1,7 +1,6 @@ package systems.danger.kotlin -import com.squareup.moshi.Json -import com.squareup.moshi.JsonClass +import kotlinx.serialization.* /** * The BitBucket server data for your pull request. @@ -10,57 +9,33 @@ import com.squareup.moshi.JsonClass * @property commits The commits associated with the pull request * @property comments The comments on the pull request * @property activities The activities such as OPENING, CLOSING, MERGING or UPDATING a pull request -*/ -@JsonClass(generateAdapter = true) + */ +@Serializable data class BitBucketServer( val metadata: BitBucketServerMetadata, - @Json(name = "pr") - val pullRequest: BitBucketServerPR, - val commits: Array, - val comments: Array, - val activities: Array -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as BitBucketServer - - if (metadata != other.metadata) return false - if (pullRequest != other.pullRequest) return false - if (!commits.contentEquals(other.commits)) return false - if (!comments.contentEquals(other.comments)) return false - if (!activities.contentEquals(other.activities)) return false - - return true - } - - override fun hashCode(): Int { - var result = metadata.hashCode() - result = 31 * result + pullRequest.hashCode() - result = 31 * result + commits.contentHashCode() - result = 31 * result + comments.contentHashCode() - result = 31 * result + activities.contentHashCode() - return result - } -} + @SerialName("pr") + val pullRequest: BitBucketServerPR, + val commits: List, + val comments: List, + val activities: List +) /** * Defines and activity such as OPENING, CLOSING, MERGING or UPDATING a pull request * @property id The activity's ID - * @property createdAt Date activity created as number of mili seconds since the unix epoch + * @property createdAt Date activity created as number of milli seconds since the unix epoch * @property user The user that triggered the activity. * @property action The action the activity describes (e.g. "COMMENTED"). * @property commentAction In case the action was "COMMENTED" it will state the command specific action (e.g. "CREATED"). */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerActivity( val id: Int, - @Json(name = "createdDate") - val createdAt: Long, + @SerialName("createdDate") + val createdAt: Long, val user: BitBucketServerUser, val action: String, - val commentAction: String? + val commentAction: String? = null ) /** @@ -68,13 +43,13 @@ data class BitBucketServerActivity( * @property pullRequestId The PR's ID * @property repoSlug The complete repo slug including project slug. */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerMetadata internal constructor( val pullRequestID: String, val repoSlug: String ) -@JsonClass(generateAdapter = true) +@Serializable internal data class BitBucketServerEnv( val pr: String, val repo: String @@ -93,19 +68,19 @@ internal data class BitBucketServerEnv( * @property commentAction Action the user did (e.g. "ADDED") if it is a new task. * @property comment Detailed data of the comment. */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerComment( val id: Int, - @Json(name = "createdDate") + @SerialName("createdDate") val createdAt: Long, val user: BitBucketServerUser, val action: String, - val fromHash: String?, - val previousFromHash: String?, - val toHash: String?, - val previousToHash: String?, - val commentAction: String?, - val comment: BitBucketServerCommentDetail? + val fromHash: String? = null, + val previousFromHash: String? = null, + val toHash: String? = null, + val previousToHash: String? = null, + val commentAction: String? = null, + val comment: BitBucketServerCommentDetail? = null ) /** @@ -120,52 +95,21 @@ data class BitBucketServerComment( * @property properties Properties associated with the comment * @property tasks Tasks associated with the comment */ -@JsonClass(generateAdapter = true) + +@Serializable data class BitBucketServerCommentDetail( val id: Int, val version: Int, val text: String, val author: BitBucketServerUser, - @Json(name = "createdDate") + @SerialName("createdDate") val createdAt: Long, - @Json(name = "updatedDate") + @SerialName("updatedDate") val updatedAt: Long, - val comments: Array, + val comments: List, val properties: BitBucketServerCommentInnerProperties, - val tasks: Array -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as BitBucketServerCommentDetail - - if (id != other.id) return false - if (version != other.version) return false - if (text != other.text) return false - if (author != other.author) return false - if (createdAt != other.createdAt) return false - if (updatedAt != other.updatedAt) return false - if (!comments.contentEquals(other.comments)) return false - if (properties != other.properties) return false - if (!tasks.contentEquals(other.tasks)) return false - - return true - } - - override fun hashCode(): Int { - var result = id - result = 31 * result + version - result = 31 * result + text.hashCode() - result = 31 * result + author.hashCode() - result = 31 * result + createdAt.hashCode() - result = 31 * result + updatedAt.hashCode() - result = 31 * result + comments.contentHashCode() - result = 31 * result + properties.hashCode() - result = 31 * result + tasks.contentHashCode() - return result - } -} + val tasks: List +) /** * Task associated with a comment @@ -175,14 +119,14 @@ data class BitBucketServerCommentDetail( * @property state The state of the task (e.g. "OPEN") * @property author The author of the comment */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerCommentTask( - val id: Int, - @Json(name = "createdDate") - val createdAt: Long, - val text: String, - val state: String, - val author: BitBucketServerUser + val id: Int, + @SerialName("createdDate") + val createdAt: Long, + val text: String, + val state: String, + val author: BitBucketServerUser ) /** @@ -190,34 +134,11 @@ data class BitBucketServerCommentTask( * @property repositoryId The ID of the repo * @property issues Slugs of linkd Jira issues */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerCommentInnerProperties( - val repositoryId: Int, - val issues: Array? -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as BitBucketServerCommentInnerProperties - - if (repositoryId != other.repositoryId) return false - if (issues != null) { - if (other.issues == null) return false - if (!issues.contentEquals(other.issues)) return false - } else { - return other.issues != null - } - - return true - } - - override fun hashCode(): Int { - var result = repositoryId - result = 31 * result + (issues?.contentHashCode() ?: 0) - return result - } -} + val repositoryId: Int, + val issues: List = listOf() +) /** * A BitBucket server commit @@ -230,57 +151,27 @@ data class BitBucketServerCommentInnerProperties( * @property message The commit's message * @property parents The commit's parents */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerCommit( val id: String, val displayId: String, val author: BitBucketServerUser, val authorTimestamp: Long, - val committer: BitBucketServerUser?, + val committer: BitBucketServerUser? = null, val committerTimestamp: Long, val message: String, - val parents: Array -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as BitBucketServerCommit - - if (id != other.id) return false - if (displayId != other.displayId) return false - if (author != other.author) return false - if (authorTimestamp != other.authorTimestamp) return false - if (committer != other.committer) return false - if (committerTimestamp != other.committerTimestamp) return false - if (message != other.message) return false - if (!parents.contentEquals(other.parents)) return false - - return true - } - - override fun hashCode(): Int { - var result = id.hashCode() - result = 31 * result + displayId.hashCode() - result = 31 * result + author.hashCode() - result = 31 * result + authorTimestamp.hashCode() - result = 31 * result + (committer?.hashCode() ?: 0) - result = 31 * result + committerTimestamp.hashCode() - result = 31 * result + message.hashCode() - result = 31 * result + parents.contentHashCode() - return result - } -} + val parents: List +) /** * A commit's parent * @property id The SHA for the commit * @property displayId The shortened SHA for the commit */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerCommitParent( - val id: String, - val displayId: String + val id: String, + val displayId: String ) /** @@ -301,75 +192,36 @@ data class BitBucketServerCommitParent( * @property reviewers People requested as reviewers * @property participants People who have participated in the PR */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerPR( val id: Int, val version: Int, val title: String, - val description: String?, - val state: String, + val description: String? = null, + val state: State, val open: Boolean, val closed: Boolean, - @Json(name = "createdDate") - val createdAt: Long, - @Json(name = "updatedDate") - val updatedAt: Long, + @SerialName("createdDate") + val createdAt: Long, + @SerialName("updatedDate") + val updatedAt: Long, val fromRef: BitBucketServerMergeRef, val toRef: BitBucketServerMergeRef, - @Json(name = "locked") - val isLocked: Boolean, + @SerialName("locked") + val isLocked: Boolean, val author: BitBucketServerParticipant, - val reviewers: Array, - val participants: Array + val reviewers: List, + val participants: List ) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as BitBucketServerPR - - if (id != other.id) return false - if (version != other.version) return false - if (title != other.title) return false - if (description != other.description) return false - if (state != other.state) return false - if (open != other.open) return false - if (closed != other.closed) return false - if (createdAt != other.createdAt) return false - if (updatedAt != other.updatedAt) return false - if (fromRef != other.fromRef) return false - if (toRef != other.toRef) return false - if (isLocked != other.isLocked) return false - if (author != other.author) return false - if (!reviewers.contentEquals(other.reviewers)) return false - if (!participants.contentEquals(other.participants)) return false - - return true - } - - override fun hashCode(): Int { - var result = id - result = 31 * result + version - result = 31 * result + title.hashCode() - result = 31 * result + (description?.hashCode() ?: 0) - result = 31 * result + state.hashCode() - result = 31 * result + open.hashCode() - result = 31 * result + closed.hashCode() - result = 31 * result + createdAt.hashCode() - result = 31 * result + updatedAt.hashCode() - result = 31 * result + fromRef.hashCode() - result = 31 * result + toRef.hashCode() - result = 31 * result + isLocked.hashCode() - result = 31 * result + author.hashCode() - result = 31 * result + reviewers.contentHashCode() - result = 31 * result + participants.contentHashCode() - return result + @Serializable + enum class State { + OPEN, MERGED, SUPERSEDED, DECLINED } } -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerParticipant( - val user: BitBucketServerUser + val user: BitBucketServerUser ) /** @@ -377,8 +229,8 @@ data class BitBucketServerParticipant( * @property user The BitBucket Server user. * @property approved The approval status. * @property lastReviewedCommit The commit SHA for the latest commit that was reviewed. -*/ -@JsonClass(generateAdapter = true) + */ +@Serializable data class BitBucketServerReviewer( val user: BitBucketServerUser, val approved: Boolean, @@ -392,12 +244,12 @@ data class BitBucketServerReviewer( * @property latestCommit The SHA for the latest commit * @property repository Info of the associated repository */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerMergeRef( - val id: String, - val displayId: String, - val latestCommit: String, - val repository: BitBucketServerRepo + val id: String, + val displayId: String, + val latestCommit: String, + val repository: BitBucketServerRepo ) /** @@ -409,15 +261,16 @@ data class BitBucketServerMergeRef( * @property forkable Can someone fork thie repo? * @property project An abtraction for grouping repos */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerRepo( - val name: String?, - val slug: String, - val scmId: String, - @Json(name = "public") - val isPublic: Boolean, - val forkable: Boolean, - val project: BitBucketServerProject + val name: String? = null, + val slug: String, + val scmId: String, + @SerialName("public") + val isPublic: Boolean, + @SerialName("forkable") + val isForkable: Boolean, + val project: BitBucketServerProject ) /** @@ -428,12 +281,12 @@ data class BitBucketServerRepo( * @property isPublic Is the project publicly available * @property type The project's type */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerProject( val id: Int, val key: String, val name: String, - @Json(name = "public") + @SerialName("public") val isPublic: Boolean, val type: String ) @@ -448,13 +301,18 @@ data class BitBucketServerProject( * @property slug The user's slug for URLs * @property type The type of a user, "NORMAL" being a typical user3 */ -@JsonClass(generateAdapter = true) +@Serializable data class BitBucketServerUser( - val id: Int?, - val name: String, - val displayName: String?, - val emailAddress: String, - val active: Boolean?, - val slug: String?, - val type: String? -) \ No newline at end of file + val id: Int? = null, + val name: String, + val displayName: String? = null, + val emailAddress: String, + val active: Boolean = false, + val slug: String? = null, + val type: Type = Type.NORMAL +) { + @Serializable + enum class Type { + NORMAL, SERVICE + } +} \ No newline at end of file diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerDSL.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerDSL.kt index 22b81e6d..07dbc9ae 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerDSL.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerDSL.kt @@ -1,34 +1,36 @@ package systems.danger.kotlin -import com.squareup.moshi.Json - +import kotlinx.serialization.* +@Serializable data class DSL( val danger: DangerDSL ) +@Serializable data class DangerDSL( - @Json(name ="github") - private val _github: GitHub?, - @Json(name ="bitbucket_server") - private val _bitBucketServer: BitBucketServer?, - @Json(name ="gitlab") - private val _gitlab: GitLab?, + @SerialName("github") + private val _github: GitHub? = null, + @SerialName("bitbucket_server") + private val _bitBucketServer: BitBucketServer? = null, + @SerialName("gitlab") + private val _gitlab: GitLab? = null, val git: Git ) { - val github: GitHub - get() = _github!! - val bitBucketServer: BitBucketServer - get() = _bitBucketServer!! - val gitlab: GitLab - get() = _gitlab!! + val github: GitHub + get() = _github!! + val bitBucketServer: BitBucketServer + get() = _bitBucketServer!! + val gitlab: GitLab + get() = _gitlab!! - val onGitHub - get() = _github != null - val onBitBucketServer - get() = _bitBucketServer != null - val onGitLab - get() = _gitlab != null + val onGitHub + get() = _github != null + val onBitBucketServer + get() = _bitBucketServer != null + val onGitLab + get() = _gitlab != null - val utils = Utils() + val utils: Utils + get() = Utils() } diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerResults.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerResults.kt index 29a640ee..cb344ec2 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerResults.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerResults.kt @@ -1,38 +1,23 @@ +@file:UseSerializers(ViolationSerializer::class) + package systems.danger.kotlin +import kotlinx.serialization.Serializable +import kotlinx.serialization.UseSerializers import systems.danger.kotlin.sdk.Violation +import systems.danger.kotlin.serializers.ViolationSerializer +@Serializable data class Meta( val runtimeName: String = "Danger Kotlin", val runtimeHref: String = "https://danger.systems" ) +@Serializable internal data class DangerResults( - var fails: Array = arrayOf(), - var warnings: Array = arrayOf(), - var messages: Array = arrayOf(), - var markdowns: Array = arrayOf(), + var fails: MutableList = mutableListOf(), + var warnings: MutableList = mutableListOf(), + var messages: MutableList = mutableListOf(), + var markdowns: MutableList = mutableListOf(), val meta: Meta = Meta() -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as DangerResults - - if (!fails.contentEquals(other.fails)) return false - if (!warnings.contentEquals(other.warnings)) return false - if (!messages.contentEquals(other.messages)) return false - if (!markdowns.contentEquals(other.markdowns)) return false - - return true - } - - override fun hashCode(): Int { - var result = fails.contentHashCode() - result = 31 * result + warnings.contentHashCode() - result = 31 * result + messages.contentHashCode() - result = 31 * result + markdowns.contentHashCode() - return result - } -} +) \ No newline at end of file diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerRunner.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerRunner.kt index 1ec6fbb6..b207f2e2 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerRunner.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/DangerRunner.kt @@ -1,54 +1,15 @@ package systems.danger.kotlin -import com.squareup.moshi.JsonAdapter -import com.squareup.moshi.JsonReader -import com.squareup.moshi.JsonWriter -import com.squareup.moshi.Moshi -import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json import systems.danger.kotlin.sdk.DangerContext import systems.danger.kotlin.sdk.DangerPlugin import systems.danger.kotlin.sdk.Violation import java.io.File -import java.text.ParseException -import java.text.SimpleDateFormat -import java.util.* private fun FilePath.readText() = File(this).readText() -fun fromISO8601UTC(dateStr: String): Date? { - val tz = TimeZone.getTimeZone("UTC") - val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") - df.timeZone = tz - - val alternativeDf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'") - alternativeDf.timeZone = tz - - try { - return df.parse(dateStr) - } catch (e: ParseException) { - try { - return alternativeDf.parse(dateStr) - } catch (e2: ParseException) { - e.printStackTrace() - e2.printStackTrace() - } - } - - return null -} - -class Rfc3339DateJsonAdapter : JsonAdapter() { - @Synchronized - override fun fromJson(reader: JsonReader): Date { - val string = reader.nextString() - return fromISO8601UTC(string)!! - } - - override fun toJson(writer: JsonWriter, value: Date?) { - //Implementation not needed right now - } -} - object register { internal var dangerPlugins = mutableListOf() @@ -95,7 +56,10 @@ private class DangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: FilePath val jsonOutputFile: File = File(jsonOutputPath) - val danger: DangerDSL + val danger: DangerDSL = Json { + ignoreUnknownKeys = true + isLenient = true + }.decodeFromString(jsonInputFilePath.readText()).danger val dangerResults: DangerResults = DangerResults() @@ -116,13 +80,7 @@ private class DangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: FilePath return dangerResults.markdowns.toList() } - private val moshi = Moshi.Builder() - .add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) - .add(KotlinJsonAdapterFactory()) - .build() - init { - this.danger = moshi.adapter(DSL::class.java).fromJson(jsonInputFilePath.readText())!!.danger register.dangerPlugins.forEach { it.withContext(this) @@ -202,27 +160,29 @@ private class DangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: FilePath } private fun warn(violation: Violation) { - dangerResults.warnings += (violation) + dangerResults.warnings.add(violation) saveDangerResults() } private fun fail(violation: Violation) { - dangerResults.fails += violation + dangerResults.fails.add(violation) saveDangerResults() } private fun message(violation: Violation) { - dangerResults.messages += violation + dangerResults.messages.add(violation) saveDangerResults() } private fun markdown(violation: Violation) { - dangerResults.markdowns += violation + dangerResults.markdowns.add(violation) saveDangerResults() } private fun saveDangerResults() { - val resultsJSON = moshi.adapter(DangerResults::class.java).toJson(dangerResults) + val resultsJSON = Json { + encodeDefaults = true + }.encodeToString(dangerResults) jsonOutputFile.writeText(resultsJSON) } } diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/Git.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/Git.kt index 299823b7..6d8df1eb 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/Git.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/Git.kt @@ -1,7 +1,6 @@ package systems.danger.kotlin -import com.squareup.moshi.Json -import com.squareup.moshi.JsonClass +import kotlinx.serialization.* typealias FilePath = String @@ -12,33 +11,13 @@ typealias FilePath = String * @property createdFiles Newly created file paths relative to the git root. * @property deletedFiles Removed file paths relative to the git root. */ -@JsonClass(generateAdapter = true) +@Serializable data class Git( - @Json(name = "modified_files") val modifiedFiles: Array, - @Json(name = "created_files") val createdFiles: Array, - @Json(name = "deleted_files") val deletedFiles: Array, - @Json(name = "commits") val commits: List -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as Git - - if (!modifiedFiles.contentEquals(other.modifiedFiles)) return false - if (!createdFiles.contentEquals(other.createdFiles)) return false - if (!deletedFiles.contentEquals(other.deletedFiles)) return false - - return true - } - - override fun hashCode(): Int { - var result = modifiedFiles.contentHashCode() - result = 31 * result + createdFiles.contentHashCode() - result = 31 * result + deletedFiles.contentHashCode() - return result - } -} + @SerialName("modified_files") val modifiedFiles: List, + @SerialName("created_files") val createdFiles: List, + @SerialName("deleted_files") val deletedFiles: List, + @SerialName("commits") val commits: List +) /** * A platform agnostic reference to a git commit. @@ -50,44 +29,15 @@ data class Git( * @property parents SHAs for the commit's parents. * @property url The URL for the commit. */ -@JsonClass(generateAdapter = true) +@Serializable data class GitCommit( - val sha: String?, + val sha: String? = null, val author: GitCommitAuthor, val committer: GitCommitAuthor, val message: String, - val parents: Array?, + val parents: List? = null, val url: String -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as GitCommit - - if (sha != other.sha) return false - if (author != other.author) return false - if (committer != other.committer) return false - if (message != other.message) return false - if (parents != null) { - if (other.parents == null) return false - if (!parents.contentEquals(other.parents)) return false - } else if (other.parents != null) return false - if (url != other.url) return false - - return true - } - - override fun hashCode(): Int { - var result = sha.hashCode() - result = 31 * result + author.hashCode() - result = 31 * result + committer.hashCode() - result = 31 * result + message.hashCode() - result = 31 * result + (parents?.contentHashCode() ?: 0) - result = 31 * result + url.hashCode() - return result - } -} +) /** * A platform agnostic reference to a git commit. @@ -96,7 +46,7 @@ data class GitCommit( * @property email The email for the author. * @property date The ISO8601 date string for the commit. */ -@JsonClass(generateAdapter = true) +@Serializable data class GitCommitAuthor( val name: String, val email: String, diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/GitHub.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/GitHub.kt index 895f168a..ee847775 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/GitHub.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/GitHub.kt @@ -1,54 +1,36 @@ +@file:UseSerializers(DateSerializer::class) + package systems.danger.kotlin -import com.squareup.moshi.Json -import com.squareup.moshi.JsonClass +import systems.danger.kotlin.serializers.DateSerializer +import kotlinx.serialization.* import java.util.* /** * The GitHub metadata for your pull request. */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHub( val issue: GitHubIssue, - @Json(name = "pr") val pullRequest: GitHubPR, - val commits: Array, - val reviews: Array, - @Json(name = "requested_reviewers") val requestedReviewers: GitHubRequestedReviewers -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as GitHub + @SerialName("pr") val pullRequest: GitHubPR, + val commits: List, + val reviews: List, + @SerialName("requested_reviewers") val requestedReviewers: GitHubRequestedReviewers +) - if (issue != other.issue) return false - if (pullRequest != other.pullRequest) return false - if (!commits.contentEquals(other.commits)) return false - if (!reviews.contentEquals(other.reviews)) return false - if (requestedReviewers != other.requestedReviewers) return false +@Serializable +enum class GitHubPullRequestState { + @SerialName("closed") + CLOSED, - return true - } + @SerialName("open") + OPEN, - override fun hashCode(): Int { - var result = issue.hashCode() - result = 31 * result + pullRequest.hashCode() - result = 31 * result + commits.contentHashCode() - result = 31 * result + reviews.contentHashCode() - result = 31 * result + requestedReviewers.hashCode() - return result - } -} + @SerialName("merged") + MERGED, -enum class GitHubPullRequestState(val value: String) { - @Json(name = "closed") - CLOSED("closed"), - @Json(name = "open") - OPEN("open"), - @Json(name = "merged") - MERGED("merged"), - @Json(name = "locked") - LOCKED("locked") + @SerialName("locked") + LOCKED } /** @@ -78,92 +60,32 @@ enum class GitHubPullRequestState(val value: String) { * @property milestone The milestone of the pull request * @property htmlURL The link back to this PR as user-facing */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubPR( val number: Int, val title: String, val body: String, val user: GitHubUser, val assignee: GitHubUser?, - val assignees: Array, - @Json(name = "created_at") val createdAt: Date, - @Json(name = "updated_at") val updatedAt: Date, - @Json(name = "closed_at") val closedAt: Date?, - @Json(name = "merged_at") val mergedAt: Date?, + val assignees: List, + @SerialName("created_at") val createdAt: Date, + @SerialName("updated_at") val updatedAt: Date, + @SerialName("closed_at") val closedAt: Date? = null, + @SerialName("merged_at") val mergedAt: Date? = null, val head: GitHubMergeRef, val base: GitHubMergeRef, val state: GitHubPullRequestState, - @Json(name = "locked") val isLocked: Boolean, - @Json(name = "merged") val isMerged: Boolean?, - @Json(name = "commits") val commitCount: Int?, - @Json(name = "comments") val commentCount: Int?, - @Json(name = "review_comments") val reviewCommentCount: Int?, + @SerialName("locked") val isLocked: Boolean, + @SerialName("merged") val isMerged: Boolean?, + @SerialName("commits") val commitCount: Int?, + @SerialName("comments") val commentCount: Int?, + @SerialName("review_comments") val reviewCommentCount: Int?, val additions: Int?, val deletions: Int?, - @Json(name = "changed_files") val changedFiles: Int?, + @SerialName("changed_files") val changedFiles: Int?, val milestone: GitHubMilestone?, - @Json(name = "html_url") val htmlURL: String -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as GitHubPR - - if (number != other.number) return false - if (title != other.title) return false - if (body != other.body) return false - if (user != other.user) return false - if (assignee != other.assignee) return false - if (!assignees.contentEquals(other.assignees)) return false - if (createdAt != other.createdAt) return false - if (updatedAt != other.updatedAt) return false - if (closedAt != other.closedAt) return false - if (mergedAt != other.mergedAt) return false - if (head != other.head) return false - if (base != other.base) return false - if (state != other.state) return false - if (isLocked != other.isLocked) return false - if (isMerged != other.isMerged) return false - if (commitCount != other.commitCount) return false - if (commentCount != other.commentCount) return false - if (reviewCommentCount != other.reviewCommentCount) return false - if (additions != other.additions) return false - if (deletions != other.deletions) return false - if (changedFiles != other.changedFiles) return false - if (milestone != other.milestone) return false - if (htmlURL != other.htmlURL) return false - - return true - } - - override fun hashCode(): Int { - var result = number - result = 31 * result + title.hashCode() - result = 31 * result + body.hashCode() - result = 31 * result + user.hashCode() - result = 31 * result + (assignee?.hashCode() ?: 0) - result = 31 * result + assignees.contentHashCode() - result = 31 * result + createdAt.hashCode() - result = 31 * result + updatedAt.hashCode() - result = 31 * result + closedAt.hashCode() - result = 31 * result + mergedAt.hashCode() - result = 31 * result + head.hashCode() - result = 31 * result + base.hashCode() - result = 31 * result + state.hashCode() - result = 31 * result + isLocked.hashCode() - result = 31 * result + (isMerged?.hashCode() ?: 0) - result = 31 * result + (commitCount ?: 0) - result = 31 * result + (commentCount ?: 0) - result = 31 * result + (reviewCommentCount ?: 0) - result = 31 * result + (additions ?: 0) - result = 31 * result + (deletions ?: 0) - result = 31 * result + (changedFiles ?: 0) - result = 31 * result + (milestone?.hashCode() ?: 0) - result = 31 * result + htmlURL.hashCode() - return result - } -} + @SerialName("html_url") val htmlURL: String +) /** * A GitHub team @@ -171,7 +93,7 @@ data class GitHubPR( * @property id The UUID for the team. * @property name The team name. */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubTeam( val id: Long, val name: String @@ -183,29 +105,11 @@ data class GitHubTeam( * @property users The list of users of whom a review has been requested.. * @property teams The list of teams of whom a review has been requested. */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubRequestedReviewers( - val users: Array, - val teams: Array -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as GitHubRequestedReviewers - - if (!users.contentEquals(other.users)) return false - if (!teams.contentEquals(other.teams)) return false - - return true - } - - override fun hashCode(): Int { - var result = users.contentHashCode() - result = 31 * result + teams.contentHashCode() - return result - } -} + val users: List, + val teams: List +) /** * Represents a branch in PR @@ -216,7 +120,7 @@ data class GitHubRequestedReviewers( * @property user The user that owns the merge reference e.g. "artsy". * @property repo The repo from which the reference comes from. */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubMergeRef( val label: String, val ref: String, @@ -236,28 +140,28 @@ data class GitHubMergeRef( * @property isFork A boolean stating whether the repo is a fork. * @property htmlURL The root web URL for the repo, e.g. https://github.com/artsy/emission */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubRepo( val id: Long, val name: String, - @Json(name = "full_name") val fullName: String, - @Json(name = "private") val isPrivate: Boolean, + @SerialName("full_name") + val fullName: String, + @SerialName("private") + val isPrivate: Boolean, val description: String?, - @Json(name = "fork") val isFork: Boolean, - @Json(name = "html_url") val htmlURL: String + @SerialName("fork") + val isFork: Boolean, + @SerialName("html_url") + val htmlURL: String ) -enum class GitHubReviewState(val value: String) { - @Json(name = "APPROVED") - APPROVED("APPROVED"), - @Json(name = "CHANGES_REQUESTED") - CHANGES_REQUESTED("CHANGES_REQUESTED"), - @Json(name = "COMMENTED") - COMMENTED("COMMENTED"), - @Json(name = "PENDING") - PENDING("PENDING"), - @Json(name = "DISMISSED") - DISMISSED("DISMISSED") +@Serializable +enum class GitHubReviewState { + APPROVED, + CHANGES_REQUESTED, + COMMENTED, + PENDING, + DISMISSED } /** @@ -269,12 +173,12 @@ enum class GitHubReviewState(val value: String) { * @property commitId The commit ID the review was made on (if a review was made). * @property state The state of the review (if a review was made). */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubReview( val user: GitHubUser, val id: Long?, val body: String?, - @Json(name = "commit_id") val commitId: String?, + @SerialName("commit_id") val commitId: String?, val state: GitHubReviewState? ) @@ -287,7 +191,7 @@ data class GitHubReview( * @property commit The raw commit metadata. * @property committer The GitHub user who shipped the code. */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubCommit( val sha: String, val url: String, @@ -296,13 +200,16 @@ data class GitHubCommit( val committer: GitHubUser? ) -enum class GitHubIssueState(val value: String) { - @Json(name = "closed") - CLOSED("closed"), - @Json(name = "open") - OPEN("open"), - @Json(name = "locked") - LOCKED("locked") +@Serializable +enum class GitHubIssueState { + @SerialName("closed") + CLOSED, + + @SerialName("open") + OPEN, + + @SerialName("locked") + LOCKED } /** @@ -331,68 +238,24 @@ enum class GitHubIssueState(val value: String) { * @property changedFiles The number of files changed in the pull request. * @property milestone The milestone of the pull request */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubIssue( val id: Long, val number: Int, val title: String, val user: GitHubUser, val state: GitHubIssueState, - @Json(name = "locked") val isLocked: Boolean, + @SerialName("locked") val isLocked: Boolean, val body: String, - @Json(name = "comments") val commentCount: Int, + @SerialName("comments") val commentCount: Int, val assignee: GitHubUser?, - val assignees: Array, + val assignees: List, val milestone: GitHubMilestone?, - @Json(name = "created_at") val createdAt: Date, - @Json(name = "updated_at") val updatedAt: Date, - @Json(name = "closed_at") val closedAt: Date?, - val labels: Array -) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as GitHubIssue - - if (id != other.id) return false - if (number != other.number) return false - if (title != other.title) return false - if (user != other.user) return false - if (state != other.state) return false - if (isLocked != other.isLocked) return false - if (body != other.body) return false - if (commentCount != other.commentCount) return false - if (assignee != other.assignee) return false - if (!assignees.contentEquals(other.assignees)) return false - if (milestone != other.milestone) return false - if (createdAt != other.createdAt) return false - if (updatedAt != other.updatedAt) return false - if (closedAt != other.closedAt) return false - if (!labels.contentEquals(other.labels)) return false - - return true - } - - override fun hashCode(): Int { - var result = id.hashCode() - result = 31 * result + number - result = 31 * result + title.hashCode() - result = 31 * result + user.hashCode() - result = 31 * result + state.hashCode() - result = 31 * result + isLocked.hashCode() - result = 31 * result + body.hashCode() - result = 31 * result + commentCount - result = 31 * result + (assignee?.hashCode() ?: 0) - result = 31 * result + assignees.contentHashCode() - result = 31 * result + milestone.hashCode() - result = 31 * result + createdAt.hashCode() - result = 31 * result + updatedAt.hashCode() - result = 31 * result + closedAt.hashCode() - result = 31 * result + labels.contentHashCode() - return result - } -} + @SerialName("created_at") val createdAt: Date, + @SerialName("updated_at") val updatedAt: Date, + @SerialName("closed_at") val closedAt: Date? = null, + val labels: List +) /** * @property id The id number of this label. @@ -400,20 +263,23 @@ data class GitHubIssue( * @property name The name of the label. * @property color TThe color associated with this label. */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubIssueLabel( - val id: Long, - val url: String, - val name: String, - val color: String + val id: Long, + val url: String, + val name: String, + val color: String ) +@Serializable enum class GitHubUserType { - @Json(name = "User") + @SerialName("User") USER, - @Json(name = "Organization") + + @SerialName("Organization") ORGANIZATION, - @Json(name = "Bot") + + @SerialName("Bot") BOT } @@ -424,22 +290,25 @@ enum class GitHubUserType { * @property login The handle for the user or organization. * @property type The type of user: user or organization. */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubUser( val id: Long, val login: String, val type: GitHubUserType, - @Json(name="avatar_url") + @SerialName("avatar_url") val avatarUrl: String ) -enum class GitHubMilestoneState(val value: String) { - @Json(name = "close") - CLOSE("close"), - @Json(name = "open") - OPEN("open"), - @Json(name = "all") - ALL("all") +@Serializable +enum class GitHubMilestoneState { + @SerialName("close") + CLOSE, + + @SerialName("open") + OPEN, + + @SerialName("all") + ALL } /** @@ -458,18 +327,18 @@ enum class GitHubMilestoneState(val value: String) { * @property closedAt The date for when the milestone was closed. * @property dueOn The date for the due of this milestone. */ -@JsonClass(generateAdapter = true) +@Serializable data class GitHubMilestone( val id: Long, val number: Int, val state: GitHubMilestoneState, val title: String, - val description: String?, + val description: String? = null, val creator: GitHubUser, - @Json(name = "open_issues") val openIssues: Int, - @Json(name = "closed_issues") val closedIssues: Int, - @Json(name = "created_at") val createdAt: Date, - @Json(name = "updated_at") val updatedAt: Date, - @Json(name = "closed_at") val closedAt: Date?, - @Json(name = "due_on") val dueOn: Date? + @SerialName("open_issues") val openIssues: Int, + @SerialName("closed_issues") val closedIssues: Int, + @SerialName("created_at") val createdAt: Date, + @SerialName("updated_at") val updatedAt: Date, + @SerialName("closed_at") val closedAt: Date? = null, + @SerialName("due_on") val dueOn: Date? = null ) diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/GitLab.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/GitLab.kt index e444d3ec..e87bff5b 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/GitLab.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/GitLab.kt @@ -1,285 +1,221 @@ +@file:UseSerializers(DateSerializer::class) + package systems.danger.kotlin -import com.squareup.moshi.Json +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.UseSerializers +import systems.danger.kotlin.serializers.DateSerializer import java.util.* +@Serializable data class GitLab( - @Json(name="mr") + @SerialName("mr") val mergeRequest: GitLabMergeRequest, val metadata: GitLabMetadata ) -data class GitLabDiffRefs ( - @Json(name="base_sha") +@Serializable +data class GitLabDiffRefs( + @SerialName("base_sha") val baseSha: String, - @Json(name="head_sha") + @SerialName("head_sha") val headSha: String, - @Json(name="start_sha") + @SerialName("start_sha") val startSha: String ) +@Serializable data class GitLabUserMergeData( - @Json(name="can_merge") + @SerialName("can_merge") val canMerge: Boolean ) +@Serializable data class GitLabMergeRequest( - @Json(name="allow_collaboration") - val allowCollaboration: Boolean?, - @Json(name="allow_maintainer_to_push") - val allowMaintainerToPush: Boolean?, - @Json(name="approvals_before_merge") + @SerialName("allow_collaboration") + val allowCollaboration: Boolean = false, + @SerialName("allow_maintainer_to_push") + val allowMaintainerToPush: Boolean = false, + @SerialName("approvals_before_merge") val approvalsBeforeMerge: Int?, val assignee: GitLabUser?, val author: GitLabUser, - @Json(name="changes_count") + @SerialName("changes_count") val changesCount: String, - @Json(name="closed_at") - val closedAt: Date?, - @Json(name="closed_by") + @SerialName("closed_at") + val closedAt: Date? = null, + @SerialName("closed_by") val closedBy: GitLabUser?, val description: String, - @Json(name="diff_refs") + @SerialName("diff_refs") val diffRefs: GitLabDiffRefs, val downvotes: Int, - @Json(name="first_deployed_to_production_at") - val firstDeployedToProductionAt: Date?, - @Json(name="force_remove_source_branch") + @SerialName("first_deployed_to_production_at") + val firstDeployedToProductionAt: Date? = null, + @SerialName("force_remove_source_branch") val forceRemoveSourceBranch: Boolean, val id: Int, val iid: Int, - @Json(name="latest_build_finished_at") - val latestBuildFinishedAt: Date?, - @Json(name="latest_build_started_at") - val latestBuildStartedAt: Date?, - val labels: Array, - @Json(name="merge_commit_sha") - val mergeCommitSha: String?, - @Json(name="merged_at") - val mergedAt: Date?, - @Json(name="merged_by") + @SerialName("latest_build_finished_at") + + val latestBuildFinishedAt: Date? = null, + @SerialName("latest_build_started_at") + + val latestBuildStartedAt: Date? = null, + val labels: List, + @SerialName("merge_commit_sha") + val mergeCommitSha: String? = null, + @SerialName("merged_at") + val mergedAt: Date? = null, + @SerialName("merged_by") val mergedBy: GitLabUser?, - @Json(name="merge_when_pipeline_succeeds") + @SerialName("merge_when_pipeline_succeeds") val mergeOnPipelineSuccess: Boolean, - val milestone: GitLabMilestone?, + val milestone: GitLabMilestone? = null, val pipeline: GitLabPipeline, - @Json(name="project_id") + @SerialName("project_id") val projectId: String, val sha: String, - @Json(name="should_remove_source_branch") - val shouldRemoveSourceBranch: Boolean?, - @Json(name="source_branch") + @SerialName("should_remove_source_branch") + val shouldRemoveSourceBranch: Boolean? = null, + @SerialName("source_branch") val sourceBranch: String, - @Json(name="source_project_id") + @SerialName("source_project_id") val sourceProjectId: String, val state: GitLabMergeRequestState, val subscribed: Boolean, - @Json(name="target_branch") + @SerialName("target_branch") val targetBranch: String, - @Json(name="target_project_id") + @SerialName("target_project_id") val targetProjectId: String, - val timeStats: GitLabMergeRequestTimeStats?, + val timeStats: GitLabMergeRequestTimeStats? = null, val title: String, val upvotes: Int, - @Json(name="user") + @SerialName("user") private val userMergeData: GitLabUserMergeData, - @Json(name="user_notes_count") + @SerialName("user_notes_count") val userNotesCount: Int, - @Json(name="web_url") + @SerialName("web_url") val webUrl: String, - @Json(name="work_in_progress") + @SerialName("work_in_progress") val workInProgress: Boolean ) { val canMerge: Boolean get() = this.userMergeData.canMerge - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as GitLabMergeRequest - - if (allowCollaboration != other.allowCollaboration) return false - if (allowMaintainerToPush != other.allowMaintainerToPush) return false - if (approvalsBeforeMerge != other.approvalsBeforeMerge) return false - if (assignee != other.assignee) return false - if (author != other.author) return false - if (changesCount != other.changesCount) return false - if (closedAt != other.closedAt) return false - if (closedBy != other.closedBy) return false - if (description != other.description) return false - if (diffRefs != other.diffRefs) return false - if (downvotes != other.downvotes) return false - if (firstDeployedToProductionAt != other.firstDeployedToProductionAt) return false - if (forceRemoveSourceBranch != other.forceRemoveSourceBranch) return false - if (id != other.id) return false - if (iid != other.iid) return false - if (latestBuildFinishedAt != other.latestBuildFinishedAt) return false - if (latestBuildStartedAt != other.latestBuildStartedAt) return false - if (!labels.contentEquals(other.labels)) return false - if (mergeCommitSha != other.mergeCommitSha) return false - if (mergedAt != other.mergedAt) return false - if (mergedBy != other.mergedBy) return false - if (mergeOnPipelineSuccess != other.mergeOnPipelineSuccess) return false - if (milestone != other.milestone) return false - if (pipeline != other.pipeline) return false - if (projectId != other.projectId) return false - if (sha != other.sha) return false - if (shouldRemoveSourceBranch != other.shouldRemoveSourceBranch) return false - if (sourceBranch != other.sourceBranch) return false - if (sourceProjectId != other.sourceProjectId) return false - if (state != other.state) return false - if (subscribed != other.subscribed) return false - if (targetBranch != other.targetBranch) return false - if (targetProjectId != other.targetProjectId) return false - if (timeStats != other.timeStats) return false - if (userMergeData != other.userMergeData) return false - if (title != other.title) return false - if (upvotes != other.upvotes) return false - if (userNotesCount != other.userNotesCount) return false - if (webUrl != other.webUrl) return false - if (workInProgress != other.workInProgress) return false - - return true - } - - override fun hashCode(): Int { - var result = allowCollaboration.hashCode() - result = 31 * result + allowMaintainerToPush.hashCode() - result = 31 * result + approvalsBeforeMerge.hashCode() - result = 31 * result + (assignee?.hashCode() ?: 0) - result = 31 * result + author.hashCode() - result = 31 * result + changesCount.hashCode() - result = 31 * result + closedAt.hashCode() - result = 31 * result + closedBy.hashCode() - result = 31 * result + description.hashCode() - result = 31 * result + diffRefs.hashCode() - result = 31 * result + downvotes - result = 31 * result + (firstDeployedToProductionAt?.hashCode() ?: 0) - result = 31 * result + forceRemoveSourceBranch.hashCode() - result = 31 * result + id - result = 31 * result + iid - result = 31 * result + latestBuildFinishedAt.hashCode() - result = 31 * result + latestBuildStartedAt.hashCode() - result = 31 * result + labels.contentHashCode() - result = 31 * result + (mergeCommitSha?.hashCode() ?: 0) - result = 31 * result + (mergedAt?.hashCode() ?: 0) - result = 31 * result + (mergedBy?.hashCode() ?: 0) - result = 31 * result + mergeOnPipelineSuccess.hashCode() - result = 31 * result + (milestone?.hashCode() ?: 0) - result = 31 * result + pipeline.hashCode() - result = 31 * result + projectId.hashCode() - result = 31 * result + sha.hashCode() - result = 31 * result + (shouldRemoveSourceBranch?.hashCode() ?: 0) - result = 31 * result + sourceBranch.hashCode() - result = 31 * result + sourceProjectId.hashCode() - result = 31 * result + state.hashCode() - result = 31 * result + subscribed.hashCode() - result = 31 * result + targetBranch.hashCode() - result = 31 * result + targetProjectId.hashCode() - result = 31 * result + timeStats.hashCode() - result = 31 * result + userMergeData.hashCode() - result = 31 * result + title.hashCode() - result = 31 * result + upvotes - result = 31 * result + userNotesCount - result = 31 * result + webUrl.hashCode() - result = 31 * result + workInProgress.hashCode() - return result - } } +@Serializable data class GitLabMergeRequestTimeStats( - @Json(name="human_time_estimate") + @SerialName("human_time_estimate") val humanTimeEstimate: Int?, - @Json(name="human_time_spent") + @SerialName("human_time_spent") val humanTimeSpent: Int?, - @Json(name="time_estimate") + @SerialName("time_estimate") val timeEstimate: Int, - @Json(name="total_time_spent") + @SerialName("total_time_spent") val totalTimeSpent: Int ) +@Serializable data class GitLabMetadata( val pullRequestID: String, val repoSlug: String ) +@Serializable enum class GitLabMergeRequestState { - @Json(name = "closed") - closed, - @Json(name = "locked") - locked, - @Json(name = "merged") - merged, - @Json(name = "opened") - opened + @SerialName("closed") + CLOSED, + + @SerialName("locked") + LOCKED, + + @SerialName("merged") + MERGED, + + @SerialName("opened") + OPENED } +@Serializable data class GitLabMilestone( - @Json(name="created_at") + @SerialName("created_at") val createdAt: Date, val description: String, - @Json(name="due_date") + @SerialName("due_date") val dueDate: Date, val id: Int, val iid: Int, - @Json(name="project_id") + @SerialName("project_id") val projectID: Int, - @Json(name="start_date") + @SerialName("start_date") val startDate: Date, val state: GitLabMilestoneState, val title: String, - @Json(name="updated_at") + @SerialName("updated_at") val updatedAt: Date, - @Json(name="web_url") + @SerialName("web_url") val webUrl: String ) +@Serializable enum class GitLabMilestoneState { - @Json(name = "active") - active, - @Json(name = "closed") - closed + @SerialName("active") + ACTIVE, + + @SerialName("closed") + CLOSED } +@Serializable data class GitLabPipeline( - val id: Int, - val ref: String, - val sha: String, - val status: GitLabPipelineStatus, - @Json(name="web_url") + val id: Int, + val ref: String, + val sha: String, + val status: GitLabPipelineStatus, + @SerialName("web_url") val webUrl: String ) +@Serializable enum class GitLabPipelineStatus { - @Json(name = "cancelled") - cancelled, - @Json(name = "failed") - failed, - @Json(name = "pending") - pending, - @Json(name = "running") - running, - @Json(name = "skipped") - skipped, - @Json(name = "success") - success + @SerialName("cancelled") + CANCELLED, + + @SerialName("failed") + FAILED, + + @SerialName("pending") + PENDING, + + @SerialName("running") + RUNNING, + + @SerialName("skipped") + SKIPPED, + + @SerialName("success") + SUCCESS } +@Serializable data class GitLabUser( - @Json(name="avatar_url") + @SerialName("avatar_url") val avatarUrl: String?, val id: Int, val name: String, val state: GitLabUserState, val username: String, - @Json(name="web_url") + @SerialName("web_url") val webUrl: String ) +@Serializable enum class GitLabUserState { - @Json(name = "active") - active, - @Json(name = "blocked") - blocked + @SerialName("active") + ACTIVE, + + @SerialName("blocked") + BLOCKED } \ No newline at end of file diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/serializers/DateSerializer.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/serializers/DateSerializer.kt new file mode 100644 index 00000000..ca182eca --- /dev/null +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/serializers/DateSerializer.kt @@ -0,0 +1,27 @@ +package systems.danger.kotlin.serializers + +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializer +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import java.util.* + +@ExperimentalSerializationApi +@Serializer(forClass = DateSerializer::class) +object DateSerializer : KSerializer { + + override val descriptor: SerialDescriptor + get() = PrimitiveSerialDescriptor("java.util.Date", PrimitiveKind.STRING) + + override fun serialize(encoder: Encoder, value: Date) { + // Implementation not needed for now + } + + override fun deserialize(decoder: Decoder): Date { + return fromISO8601UTC(decoder.decodeString())!! + } +} \ No newline at end of file diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/serializers/DateUtils.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/serializers/DateUtils.kt new file mode 100644 index 00000000..5ba0075b --- /dev/null +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/serializers/DateUtils.kt @@ -0,0 +1,27 @@ +package systems.danger.kotlin.serializers + +import java.text.ParseException +import java.text.SimpleDateFormat +import java.util.* + +internal fun fromISO8601UTC(dateStr: String): Date? { + val tz = TimeZone.getTimeZone("UTC") + val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") + df.timeZone = tz + + val alternativeDf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'") + alternativeDf.timeZone = tz + + try { + return df.parse(dateStr) + } catch (e: ParseException) { + try { + return alternativeDf.parse(dateStr) + } catch (e2: ParseException) { + e.printStackTrace() + e2.printStackTrace() + } + } + + return null +} \ No newline at end of file diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/serializers/ViolationSerializer.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/serializers/ViolationSerializer.kt new file mode 100644 index 00000000..3037a883 --- /dev/null +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/serializers/ViolationSerializer.kt @@ -0,0 +1,55 @@ +package systems.danger.kotlin.serializers + +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.descriptors.buildClassSerialDescriptor +import kotlinx.serialization.descriptors.element +import kotlinx.serialization.encoding.* +import systems.danger.kotlin.sdk.Violation + +@ExperimentalSerializationApi +@Serializer(forClass = ViolationSerializer::class) +object ViolationSerializer : KSerializer { + override val descriptor: SerialDescriptor + get() = buildClassSerialDescriptor( + "systems.danger.kotlin.sdk.Violation" + ) { + element("message") + element("file") + element("line") + } + + override fun deserialize(decoder: Decoder): Violation { + // Do not need implementation (we are only serialising + return decoder.decodeStructure(descriptor) { + lateinit var message: String + var file: String? = null + var line: Int? = null + while (true) { + when (val index = decodeElementIndex(descriptor)) { + 0 -> message = decodeStringElement(descriptor, 0) + 1 -> file = decodeStringElement(descriptor, 1) + 2 -> line = decodeIntElement(descriptor, 2) + CompositeDecoder.DECODE_DONE -> break + else -> error("Unexpected index: $index") + } + } + Violation(message, file, line) + } + } + + override fun serialize(encoder: Encoder, value: Violation) { + encoder.encodeStructure(descriptor) { + encodeStringElement(descriptor, 0, value.message) + value.file?.let { + encodeStringElement(descriptor, 1, it) + } + value.line?.let { + encodeIntElement(descriptor, 2, it) + } + } + } + +} diff --git a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/BitBucketServerParsingTests.kt b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/BitBucketServerParsingTests.kt index d03c8592..5890226d 100644 --- a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/BitBucketServerParsingTests.kt +++ b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/BitBucketServerParsingTests.kt @@ -1,19 +1,16 @@ package systems.danger.kotlin -import com.squareup.moshi.Moshi -import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory +import kotlinx.serialization.decodeFromString import org.junit.Assert.assertEquals import org.junit.Test -import java.util.* +import systems.danger.kotlin.utils.TestUtils.JSONFiles +import systems.danger.kotlin.utils.TestUtils class BitBucketServerParsingTests { - private val jsonFiles = JSONFiles() - private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()).build().adapter( - DSL::class.java) - private val dsl - get() = moshi.fromJson(jsonFiles.dangerBitBucketServerJSON) - private val bitBucketServer: BitBucketServer - get() = dsl!!.danger.bitBucketServer + + private val dsl: DSL = TestUtils.Json.decodeFromString(JSONFiles.dangerBitBucketServerJSON) + + private val bitBucketServer: BitBucketServer = dsl.danger.bitBucketServer @Test fun testItParsesTheBitBucketPullRequest() { @@ -23,9 +20,9 @@ class BitBucketServerParsingTests { "test", null, "user@email.com", + true, null, - null, - null + BitBucketServerUser.Type.NORMAL ) assertEquals(expectedUser, author.user) @@ -62,7 +59,7 @@ class BitBucketServerParsingTests { "user@email.com", true, "danger", - "NORMAL" + BitBucketServerUser.Type.NORMAL ) assertEquals(1, participants.count()) assertEquals(expectedPartecipant, participants[0].user) @@ -71,7 +68,7 @@ class BitBucketServerParsingTests { assertEquals(1518863923273, createdAt) assertEquals(false, isLocked) assertEquals(true, open) - assertEquals("OPEN", state) + assertEquals(BitBucketServerPR.State.OPEN, state) assertEquals("Pull request title", title) val expectedReviewerUser = BitBucketServerUser( @@ -81,7 +78,7 @@ class BitBucketServerParsingTests { "foo@bar.com", true, "danger", - "NORMAL" + BitBucketServerUser.Type.NORMAL ) val expectedReviewer = BitBucketServerReviewer( expectedReviewerUser, @@ -103,7 +100,7 @@ class BitBucketServerParsingTests { "foo@bar.com", true, "danger", - "NORMAL" + BitBucketServerUser.Type.NORMAL ) val expectedParent = BitBucketServerCommitParent( "c62ada76533a2de045d4c6062988ba84df140729", @@ -117,7 +114,7 @@ class BitBucketServerParsingTests { expectedUser, 1519442341000, "Modify and remove files", - arrayOf(expectedParent) + listOf(expectedParent) ) assertEquals(expectedCommit, first()) assertEquals(2, count()) @@ -134,11 +131,11 @@ class BitBucketServerParsingTests { "foo@bar.com", true, "danger", - "NORMAL" + BitBucketServerUser.Type.NORMAL ) val commentText = "test" val expectedProperty = - BitBucketServerCommentInnerProperties(1, null) + BitBucketServerCommentInnerProperties(1, listOf()) val expectedCommentDetail = BitBucketServerCommentDetail( 10, 23, @@ -146,9 +143,9 @@ class BitBucketServerParsingTests { expectedUser, 1518939353345, 1519449132488, - arrayOf(), + listOf(), expectedProperty, - arrayOf() + listOf() ) val expectedComment = BitBucketServerComment( 52, @@ -186,7 +183,7 @@ class BitBucketServerParsingTests { "foo@bar.com", true, "test", - "NORMAL" + BitBucketServerUser.Type.NORMAL ) val expectedActivity = BitBucketServerActivity( 61, @@ -203,16 +200,16 @@ class BitBucketServerParsingTests { @Test fun testOnBitBucketIsTrue() { - assertEquals(true, dsl!!.danger.onBitBucketServer) + assertEquals(true, dsl.danger.onBitBucketServer) } @Test fun testOnGitHubIsFalse() { - assertEquals(false, dsl!!.danger.onGitHub) + assertEquals(false, dsl.danger.onGitHub) } @Test fun testOnGitLabIsFalse() { - assertEquals(false, dsl!!.danger.onGitLab) + assertEquals(false, dsl.danger.onGitLab) } } diff --git a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitHubParsingTests.kt b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitHubParsingTests.kt index 0d74ec7f..5d5fb409 100644 --- a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitHubParsingTests.kt +++ b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitHubParsingTests.kt @@ -1,19 +1,16 @@ package systems.danger.kotlin -import com.squareup.moshi.Moshi -import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json import org.junit.Assert.* import org.junit.Test +import systems.danger.kotlin.utils.TestUtils.JSONFiles +import systems.danger.kotlin.utils.TestUtils import java.util.* class GitHubParsingTests { - private val jsonFiles = JSONFiles() - private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()).build().adapter( - DSL::class.java) - private val dsl - get() = moshi.fromJson(jsonFiles.githubDangerJSON)!! - private val github - get() = dsl.danger.github + private val dsl: DSL = TestUtils.Json.decodeFromString(JSONFiles.githubDangerJSON) + private val github = dsl.danger.github @Test fun testItParsesTheGithubPullRequest() { @@ -197,7 +194,10 @@ class GitHubParsingTests { @Test fun testItParsesTheMilestonesWithSomeNullAttributes() { - val dsl = moshi.fromJson(jsonFiles.githubWithSomeNullsAttributeDangerJSON)!! + val dsl: DSL = Json { + ignoreUnknownKeys = true + isLenient = true + }.decodeFromString(JSONFiles.githubWithSomeNullsAttributeDangerJSON) val github = dsl.danger.github with(github.issue.milestone!!) { diff --git a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitKtxTest.kt b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitKtxTest.kt index b4e47c4d..d43cd913 100644 --- a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitKtxTest.kt +++ b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitKtxTest.kt @@ -23,9 +23,9 @@ internal class GitKtxTest { } private val basicGit = Git( - modifiedFiles = emptyArray(), - createdFiles = emptyArray(), - deletedFiles = emptyArray(), + modifiedFiles = emptyList(), + createdFiles = emptyList(), + deletedFiles = emptyList(), commits = listOf( GitCommit( sha = "commit1", diff --git a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitLabParsingTests.kt b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitLabParsingTests.kt index f99ac826..bcfc1bae 100644 --- a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitLabParsingTests.kt +++ b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitLabParsingTests.kt @@ -1,19 +1,17 @@ package systems.danger.kotlin -import com.squareup.moshi.Moshi -import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory +import kotlinx.serialization.decodeFromString import org.junit.Assert.* import org.junit.Test +import systems.danger.kotlin.utils.TestUtils.JSONFiles +import systems.danger.kotlin.utils.TestUtils import java.util.* class GitLabParsingTests { - private val jsonFiles = JSONFiles() - private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()).build().adapter( - DSL::class.java) - private val dsl - get() = moshi.fromJson(jsonFiles.gitlabJSON) + private val dsl: DSL + get() = TestUtils.Json.decodeFromString(JSONFiles.gitlabJSON) private val gitLab: GitLab - get() = dsl!!.danger.gitlab + get() = dsl.danger.gitlab @Test fun testItParsesTheGitLabMergeRequest() { @@ -21,17 +19,30 @@ class GitLabParsingTests { assertEquals(false, allowCollaboration) assertEquals(false, allowMaintainerToPush) assertEquals(1, approvalsBeforeMerge) - val orta = GitLabUser("https://secure.gravatar.com/avatar/f116cb3be23153ec08b94e8bd4dbcfeb?s=80&d=identicon", 377669, "Orta", - GitLabUserState.active, "orta", "https://gitlab.com/orta") + val orta = GitLabUser( + "https://secure.gravatar.com/avatar/f116cb3be23153ec08b94e8bd4dbcfeb?s=80&d=identicon", 377669, "Orta", + GitLabUserState.ACTIVE, "orta", "https://gitlab.com/orta" + ) assertEquals(orta, assignee) - val fmeloni = GitLabUser("https://secure.gravatar.com/avatar/3d90e967de2beab6d44cfadbb4976b87?s=80&d=identicon", 3331525, "Franco Meloni", GitLabUserState.active, "f-meloni", "https://gitlab.com/f-meloni") + val fmeloni = GitLabUser( + "https://secure.gravatar.com/avatar/3d90e967de2beab6d44cfadbb4976b87?s=80&d=identicon", + 3331525, + "Franco Meloni", + GitLabUserState.ACTIVE, + "f-meloni", + "https://gitlab.com/f-meloni" + ) assertEquals(fmeloni, author) assertEquals(false, canMerge) assertEquals("1", changesCount) assertEquals(null, closedAt) assertEquals(null, closedBy) assertEquals("Updating it to avoid problems like https://github.com/danger/swift/issues/221", description) - val expectedDiffRefs = GitLabDiffRefs("ef28580bb2a00d985bffe4a4ce3fe09fdb12283f", "621bc3348549e51c5bd6ea9f094913e9e4667c7b", "ef28580bb2a00d985bffe4a4ce3fe09fdb12283f") + val expectedDiffRefs = GitLabDiffRefs( + "ef28580bb2a00d985bffe4a4ce3fe09fdb12283f", + "621bc3348549e51c5bd6ea9f094913e9e4667c7b", + "ef28580bb2a00d985bffe4a4ce3fe09fdb12283f" + ) assertEquals(expectedDiffRefs, diffRefs) assertEquals(0, downvotes) assertEquals(Date(1554942622492), firstDeployedToProductionAt) @@ -40,21 +51,39 @@ class GitLabParsingTests { assertEquals(182, iid) assertEquals(Date(1554942802492), latestBuildFinishedAt) assertEquals(Date(1554942022492), latestBuildStartedAt) - assertArrayEquals(arrayOf(), labels) + assertEquals(listOf(), labels) assertEquals("377a24fb7a0f30364f089f7bca67752a8b61f477", mergeCommitSha) assertEquals(Date(1554943042492), mergedAt) assertEquals(orta, mergedBy) assertEquals(false, mergeOnPipelineSuccess) - val expectedMilestone = GitLabMilestone(Date(1554933465346), "Test Description",Date(1560124800000), 1, 2, 1000, Date(1554933465346), GitLabMilestoneState.closed, "Test Milestone", Date(1554933465346), "https://gitlab.com/milestone") + val expectedMilestone = GitLabMilestone( + Date(1554933465346), + "Test Description", + Date(1560124800000), + 1, + 2, + 1000, + Date(1554933465346), + GitLabMilestoneState.CLOSED, + "Test Milestone", + Date(1554933465346), + "https://gitlab.com/milestone" + ) assertEquals(expectedMilestone, milestone) - val expectedPipeline = GitLabPipeline(50, "ef28580bb2a00d985bffe4a4ce3fe09fdb12283f", "621bc3348549e51c5bd6ea9f094913e9e4667c7b", GitLabPipelineStatus.success, "https://gitlab.com/danger-systems/danger.systems/pipeline/621bc3348549e51c5bd6ea9f094913e9e4667c7b") + val expectedPipeline = GitLabPipeline( + 50, + "ef28580bb2a00d985bffe4a4ce3fe09fdb12283f", + "621bc3348549e51c5bd6ea9f094913e9e4667c7b", + GitLabPipelineStatus.SUCCESS, + "https://gitlab.com/danger-systems/danger.systems/pipeline/621bc3348549e51c5bd6ea9f094913e9e4667c7b" + ) assertEquals(expectedPipeline, pipeline) assertEquals("1620437", projectId) assertEquals("621bc3348549e51c5bd6ea9f094913e9e4667c7b", sha) assertEquals(null, shouldRemoveSourceBranch) assertEquals("patch-2", sourceBranch) assertEquals("10132593", sourceProjectId) - assertEquals(GitLabMergeRequestState.merged, state) + assertEquals(GitLabMergeRequestState.MERGED, state) assertEquals(false, subscribed) assertEquals("master", targetBranch) assertEquals("1620437", targetProjectId) @@ -76,17 +105,17 @@ class GitLabParsingTests { } @Test - fun testOnGitHubIsFalse(){ - assertEquals(false, dsl!!.danger.onGitHub) + fun testOnGitHubIsFalse() { + assertEquals(false, dsl.danger.onGitHub) } @Test - fun testOnBitBucketIsFalse(){ - assertEquals(false, dsl!!.danger.onBitBucketServer) + fun testOnBitBucketIsFalse() { + assertEquals(false, dsl.danger.onBitBucketServer) } @Test fun testOnGitLabIsTrue() { - assertEquals(true, dsl!!.danger.onGitLab) + assertEquals(true, dsl.danger.onGitLab) } } \ No newline at end of file diff --git a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitParsingTests.kt b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitParsingTests.kt index d0053870..654c2f1d 100644 --- a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitParsingTests.kt +++ b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/GitParsingTests.kt @@ -1,28 +1,25 @@ package systems.danger.kotlin -import com.squareup.moshi.* -import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory -import org.junit.Assert +import kotlinx.serialization.decodeFromString +import org.junit.Assert.* import org.junit.Test -import java.util.* +import systems.danger.kotlin.utils.TestUtils.JSONFiles +import systems.danger.kotlin.utils.TestUtils class GitParsingTests { - private val jsonFiles = JSONFiles() - private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()).build().adapter( - DSL::class.java) - private val dsl - get() = moshi.fromJson(jsonFiles.githubDangerJSON)!! + + private val dsl: DSL = TestUtils.Json.decodeFromString(JSONFiles.githubDangerJSON) @Test fun testItParsesCorrectlyTheGitFiles() { val git = dsl.danger.git - Assert.assertTrue(expectedModifiedFiles.containsAll(git.modifiedFiles.toList())) - Assert.assertTrue(git.createdFiles[0] == ".ruby-version") + assertTrue(expectedModifiedFiles.containsAll(git.modifiedFiles.toList())) + assertTrue(git.createdFiles[0] == ".ruby-version") } - - private val expectedModifiedFiles = arrayListOf(".travis.yml", + private val expectedModifiedFiles = listOf( + ".travis.yml", "Kiosk.xcodeproj/project.pbxproj", "Kiosk/App/Logger.swift", "Kiosk/App/Networking/NetworkLogger.swift", @@ -76,5 +73,6 @@ class GitParsingTests { "KioskTests/ReferenceImages/YourBiddingDetailsViewControllerTests/displays_bidder_number_and_PIN@2x.png", "KioskTests/XAppTokenSpec.swift", "Podfile", - "Podfile.lock") + "Podfile.lock" + ) } diff --git a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/JSONFiles.kt b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/JSONFiles.kt deleted file mode 100644 index dc383d36..00000000 --- a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/JSONFiles.kt +++ /dev/null @@ -1,16 +0,0 @@ -package systems.danger.kotlin - -class JSONFiles { - val githubDangerJSON - get() = loadJSON("githubDangerJSON.json") - val githubWithSomeNullsAttributeDangerJSON - get() = loadJSON("githubWithSomeNullsAttributeDangerJSON.json") - val dangerBitBucketServerJSON - get() = loadJSON("bitbucketServerDangerJSON.json") - val gitlabJSON - get() = loadJSON("gitlabDangerJSON.json") - - private fun loadJSON(named: String): String { - return this.javaClass.classLoader.getResource(named).readText() - } -} diff --git a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/UtilsTests.kt b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/UtilsTests.kt index 00922b4e..73daf69b 100644 --- a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/UtilsTests.kt +++ b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/UtilsTests.kt @@ -1,18 +1,14 @@ package systems.danger.kotlin -import com.squareup.moshi.* -import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory +import kotlinx.serialization.decodeFromString import org.junit.Assert import org.junit.Test +import systems.danger.kotlin.utils.TestUtils.JSONFiles +import systems.danger.kotlin.utils.TestUtils import java.io.File -import java.util.* class UtilsTests { - private val jsonFiles = JSONFiles() - private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()).build().adapter( - DSL::class.java) - private val dsl - get() = moshi.fromJson(jsonFiles.githubDangerJSON)!! + private val dsl: DSL = TestUtils.Json.decodeFromString(JSONFiles.githubDangerJSON) @Test fun testReadText() { diff --git a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/utils/TestUtils.kt b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/utils/TestUtils.kt new file mode 100644 index 00000000..fe0fe5df --- /dev/null +++ b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/utils/TestUtils.kt @@ -0,0 +1,34 @@ +package systems.danger.kotlin.utils + +import kotlinx.serialization.json.Json + +object TestUtils { + + val Json: Json by lazy { + Json { + isLenient = true + ignoreUnknownKeys = true + } + } + + object JSONFiles { + val githubDangerJSON by lazy { + loadJSON("githubDangerJSON.json") + } + + val githubWithSomeNullsAttributeDangerJSON by lazy { + loadJSON("githubWithSomeNullsAttributeDangerJSON.json") + } + val dangerBitBucketServerJSON by lazy { + loadJSON("bitbucketServerDangerJSON.json") + } + + val gitlabJSON by lazy { + loadJSON("gitlabDangerJSON.json") + } + + private fun loadJSON(named: String): String { + return this.javaClass.classLoader.getResource(named).readText() + } + } +} \ No newline at end of file diff --git a/danger-kotlin-library/src/test/resources/bitbucketServerDangerJSON.json b/danger-kotlin-library/src/test/resources/bitbucketServerDangerJSON.json index 488e9848..04980da2 100644 --- a/danger-kotlin-library/src/test/resources/bitbucketServerDangerJSON.json +++ b/danger-kotlin-library/src/test/resources/bitbucketServerDangerJSON.json @@ -65,7 +65,8 @@ "status": "UNAPPROVED", "user": { "emailAddress": "user@email.com", - "name": "test" + "name": "test", + "active": true } }, "closed": false, diff --git a/dependencyVersions.gradle b/dependencyVersions.gradle index 1656f2d2..b2fd88f8 100644 --- a/dependencyVersions.gradle +++ b/dependencyVersions.gradle @@ -6,18 +6,12 @@ def group(Closure closure) { } // Utils -project.ext.versionMoshi = '1.8.0' -project.ext.groupIdMoshi = 'com.squareup.moshi' -project.ext.artifactIdMoshiKotlin = 'moshi-kotlin' - ext.utils = [ - moshi: "$groupIdMoshi:$artifactIdMoshiKotlin:$versionMoshi" + // Empty ] ext.utilsDependencies = group { - includeRecursiveJar(utils.moshi) { - exclude group: groupIdKotlin - } + // Empty } project.ext.artifactIdMoshi = 'moshi' @@ -28,12 +22,24 @@ project.ext.artifactIdOkio = 'okio' // Kotlin project.ext.versionKotlin = '1.4.10' project.ext.groupIdKotlin = 'org.jetbrains.kotlin' +project.ext.groupIdKotlinx = 'org.jetbrains.kotlinx' project.ext.artifactIdKotlinMain = 'kotlin-main-kts' +project.ext.artifactIdKotlinSerializationJson = 'kotlinx-serialization-json' +project.ext.artifactIdKotlinDatetime = "kotlinx-datetime" +project.ext.versionKotlinSerializationJson = '1.0.1' +project.ext.versionKotlinDatetime = '0.1.0' + ext.kotlin = [ - mainKts: "$groupIdKotlin:$artifactIdKotlinMain:$versionKotlin" + mainKts: "$groupIdKotlin:$artifactIdKotlinMain:$versionKotlin", + serialization: "$groupIdKotlinx:$artifactIdKotlinSerializationJson:$versionKotlinSerializationJson", + // Migrate to kotlin-datetime and replace java.util.Date + //datetime: "$groupIdKotlinx:$artifactIdKotlinDatetime:$versionKotlinDatetime" ] ext.kotlinDependencies = group { includeJar kotlin.mainKts + includeRecursiveJar kotlin.serialization + // Migrate to kotlin-datetime and replace java.util.Date + //includeRecursiveJar kotlin.datetime } // Testing