Skip to content

Commit

Permalink
Solve #92 - Add github issues assigness to work items (#93)
Browse files Browse the repository at this point in the history
* Remove WriteGithubIssueRequest bot

* Fix the github assigness update on notion

* Add state manage

* Fix rubocop warning

* Increase coverage
  • Loading branch information
FelipeGuzmanSierra authored Aug 12, 2024
1 parent 40a55af commit 03654e0
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 309 deletions.
13 changes: 7 additions & 6 deletions lib/bas/bot/create_work_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class CreateWorkItem < Bot::Base
include Utils::Notion::Types

UPDATE_REQUEST = "UpdateWorkItemRequest"
STATUS = "Backlog"

# read function to execute the PostgresDB Read component
#
Expand Down Expand Up @@ -117,17 +118,17 @@ def body

def properties # rubocop:disable Metrics/AbcSize
{
"Responsible domain": select(process_options[:domain]),
"Github Issue id": rich_text(read_response.data["issue"]["id"].to_s),
"Status": status(process_options[:status]),
"Responsible domain": select(read_response.data["domain"]),
"Github Issue Id": rich_text(read_response.data["issue"]["id"].to_s),
"Status": status(STATUS),
"Detail": title(read_response.data["issue"]["title"])
}.merge(work_item_type)
end

def work_item_type
case process_options[:work_item_type]
when "activity" then { "Activity": relation(process_options[:activity]) }
when "project" then { "Project": relation(process_options[:project]) }
case read_response.data["work_item_type"]
when "activity" then { "Activity": relation(read_response.data["type_id"]) }
when "project" then { "Project": relation(read_response.data["type_id"]) }
else {}
end
end
Expand Down
32 changes: 27 additions & 5 deletions lib/bas/bot/fetch_github_issues.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative "./base"
require_relative "../read/default"
require_relative "../read/postgres"
require_relative "../utils/github/octokit_client"
require_relative "../write/postgres"

Expand Down Expand Up @@ -32,6 +32,15 @@ module Bot
# repo: "repository name",
# filters: "hash with filters",
# organization: "GitHub organization name"
# connection: {
# host: "localhost",
# port: 5432,
# dbname: "bas",
# user: "postgres",
# password: "postgres"
# },
# db_table: "github_issues",
# tag: "GithubIssueRequest"
# },
# write_options: {
# connection: {
Expand All @@ -50,7 +59,7 @@ module Bot
# bot.execute
#
class FetchGithubIssues < Bot::Base
ISSUE_PARAMS = %i[id html_url title body labels state created_at updated_at].freeze
ISSUE_PARAMS = %i[id html_url title body labels state created_at updated_at state].freeze
PER_PAGE = 100

# read function to execute the PostgresDB Read component
Expand All @@ -69,9 +78,9 @@ def process
if octokit[:client]
repo_issues = octokit[:client].issues(@process_options[:repo], filters)

issues = normalize_response(repo_issues)
normalize_response(repo_issues).each { |issue| create_request(issue) }

{ success: { issues: } }
{ success: { created: true } }
else
{ error: octokit[:error] }
end
Expand Down Expand Up @@ -107,7 +116,7 @@ def params
def filters
default_filter = { per_page: PER_PAGE }

filters = @process_options[:filters]
filters = process_options[:filters]
filters = filters.merge({ since: read_response.inserted_at }) unless read_response.nil?

filters.is_a?(Hash) ? default_filter.merge(filters) : default_filter
Expand All @@ -121,5 +130,18 @@ def normalize_response(issues)
end
end
end

def create_request(issue)
write_data = {
success: {
issue:,
work_item_type: process_options[:work_item_type],
type_id: process_options[:type_id],
domain: process_options[:domain]
}
}

Write::Postgres.new(process_options, write_data).execute
end
end
end
8 changes: 3 additions & 5 deletions lib/bas/bot/format_do_bill_alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,9 @@ def message
balance = read_response.data["billing"]["month_to_date_balance"]
threshold = process_options[:threshold]

""":warning: The **DigitalOcean** daily usage was exceeded.
Current balance: #{balance}
Threshold: #{threshold}
Current daily usage: #{daily_usage.round(3)}
"""
":warning: The **DigitalOcean** daily usage was exceeded. \
Current balance: #{balance}, Threshold: #{threshold}, \
Current daily usage: #{daily_usage.round(3)}"
end
end
end
59 changes: 54 additions & 5 deletions lib/bas/bot/update_work_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
require_relative "../utils/notion/request"
require_relative "../utils/notion/types"
require_relative "../utils/notion/delete_page_blocks"
require_relative "../utils/notion/fetch_database_record"
require_relative "../utils/notion/update_db_page"
require_relative "../write/postgres"

