Skip to content

Commit 38fe0b2

Browse files
Merge pull request #102 from RodrigoMNardi/feature/github/orgs
Organization
2 parents e472ef8 + 04f1a96 commit 38fe0b2

16 files changed

+325
-40
lines changed

.rubocop.yml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ AllCops:
1010
Exclude:
1111
- 'githubapi/**/*'
1212
- 'db/schema.rb'
13+
- 'bin/console'
1314
Metrics/MethodLength:
1415
Max: 20
1516

.simplecov

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ SimpleCov.start do
1313
primary_coverage :branch
1414
add_filter %r{^/(spec|config)/}
1515
add_filter 'database_loader.rb'
16+
add_filter 'workers/slack_username2_id.rb'
1617
add_group 'Models', 'lib/models'
1718
add_group 'GitHub Functions', 'lib/github'
1819
add_group 'Bamboo CI Functions', 'lib/bamboo_ci'

bin/console

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/usr/bin/env ruby
2+
# SPDX-License-Identifier: BSD-2-Clause
3+
#
4+
# console
5+
# Part of NetDEF CI System
6+
#
7+
# Copyright (c) 2023 by
8+
# Network Device Education Foundation, Inc. ("NetDEF")
9+
#
10+
# frozen_string_literal: true
11+
12+
require 'irb'
13+
14+
ENV['RAILS_ENV'] = ARGV.shift || 'production'
15+
16+
puts "Starting console: #{ENV.fetch('RAILS_ENV', nil)}"
17+
18+
require_relative '../config/setup'
19+
require_relative '../config/delayed_job'
20+
21+
def find_organization(name)
22+
organization = Organization.find_by(name: name)
23+
if organization
24+
puts "> #{organization.inspect}"
25+
else
26+
puts 'Organization not found'
27+
end
28+
end
29+
30+
def create_organization(name, attributes = {})
31+
organization = Organization.create(name: name, **attributes)
32+
if organization.persisted?
33+
puts "Organization created: #{organization.inspect}"
34+
else
35+
puts "Failed to create organization: #{organization.errors.full_messages.join(', ')}"
36+
end
37+
end
38+
39+
def edit_organization(name, attributes = {})
40+
organization = Organization.find_by(name: name)
41+
42+
if organization.nil?
43+
puts 'Organization not found'
44+
return
45+
end
46+
47+
organization.update(**attributes)
48+
49+
if organization.persisted?
50+
puts "Organization updated: #{organization.inspect}"
51+
else
52+
puts "Failed to create organization: #{organization.errors.full_messages.join(', ')}"
53+
end
54+
end
55+
56+
def find_github_user(login)
57+
user = GithubUser.find_by(github_login: login)
58+
if user
59+
puts "> #{user.inspect}"
60+
else
61+
puts 'Github user not found'
62+
end
63+
end
64+
65+
def add_user_in_organization(github_login, organization_name)
66+
user = GithubUser.find_by(github_login: github_login)
67+
organization = Organization.find_by(name: organization_name)
68+
69+
if user.nil?
70+
puts 'Github user not found'
71+
return
72+
end
73+
74+
if organization.nil?
75+
puts 'Organization not found'
76+
return
77+
end
78+
79+
user.update(organization: organization)
80+
81+
if user.persisted?
82+
puts "Github user linked to organization: #{user.inspect}"
83+
else
84+
puts "Failed to link github user to organization: #{user.errors.full_messages.join(', ')}"
85+
end
86+
end
87+
88+
def remove_user_from_organization(github_login)
89+
user = GithubUser.find_by(github_login: github_login)
90+
91+
if user.nil?
92+
puts 'Github user not found'
93+
return
94+
end
95+
96+
user.update(organization: nil)
97+
98+
if user.persisted?
99+
puts "Github user removed from organization: #{user.inspect}"
100+
else
101+
puts "Failed to remove github user from organization: #{user.errors.full_messages.join(', ')}"
102+
end
103+
end
104+
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?
124+
puts <<~HELP
125+
Available commands:
126+
- find_organization(name)
127+
- create_organization(name, attributes = {})
128+
- edit_organization(name, attributes = {})
129+
- find_github_user(login)
130+
- add_user_in_organization(login, organization_name)
131+
- remove_user_from_organization(login)
132+
- add_github_user_slack_user(github_login, slack_user)
133+
134+
create_organization / edit_organization attributes:
135+
- contact_email: string
136+
- contact_name: string
137+
- url: string
138+
139+
Example:
140+
- find_organization('NetDEF')
141+
- create_organization('NetDEF', contact_name: 'Rodrigo Nardi')
142+
- edit_organization('NetDEF', contact_name: 'Martin Winter')
143+
- find_github_user('rodrigonardi')
144+
- add_user_in_organization('rodrigonardi', 'NetDEF')
145+
- remove_user_from_organization('rodrigonardi')
146+
HELP
147+
end
148+
149+
IRB.start

