Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gitlab and [Unreleased] tags in git diff links #37

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ before_script:
- bundle exec rake
language: ruby
rvm:
- 2.2
- 2.3
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added:
- Support for gitlab projects
- Support for diffs with `[Unreleased]` tags (and `HEAD` reference)

## [0.12.0] - 2018-11-16
### Fixed:
Expand Down Expand Up @@ -83,3 +86,4 @@ This project adheres to [Semantic Versioning](http://semver.org/).
[0.9.0]: https://github.com/pajapro/fastlane-plugin-changelog/compare/v0.8.0...v0.9.0
[0.10.0]: https://github.com/pajapro/fastlane-plugin-changelog/compare/v0.9.0...v0.10.0
[0.12.0]: https://github.com/pajapro/fastlane-plugin-changelog/compare/v0.10.0...v0.12.0
[Unreleased]: https://github.com/pajapro/fastlane-plugin-changelog/compare/v0.12.0...HEAD
93 changes: 73 additions & 20 deletions lib/fastlane/plugin/changelog/actions/stamp_changelog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ def self.run(params)
def self.stamp(changelog_path, section_identifier, stamp_date, git_tag, placeholder_line)
# 1. Update [Unreleased] section with given identifier
Actions::UpdateChangelogAction.run(changelog_path: changelog_path,
section_identifier: UNRELEASED_IDENTIFIER,
updated_section_identifier: section_identifier,
append_date: stamp_date,
excluded_placeholder_line: placeholder_line)
section_identifier: UNRELEASED_IDENTIFIER,
updated_section_identifier: section_identifier,
append_date: stamp_date,
excluded_placeholder_line: placeholder_line)

file_content = ""
last_line = ""