module Bot
Expand Down Expand Up @@ -53,6 +55,7 @@ class UpdateWorkItem < Bot::Base
include Utils::Notion::Types

DESCRIPTION = "Issue Description"
GITHUB_COLUMN = "Username"

# read function to execute the PostgresDB Read component
#
Expand All @@ -67,11 +70,11 @@ def read
def process
return { success: { updated: nil } } if unprocessable_response

delete_wi

response = Utils::Notion::Request.execute(params)
response = process_wi

if response.code == 200
update_assigness

{ success: { issue: read_response.data["issue"] } }
else
{ error: { message: response.parsed_response, status_code: response.code } }
Expand All @@ -95,6 +98,12 @@ def conditions
}
end

def process_wi
delete_wi

Utils::Notion::Request.execute(params)
end

def params
{
endpoint: "blocks/#{read_response.data["notion_wi"]}/children",
Expand All @@ -121,12 +130,52 @@ def issue_reference
end

def delete_wi
params = {
options = {
page_id: read_response.data["notion_wi"],
secret: process_options[:secret]
}

Utils::Notion::DeletePageBlocks.new(params).execute
Utils::Notion::DeletePageBlocks.new(options).execute
end

def update_assigness
relation = users.map { |user| user_id(user) }

options = {
page_id: read_response.data["notion_wi"],
secret: process_options[:secret],
body: { properties: { People: { relation: } }.merge(status) }
}

Utils::Notion::UpdateDatabasePage.new(options).execute
end

def users
options = {
database_id: process_options[:users_database_id],
secret: process_options[:secret],
body: { filter: { or: github_usernames } }
}

Utils::Notion::FetchDatabaseRecord.new(options).execute
end

def github_usernames
read_response.data["issue"]["assignees"].map do |username|
{ property: GITHUB_COLUMN, rich_text: { equals: username } }
end
end

def user_id(user)
relation = user.dig("properties", "People", "relation")

relation.nil? ? {} : relation.first
end

def status
return {} unless read_response.data["issue"]["state"] == "closed"

{ Status: { status: { name: "Done" } } }
end
end
end
7 changes: 4 additions & 3 deletions lib/bas/bot/verify_issue_existance_in_notion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module Bot
#
class VerifyIssueExistanceInNotion < Bot::Base
NOT_FOUND = "not found"
NOTION_PROPERTY = "Github Issue Id"

# read function to execute the PostgresDB Read component
#
Expand All @@ -70,7 +71,7 @@ def process
if response.code == 200
result = response.parsed_response["results"].first

{ success: { issue: read_response.data["request"], notion_wi: notion_wi_id(result) } }
{ success: read_response.data.merge({ notion_wi: notion_wi_id(result) }) }
else
{ error: { message: response.parsed_response, status_code: response.code } }
end
Expand Down Expand Up @@ -107,8 +108,8 @@ def params
def body
{
filter: {
property: "Github Issue id",
rich_text: { equals: read_response.data["request"]["id"].to_s }
property: NOTION_PROPERTY,
rich_text: { equals: read_response.data["issue"]["id"].to_s }
}
}
end
Expand Down
96 changes: 0 additions & 96 deletions lib/bas/bot/write_github_issue_requests.rb

This file was deleted.

46 changes: 46 additions & 0 deletions lib/bas/utils/notion/fetch_database_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require "httparty"
require_relative "request"

module Utils
module Notion
##
# This module is a Notion utility for fetching record from a database.
#
class FetchDatabaseRecord
# Implements the fetch page process logic to Notion.
#
# <br>
# <b>Params:</b>
# * <tt>database_id</tt> Id of the notion database.
# * <tt>secret</tt> Notion secret.
# * <tt>body</tt> Body with the filters.
#
# <br>
# <b>returns</b> <tt>HTTParty::Response</tt>
#
#
def initialize(options)
@options = options
end

def execute
records = Utils::Notion::Request.execute(params)

records.parsed_response["results"] || []
end

private

def params
{
endpoint: "databases/#{@options[:database_id]}/query",
secret: @options[:secret],
method: "post",
body: @options[:body]
}
end
end
end
end
Loading

0 comments on commit 03654e0

Please sign in to comment.