bin/console.rb

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 CreateOrganization < ActiveRecord::Migration[6.0]
12+
def change
13+
create_table :organizations do |t|
14+
t.string :name, null: false
15+
t.string :contact_email
16+
t.string :contact_name
17+
t.string :url
18+
19+
t.timestamps null: false
20+
end
21+
end
22+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 AddGithubUserOrganization < ActiveRecord::Migration[6.0]
12+
def change
13+
add_reference :github_users, :organization, index: true, foreign_key: true
14+
end
15+
end
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

+15-1
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_09_24_140825) 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

@@ -107,7 +107,20 @@
107107
t.string "organization_url"
108108
t.datetime "created_at", null: false
109109
t.datetime "updated_at", null: false
110+
t.bigint "organization_id"
111+
t.string "slack_username"
112+
t.string "slack_id"
110113
t.index ["github_id"], name: "index_github_users_on_github_id", unique: true
114+
t.index ["organization_id"], name: "index_github_users_on_organization_id"
115+
end
116+
117+
create_table "organizations", force: :cascade do |t|
118+
t.string "name", null: false
119+
t.string "contact_email"
120+
t.string "contact_name"
121+
t.string "url"
122+
t.datetime "created_at", null: false
123+
t.datetime "updated_at", null: false
111124
end
112125

113126
create_table "plans", force: :cascade do |t|
@@ -185,6 +198,7 @@
185198
add_foreign_key "check_suites", "stages", column: "stopped_in_stage_id"
186199
add_foreign_key "ci_jobs", "check_suites"
187200
add_foreign_key "ci_jobs", "stages"
201+
add_foreign_key "github_users", "organizations"
188202
add_foreign_key "plans", "check_suites"
189203
add_foreign_key "pull_request_subscriptions", "pull_requests"
190204
add_foreign_key "pull_requests", "github_users"

lib/github/update_status.rb

+2-16
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,9 @@ def update_status
6464
when 'success'
6565
@job.success(@github_check)
6666
@job.update_execution_time
67-
slack_notify_success
6867
else
6968
failure
7069
@job.update_execution_time
71-
slack_notify_failure
7270
end
7371

7472
return [200, 'Success'] unless @job.check_suite.pull_request.current_execution? @job.check_suite
@@ -83,7 +81,7 @@ def update_status
8381
end
8482

8583
def create_timeout_worker
86-
Delayed::Job.where('handler LIKE ?', "%TimeoutExecution%args%-%#{@check_suite.id}%")&.delete_all
84+
Delayed::Job.where('handler LIKE ?', "%TimeoutExecution%args%-%#{@check_suite.id}%").delete_all
8785

8886
logger(Logger::INFO, "CiJobStatus::Update: TimeoutExecution for '#{@check_suite.id}'")
8987

@@ -99,7 +97,7 @@ def insert_new_delayed_job
9997
end
10098

10199
def delete_and_create_delayed_job(queue)
102-
fetch_delayed_job&.destroy_all
100+
fetch_delayed_job.destroy_all
103101

104102
CiJobStatus
105103
.delay(run_at: DELAYED_JOB_TIMER.seconds.from_now.utc, queue: queue)
@@ -132,18 +130,6 @@ def failure
132130
.update(@job.id, 1)
133131
end
134132

135-
def slack_notify_success
136-
return unless current_execution?
137-
138-
SlackBot.instance.notify_success(@job)
139-
end
140-
141-
def slack_notify_failure
142-
return unless current_execution?
143-
144-
SlackBot.instance.notify_errors(@job)
145-
end
146-
147133
def logger(severity, message)
148134
@loggers.each do |logger_object|
149135
logger_object.add(severity, message)

lib/github_ci_app.rb

+1
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

+2-2
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/models/github_user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ class GithubUser < ActiveRecord::Base
1414
has_many :pull_requests, dependent: :nullify
1515
has_many :check_suites, dependent: :nullify
1616
has_many :audit_retries, dependent: :nullify
17+
18+
belongs_to :organization
1719
end

lib/models/organization.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SPDX-License-Identifier: BSD-2-Clause
2+
#
3+
# audit_retry.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+
require 'otr-activerecord'
12+
13+
class Organization < ActiveRecord::Base
14+
has_many :github_users
15+
16+
# :nocov:
17+
def inspect
18+
"Organization id: #{id}, name: #{name}, contact_email: #{contact_email}, " \
19+
"contact_name: #{contact_name}, url: #{url} " \
20+
"created_at: #{created_at}, updated_at: #{updated_at}"
21+
end
22+
# :nocov:
23+
end

lib/slack_bot/slack_bot.rb

+5
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

0 commit comments

Comments
 (0)