File: project_export_job.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (66 lines) | stat: -rw-r--r-- 1,563 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

class ProjectExportJob < ApplicationRecord
  include EachBatch
  include AfterCommitQueue

  EXPIRES_IN = 7.days

  belongs_to :project
  belongs_to :user
  has_many :relation_exports, class_name: 'Projects::ImportExport::RelationExport'

  validates :project, :jid, :status, presence: true

  STATUS = {
    queued: 0,
    started: 1,
    finished: 2,
    failed: 3
  }.freeze

  scope :prunable, -> { where("updated_at < ?", EXPIRES_IN.ago) }
  scope :order_by_updated_at, -> { order(:updated_at, :id) }
  scope :by_user_id, ->(user_id) { where(user_id: user_id) }

  state_machine :status, initial: :queued do
    event :start do
      transition [:queued] => :started
    end

    event :finish do
      transition [:started] => :finished
    end

    event :fail_op do
      transition [:queued, :started] => :failed
    end

    state :queued, value: STATUS[:queued]
    state :started, value: STATUS[:started]
    state :finished, value: STATUS[:finished]
    state :failed, value: STATUS[:failed]

    after_transition any => :finished do |export_job|
      export_job.run_after_commit_or_now do
        audit_project_exported
      end
    end
  end

  private

  def audit_project_exported
    return if exported_by_admin? && Gitlab::CurrentSettings.silent_admin_exports_enabled?

    audit_context = {
      name: 'project_export_created',
      author: user,
      scope: project,
      target: project,
      message: 'Profile file export was created'
    }

    ::Gitlab::Audit::Auditor.audit(audit_context)
  end
end