Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/kotlin/app/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package app.api

import app.model.Author
import app.model.Commit
import app.model.Fact
import app.model.Repo
Expand All @@ -20,4 +21,5 @@ interface Api {
fun postCommits(commitsList: List<Commit>): Result<Unit>
fun deleteCommits(commitsList: List<Commit>): Result<Unit>
fun postFacts(factsList: List<Fact>): Result<Unit>
fun postAuthors(authorsList: List<Author>): Result<Unit>
}
6 changes: 3 additions & 3 deletions src/main/kotlin/app/api/ApiError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/main/kotlin/app/api/MockApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +18,7 @@ class MockApi( // GET requests.
var receivedRepos: MutableList<Repo> = mutableListOf()
var receivedAddedCommits: MutableList<Commit> = mutableListOf()
var receivedFacts: MutableList<Fact> = mutableListOf()
var receivedAuthors: MutableList<Author> = mutableListOf()

// DELETE requests.
var receivedDeletedCommits: MutableList<Commit> = mutableListOf()
Expand Down Expand Up @@ -58,8 +60,15 @@ class MockApi( // GET requests.
}

override fun postFacts(factsList: List<Fact>): Result<Unit> {
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<Author>): Result<Unit> {
Logger.debug { "MockApi: postAuthors request (${authorsList.size} " +
"stats)" }
receivedAuthors.addAll(authorsList)
return Result()
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/app/api/ServerApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <T> makeRequest(request: Request,
requestName: String,
parser: (ByteArray) -> T): Result<T> {
Expand Down Expand Up @@ -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<Author>): Result<Unit> {
val authors = AuthorGroup(authorsList)
return makeRequest(createRequestPostAuthors(authors), "postAuthors", {})
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/app/hashers/RepoHasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -130,6 +134,12 @@ class RepoHasher(private val localRepo: LocalRepo, private val api: Api,
Logger.debug { serverRepo.toString() }
}

private fun postAuthorsToServer(emails: HashSet<String>) {
api.postAuthors(emails.map { email ->
Author(email=email, repo=serverRepo)
}).onErrorThrow()
}

private fun initServerRepo(initCommitRehash: String) {
serverRepo = Repo(userEmail = localRepo.author.email)
serverRepo.initialCommitRehash = initCommitRehash
Expand Down
32 changes: 31 additions & 1 deletion src/main/kotlin/app/model/Author.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,40 @@

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 = "",
var repo: Repo = Repo()
) {
@Throws(InvalidParameterException::class)
constructor(proto: Protos.Author) : this() {
email = proto.email
repo = Repo(proto.repoRehash)
}

@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)
.setRepoRehash(repo.rehash)
.build()
}

fun serialize(): ByteArray {
return getProto().toByteArray()
}

// Email defines user identity.
override fun equals(other: Any?): Boolean {
if (other is Author) {
Expand Down
32 changes: 32 additions & 0 deletions src/main/kotlin/app/model/AuthorGroup.kt
Original file line number Diff line number Diff line change
@@ -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<Author> = 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()
}
}
10 changes: 10 additions & 0 deletions src/main/proto/sourcerer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ message FactGroup {
repeated Fact facts = 1;
}

// Used to authors for invitational procedures.
message Author {
string email = 1;
string repo_rehash = 2;
}

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.
Expand Down