Sha256: 50b1abf641def2b8ae125755e1d06caa99cccaeb9cbddaa507491cd8f152ffe3

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 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!)
    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

4 entries across 4 versions & 1 rubygems

Version Path
shipit-engine-0.8.7 app/models/shipit/commit_deployment.rb
shipit-engine-0.8.6 app/models/shipit/commit_deployment.rb
shipit-engine-0.8.5 app/models/shipit/commit_deployment.rb
shipit-engine-0.8.4 app/models/shipit/commit_deployment.rb