Class: CollavreGithub::Creatives::IntegrationsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- CollavreGithub::Creatives::IntegrationsController
- Includes:
- Collavre::IntegrationPermission, Collavre::IntegrationSetup
- Defined in:
- app/controllers/collavre_github/creatives/integrations_controller.rb
Instance Method Summary collapse
Instance Method Details
#destroy ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'app/controllers/collavre_github/creatives/integrations_controller.rb', line 131 def destroy unless @creative.(Current.user, :write) render json: { error: }, status: :forbidden return end account = Current.user.github_account unless account render json: { error: I18n.t("collavre_github.errors.not_connected") }, status: :unprocessable_entity return end repositories = Array(params[:repositories]).filter_map { |value| value.to_s.presence } repository = params[:repository].presence || params[:repository_full_name].presence scope = linked_repository_links(account) removed_repositories = [] if repositories.present? links_to_remove = scope.where(repository_full_name: repositories).to_a missing_repositories = repositories - links_to_remove.map(&:repository_full_name) if missing_repositories.present? render json: { error: I18n.t("collavre_github.errors.not_found") }, status: :not_found return end CollavreGithub::RepositoryLink.transaction do removed_repositories = links_to_remove.map(&:repository_full_name) links_to_remove.each(&:destroy!) end elsif repository link = scope.find_by(repository_full_name: repository) unless link render json: { error: I18n.t("collavre_github.errors.not_found") }, status: :not_found return end CollavreGithub::RepositoryLink.transaction do removed_repositories = [ link.repository_full_name ] link.destroy! end else CollavreGithub::RepositoryLink.transaction do removed_repositories = scope.pluck(:repository_full_name) scope.destroy_all end end if removed_repositories.present? CollavreGithub::WebhookProvisioner.remove_for_repositories( account: account, repositories: removed_repositories, webhook_url: github_webhook_url ) end links = linked_repository_links(account) render json: { success: true, selected_repositories: links.pluck(:repository_full_name), webhooks: serialize_webhooks(links) } end |
#resync ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/controllers/collavre_github/creatives/integrations_controller.rb', line 107 def resync account = Current.user.github_account unless account render json: { error: I18n.t("collavre_github.errors.not_connected") }, status: :unprocessable_entity return end repo = params[:repository] link = linked_repository_links(account).find_by(repository_full_name: repo) unless link&.markdown_sync_enabled? render json: { error: I18n.t("collavre_github.markdown_sync.not_enabled") }, status: :unprocessable_entity return end # Archive existing synced tree and re-import if link.markdown_root_creative link.markdown_root_creative.archive! if link.markdown_root_creative.respond_to?(:archive!) link.update!(markdown_root_creative_id: nil) end CollavreGithub::InitialMarkdownSyncJob.perform_later(link.id) render json: { success: true, message: I18n.t("collavre_github.markdown_sync.resync_started") } end |
#show ⇒ Object
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 |
# File 'app/controllers/collavre_github/creatives/integrations_controller.rb', line 12 def show account = Current.user.github_account links = linked_repository_links(account) # Also include repositories linked by other users for this creative all_links = @creative.github_repository_links.includes(:github_account) all_repositories = all_links.map(&:repository_full_name).uniq render json: { connected: account.present?, account: account && { login: account.login, name: account.name, avatar_url: account.avatar_url }, selected_repositories: links.map(&:repository_full_name), all_repositories: all_repositories, webhooks: serialize_webhooks(links), markdown_sync: links.each_with_object({}) { |l, h| h[l.repository_full_name] = { enabled: l.markdown_sync_enabled?, last_synced_at: l.last_synced_at, sync_branch: l.markdown_sync_branch } } } end |
#update ⇒ Object
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/controllers/collavre_github/creatives/integrations_controller.rb', line 40 def update account = Current.user.github_account unless account render json: { error: I18n.t("collavre_github.errors.not_connected") }, status: :unprocessable_entity return end integration_attributes = integration_params repositories = Array(integration_attributes[:repositories]).map(&:to_s).uniq links = nil CollavreGithub::RepositoryLink.transaction do linked_repository_links(account) .where.not(repository_full_name: repositories) .delete_all repositories.each do |full_name| @origin.github_repository_links.find_or_create_by!( github_account: account, repository_full_name: full_name ) end links = linked_repository_links(account).to_a end # Handle markdown sync toggle BEFORE webhook provisioning # so events_for() sees the updated markdown_sync_enabled state markdown_sync = integration_attributes[:markdown_sync] if markdown_sync.is_a?(ActionController::Parameters) || markdown_sync.is_a?(Hash) links.each do |link| repo = link.repository_full_name next unless markdown_sync.key?(repo) enabled = ActiveModel::Type::Boolean.new.cast(markdown_sync[repo]) was_enabled = link.markdown_sync_enabled? link.update!(markdown_sync_enabled: enabled) if enabled && !was_enabled CollavreGithub::InitialMarkdownSyncJob.perform_later(link.id) end end end CollavreGithub::WebhookProvisioner.ensure_for_links( account: account, links: links, webhook_url: github_webhook_url ) if links.present? render json: { success: true, selected_repositories: links.map(&:repository_full_name), webhooks: serialize_webhooks(links), markdown_sync: links.each_with_object({}) { |l, h| h[l.repository_full_name] = { enabled: l.markdown_sync_enabled?, last_synced_at: l.last_synced_at, sync_branch: l.markdown_sync_branch } } } rescue ActiveRecord::RecordInvalid => e render json: { error: e. }, status: :unprocessable_entity end |