Skip to content

Commit 18bd57e

Browse files
committed
Console - GitHub / Slack
Adding details about slack account Signed-off-by: Rodrigo Nardi <rnardi@netdef.org>
1 parent 2e21fca commit 18bd57e

8 files changed

Lines changed: 86 additions & 28 deletions

File tree

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22
# SPDX-License-Identifier: BSD-2-Clause
33
#
4-
# organization_console.rb
4+
# console
55
# Part of NetDEF CI System
66
#
77
# Copyright (c) 2023 by
@@ -10,9 +10,14 @@
1010
# frozen_string_literal: true
1111

1212
require 'irb'
13+
14+
ENV['RAILS_ENV'] = ARGV.shift || 'production'
15+
16+
puts "Starting console: #{ENV.fetch('RAILS_ENV', nil)}"
17+
1318
require_relative '../config/setup'
19+
require_relative '../config/delayed_job'
1420

15-
# Defina métodos para buscar e criar empresas
1621
def find_organization(name)
1722
organization = Organization.find_by(name: name)
1823
if organization
@@ -97,7 +102,25 @@ def remove_user_from_organization(github_login)
97102
end
98103
end
99104

100-
def help
105+
def add_github_user_slack_user(github_login, slack_user)
106+
user = GithubUser.find_by(github_login: github_login)
107+
108+
if user.nil?
109+
puts 'Github user not found'
110+
return
111+
end
112+
113+
user.update(slack_username: slack_user)
114+
SlackUsername2Id.fetch_id(github_login, slack_user)
115+
116+
if user.persisted?
117+
puts "Slack user linked to github user: #{user.inspect}"
118+
else
119+
puts "Failed to link slack user to github user: #{user.errors.full_messages.join(', ')}"
120+
end
121+
end
122+
123+
def help?
101124
puts <<~HELP
102125
Available commands:
103126
- find_organization(name)
@@ -106,6 +129,7 @@ def help
106129
- find_github_user(login)
107130
- add_user_in_organization(login, organization_name)
108131
- remove_user_from_organization(login)
132+
- add_github_user_slack_user(github_login, slack_user)
109133
110134
create_organization / edit_organization attributes:
111135
- contact_email: string
@@ -122,5 +146,4 @@ def help
122146
HELP
123147
end
124148

125-
# Inicie o console IRB
126149
IRB.start

bin/console.rb

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-License-Identifier: BSD-2-Clause
2+
#
3+
# 20240617121935_create_delayed_jobs.rb
4+
# Part of NetDEF CI System
5+
#
6+
# Copyright (c) 2024 by
7+
# Network Device Education Foundation, Inc. ("NetDEF")
8+
#
9+
# frozen_string_literal: true
10+
11+
class AddGithubUserSlackId < ActiveRecord::Migration[6.0]
12+
def change
13+
add_column :github_users, :slack_username, :string
14+
add_column :github_users, :slack_id, :string
15+
end
16+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.2].define(version: 2024_10_09_102350) do
13+
ActiveRecord::Schema[7.2].define(version: 2024_10_14_134659) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "plpgsql"
1616

@@ -108,6 +108,8 @@
108108
t.datetime "created_at", null: false
109109
t.datetime "updated_at", null: false
110110
t.bigint "organization_id"
111+
t.string "slack_username"
112+
t.string "slack_id"
111113
t.index ["github_id"], name: "index_github_users_on_github_id", unique: true
112114
t.index ["organization_id"], name: "index_github_users_on_organization_id"
113115
end

lib/github_ci_app.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
require_relative '../workers/ci_job_status'
4343
require_relative '../workers/timeout_execution'
4444
require_relative '../workers/ci_job_fetch_topotest_failures'
45+
require_relative '../workers/slack_username2_id'
4546

4647
# Slack libs
4748
require_relative 'slack/slack'

lib/helpers/request.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def download(uri, machine: 'ci1.netdef.org')
2828
http.request(req).body
2929
end
3030

31-
def get_request(uri, machine: 'ci1.netdef.org')
31+
def get_request(uri, machine: 'ci1.netdef.org', json: true)
3232
user, passwd = fetch_user_pass(machine)
3333
http = create_http(uri)
3434

@@ -40,7 +40,7 @@ def get_request(uri, machine: 'ci1.netdef.org')
4040
# Add JSON request header
4141
req.add_field 'Accept', 'application/json'
4242

43-
JSON.parse(http.request(req).body)
43+
json ? JSON.parse(http.request(req).body) : http.request(req).body
4444
rescue StandardError => e
4545
logger(Logger::ERROR, "HTTP GET Request failed (#{e.message}) for #{uri.host}")
4646
end

lib/slack_bot/slack_bot.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ def initialize
1919
@logger_manager << GithubLogger.instance.create('github_retry.log', Logger::INFO)
2020
end
2121

22+
def find_user_id_by_name(username)
23+
url = "#{GitHubApp::Configuration.instance.config['slack_bot_url']}/github/translate/#{username}"
24+
get_request(URI(url), json: false)
25+
end
26+
2227
def invalid_rerun_group(job)
2328
return unless current_execution?(job.check_suite)
2429

workers/slack_username2_id.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
# SPDX-License-Identifier: BSD-2-Clause
4+
#
5+
# slack_username2_id.rb
6+
# Part of NetDEF CI System
7+
#
8+
# Copyright (c) 2024 by
9+
# Network Device Education Foundation, Inc. ("NetDEF")
10+
#
11+
# frozen_string_literal: true
12+
13+
class SlackUsername2Id
14+
class << self
15+
def fetch_id(username, slack_name)
16+
@logger = GithubLogger.instance.create('slack_username_to_id.log', Logger::INFO)
17+
18+
user = GithubUser.find_by(github_login: username)
19+
20+
return false unless user
21+
22+
id = SlackBot.instance.find_user_id_by_name(slack_name)
23+
24+
@logger.info("Username: '#{username}' -> '#{id}'")
25+
26+
user.slack_id = id
27+
user.save
28+
29+
true
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)