Verified Commit edb4a0a9 authored by Bob Van Landuyt's avatar Bob Van Landuyt 💬
Browse files

fix(tooling): Fix the update changelog script

This fixes the update-changelog script after the repository has
moved. Instead of updating the paths, I've opted to use environment
variables that CI automatically provides.

To develop on this locally, we've added an example environment file
that when sourced will populate the right environment variables to run
the tooling scripts.
parent 17e642c7
Loading
Loading
Loading
Loading

.env.example.sh

0 → 100644
+7 −0
Original line number Diff line number Diff line
export CI_PROJECT_ID=10947578
export CI_API_V4_URL="https://gitlab.com/api/v4"
export CI_PROJECT_URL="https://gitlab.com/gitlab-org/ruby/gems/labkit-ruby"
# Don't keep secrets in plaintext files. Use a keyring or 1password to load
# it instead and export it as an env var.
token=$(load-your-token)
export GITLAB_TOKEN=$token
+1 −0
Original line number Diff line number Diff line
@@ -2,3 +2,4 @@ Gemfile.lock
*.gem
node_modules
.bundle
/.env.sh
+14 −0
Original line number Diff line number Diff line
@@ -44,6 +44,20 @@ Note that LabKit-Ruby uses the [`rufo`](https://github.com/ruby-formatter/rufo)

Please also review the [development section of the LabKit (go) README](https://gitlab.com/gitlab-org/labkit#developing-labkit) for details of the LabKit architectural philosophy.

To work on some of the scripts we use for releasing a new version,
make sure to add a new `.env.sh`.

```console
cp .env.example.sh .env.sh`
```

Inside `.env.sh`, add a personal acccess token for the `GITLAB_TOKEN`
environment variable. Next source the file:

```console
. .env.sh
```

### Releasing a new version

Releasing a new version can be done by pushing a new tag, or creating
+20 −10
Original line number Diff line number Diff line
@@ -9,8 +9,15 @@ require "optparse"
GitlabError = Class.new(StandardError)
GitlabClientError = Class.new(GitlabError)

PROJECT_ID = ENV.fetch("CI_PROJECT_ID")
API_URL = ENV.fetch("CI_API_V4_URL")
GITLAB_TOKEN = ENV.fetch("GITLAB_TOKEN")
PROJECT_URL = ENV.fetch("CI_PROJECT_URL")

def gitlab_client(path, method, body = nil)
  url = URI("https://gitlab.com#{path}")
  # The `path` needs to be the full path starting with a `/` to be joined to the
  # host correctly.
  url = URI.join(API_URL, path)

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
@@ -27,7 +34,7 @@ def gitlab_client(path, method, body = nil)
            end

  request["content-type"] = "application/json"
  request["PRIVATE-TOKEN"] = ENV["GITLAB_TOKEN"]
  request["PRIVATE-TOKEN"] = GITLAB_TOKEN
  request.body = JSON.dump(body) if body

  response = http.request(request)
@@ -38,7 +45,7 @@ def gitlab_client(path, method, body = nil)
  when Net::HTTPClientError
    raise GitlabClientError, "HTTP #{response.code} for #{method} #{url}: #{response.read_body}"
  else
    raise GitLabError, "HTTP #{response.code} for #{method} #{url}: #{response.read_body}"
    raise GitlabError, "HTTP #{response.code} for #{method} #{url}: #{response.read_body}"
  end
end

@@ -48,17 +55,17 @@ def list_commits_for_tag(tag)
end

def get_mrs_for_commit(commit_id)
  gitlab_client("/api/v4/projects/gitlab-org%2flabkit-ruby/repository/commits/#{commit_id}/merge_requests", :get)
  gitlab_client("/api/v4/projects/#{PROJECT_ID}/repository/commits/#{commit_id}/merge_requests", :get)
end

def create_release_for_tag(tag, description)
  req_body = { tag_name: tag, description: description }
  gitlab_client("/api/v4/projects/gitlab-org%2flabkit-ruby/releases", :post, req_body)
  gitlab_client("/api/v4/projects/#{PROJECT_ID}/releases", :post, req_body)
end

def update_release_for_tag(tag, description)
  req_body = { description: description }
  gitlab_client("/api/v4/projects/gitlab-org%2flabkit-ruby/releases/#{tag}", :put, req_body)
  gitlab_client("/api/v4/projects/#{PROJECT_ID}/releases/#{tag}", :put, req_body)
end

def get_unique_mrs_for_tag(tag)
@@ -80,7 +87,7 @@ end.parse!

# Check required conditions
unless ARGV.length == 1
  puts optparse
  puts "Missing required argument: tag"
  exit(-1)
end

@@ -92,15 +99,18 @@ raise "No new changes found" if changelog.empty?

changelog = "## Changes in #{tag}\n\n" + changelog

release_path = URI.join("#{PROJECT_URL}/", "-/releases##{tag}")

if options[:dry_run]
  puts changelog
  puts [changelog, release_path].join("\n\n")

  exit
end

begin
  create_release_for_tag(tag, changelog)
  puts "Created release: https://gitlab.com/gitlab-org/labkit-ruby/-/releases##{tag}"
  puts "Created release: #{release_path}"
rescue GitlabClientError
  update_release_for_tag(tag, changelog)
  puts "Updated release: https://gitlab.com/gitlab-org/labkit-ruby/-/releases##{tag}"
  puts "Updated release: #{release_path}"
end