Skip to content

Commit

Permalink
Merge pull request #102 from RodrigoMNardi/feature/github/orgs
Browse files Browse the repository at this point in the history
Organization
  • Loading branch information
RodrigoMNardi authored Oct 19, 2024
2 parents e472ef8 + 04f1a96 commit 38fe0b2
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 40 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ AllCops:
Exclude:
- 'githubapi/**/*'
- 'db/schema.rb'
- 'bin/console'
Metrics/MethodLength:
Max: 20

Expand Down
1 change: 1 addition & 0 deletions .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ SimpleCov.start do
primary_coverage :branch
add_filter %r{^/(spec|config)/}
add_filter 'database_loader.rb'
add_filter 'workers/slack_username2_id.rb'
add_group 'Models', 'lib/models'
add_group 'GitHub Functions', 'lib/github'
add_group 'Bamboo CI Functions', 'lib/bamboo_ci'
Expand Down
149 changes: 149 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#!/usr/bin/env ruby
# SPDX-License-Identifier: BSD-2-Clause
#
# console
# Part of NetDEF CI System
#
# Copyright (c) 2023 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

require 'irb'

ENV['RAILS_ENV'] = ARGV.shift || 'production'

puts "Starting console: #{ENV.fetch('RAILS_ENV', nil)}"

require_relative '../config/setup'
require_relative '../config/delayed_job'

def find_organization(name)
organization = Organization.find_by(name: name)
if organization
puts "> #{organization.inspect}"
else
puts 'Organization not found'
end
end

def create_organization(name, attributes = {})
organization = Organization.create(name: name, **attributes)
if organization.persisted?
puts "Organization created: #{organization.inspect}"
else
puts "Failed to create organization: #{organization.errors.full_messages.join(', ')}"
end
end

def edit_organization(name, attributes = {})
organization = Organization.find_by(name: name)

if organization.nil?
puts 'Organization not found'
return
end

organization.update(**attributes)

if organization.persisted?
puts "Organization updated: #{organization.inspect}"
else
puts "Failed to create organization: #{organization.errors.full_messages.join(', ')}"
end
end

def find_github_user(login)
user = GithubUser.find_by(github_login: login)
if user
puts "> #{user.inspect}"
else
puts 'Github user not found'
end
end

def add_user_in_organization(github_login, organization_name)
user = GithubUser.find_by(github_login: github_login)
organization = Organization.find_by(name: organization_name)

if user.nil?
puts 'Github user not found'
return
end

if organization.nil?
puts 'Organization not found'
return
end

user.update(organization: organization)

if user.persisted?
puts "Github user linked to organization: #{user.inspect}"
else
puts "Failed to link github user to organization: #{user.errors.full_messages.join(', ')}"
end
end

def remove_user_from_organization(github_login)
user = GithubUser.find_by(github_login: github_login)

if user.nil?
puts 'Github user not found'
return
end

user.update(organization: nil)

if user.persisted?
puts "Github user removed from organization: #{user.inspect}"
else
puts "Failed to remove github user from organization: #{user.errors.full_messages.join(', ')}"
end
end

def add_github_user_slack_user(github_login, slack_user)
user = GithubUser.find_by(github_login: github_login)

if user.nil?
puts 'Github user not found'
return
end

user.update(slack_username: slack_user)
SlackUsername2Id.fetch_id(github_login, slack_user)

if user.persisted?
puts "Slack user linked to github user: #{user.inspect}"
else
puts "Failed to link slack user to github user: #{user.errors.full_messages.join(', ')}"
end
end

def help?
puts <<~HELP
Available commands:
- find_organization(name)
- create_organization(name, attributes = {})
- edit_organization(name, attributes = {})
- find_github_user(login)
- add_user_in_organization(login, organization_name)
- remove_user_from_organization(login)
- add_github_user_slack_user(github_login, slack_user)
create_organization / edit_organization attributes:
- contact_email: string
- contact_name: string
- url: string
Example:
- find_organization('NetDEF')
- create_organization('NetDEF', contact_name: 'Rodrigo Nardi')
- edit_organization('NetDEF', contact_name: 'Martin Winter')
- find_github_user('rodrigonardi')
- add_user_in_organization('rodrigonardi', 'NetDEF')
- remove_user_from_organization('rodrigonardi')
HELP
end

IRB.start
21 changes: 0 additions & 21 deletions bin/console.rb

This file was deleted.

22 changes: 22 additions & 0 deletions db/migrate/20241008100525_create_organization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20240617121935_create_delayed_jobs.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class CreateOrganization < ActiveRecord::Migration[6.0]
def change
create_table :organizations do |t|
t.string :name, null: false
t.string :contact_email
t.string :contact_name
t.string :url

