Sha256: 34fedbcceef3c33c01938b8bc9755644c82e2a7846dc5ebe2255c28b559f0b47

Contents?: true

Size: 1.8 KB

Versions: 7

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true
module Shipit
  class CommitDeploymentStatus < Record
    DESCRIPTION_CHARACTER_LIMIT_ON_GITHUB = 140

    belongs_to :commit_deployment

    after_commit :schedule_create_on_github, on: :create

    delegate :stack, :task, :author, to: :commit_deployment

    def create_on_github!
      return if github_id?
      response = begin
        create_status_on_github(stack.github_api)
      rescue Octokit::ClientError
        raise if Shipit.github(organization: stack.repository.owner).api == stack.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_status_on_github(stack.github_api)
      end
      update!(github_id: response.id, api_url: response.url)
    end

    def description
      I18n.t(
        "deployment_description.#{task_type}.#{status}",
        sha: task.until_commit.short_sha,
        author: task.author.login,
        stack: stack.to_param,
      )
    end

    def task_type
      task.class.name.demodulize.underscore
    end

    def schedule_create_on_github
      CreateOnGithubJob.perform_later(commit_deployment)
    end

    private

    def create_status_on_github(client)
      client.create_deployment_status(
        commit_deployment.api_url,
        status,
        accept: 'application/vnd.github.flash-preview+json',
        target_url: url_helpers.stack_deploy_url(stack, task),
        description: description.truncate(DESCRIPTION_CHARACTER_LIMIT_ON_GITHUB),
        environment_url: stack.deploy_url,
      )
    end

    def url_helpers
      Engine.routes.url_helpers
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
shipit-engine-0.38.0 app/models/shipit/commit_deployment_status.rb
shipit-engine-0.37.0 app/models/shipit/commit_deployment_status.rb
shipit-engine-0.36.1 app/models/shipit/commit_deployment_status.rb
shipit-engine-0.36.0 app/models/shipit/commit_deployment_status.rb
shipit-engine-0.35.1 app/models/shipit/commit_deployment_status.rb
shipit-engine-0.35.0 app/models/shipit/commit_deployment_status.rb
shipit-engine-0.34.0 app/models/shipit/commit_deployment_status.rb