Skip to content

Commit

Permalink
Change the MentionId type and also create a directory to store GitHub…
Browse files Browse the repository at this point in the history
… REST files.
  • Loading branch information
MykytaPimonovTD committed May 24, 2024
1 parent 52a03fd commit 2c85b29
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ package io.spine.examples.pingh.github
import io.spine.net.Url
import kotlin.reflect.KClass

/**
* Creates a new [NodeId] with the specified string value.
*/
public fun KClass<NodeId>.buildBy(value: String): NodeId =
NodeId.newBuilder()
.setValue(value)
.vBuild()

/**
* Creates a new [Username] with the specified string value.
*/
Expand Down
51 changes: 2 additions & 49 deletions github/src/main/proto/spine_examples/pingh/github/github.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ option java_multiple_files = true;

import "google/protobuf/timestamp.proto";
import "spine/net/url.proto";
import "spine_examples/pingh/github/identifiers.proto";

// The username used in the GitHub account.
message Username {
Expand All @@ -62,59 +63,11 @@ message User {
spine.net.Url avatar_url = 2 [(required) = true];
}

// GitHub user information.
//
// The data is stored in primitive types. It is used to retrieve user data
// from JSON responses from the GitHub API.
//
message GitHubUser {

// The unique username that identifies the user.
string username = 1 [(required) = true, json_name = "login"];

// The URL to the user's avatar.
string avatar_url = 2 [(required) = true, json_name = "avatar_url"];
}

// Information about the GitHub item obtained by requesting to search for mentions
// in issues and pull requests.
//
// It is used to retrieve item data from JSON responses from the GitHub API.
//
message IssuesSearchResultItem {

// The item's ID in GitHub.
int64 id = 1 [(required) = true];

// The item's author.
GitHubUser who_created = 2 [(required) = true, json_name = "user"];

// The item's title.
string title = 3 [(required) = true];

// Time when this item was created.
string when_created = 4 [(required) = true, json_name = "created_at"];

// The URL to this GitHub item.
string html_url = 5 [(required) = true, json_name = "html_url"];
}

// GitHub API response received when requesting to search for mentions
// in issues and pull requests.
//
// It is used to retrieve data from JSON responses from the GitHub API.
//
message IssuesSearchResult {

// GitHub items on which the mention occurred.
repeated IssuesSearchResultItem item = 1 [(required) = true, json_name = "items"];
}

// The mention of a user on the GitHub.
message Mention {

// The ID of the mention on the GitHub.
int64 id = 1;
NodeId id = 1;

// The user who created the mention.
User who_mentioned = 2 [(required) = true];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024, TeamDev. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
syntax = "proto3";

package spine_examples.pingh.github;

import "spine/options.proto";

option (type_url_prefix) = "type.pingh.spine.io";
option java_package = "io.spine.examples.pingh.github";
option java_outer_classname = "IdentifiersProto";
option java_multiple_files = true;

// The global identifier for any item in the GitHub.
message NodeId {

string value = 1 [(required) = true];
}
91 changes: 91 additions & 0 deletions github/src/main/proto/spine_examples/pingh/github/rest/rest.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2024, TeamDev. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
syntax = "proto3";

package spine_examples.pingh.github.rest;

import "spine/options.proto";

option (type_url_prefix) = "type.pingh.spine.io";
option java_package = "io.spine.examples.pingh.github.rest";
option java_outer_classname = "GitHubRestProto";
option java_multiple_files = true;

import "google/protobuf/timestamp.proto";
import "spine/net/url.proto";

// GitHub user information.
//
// The data is stored in primitive types. It is used to retrieve user data
// from JSON responses from the GitHub API.
//
message UserFragment {

// The unique username that identifies the user.
string username = 1 [(required) = true, json_name = "login"];

// The URL to the user's avatar.
string avatar_url = 2 [(required) = true, json_name = "avatar_url"];
}

// Contains data about the issue or pull request received from the GitHub API.
//
// In the GitHub context, issues and pull requests have the same data structure.
//
// It is used to retrieve item data from JSON responses from the GitHub API.
//
message IssueOrPullRequestFragment {

// The item's ID in GitHub.
string node_id = 1 [(required) = true, json_name = "node_id"];

// The item's author.
UserFragment who_created = 2 [(required) = true, json_name = "user"];

// The item's title.
string title = 3 [(required) = true];

// Time when this item was created.
string when_created = 4 [(required) = true, json_name = "created_at"];

// The URL to this GitHub item.
string html_url = 5 [(required) = true, json_name = "html_url"];
}

// The GitHub API response received when requesting to search for mentions
// in issues and pull requests.
//
// In the GitHub context, issues and pull requests have the same data structure.
// Request to the GitHub API to retrieve them is the same and returns a list containing
// both issues and pull requests.
//
// It is used to retrieve data from JSON responses from the GitHub API.
//
message IssuesAndPullRequestsSearchResult {

// GitHub items on which the mention occurred.
repeated IssueOrPullRequestFragment item = 1 [(required) = true, json_name = "items"];
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ public class GitHubClientProcess :
gitHubMentions
.map { mention ->
with(UserMentioned.newBuilder()) {
id = MentionId::class.buildBy(mention.id)
id = MentionId::class.buildBy(
mention.id,
state().id.username
)
whoMentioned = mention.whoMentioned
title = mention.title
whenMentioned = mention.whenMentioned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ package io.spine.examples.pingh.mentions

import com.google.protobuf.util.JsonFormat
import com.google.protobuf.util.Timestamps
import io.spine.examples.pingh.github.IssuesSearchResult
import io.spine.examples.pingh.github.IssuesSearchResultItem
import io.spine.examples.pingh.github.Mention
import io.spine.examples.pingh.github.NodeId
import io.spine.examples.pingh.github.User
import io.spine.examples.pingh.github.buildBy
import io.spine.examples.pingh.github.rest.IssueOrPullRequestFragment
import io.spine.examples.pingh.github.rest.IssuesAndPullRequestsSearchResult
import io.spine.net.Url

/**
Expand All @@ -45,7 +46,7 @@ public class MentionsParser {
* Converts JSON to a list of [Mention]s.
*/
public fun parseJson(json: String): List<Mention> {
val responseBuilder = IssuesSearchResult.newBuilder()
val responseBuilder = IssuesAndPullRequestsSearchResult.newBuilder()
JsonFormat.parser()
.ignoringUnknownFields()
.merge(json, responseBuilder)
Expand All @@ -54,21 +55,21 @@ public class MentionsParser {
}

/**
* Converts list of [IssuesSearchResultItem]s to list of [Mention]s.
* Converts list of [IssueOrPullRequestFragment]s to list of [Mention]s.
*/
private fun mapToMention(gitHubItems: List<IssuesSearchResultItem>):
private fun mapToMention(gitHubItems: List<IssueOrPullRequestFragment>):
List<Mention> =
gitHubItems
.map { item ->
.map { fragment ->
with(Mention.newBuilder()) {
id = item.id
id = NodeId::class.buildBy(fragment.nodeId)
whoMentioned = User::class.buildBy(
item.whoCreated.username,
item.whoCreated.avatarUrl
fragment.whoCreated.username,
fragment.whoCreated.avatarUrl
)
title = item.title
whenMentioned = Timestamps.parse(item.whenCreated)
url = Url::class.buildBy(item.htmlUrl)
title = fragment.title
whenMentioned = Timestamps.parse(fragment.whenCreated)
url = Url::class.buildBy(fragment.htmlUrl)
vBuild()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@

package io.spine.examples.pingh.mentions

import io.spine.examples.pingh.github.NodeId
import io.spine.examples.pingh.github.Username
import kotlin.reflect.KClass

/**
* Creates a new [MentionId] with the specified long value.
* Creates a new [MentionId] with the specified [NodeId] and [Username].
*/
internal fun KClass<MentionId>.buildBy(value: Long): MentionId =
internal fun KClass<MentionId>.buildBy(nodeId: NodeId, whomMentioned: Username): MentionId =
MentionId.newBuilder()
.setUuid("$value")
.setWhere(nodeId)
.setUser(whomMentioned)
.vBuild()

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ option java_outer_classname = "IdentifiersProto";
option java_multiple_files = true;

import "spine_examples/pingh/github/github.proto";
import "spine_examples/pingh/github/identifiers.proto";

// Identifies a mention.
//
// The unique username is used because multiple users can be mentioned in one GitHub item.
//
message MentionId {

string uuid = 1 [(required) = true];
// The ID of the GitHub item where the user was mentioned.
spine_examples.pingh.github.NodeId where = 1 [(required) = true];

// The username of the GitHub user whom mentioned.
spine_examples.pingh.github.Username user = 2 [(required) = true];
}

// Identifies a process of updating mentions from the GitHub.
Expand Down

0 comments on commit 2c85b29

Please sign in to comment.