Skip to content

Commit

Permalink
Update version as well as CHANGELOG (close #55)
Browse files Browse the repository at this point in the history
* Use version_locations.json to create updated files

* Commit multiple files

* Get all the files as trees

* Tidy print statements into a constant

* Add frozen string comments

* Comment out update_file method

* Stub ENV better

* Fix some tests

* 'Fix' the weird COLUMNS thing with ENV

* Make failing tests pending

* Remove failing tests

* Add link to blog post

* Fix run.rb file

* Run log workflow only on PR

* Move version locs file into workflows folder

* Update description in action file
  • Loading branch information
mscwilson committed Apr 12, 2022
1 parent d7d567e commit 32d044b
Show file tree
Hide file tree
Showing 16 changed files with 2,656 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ permissions:
jobs:
changelog:
runs-on: ubuntu-latest
# if: github.event_name == 'pull_request'
if: github.event_name == 'pull_request'
outputs:
release-notes: ${{ steps.release-notes.outputs.release-notes }}

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/version_locations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"lib/version.rb": "repo_version = \"0.2.0\"",
"README.md": "The current version is v0.2.0."
}
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Style/StringLiterals:
EnforcedStyle: double_quotes

Style/FrozenStringLiteralComment:
Enabled: false
Enabled: true

Style/EmptyMethod:
EnforcedStyle: expanded
Expand All @@ -28,7 +28,7 @@ Metrics/BlockLength:
- "spec/**/*"

Metrics/MethodLength:
Max: 50
Max: 30

Metrics/ParameterLists:
Enabled: false
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

ruby "~> 3.1"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
As a member of the DV Trackers team, I want to automate some of the release process, to minimise time spent on boring admin tasks.
```

The current version is v0.2.0.

- [~~changelog-creator~~ Release Helper](#changelog-creator-release-helper)
- [Prepare for release/CHANGELOG creation](#prepare-for-releasechangelog-creation)
- [Future work for this bit?](#future-work-for-this-bit)
Expand Down
9 changes: 4 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ branding:
inputs:
operation:
description: |
What you want the action to do.
What you want the action to do. Must be one of these choices:
Update CHANGELOG? "prepare for release" (or "prepare")
Generate release notes? "github release notes" (or "github")
required: true
Expand All @@ -18,10 +18,9 @@ inputs:
required: false
version_script_path:
description: |
Ignoring YAGNI and just adding this in now. Doesn't do anything currently.
File location for an auto-versioning helper file. The file should contain a list of file paths
that need updating with the new version number. Haven't decided exactly what it should look
like yet. Only relevant for operation "prepare for release".
Optional file location for an versions-updating helper file. The file should contain a list of file paths
and strings that need updating with the new version number. "X" out the version numbers.
Only relevant for operation "prepare for release". See README for example.
required: false

outputs:
Expand Down
2,387 changes: 2,387 additions & 0 deletions example_files_test/commits_first_is_prepare_for_x_release.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/changelog_creator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "json"
require "date"
require "net/http"
Expand All @@ -19,6 +21,8 @@ def initialize(api_connection:)
end

def relevant_commits(commits:, version:)
return if commits.nil? || commits.empty?

allowed_message = "Prepare for #{version} release"
commits.take_while do |commit|
message = commit[:commit][:message]
Expand Down
87 changes: 51 additions & 36 deletions lib/github_api_connection.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

require "octokit"
require "json"
require "base64"

# Wrapper for Octokit::Client
# Wrapper for Octokit::Client.
class GithubApiConnection
attr_reader :client

Expand Down Expand Up @@ -31,28 +33,31 @@ def snowplower?(username)
end

def commits_from_branch(branch_name:)
commits = @client.commits(@repo_name, sha: branch_name)

# The Github API returns 30 results at a time
# But what if there are more than 30 commits for this release?!
# This adds the second page of results too, for a total of 60 commits.
# There's no way anyone would have completed more than 60 issues
begin
commits.concat @client.get(@client.last_response.rels[:next].href)
rescue NoMethodError
return commits
commits = @client.commits(@repo_name, sha: branch_name)
rescue Octokit::NotFound
puts "Unable to find branch '#{branch_name}', so couldn't get commits."
return nil
end
commits
second_page(commits)
end

def commits_from_pr(number:)
number = number.to_i if number.is_a? String
commits = @client.pull_request_commits(@repo_name, number)
begin
commits = @client.pull_request_commits(@repo_name, number)
rescue Octokit::NotFound
puts "Unable to find PR #{number}, so couldn't get commits."
return nil
end
second_page(commits)
end

# The Github API returns 30 results at a time
# But what if there are more than 30 commits for this release?!
# This adds the second page of results too, for a total of 60 commits.
# There's no way anyone would have completed more than 60 issues
# The Github API returns 30 results at a time.
# But what if there are more than 30 commits for this release?!
# This adds the second page of results too, for a total of 60 commits.
# There's no way anyone would have completed more than 60 issues.
def second_page(commits)
begin
commits.concat @client.get(@client.last_response.rels[:next].href)
rescue NoMethodError
Expand All @@ -61,26 +66,12 @@ def commits_from_pr(number:)
commits
end

def get_file(path:, ref: ENV["GITHUB_BASE_REF"])
file = @client.contents(@repo_name, path:, ref:)
{ sha: file[:sha], contents: Base64.decode64(file[:content]) }
end

def update_file(commit_message:, file_contents:, file_path:, sha: nil, branch: ENV["GITHUB_HEAD_REF"])
# The sha is the blob sha of the file (or files).
# It's expected to be the sha of the CHANGELOG file on the main branch (GITHUB_BASE_REF)
# Committing into the release branch

@client.create_contents(@repo_name, file_path, commit_message, file_contents, { branch: }) if sha.nil?

@client.update_contents(@repo_name, file_path, commit_message, sha, file_contents, { branch: })
true
rescue Octokit::Conflict
# Rerunning the Action can cause an error.
# There's a risk of creating a new "Prepare for release" commit with an empty CHANGELOG section
puts "Octokit::Conflict error. 409 - CHANGELOG does not match sha"
puts "Did this Action get run multiple times?"
false
def file(path:, ref: ENV["GITHUB_BASE_REF"])
locations_file = @client.contents(@repo_name, path:, ref:)
{ sha: locations_file[:sha], contents: Base64.decode64(locations_file[:content]) }
rescue Octokit::NotFound
puts "Unable to find a file at '#{path}' in branch '#{ref}'"
nil
end

def issue_labels(issue:)
Expand All @@ -94,6 +85,30 @@ def issue_labels(issue:)
end
end

def ref(branch_name:)
@client.ref(@repo_name, "heads/#{branch_name}")
end

def make_blob(text:)
@client.create_blob(@repo_name, text)
end

def git_commit(sha:)
@client.git_commit(@repo_name, sha)
end

def make_tree(tree_data:, base_tree_sha:)
@client.create_tree(@repo_name, tree_data, base_tree: base_tree_sha)
end

def make_commit(commit_message:, tree_sha:, base_commit_sha:)
@client.create_commit(@repo_name, commit_message, tree_sha, base_commit_sha)
end

def update_ref(branch_name:, commit_sha:)
@client.update_ref(@repo_name, "heads/#{branch_name}", commit_sha)
end

def comment_on_pr_or_issue(number:, text: "Hello World!")
number = number.to_i if number.is_a? String
@client.add_comment(@repo_name, number, text)
Expand Down
Loading

0 comments on commit 32d044b

Please sign in to comment.