# 2. Create new [Unreleased] section (placeholder)
inserted_unreleased = false
just_inserted = false
File.open(changelog_path, "r") do |file|
file.each_line do |line|
# Find the first section identifier
Expand All @@ -48,6 +50,7 @@ def self.stamp(changelog_path, section_identifier, stamp_date, git_tag, placehol
end

inserted_unreleased = true
just_inserted = true

UI.message("Created [Unreleased] placeholder section")

Expand All @@ -57,37 +60,87 @@ def self.stamp(changelog_path, section_identifier, stamp_date, git_tag, placehol
end

# Output read line
file_content.concat(line)
if inserted_unreleased
unless just_inserted
file_content.concat(last_line)
end
just_inserted = false
last_line = line
else
file_content.concat(line)
end
end
end

# 3. Create link to git tags diff
if !git_tag.nil? && !git_tag.empty?
last_line = file_content.lines.last
git = ''
previous_tag = ""
previous_previous_tag = ""

if last_line.include? 'https://github.com' # GitHub uses compare/olderTag...newerTag structure
previous_previous_tag = %r{(?<=compare\/)(.*)?(?=\.{3})}.match(last_line)
previous_tag = /(?<=\.{3})(.*)?/.match(last_line)
github_tag = 'github/gitlab'
bitbucket_tag = 'bitbucket'

if last_line.include? 'https://github.com' or last_line.include? 'http://gitlab.' or last_line.include? 'https://gitlab.com' # GitHub and Gitlab use compare/olderTag...newerTag structure
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to use http(s)?://gitlab.com regex?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't know how to do that in Ruby... I worked out how to to the rest, this is my first time in this language.

git = github_tag
previous_previous_tag = %r{(?<=compare\/)(.*)?(?=\.{3})}.match(last_line).to_s
previous_tag = %r{(?<=\.{3})(.*)?}.match(last_line)
elsif last_line.include? 'https://bitbucket.org' # Bitbucket uses compare/newerTag..olderTag structure
git = bitbucket_tag
previous_previous_tag = %r{(?<=\.{2})(.*)?}.match(last_line).to_s
previous_tag = %r{(?<=compare\/)(.*)?(?=\.{2})}.match(last_line)
previous_previous_tag = /(?<=\.{2})(.*)?/.match(last_line)
end

# Replace section identifier
cleared_git_tag = git_tag.delete('[a-z]')
cleared_previous_git_tag = previous_tag.to_s.delete('[a-z]')
last_line.sub!("[#{cleared_previous_git_tag}]", "[#{cleared_git_tag}]")
if last_line.include? UNRELEASED_IDENTIFIER
if git == github_tag
last_line.sub!("...HEAD", "...#{git_tag}")
last_line.sub!(UNRELEASED_IDENTIFIER, "[#{section_identifier}]")

file_content.concat(last_line)

# Replace previous-previous tag with previous
last_line.sub!(previous_previous_tag.to_s, previous_tag.to_s)
# Using the modified line to create a new [Unreleased] line
last_line.sub!("...#{git_tag}", "...HEAD")
last_line.sub!("[#{section_identifier}]", UNRELEASED_IDENTIFIER)
last_line.sub!("#{previous_previous_tag}...", "#{git_tag}...")

# Replace previous tag with new
last_line.sub!("..#{previous_tag}", "..#{git_tag}")
file_content.concat(last_line)

UI.message("Created a link for comparison between #{previous_tag} and #{git_tag} tag")
elsif git == bitbucket_tag
last_line.sub!("HEAD...", "#{git_tag}...")
last_line.sub!(UNRELEASED_IDENTIFIER, "[#{section_identifier}]")

file_content.concat(last_line)

# Using the modified line to create a new [Unreleased] line
last_line.sub!("#{git_tag}...", "HEAD...")
last_line.sub!("[#{section_identifier}]", UNRELEASED_IDENTIFIER)
last_line.sub!("...#{previous_previous_tag}", "...#{git_tag}")

file_content.concat(last_line)
end

UI.message("Updated the link for comparison between #{previous_previous_tag} and #{git_tag} tag")
UI.message("Created a link for comparison between #{git_tag} and HEAD tag")

else
file_content.concat(last_line)

# Replace section identifier
cleared_git_tag = git_tag.delete('[a-z]')
cleared_previous_git_tag = previous_tag.to_s.delete('[a-z]')
last_line.sub!("[#{cleared_previous_git_tag}]", "[#{cleared_git_tag}]")

# Replace previous-previous tag with previous
last_line.sub!(previous_previous_tag.to_s, previous_tag.to_s)

# Replace previous tag with new
last_line.sub!("..#{previous_tag}", "..#{git_tag}")

UI.message("Created a link for comparison between #{previous_tag} and #{git_tag} tag")

file_content.concat(last_line)
end
else
file_content.concat(last_line)
end

Expand Down Expand Up @@ -137,7 +190,7 @@ def self.available_options
env_name: "FL_STAMP_CHANGELOG_PLACEHOLDER_LINE",
description: "The placeholder line to be excluded in stamped section and added to [Unreleased] section",
is_string: true,
optional: true)
optional: true)
]
end

Expand Down
119 changes: 119 additions & 0 deletions spec/fixtures/CHANGELOG_MOCK_UNRELEASED.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- New awesome feature

## [0.3.0] - 2015-12-03
### Added
- RU translation from @aishek.
- pt-BR translation from @tallesl.
- es-ES translation from @ZeliosAriex.

## [0.2.0] - 2015-10-06
### Changed
- Remove exclusionary mentions of "open source" since this project can benefit
both "open" and "closed" source projects equally.

## [0.1.0] - 2015-10-06
### Added
- Answer "Should you ever rewrite a change log?".

