From f38475a9dbc7810f0543869bba01336d15853fb5 Mon Sep 17 00:00:00 2001 From: Anatoly Kislov Date: Fri, 10 Nov 2017 05:09:05 +0300 Subject: [PATCH 1/3] feat: invite authors --- src/main/kotlin/app/api/Api.kt | 2 ++ src/main/kotlin/app/api/MockApi.kt | 11 +++++++- src/main/kotlin/app/api/ServerApi.kt | 12 +++++++++ src/main/kotlin/app/hashers/RepoHasher.kt | 8 ++++++ src/main/kotlin/app/model/Author.kt | 29 +++++++++++++++++++- src/main/kotlin/app/model/AuthorGroup.kt | 32 +++++++++++++++++++++++ src/main/proto/sourcerer.proto | 9 +++++++ 7 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/app/model/AuthorGroup.kt diff --git a/src/main/kotlin/app/api/Api.kt b/src/main/kotlin/app/api/Api.kt index a2a5658e..b42f9a5b 100644 --- a/src/main/kotlin/app/api/Api.kt +++ b/src/main/kotlin/app/api/Api.kt @@ -3,6 +3,7 @@ package app.api +import app.model.Author import app.model.Commit import app.model.Fact import app.model.Repo @@ -20,4 +21,5 @@ interface Api { fun postCommits(commitsList: List): Result fun deleteCommits(commitsList: List): Result fun postFacts(factsList: List): Result + fun postAuthors(authorsList: List): Result } diff --git a/src/main/kotlin/app/api/MockApi.kt b/src/main/kotlin/app/api/MockApi.kt index c08688ec..042e7aa5 100644 --- a/src/main/kotlin/app/api/MockApi.kt +++ b/src/main/kotlin/app/api/MockApi.kt @@ -4,6 +4,7 @@ package app.api import app.Logger +import app.model.Author import app.model.Commit import app.model.Repo import app.model.Fact @@ -17,6 +18,7 @@ class MockApi( // GET requests. var receivedRepos: MutableList = mutableListOf() var receivedAddedCommits: MutableList = mutableListOf() var receivedFacts: MutableList = mutableListOf() + var receivedAuthors: MutableList = mutableListOf() // DELETE requests. var receivedDeletedCommits: MutableList = mutableListOf() @@ -58,8 +60,15 @@ class MockApi( // GET requests. } override fun postFacts(factsList: List): Result { - Logger.debug { "MockApi: postStats request (${factsList.size} stats)" } + Logger.debug { "MockApi: postFacts request (${factsList.size} facts)" } receivedFacts.addAll(factsList) return Result() } + + override fun postAuthors(authorsList: List): Result { + Logger.debug { "MockApi: postAuthors request (${authorsList.size} " + + "stats)" } + receivedAuthors.addAll(authorsList) + return Result() + } } diff --git a/src/main/kotlin/app/api/ServerApi.kt b/src/main/kotlin/app/api/ServerApi.kt index f475c6a4..a7a03d85 100644 --- a/src/main/kotlin/app/api/ServerApi.kt +++ b/src/main/kotlin/app/api/ServerApi.kt @@ -6,6 +6,8 @@ package app.api import app.BuildConfig import app.Logger import app.config.Configurator +import app.model.Author +import app.model.AuthorGroup import app.model.Commit import app.model.CommitGroup import app.model.Fact @@ -105,6 +107,11 @@ class ServerApi (private val configurator: Configurator) : Api { .body(facts.serialize()) } + private fun createRequestPostAuthors(authors: AuthorGroup): Request { + return post("/authors").header(getContentTypeHeader()) + .body(authors.serialize()) + } + private fun makeRequest(request: Request, requestName: String, parser: (ByteArray) -> T): Result { @@ -177,4 +184,9 @@ class ServerApi (private val configurator: Configurator) : Api { val facts = FactGroup(factsList) return makeRequest(createRequestPostFacts(facts), "postFacts", {}) } + + override fun postAuthors(authorsList: List): Result { + val authors = AuthorGroup(authorsList) + return makeRequest(createRequestPostAuthors(authors), "postAuthors", {}) + } } diff --git a/src/main/kotlin/app/hashers/RepoHasher.kt b/src/main/kotlin/app/hashers/RepoHasher.kt index 069a6aa1..73695aef 100644 --- a/src/main/kotlin/app/hashers/RepoHasher.kt +++ b/src/main/kotlin/app/hashers/RepoHasher.kt @@ -6,6 +6,7 @@ package app.hashers import app.Logger import app.api.Api import app.config.Configurator +import app.model.Author import app.model.LocalRepo import app.model.Repo import app.utils.HashingException @@ -51,6 +52,9 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api, // Notify server about new contributor and his email. postRepoToServer() } + + postAuthorsToServer(filteredEmails) + // Get repo setup (commits, emails to hash) from server. getRepoFromServer() @@ -130,6 +134,10 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api, Logger.debug { serverRepo.toString() } } + private fun postAuthorsToServer(emails: HashSet) { + api.postAuthors(emails.map { Author(it) }).onErrorThrow() + } + private fun initServerRepo(initCommitRehash: String) { serverRepo = Repo(userEmail = localRepo.author.email) serverRepo.initialCommitRehash = initCommitRehash diff --git a/src/main/kotlin/app/model/Author.kt b/src/main/kotlin/app/model/Author.kt index 6f690f53..fbdea79d 100644 --- a/src/main/kotlin/app/model/Author.kt +++ b/src/main/kotlin/app/model/Author.kt @@ -3,10 +3,37 @@ package app.model +import app.Protos +import com.google.protobuf.InvalidProtocolBufferException +import java.security.InvalidParameterException + /** * Commit author. */ -data class Author(var name: String = "", var email: String = "") { +data class Author( + var name: String = "", + var email: String = "" +) { + @Throws(InvalidParameterException::class) + constructor(proto: Protos.Author) : this() { + email = proto.email + } + + @Throws(InvalidProtocolBufferException::class) + constructor(bytes: ByteArray) : this(Protos.Author.parseFrom(bytes)) + + constructor(serialized: String) : this(serialized.toByteArray()) + + fun getProto(): Protos.Author { + return Protos.Author.newBuilder() + .setEmail(email) + .build() + } + + fun serialize(): ByteArray { + return getProto().toByteArray() + } + // Email defines user identity. override fun equals(other: Any?): Boolean { if (other is Author) { diff --git a/src/main/kotlin/app/model/AuthorGroup.kt b/src/main/kotlin/app/model/AuthorGroup.kt new file mode 100644 index 00000000..bf252d78 --- /dev/null +++ b/src/main/kotlin/app/model/AuthorGroup.kt @@ -0,0 +1,32 @@ +package app.model + +import app.Protos +import com.google.protobuf.InvalidProtocolBufferException +import java.security.InvalidParameterException + +/** + * Group of commit authors. + */ +data class AuthorGroup( + var authors: List = listOf() +) { + @Throws(InvalidParameterException::class) + constructor(proto: Protos.AuthorGroup) : this() { + authors = proto.authorsList.map { it -> Author(it) } + } + + @Throws(InvalidProtocolBufferException::class) + constructor(bytes: ByteArray) : this(Protos.AuthorGroup.parseFrom(bytes)) + + constructor(serialized: String) : this(serialized.toByteArray()) + + fun getProto(): Protos.AuthorGroup { + return Protos.AuthorGroup.newBuilder() + .addAllAuthors(authors.map { it -> it.getProto() }) + .build() + } + + fun serialize(): ByteArray { + return getProto().toByteArray() + } +} diff --git a/src/main/proto/sourcerer.proto b/src/main/proto/sourcerer.proto index f30a9c9a..2e171a6d 100644 --- a/src/main/proto/sourcerer.proto +++ b/src/main/proto/sourcerer.proto @@ -72,6 +72,15 @@ message FactGroup { repeated Fact facts = 1; } +// Used to authors for invitational procedures. +message Author { + string email = 1; +} + +message AuthorGroup { + repeated Author authors = 1; +} + // User properties and indentity information about repos. message User { // List of known repos containing basic information for indentifying repo. From 96d1c1a1125ab07922d6e360981d804e160ac2d7 Mon Sep 17 00:00:00 2001 From: Anatoly Kislov Date: Sat, 11 Nov 2017 00:24:11 +0300 Subject: [PATCH 2/3] feat: invite coworkers request --- src/main/kotlin/app/hashers/RepoHasher.kt | 4 +++- src/main/kotlin/app/model/Author.kt | 5 ++++- src/main/proto/sourcerer.proto | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/app/hashers/RepoHasher.kt b/src/main/kotlin/app/hashers/RepoHasher.kt index 73695aef..7e4c71b7 100644 --- a/src/main/kotlin/app/hashers/RepoHasher.kt +++ b/src/main/kotlin/app/hashers/RepoHasher.kt @@ -135,7 +135,9 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api, } private fun postAuthorsToServer(emails: HashSet) { - api.postAuthors(emails.map { Author(it) }).onErrorThrow() + api.postAuthors(emails.map { email -> + Author(email=email, repo=serverRepo) + }).onErrorThrow() } private fun initServerRepo(initCommitRehash: String) { diff --git a/src/main/kotlin/app/model/Author.kt b/src/main/kotlin/app/model/Author.kt index fbdea79d..f302cc11 100644 --- a/src/main/kotlin/app/model/Author.kt +++ b/src/main/kotlin/app/model/Author.kt @@ -12,11 +12,13 @@ import java.security.InvalidParameterException */ data class Author( var name: String = "", - var email: String = "" + var email: String = "", + var repo: Repo = Repo() ) { @Throws(InvalidParameterException::class) constructor(proto: Protos.Author) : this() { email = proto.email + repo = Repo(proto.repoRehash) } @Throws(InvalidProtocolBufferException::class) @@ -27,6 +29,7 @@ data class Author( fun getProto(): Protos.Author { return Protos.Author.newBuilder() .setEmail(email) + .setRepoRehash(repo.rehash) .build() } diff --git a/src/main/proto/sourcerer.proto b/src/main/proto/sourcerer.proto index 2e171a6d..f3d11913 100644 --- a/src/main/proto/sourcerer.proto +++ b/src/main/proto/sourcerer.proto @@ -75,6 +75,7 @@ message FactGroup { // Used to authors for invitational procedures. message Author { string email = 1; + string repo_rehash = 2; } message AuthorGroup { From 51f0bf3c601035167b3ed0826c0cdc1df4690be9 Mon Sep 17 00:00:00 2001 From: Anatoly Kislov Date: Sat, 11 Nov 2017 00:30:36 +0300 Subject: [PATCH 3/3] fix: rename properties --- src/main/kotlin/app/api/ApiError.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/app/api/ApiError.kt b/src/main/kotlin/app/api/ApiError.kt index 981f1d5b..1093cf1e 100644 --- a/src/main/kotlin/app/api/ApiError.kt +++ b/src/main/kotlin/app/api/ApiError.kt @@ -30,9 +30,9 @@ class ApiError(exception: Exception) : Exception(exception.message) { get() = AUTH_ERROR_CODES.contains(httpStatusCode) constructor(fuelError: FuelError) : this(fuelError as Exception) { - httpStatusCode = fuelError.response.httpStatusCode - httpResponseMessage = fuelError.response.httpResponseMessage - if (fuelError.response.httpResponseHeaders["Content-Type"] + httpStatusCode = fuelError.response.statusCode + httpResponseMessage = fuelError.response.responseMessage + if (fuelError.response.headers["Content-Type"] ?.contains("application/octet-stream") == true) { try { serverErrors = Errors(fuelError.response.data).errors