Sha256: 5f96f314d7691d94f020c1872c2f920e8e5054a04652c2f26e83c89ff61e4b36

Contents?: true

Size: 1.78 KB

Versions: 17

Compression:

Stored size: 1.78 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::NotFound, Octokit::Forbidden
        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

17 entries across 17 versions & 1 rubygems

Version Path
shipit-engine-0.20.1 app/models/shipit/commit_deployment.rb
shipit-engine-0.20.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.19.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.18.1 app/models/shipit/commit_deployment.rb
shipit-engine-0.18.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.17.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.16.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.15.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.14.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.13.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.12.1 app/models/shipit/commit_deployment.rb
shipit-engine-0.12.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.11.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.10.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.9.0 app/models/shipit/commit_deployment.rb
shipit-engine-0.8.9 app/models/shipit/commit_deployment.rb
shipit-engine-0.8.8 app/models/shipit/commit_deployment.rb