### Changed
- Improve argument against commit logs.
- Start following [SemVer](http://semver.org) properly.

## [0.0.8] - 2015-02-17
### Changed
- Update year to match in every README example.
- Reluctantly stop making fun of Brits only, since most of the world
writes dates in a strange way.

### Fixed
- Fix typos in recent README changes.
- Update outdated unreleased diff link.

## [0.0.7] - 2015-02-16
### Added
- Link, and make it obvious that date format is ISO 8601.

### Changed
- Clarified the section on "Is there a standard change log format?".

### Fixed
- Fix Markdown links to tag comparison URL with footnote-style links.

## [0.0.6] - 2014-12-12
### Added
- New awesome feature

### Changed
- Onboarding flow

### Fixed
- Fix Markdown links

### Removed
- User tracking

### Work In Progress
- Sales screen

### Security
- Enable SSL pinning

### Deprecated
- Obsolete contact screen

## [0.0.5 (rc1)] - 2014-08-09
### Added
- Markdown links to version tags on release headings.
- Unreleased section to gather unreleased changes and encourage note
keeping prior to releases.

## [0.0.4] - 2014-08-09
### Added
- Better explanation of the difference between the file ("CHANGELOG")
and its function "the change log".

### Changed
- Refer to a "change log" instead of a "CHANGELOG" throughout the site
to differentiate between the file and the purpose of the file — the
logging of changes.

### Removed
- Remove empty sections from CHANGELOG, they occupy too much space and
create too much noise in the file. People will have to assume that the
missing sections were intentionally left out because they contained no
notable changes.

## [0.0.3] - 2014-08-09
### Added
- "Why should I care?" section mentioning The Changelog podcast.

## [0.0.2] - 2014-07-10
### Added
- Explanation of the recommended reverse chronological release ordering.

## [0.0.1] - 2014-05-31
### Added
- This CHANGELOG file to hopefully serve as an evolving example of a standardized open source project CHANGELOG.
- CNAME file to enable GitHub Pages custom domain
- README now contains answers to common questions about CHANGELOGs
- Good examples and basic guidelines, including proper date formatting.
- Counter-examples: "What makes unicorns cry?"

[0.0.1]: https://github.com/olivierlacan/keep-a-changelog/compare/...v0.0.1
[0.0.2]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.1...v0.0.2
[0.0.3]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.2...v0.0.3
[0.0.4]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.3...v0.0.4
[0.0.5]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.4...v0.0.5
[0.0.6]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.5...v0.0.6
[0.0.7]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.6...v0.0.7
[0.0.8]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.7...v0.0.8
[0.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.0.8...v0.1.0
[0.2.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.1.0...v0.2.0
[0.3.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.2.0...v0.3.0
[Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.3.0...HEAD
16 changes: 16 additions & 0 deletions spec/stamp_changelog_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
describe 'Stamp CHANGELOG.md action' do
let (:changelog_mock_path) { './../spec/fixtures/CHANGELOG_MOCK.md' }
let (:changelog_mock_path_hook) { './spec/fixtures/CHANGELOG_MOCK.md' }
let (:changelog_mock_unreleased_path) { './../spec/fixtures/CHANGELOG_MOCK_UNRELEASED.md' }
let (:changelog_mock_unreleased_path_hook) { './spec/fixtures/CHANGELOG_MOCK_UNRELEASED.md' }
let (:updated_section_identifier) { '12.34.56' }

before(:each) do
@original_content = File.read(changelog_mock_path_hook)
@original_content_unreleased = File.read(changelog_mock_unreleased_path_hook)
end

after(:each) do
File.open(changelog_mock_path_hook, 'w') { |f| f.write(@original_content) }
File.open(changelog_mock_unreleased_path_hook, 'w') { |f| f.write(@original_content_unreleased) }
end

it 'stamps [Unreleased] section with given identifier' do
Expand Down Expand Up @@ -75,5 +79,17 @@
modified_file = File.read(changelog_mock_path_hook)
expect(modified_file.lines.last).to eq("[12.34.56]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.3.0...v12.34.56\n")
end

it 'creates tags comparion GitHub link with prefix working with [Unreleased] identifiers' do
# Stamp [Unreleased] with given section identifier
Fastlane::FastFile.new.parse("lane :test do
stamp_changelog(changelog_path: '#{changelog_mock_unreleased_path}',
section_identifier: '#{updated_section_identifier}',
git_tag: 'v#{updated_section_identifier}')
end").runner.execute(:test)

modified_file = File.read(changelog_mock_unreleased_path_hook)
expect(modified_file.lines.last).to eq("[Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v12.34.56...HEAD\n")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, could you also add verification that the before-last line has been stamped properly, e.g.:

expect(modified_file.lines.before_last).to eq("[12.34.56]: https://github.com/olivierlacan/keep-a-changelog/compare/v0.3.0...v12.34.56\n")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The before_last don't work. I really don't know how to write this without making ton of code...

end
end
end