Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI Features #53

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions db/migrate/20240306093420_create_retry_stage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20231214093515_create_retry_stage.rb
# Part of NetDEF CI System
#
# Copyright (c) 2023 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class CreateRetryStage < ActiveRecord::Migration[6.0]
def change
create_table :retry_stages do |t|
t.text :failure_jobs, null: false
t.timestamps

t.references :check_suite, index: true, foreign_key: true
t.references :stage, index: true, foreign_key: true
end
end
end
20 changes: 20 additions & 0 deletions db/migrate/20240306110430_create_company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20231214093515_create_company.rb
# Part of NetDEF CI System
#
# Copyright (c) 2023 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class CreateCompany < ActiveRecord::Migration[6.0]
def change
create_table :companies do |t|
t.string :name, null: false
t.string :email, null: false
t.string :contact, null: false
t.timestamps
end
end
end
19 changes: 19 additions & 0 deletions db/migrate/20240306110450_create_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20231214093515_create_group.rb
# Part of NetDEF CI System
#
# Copyright (c) 2023 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class CreateGroup < ActiveRecord::Migration[6.0]
def change
create_table :groups do |t|
t.string :name, null: false
t.boolean :anonymous, null: false, default: true
t.timestamps
end
end
end
26 changes: 26 additions & 0 deletions db/migrate/20240306155801_create_feature.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20231214093515_create_feature.rb
# Part of NetDEF CI System
#
# Copyright (c) 2023 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class CreateFeature < ActiveRecord::Migration[6.0]
def change
create_table :features do |t|
t.boolean :rerun, null: false, default: true
t.integer :max_rerun_per_pull_request, null: false, default: 3
t.timestamps

t.references :group, index: true, foreign_key: true
end

community = Group.find_by(name: 'Community', anonymous: true)
community = Group.create(name: 'Community', anonymous: true) if community.nil?

Feature.create(group: community, rerun: true, max_rerun_per_pull_request: 3)
end
end
17 changes: 17 additions & 0 deletions db/migrate/20240308094702_alter_group_anonymous_to_public.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20240308094702_alter_group_anonymous_to_public.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class AlterGroupAnonymousToPublic < ActiveRecord::Migration[6.0]
def change
rename_column :groups, :anonymous, :public

add_index :groups, [:public], unique: true
end
end
15 changes: 15 additions & 0 deletions db/migrate/20240402114622_add_github_users_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# 20240402114622_add_github_users_group.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

class AddGithubUsersGroup < ActiveRecord::Migration[6.0]
def change
add_reference :github_users, :group, foreign_key: true
end
end
42 changes: 42 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@
t.datetime "updated_at", null: false
end

create_table "companies", force: :cascade do |t|
t.string "name", null: false
t.string "email", null: false
t.string "contact", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "features", force: :cascade do |t|
t.boolean "rerun", default: true, null: false
t.integer "max_rerun_per_pull_request", default: 3, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "group_id"
t.index ["group_id"], name: "index_features_on_group_id"
end


create_table "github_users", force: :cascade do |t|
t.string "github_login"
t.string "github_username"
Expand All @@ -106,7 +124,17 @@
t.string "organization_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "group_id"
t.index ["github_id"], name: "index_github_users_on_github_id", unique: true
t.index ["group_id"], name: "index_github_users_on_group_id"
end

create_table "groups", force: :cascade do |t|
t.string "name", null: false
t.boolean "public", default: true, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["public"], name: "index_groups_on_public", unique: true
end

create_table "plans", force: :cascade do |t|
Expand Down Expand Up @@ -141,6 +169,16 @@
t.index ["github_user_id"], name: "index_pull_requests_on_github_user_id"
end

create_table "retry_stages", force: :cascade do |t|
t.text "failure_jobs", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "check_suite_id"
t.bigint "stage_id"
t.index ["check_suite_id"], name: "index_retry_stages_on_check_suite_id"
t.index ["stage_id"], name: "index_retry_stages_on_stage_id"
end

create_table "stage_configurations", force: :cascade do |t|
t.string "bamboo_stage_name", null: false
t.string "github_check_run_name", null: false
Expand Down Expand Up @@ -183,9 +221,13 @@
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 "features", "groups"
add_foreign_key "github_users", "groups"
add_foreign_key "plans", "check_suites"
add_foreign_key "pull_request_subscriptions", "pull_requests"
add_foreign_key "pull_requests", "github_users"
add_foreign_key "retry_stages", "check_suites"
add_foreign_key "retry_stages", "stages"
add_foreign_key "stages", "check_suites"
add_foreign_key "stages", "stage_configurations"
add_foreign_key "topotest_failures", "ci_jobs"
Expand Down
15 changes: 12 additions & 3 deletions lib/github/build/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def create_summary(rerun: false)
def create_jobs(rerun)
@jobs.each do |job|
ci_job = create_ci_job(job)
next if ci_job.nil?

next if ci_job.nil?

Expand All @@ -66,6 +67,8 @@ def stage_with_start_in_progress(ci_job)
end

def create_ci_job(job)
logger(Logger::INFO, "create_jobs - #{job.inspect}")

stage_config = StageConfiguration.find_by(bamboo_stage_name: job[:stage])

return if stage_config.nil?
Expand All @@ -87,7 +90,9 @@ def create_check_run_stage(stage_config)

