Class: EasyChangelog::Entry

Inherits:
Struct
  • Object
show all
Defined in:
lib/easy_changelog/entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, body: last_commit_title, ref_type: nil, ref_id: nil, card_id: nil, cards_url: nil, user: github_user, branch_name: nil) ⇒ Entry

Returns a new instance of Entry.



5
6
7
8
9
10
11
12
13
# File 'lib/easy_changelog/entry.rb', line 5

def initialize(type:, body: last_commit_title, ref_type: nil, ref_id: nil, card_id: nil, cards_url: nil,
               user: github_user, branch_name: nil)
  id, body = EasyChangelog::Utility.extract_id(body)
  ref_id ||= id || last_commit_id
  ref_type ||= id ? :pull : :commit
  card_id ||= EasyChangelog::Utility.discover_card_id(branch_name)

  super
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



4
5
6
# File 'lib/easy_changelog/entry.rb', line 4

def body
  @body
end

#branch_nameObject

Returns the value of attribute branch_name

Returns:

  • (Object)

    the current value of branch_name



4
5
6
# File 'lib/easy_changelog/entry.rb', line 4

def branch_name
  @branch_name
end

#card_idObject

Returns the value of attribute card_id

Returns:

  • (Object)

    the current value of card_id



4
5
6
# File 'lib/easy_changelog/entry.rb', line 4

def card_id
  @card_id
end

#cards_urlObject

Returns the value of attribute cards_url

Returns:

  • (Object)

    the current value of cards_url



4
5
6
# File 'lib/easy_changelog/entry.rb', line 4

def cards_url
  @cards_url
end

#ref_idObject

Returns the value of attribute ref_id

Returns:

  • (Object)

    the current value of ref_id



4
5
6
# File 'lib/easy_changelog/entry.rb', line 4

def ref_id
  @ref_id
end

#ref_typeObject

Returns the value of attribute ref_type

Returns:

  • (Object)

    the current value of ref_type



4
5
6
# File 'lib/easy_changelog/entry.rb', line 4

def ref_type
  @ref_type
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



4
5
6
# File 'lib/easy_changelog/entry.rb', line 4

def type
  @type
end

#userObject

Returns the value of attribute user

Returns:

  • (Object)

    the current value of user



4
5
6
# File 'lib/easy_changelog/entry.rb', line 4

def user
  @user
end

Instance Method Details

#card_refObject



44
45
46
47
48
49
50
51
52
# File 'lib/easy_changelog/entry.rb', line 44

def card_ref
  return EasyChangelog.configuration.include_empty_card_id ? '[] ' : '' if card_id.nil? || card_id.empty?

  link = "[#{card_id}]"
  base_url = cards_url || EasyChangelog.configuration.cards_url
  link += "(#{base_url}/#{card_id})" if base_url

  link
end

#contentObject



29
30
31
32
33
34
35
36
# File 'lib/easy_changelog/entry.rb', line 29

def content
  title = body.dup
  title += '.' unless title.end_with? '.'

  options = { ref: ref, card_ref: card_ref, title: title, username: user }

  format(EasyChangelog.configuration.entry_row_template, options)
end

#github_userObject



62
63
64
65
66
67
# File 'lib/easy_changelog/entry.rb', line 62

def github_user
  user = `git config --global credential.username`.chomp
  warn 'Set your username with `git config --global credential.username "myusernamehere"`' if user.empty?

  user
end

#last_commit_idObject



58
59
60
# File 'lib/easy_changelog/entry.rb', line 58

def last_commit_id
  `git log -n1 --format="%h"`.chomp
end

#last_commit_titleObject



54
55
56
# File 'lib/easy_changelog/entry.rb', line 54

def last_commit_title
  `git log -1 --pretty=%B`.lines.first.chomp
end

#pathObject



22
23
24
25
26
27
# File 'lib/easy_changelog/entry.rb', line 22

def path
  filename = EasyChangelog::Utility.str_to_filename(body)
  options = { type: type, name: filename, timestamp: Time.now.strftime('%Y%m%d%H%M%S') }

  format(EasyChangelog.configuration.entry_path_template, options)
end

#refObject

Raises:

  • (ArgumentError)


38
39
40
41
42
# File 'lib/easy_changelog/entry.rb', line 38

def ref
  raise ArgumentError, 'ref_type must be issues, pull, or commit' unless %w[issues pull commit].include?(ref_type)

  "[##{ref_id}](#{EasyChangelog.configuration.repo_url}/#{ref_type}/#{ref_id})"
end

#writeObject



15
16
17
18
19
20
# File 'lib/easy_changelog/entry.rb', line 15

def write
  EasyChangelog::Utility.ensure_entries_dir_exists

  File.write(path, content)
  path
end