summaryrefslogtreecommitdiff
path: root/app/services/snippets/update_service.rb
blob: 874357f36cca9ad5bfa14006733d815804af9694 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

module Snippets
  class UpdateService < Snippets::BaseService
    include SpamCheckMethods

    UpdateError = Class.new(StandardError)
    CreateRepositoryError = Class.new(StandardError)

    def execute(snippet)
      # check that user is allowed to set specified visibility_level
      new_visibility = visibility_level

      if new_visibility && new_visibility.to_i != snippet.visibility_level
        unless Gitlab::VisibilityLevel.allowed_for?(current_user, new_visibility)
          deny_visibility_level(snippet, new_visibility)

          return snippet_error_response(snippet, 403)
        end
      end

      filter_spam_check_params
      snippet.assign_attributes(params)
      spam_check(snippet, current_user)

      if save_and_commit(snippet)
        Gitlab::UsageDataCounters::SnippetCounter.count(:update)

        ServiceResponse.success(payload: { snippet: snippet } )
      else
        snippet_error_response(snippet, 400)
      end
    end

    private

    def save_and_commit(snippet)
      snippet.with_transaction_returning_status do
        snippet.save.tap do |saved|
          break false unless saved

          # In order to avoid non migrated snippets scenarios,
          # if the snippet does not have a repository we created it
          # We don't need to check if the repository exists
          # because `create_repository` already handles it
          if Feature.enabled?(:version_snippets, current_user)
            create_repository_for(snippet)
          end

          # If the snippet repository exists we commit always
          # the changes
          create_commit(snippet) if snippet.repository_exists?
        end
      rescue
        snippet.errors.add(:repository, 'Error updating the snippet')

        false
      end
    end

    def create_repository_for(snippet)
      snippet.create_repository

      raise CreateRepositoryError, 'Repository could not be created' unless snippet.repository_exists?
    end

    def create_commit(snippet)
      raise UpdateError unless snippet.snippet_repository

      commit_attrs = {
        branch_name: 'master',
        message: 'Update snippet'
      }

      snippet.snippet_repository.multi_files_action(current_user, snippet_files(snippet), commit_attrs)
    end

    def snippet_files(snippet)
      [{ previous_path: snippet.blobs.first&.path,
         file_path: params[:file_name],
         content: params[:content] }]
    end
  end
end