t.timestamps null: false
end
end
end
15 changes: 15 additions & 0 deletions db/migrate/20241009102350_add_github_user_organization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20240617121935_create_delayed_jobs.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class AddGithubUserOrganization < ActiveRecord::Migration[6.0]
def change
add_reference :github_users, :organization, index: true, foreign_key: true
end
end
16 changes: 16 additions & 0 deletions db/migrate/20241014134659_add_github_user_slack_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20240617121935_create_delayed_jobs.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class AddGithubUserSlackId < ActiveRecord::Migration[6.0]
def change
add_column :github_users, :slack_username, :string
add_column :github_users, :slack_id, :string
end
end
16 changes: 15 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_09_24_140825) do
ActiveRecord::Schema[7.2].define(version: 2024_10_14_134659) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -107,7 +107,20 @@
t.string "organization_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "organization_id"
t.string "slack_username"
t.string "slack_id"
t.index ["github_id"], name: "index_github_users_on_github_id", unique: true
t.index ["organization_id"], name: "index_github_users_on_organization_id"
end

create_table "organizations", force: :cascade do |t|
t.string "name", null: false
t.string "contact_email"
t.string "contact_name"
t.string "url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "plans", force: :cascade do |t|
Expand Down Expand Up @@ -185,6 +198,7 @@
add_foreign_key "check_suites", "stages", column: "stopped_in_stage_id"
add_foreign_key "ci_jobs", "check_suites"
add_foreign_key "ci_jobs", "stages"
add_foreign_key "github_users", "organizations"
add_foreign_key "plans", "check_suites"
add_foreign_key "pull_request_subscriptions", "pull_requests"
add_foreign_key "pull_requests", "github_users"
Expand Down
18 changes: 2 additions & 16 deletions lib/github/update_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ def update_status
when 'success'
@job.success(@github_check)
@job.update_execution_time
slack_notify_success
else
failure
@job.update_execution_time
slack_notify_failure
end

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

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

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

Expand All @@ -99,7 +97,7 @@ def insert_new_delayed_job
end

def delete_and_create_delayed_job(queue)
fetch_delayed_job&.destroy_all
fetch_delayed_job.destroy_all

CiJobStatus
.delay(run_at: DELAYED_JOB_TIMER.seconds.from_now.utc, queue: queue)
Expand Down Expand Up @@ -132,18 +130,6 @@ def failure
.update(@job.id, 1)
end

def slack_notify_success
return unless current_execution?

SlackBot.instance.notify_success(@job)
end

def slack_notify_failure
return unless current_execution?

SlackBot.instance.notify_errors(@job)
end

def logger(severity, message)
@loggers.each do |logger_object|
logger_object.add(severity, message)
Expand Down
1 change: 1 addition & 0 deletions lib/github_ci_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
require_relative '../workers/ci_job_status'
require_relative '../workers/timeout_execution'
require_relative '../workers/ci_job_fetch_topotest_failures'
require_relative '../workers/slack_username2_id'

# Slack libs
require_relative 'slack/slack'
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def download(uri, machine: 'ci1.netdef.org')
http.request(req).body
end

def get_request(uri, machine: 'ci1.netdef.org')
def get_request(uri, machine: 'ci1.netdef.org', json: true)
user, passwd = fetch_user_pass(machine)
http = create_http(uri)

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

JSON.parse(http.request(req).body)
json ? JSON.parse(http.request(req).body) : http.request(req).body
rescue StandardError => e
logger(Logger::ERROR, "HTTP GET Request failed (#{e.message}) for #{uri.host}")
end
Expand Down
2 changes: 2 additions & 0 deletions lib/models/github_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ class GithubUser < ActiveRecord::Base
has_many :pull_requests, dependent: :nullify
has_many :check_suites, dependent: :nullify
has_many :audit_retries, dependent: :nullify

belongs_to :organization
end
23 changes: 23 additions & 0 deletions lib/models/organization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# audit_retry.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

require 'otr-activerecord'

class Organization < ActiveRecord::Base
has_many :github_users

# :nocov:
def inspect
"Organization id: #{id}, name: #{name}, contact_email: #{contact_email}, " \
"contact_name: #{contact_name}, url: #{url} " \
"created_at: #{created_at}, updated_at: #{updated_at}"
end
# :nocov:
end
5 changes: 5 additions & 0 deletions lib/slack_bot/slack_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def initialize
@logger_manager << GithubLogger.instance.create('github_retry.log', Logger::INFO)
end

def find_user_id_by_name(username)
url = "#{GitHubApp::Configuration.instance.config['slack_bot_url']}/github/translate/#{username}"
get_request(URI(url), json: false)
end

def invalid_rerun_group(job)
return unless current_execution?(job.check_suite)

Expand Down
Loading

0 comments on commit 38fe0b2

Please sign in to comment.