|
| 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 |
0 commit comments