|
| 1 | +# SPDX-License-Identifier: BSD-2-Clause |
| 2 | +# |
| 3 | +# check_suite_finished.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_relative '../../slack_bot/slack_bot' |
| 12 | +require_relative '../../github/build/action' |
| 13 | +require_relative '../../github/build/summary' |
| 14 | + |
| 15 | +module Github |
| 16 | + module PlanExecution |
| 17 | + class Finished |
| 18 | + include BambooCi::Api |
| 19 | + |
| 20 | + def initialize(payload) |
| 21 | + @check_suite = CheckSuite.find_by(bamboo_ci_ref: payload['bamboo_ref']) |
| 22 | + @logger = GithubLogger.instance.create('github_plan_execution_finished.log', Logger::INFO) |
| 23 | + end |
| 24 | + |
| 25 | + def finished |
| 26 | + @logger.info ">>> Check Suite: #{@check_suite.inspect}" |
| 27 | + |
| 28 | + return [404, 'Check Suite not found'] if @check_suite.nil? |
| 29 | + |
| 30 | + fetch_ci_execution |
| 31 | + build_status = fetch_build_status |
| 32 | + |
| 33 | + @logger.info ">>> build_status: #{build_status.inspect}" |
| 34 | + |
| 35 | + return [200, 'Still running'] if in_progress?(build_status) |
| 36 | + |
| 37 | + check_stages |
| 38 | + clear_deleted_jobs |
| 39 | + |
| 40 | + [200, 'Finished'] |
| 41 | + end |
| 42 | + |
| 43 | + private |
| 44 | + |
| 45 | + # This method will move all tests that no longer exist in BambooCI to the skipped state, |
| 46 | + # because there are no executions for them. |
| 47 | + def clear_deleted_jobs |
| 48 | + github_check = Github::Check.new(@check_suite) |
| 49 | + |
| 50 | + @check_suite.ci_jobs.where(status: %w[queued in_progress]).each do |ci_job| |
| 51 | + ci_job.skipped(github_check) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + # Checks if CI still running |
| 56 | + def in_progress?(build_status) |
| 57 | + @logger.info ">>> ci_stopped?: #{ci_stopped?(build_status)}" |
| 58 | + @logger.info ">>> ci_hanged?: #{ci_hanged?(build_status)}" |
| 59 | + |
| 60 | + return false if ci_hanged?(build_status) |
| 61 | + return false if build_status['currentStage'].casecmp('final').zero? |
| 62 | + |
| 63 | + true |
| 64 | + end |
| 65 | + |
| 66 | + def ci_stopped?(build_status) |
| 67 | + build_status.key?('message') and !build_status.key?('finished') |
| 68 | + end |
| 69 | + |
| 70 | + def ci_hanged?(build_status) |
| 71 | + return true if ci_stopped?(build_status) |
| 72 | + |
| 73 | + build_status.dig('progress', 'percentageCompleted').to_f >= 2.0 |
| 74 | + end |
| 75 | + |
| 76 | + def update_stage_status(ci_job, result, github) |
| 77 | + return if ci_job.nil? || (ci_job.finished? && !ci_job.job_ref.nil?) |
| 78 | + |
| 79 | + update_ci_job_status(github, ci_job, result['state']) |
| 80 | + end |
| 81 | + |
| 82 | + def update_ci_job_status(github_check, ci_job, state) |
| 83 | + ci_job.enqueue(github_check) if ci_job.job_ref.nil? |
| 84 | + |
| 85 | + output = create_output_message(ci_job) |
| 86 | + |
| 87 | + case state |
| 88 | + when 'Unknown' |
| 89 | + ci_job.cancelled(github_check, output: output, agent: 'WatchDog') |
| 90 | + slack_notify_cancelled(ci_job) |
| 91 | + when 'Failed' |
| 92 | + ci_job.failure(github_check, output: output, agent: 'WatchDog') |
| 93 | + slack_notify_failure(ci_job) |
| 94 | + when 'Successful' |
| 95 | + ci_job.success(github_check, output: output, agent: 'WatchDog') |
| 96 | + slack_notify_success(ci_job) |
| 97 | + else |
| 98 | + puts 'Ignored' |
| 99 | + end |
| 100 | + |
| 101 | + build_summary(ci_job) |
| 102 | + end |
| 103 | + |
| 104 | + def create_output_message(ci_job) |
| 105 | + url = "https://ci1.netdef.org/browse/#{ci_job.job_ref}" |
| 106 | + |
| 107 | + { |
| 108 | + title: ci_job.name, |
| 109 | + summary: "Details at [#{url}](#{url})\nUnfortunately we were unable to access the execution results." |
| 110 | + } |
| 111 | + end |
| 112 | + |
| 113 | + def build_summary(ci_job) |
| 114 | + summary = Github::Build::Summary.new(ci_job, agent: 'WatchDog') |
| 115 | + summary.build_summary |
| 116 | + |
| 117 | + finished_execution?(ci_job.check_suite) |
| 118 | + end |
| 119 | + |
| 120 | + def finished_execution?(check_suite) |
| 121 | + return false unless current_execution?(check_suite) |
| 122 | + return false unless check_suite.finished? |
| 123 | + |
| 124 | + SlackBot.instance.execution_finished_notification(check_suite) |
| 125 | + end |
| 126 | + |
| 127 | + def current_execution?(check_suite) |
| 128 | + pull_request = check_suite.pull_request |
| 129 | + last_check_suite = pull_request.check_suites.reload.all.order(:created_at).last |
| 130 | + |
| 131 | + check_suite.id == last_check_suite.id |
| 132 | + end |
| 133 | + |
| 134 | + def slack_notify_success(job) |
| 135 | + SlackBot.instance.notify_success(job) |
| 136 | + end |
| 137 | + |
| 138 | + def slack_notify_failure(job) |
| 139 | + SlackBot.instance.notify_errors(job) |
| 140 | + end |
| 141 | + |
| 142 | + def slack_notify_cancelled(job) |
| 143 | + SlackBot.instance.notify_cancelled(job) |
| 144 | + end |
| 145 | + |
| 146 | + def check_stages |
| 147 | + github_check = Github::Check.new(@check_suite) |
| 148 | + @logger.info ">>> @result: #{@result.inspect}" |
| 149 | + @result.dig('stages', 'stage').each do |stage| |
| 150 | + stage.dig('results', 'result').each do |result| |
| 151 | + ci_job = CiJob.find_by(job_ref: result['buildResultKey'], check_suite_id: @check_suite.id) |
| 152 | + |
| 153 | + update_stage_status(ci_job, result, github_check) |
| 154 | + end |
| 155 | + end |
| 156 | + end |
| 157 | + |
| 158 | + def fetch_ci_execution |
| 159 | + @result = get_status(@check_suite.bamboo_ci_ref) |
| 160 | + end |
| 161 | + |
| 162 | + def fetch_build_status |
| 163 | + get_request(URI("https://127.0.0.1/rest/api/latest/result/status/#{@check_suite.bamboo_ci_ref}")) |
| 164 | + end |
| 165 | + end |
| 166 | + end |
| 167 | +end |
0 commit comments