Sha256: 31d4e5696e52c56422b696bb9def89da05d0fb3c77892b104fadf0b41e8b25b7

Contents?: true

Size: 1.77 KB

Versions: 12

Compression:

Stored size: 1.77 KB

Contents

module Shipit
  class CommitDeployment < ActiveRecord::Base
    belongs_to :commit
    belongs_to :task
    has_many :statuses, dependent: :destroy, class_name: 'CommitDeploymentStatus'

    after_commit :schedule_create_on_github, on: :create

    delegate :stack, :author, to: :task

    def create_on_github!
      return unless commit.pull_request?

      create_deployment_on_github!
      statuses.order(id: :asc).each(&:create_on_github!)
    rescue Octokit::NotFound, Octokit::Forbidden
      # If no one can create the deployment we can only give up
    end

    def create_deployment_on_github!
      return if github_id?

      response = begin
        create_deployment_on_github(author.github_api)
      rescue Octokit::ClientError
        raise if Shipit.github.api == author.github_api
        # If the deploy author didn't gave us the permission to create the deployment we falback the the main shipit
        # user.
        #
        # Octokit currently raise NotFound, but I'm convinced it should be Forbidden if the user can see the repository.
        # So to be future proof I catch boths.
        create_deployment_on_github(Shipit.github.api)
      end
      update!(github_id: response.id, api_url: response.url)
    end

    def pull_request_head
      pull_request = Shipit.github.api.pull_request(stack.github_repo_name, commit.pull_request_number)
      pull_request.head.sha
    end

    def schedule_create_on_github
      CreateOnGithubJob.perform_later(self)
    end

    private

    def create_deployment_on_github(client)
      client.create_deployment(
        stack.github_repo_name,
        pull_request_head,
        auto_merge: false,
        required_contexts: [],
        description: "Via Shipit",
        environment: stack.environment,
      )
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
shipit-engine-0.29.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.28.1 app/models/shipit/commit_deployment.rb
shipit-engine-0.28.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.27.1 app/models/shipit/commit_deployment.rb
shipit-engine-0.27.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.26.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.25.1 app/models/shipit/commit_deployment.rb
shipit-engine-0.25.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.24.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.23.1 app/models/shipit/commit_deployment.rb
shipit-engine-0.23.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.22.0 app/models/shipit/commit_deployment.rb