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:, summary_path:) ⇒ 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

  • summary_path (String, nil)


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
60
61
62
# File 'lib/sashimi_tanpopo/provider/github.rb', line 31

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:,
               summary_path:)
  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
  @summary_path = summary_path || ""

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

Class Method Details

.generate_summary(changed_files:, dry_run:) ⇒ String

Parameters:

  • changed_files (Hash<String, { before_content: String, after_content: String, mode: String }>)

    key: f path, value: Hash

  • dry_run (Boolean)

Returns:

  • (String)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sashimi_tanpopo/provider/github.rb', line 109

def self.generate_summary(changed_files:, dry_run:)
  header = +"## :page_facing_up: sashimi_tanpopo report"
  header << " (dry run)" if dry_run

  lines = [header]

  if changed_files.empty?
    lines.push(
      "no changes",
      "",
    )
  else
    changed_files.each do |path, data|
      lines.push(
        "### :memo: #{path}",
        "```diff",
        SashimiTanpopo::DiffHelper.generate_diff(data[:before_content], data[:after_content], is_colored: false).rstrip,
        "```",
        "",
      )
    end
  end

  lines.join("\n")
end

.github_host(api_endpoint) ⇒ String

Get GitHub host from api_endpoint

Parameters:

  • api_endpoint (String)

Returns:

  • (String)


96
97
98
99
100
101
102
103
# File 'lib/sashimi_tanpopo/provider/github.rb', line 96

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sashimi_tanpopo/provider/github.rb', line 68

def perform
  changed_files = apply_recipe_files

  write_summary_file(changed_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