logger(Logger::INFO, ">>> Enqueued #{stage.inspect}")

stage.enqueue(@github, output: initial_output(stage))
stage_configure_status(stage, stage_config)

stage
end

def create_stage(stage_config)
Expand All @@ -99,13 +104,17 @@ def create_stage(stage_config)
status: 'queued',
name: name)

stage_configure_status(stage, stage_config)

stage
end

def stage_configure_status(stage, stage_config)
url = "https://ci1.netdef.org/browse/#{stage.check_suite.bamboo_ci_ref}"
output = { title: "#{stage.name} summary", summary: "Uninitialized stage\nDetails at [#{url}](#{url})" }

stage.enqueue(@github, output: output)
stage.in_progress(@github) if stage_config.start_in_progress?

stage
end

def initial_output(ci_job)
Expand Down
19 changes: 19 additions & 0 deletions lib/github/build/plan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: BSD-2-Clause
#
# plan.rb
# Part of NetDEF CI System
#
# Copyright (c) 2024 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true

module Github
module Build
class Plan
def initialize(user)
@user = user
end
end
end
end
2 changes: 2 additions & 0 deletions lib/github/build/retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def enqueued_stages
next if stage.nil?
next if stage.success?

RetryStage.create(check_suite: @check_suite, stage: stage, failure_jobs: stage.failure_jobs_output)

url = "https://ci1.netdef.org/browse/#{stage.check_suite.bamboo_ci_ref}"
output = { title: "#{stage.name} summary", summary: "Uninitialized stage\nDetails at [#{url}](#{url})" }

Expand Down
5 changes: 4 additions & 1 deletion lib/github/build_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,19 @@ def github_pr
end

def create_pull_request
@user = Github::UserInfo.new(@payload.dig('pull_request', 'user', 'id')).user

@pull_request =
PullRequest.create(
author: @payload.dig('pull_request', 'user', 'login'),
author: @user.github_login,
github_pr_id: github_pr,
branch_name: @payload.dig('pull_request', 'head', 'ref'),
repository: @payload.dig('repository', 'full_name'),
plan: fetch_plan
)

Github::UserInfo.new(@payload.dig('pull_request', 'user', 'id'), pull_request: @pull_request)
@pull_request
end

def start_new_execution
Expand Down
4 changes: 4 additions & 0 deletions lib/github/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
accept: Octokit::Preview::PREVIEW_TYPES[:reactions])
end

def comment_reaction_thumb_down(repo, comment_id)

Check warning on line 63 in lib/github/check.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Method `Github::Check#comment_reaction_thumb_down` is defined at both lib/github/check.rb:58 and lib/github/check.rb:63. Raw Output: lib/github/check.rb:63:5: W: Lint/DuplicateMethods: Method `Github::Check#comment_reaction_thumb_down` is defined at both lib/github/check.rb:58 and lib/github/check.rb:63.

Check warning on line 63 in lib/github/check.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Method `Github::Check#comment_reaction_thumb_down` is defined at both lib/github/check.rb:58 and lib/github/check.rb:63. Raw Output: lib/github/check.rb:63:5: W: Lint/DuplicateMethods: Method `Github::Check#comment_reaction_thumb_down` is defined at both lib/github/check.rb:58 and lib/github/check.rb:63.
@app.create_issue_comment_reaction(repo, comment_id, '-1')
end

def create(name)
@app.create_check_run(
@check_suite.pull_request.repository,
Expand Down
52 changes: 51 additions & 1 deletion lib/github/re_run/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,50 @@ def initialize(payload, logger_level: Logger::INFO)
@logger_manager << GithubLogger.instance.create('github_app.log', logger_level)

@payload = payload

@user = Github::UserInfo.new(@payload.dig('sender', 'id')).user
end

private

def valid_user_and_payload?(github_user)
[email protected]? or @payload.nil? or @payload.empty? or github_user.nil? or github_user.empty?
end

def notify_error_rerun(comment_id: nil)
@github_check = Github::Check.new(nil)

comment_thumb_down(comment_id) unless comment_id.nil?

logger(Logger::WARN, 'No permission to run')

[402, 'No permission to run']
end

def comment_thumb_down(comment_id)
@github_check.comment_reaction_thumb_down(repo, comment_id)
end

def reach_max_rerun_per_pull_request?
max_rerun = @user.group.feature.max_rerun_per_pull_request

return false if max_rerun.zero?

github_check = Github::Check.new(nil)
pull_request_info = github_check.pull_request_info(pr_id, repo)

if max_rerun <
CheckSuite.where(work_branch: pull_request_info.dig(:head, :ref), re_run: true).count
return true
end

false
end

def can_rerun?
@user.group.feature.rerun
end

def fetch_run_ci_by_pr
CheckSuite
.joins(:pull_request)
Expand Down Expand Up @@ -139,7 +179,17 @@ def action
end

def pr_id
@payload.dig('issue', 'number') || @payload.dig('check_suite', 'pull_requests')&.last&.[]('number')
pr_id_from_issue || pr_id_from_check_suite
end

def pr_id_from_issue
@payload.dig('issue', 'number')
end

def pr_id_from_check_suite
return if @payload.dig('check_suite', 'pull_requests').nil?

@payload.dig('check_suite', 'pull_requests').last&.[]('number')
end

def repo
Expand Down
Loading
Loading