Class: SashimiTanpopo::Provider::GitHub

Inherits:
Base
  • Object
show all
Defined in:
lib/sashimi_tanpopo/provider/github.rb

Overview

Apply recipe files and create Pull Request

Constant Summary collapse

DEFAULT_API_ENDPOINT =
"https://api.github.com/"
DEFAULT_GITHUB_HOST =
"github.com"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#apply_recipe_files

Constructor Details

#initialize(recipe_paths:, target_dir:, params:, dry_run:, is_colored:, git_username:, git_email:, commit_message:, repository:, access_token:, api_endpoint: DEFAULT_API_ENDPOINT, pr_title:, pr_body:, pr_source_branch:, pr_target_branch:, pr_assignees: [], pr_reviewers: [], pr_labels: [], is_draft_pr:) ⇒ GitHub

Returns a new instance of GitHub.

Parameters:

  • recipe_paths (Array<String>)
  • target_dir (String, nil)
  • params (Hash<Symbol, String>)
  • dry_run (Boolean)
  • is_colored (Boolean)

    Whether show color diff

  • git_username (String, nil)
  • git_email (String, nil)
  • commit_message (String)
  • repository (String)
  • access_token (String)
  • api_endpoint (String) (defaults to: DEFAULT_API_ENDPOINT)
  • pr_title (String)
  • pr_body (String)
  • pr_source_branch (String)

    Pull Request source branch (a.k.a. head branch)

  • pr_target_branch (String, nil)

    Pull Request target branch (a.k.a. base branch)

  • pr_assignees (Array<String>) (defaults to: [])
  • pr_reviewers (Array<String>) (defaults to: [])
  • pr_labels (Array<String>) (defaults to: [])
  • is_draft_pr (Boolean)

    Whether create draft Pull Request



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sashimi_tanpopo/provider/github.rb', line 30

def initialize(recipe_paths:, target_dir:, params:, dry_run:, is_colored:,
               git_username:, git_email:, commit_message:,
               repository:, access_token:, api_endpoint: DEFAULT_API_ENDPOINT,
               pr_title:, pr_body:, pr_source_branch:, pr_target_branch:,
               pr_assignees: [], pr_reviewers: [], pr_labels: [], is_draft_pr:)
  super(
    recipe_paths:    recipe_paths,
    target_dir:      target_dir,
    params:          params,
    dry_run:         dry_run,
    is_colored:      is_colored,
    is_update_local: false,
  )

  @commit_message = commit_message
  @repository = repository
  @pr_title = pr_title
  @pr_body = pr_body
  @pr_source_branch = pr_source_branch
  @pr_target_branch = pr_target_branch
  @pr_assignees = pr_assignees
  @pr_reviewers = pr_reviewers
  @pr_labels = pr_labels
  @is_draft_pr = is_draft_pr
  @git_username = git_username
  @git_email = git_email
  @api_endpoint = api_endpoint

  @client = Octokit::Client.new(api_endpoint: api_endpoint, access_token: access_token)
end

Class Method Details

.github_host(api_endpoint) ⇒ String

Get GitHub host from api_endpoint

Parameters:

  • api_endpoint (String)

Returns:

  • (String)


91
92
93
94
95
96
97
98
# File 'lib/sashimi_tanpopo/provider/github.rb', line 91

def self.github_host(api_endpoint)
  return DEFAULT_GITHUB_HOST if api_endpoint == DEFAULT_API_ENDPOINT

  matched = %r{^https?://(.+)/api}.match(api_endpoint)
  return matched[1] if matched # steep:ignore

  DEFAULT_GITHUB_HOST
end

Instance Method Details

#performString?

Apply recipe files

Returns:

  • (String)

    Created Pull Request URL

  • (nil)

    Pull Request isn’t created



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sashimi_tanpopo/provider/github.rb', line 65

def perform
  changed_files = apply_recipe_files

  return nil if changed_files.empty? || @dry_run

  if exists_branch?(@pr_source_branch)
    SashimiTanpopo.logger.info "Skipped because branch #{@pr_source_branch} already exists on #{@repository}"
    return nil
  end

  create_branch_and_push_changes(changed_files)

  pr = create_pull_request

  add_pr_labels(pr[:number])
  add_pr_assignees(pr[:number])
  add_pr_reviewers(pr[:number])

  pr[:html_url]
end