diff options
Diffstat (limited to 'workhorse/_support')
-rwxr-xr-x | workhorse/_support/changelog | 243 | ||||
-rwxr-xr-x | workhorse/_support/check_changelog.sh | 22 | ||||
-rwxr-xr-x | workhorse/_support/detect-assert.sh | 9 | ||||
-rwxr-xr-x | workhorse/_support/detect-context.sh | 10 | ||||
-rw-r--r-- | workhorse/_support/fake-auth-backend.go | 22 | ||||
-rwxr-xr-x | workhorse/_support/generate_changelog | 75 | ||||
-rwxr-xr-x | workhorse/_support/lint.sh | 11 | ||||
-rw-r--r-- | workhorse/_support/tag.sh | 45 | ||||
-rwxr-xr-x | workhorse/_support/validate-formatting.sh | 9 |
9 files changed, 446 insertions, 0 deletions
diff --git a/workhorse/_support/changelog b/workhorse/_support/changelog new file mode 100755 index 00000000000..0e733cc0062 --- /dev/null +++ b/workhorse/_support/changelog @@ -0,0 +1,243 @@ +#!/usr/bin/env ruby +# +# Generate a changelog entry file in the correct location. +# +# Automatically stages the file and amends the previous commit if the `--amend` +# argument is used. +# +# Stolen from gitlab-org/gitaly, lifted from gitlab-org/gitlab-ce + +require 'optparse' +require 'yaml' + +Options = Struct.new( + :amend, + :author, + :dry_run, + :force, + :merge_request, + :title, + :type +) +INVALID_TYPE = -1 + +class ChangelogOptionParser + Type = Struct.new(:name, :description) + TYPES = [ + Type.new('added', 'New feature'), + Type.new('fixed', 'Bug fix'), + Type.new('changed', 'Feature change'), + Type.new('deprecated', 'New deprecation'), + Type.new('removed', 'Feature removal'), + Type.new('security', 'Security fix'), + Type.new('performance', 'Performance improvement'), + Type.new('other', 'Other') + ].freeze + TYPES_OFFSET = 1 + + class << self + def parse(argv) + options = Options.new + + parser = OptionParser.new do |opts| + opts.banner = "Usage: #{__FILE__} [options] [title]\n\n" + + # Note: We do not provide a shorthand for this in order to match the `git + # commit` interface + opts.on('--amend', 'Amend the previous commit') do |value| + options.amend = value + end + + opts.on('-f', '--force', 'Overwrite an existing entry') do |value| + options.force = value + end + + opts.on('-m', '--merge-request [integer]', Integer, 'Merge Request ID') do |value| + options.merge_request = value + end + + opts.on('-n', '--dry-run', "Don't actually write anything, just print") do |value| + options.dry_run = value + end + + opts.on('-u', '--git-username', 'Use Git user.name configuration as the author') do |value| + options.author = git_user_name if value + end + + opts.on('-t', '--type [string]', String, "The category of the change, valid options are: #{TYPES.map(&:name).join(', ')}") do |value| + options.type = parse_type(value) + end + + opts.on('-h', '--help', 'Print help message') do + $stdout.puts opts + exit + end + end + + parser.parse!(argv) + + # Title is everything that remains, but let's clean it up a bit + options.title = argv.join(' ').strip.squeeze(' ').tr("\r\n", '') + + options + end + + def read_type + read_type_message + + type = TYPES[$stdin.getc.to_i - TYPES_OFFSET] + assert_valid_type!(type) + + type.name + end + + private + + def parse_type(name) + type_found = TYPES.find do |type| + type.name == name + end + type_found ? type_found.name : INVALID_TYPE + end + + def read_type_message + $stdout.puts "\n>> Please specify the index for the category of your change:" + TYPES.each_with_index do |type, index| + $stdout.puts "#{index + TYPES_OFFSET}. #{type.description}" + end + $stdout.print "\n?> " + end + + def assert_valid_type!(type) + unless type + $stderr.puts "Invalid category index, please select an index between 1 and #{TYPES.length}" + exit 1 + end + end + + def git_user_name + %x{git config user.name}.strip + end + end +end + +class ChangelogEntry + attr_reader :options + + def initialize(options) + @options = options + + assert_feature_branch! + assert_title! + assert_new_file! + + # Read type from $stdin unless is already set + options.type ||= ChangelogOptionParser.read_type + assert_valid_type! + + $stdout.puts "\e[32mcreate\e[0m #{file_path}" + $stdout.puts contents + + unless options.dry_run + write + amend_commit if options.amend + end + end + + private + + def contents + yaml_content = YAML.dump( + 'title' => title, + 'merge_request' => options.merge_request, + 'author' => options.author, + 'type' => options.type + ) + remove_trailing_whitespace(yaml_content) + end + + def write + File.write(file_path, contents) + end + + def amend_commit + %x{git add #{file_path}} + exec("git commit --amend") + end + + def fail_with(message) + $stderr.puts "\e[31merror\e[0m #{message}" + exit 1 + end + + def assert_feature_branch! + return unless branch_name == 'master' + + fail_with "Create a branch first!" + end + + def assert_new_file! + return unless File.exist?(file_path) + return if options.force + + fail_with "#{file_path} already exists! Use `--force` to overwrite." + end + + def assert_title! + return if options.title.length > 0 || options.amend + + fail_with "Provide a title for the changelog entry or use `--amend`" \ + " to use the title from the previous commit." + end + + def assert_valid_type! + return unless options.type && options.type == INVALID_TYPE + + fail_with 'Invalid category given!' + end + + def title + if options.title.empty? + last_commit_subject + else + options.title + end + end + + def last_commit_subject + %x{git log --format="%s" -1}.strip + end + + def file_path + File.join( + unreleased_path, + branch_name.gsub(/[^\w-]/, '-') << '.yml' + ) + end + + def unreleased_path + path = File.join('changelogs', 'unreleased') + path = File.join('ee', path) if ee? + + path + end + + def ee? + @ee ||= File.exist?(File.expand_path('../CHANGELOG-EE.md', __dir__)) + end + + def branch_name + @branch_name ||= %x{git symbolic-ref --short HEAD}.strip + end + + def remove_trailing_whitespace(yaml_content) + yaml_content.gsub(/ +$/, '') + end +end + +if $0 == __FILE__ + options = ChangelogOptionParser.parse(ARGV) + ChangelogEntry.new(options) +end + +# vim: ft=ruby diff --git a/workhorse/_support/check_changelog.sh b/workhorse/_support/check_changelog.sh new file mode 100755 index 00000000000..2f9850f09fd --- /dev/null +++ b/workhorse/_support/check_changelog.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +set -e + +# we skip the changelog check if the merge requet title ends with "NO CHANGELOG" +if echo "$CI_MERGE_REQUEST_TITLE" | grep -q ' NO CHANGELOG$'; then + echo "Changelog not needed" + + exit 0 +fi + +target=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-master} + +if git diff --name-only "origin/$target" | grep -q '^changelogs/' ; then + echo "Changelog included" +else + echo "Please add a changelog running '_support/changelog'" + echo "or disable this check adding 'NO CHANGELOG' at the end of the merge request title" + echo "/title $CI_MERGE_REQUEST_TITLE NO CHANGELOG" + + exit 1 +fi diff --git a/workhorse/_support/detect-assert.sh b/workhorse/_support/detect-assert.sh new file mode 100755 index 00000000000..351cef763b5 --- /dev/null +++ b/workhorse/_support/detect-assert.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +git grep 'testify/assert"' | \ + grep -e '^[^:]*\.go' | \ + awk '{ + print "error: please use testify/require instead of testify/assert" + print + exit 1 +}' diff --git a/workhorse/_support/detect-context.sh b/workhorse/_support/detect-context.sh new file mode 100755 index 00000000000..60ad212decf --- /dev/null +++ b/workhorse/_support/detect-context.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +git grep 'context.\(Background\|TODO\)' | \ + grep -v -e '^[^:]*_test\.go:' -v -e "lint:allow context.Background" -e '^vendor/' -e '^_support/' -e '^cmd/[^:]*/main.go' | \ + grep -e '^[^:]*\.go' | \ + awk '{ + print "Found disallowed use of context.Background or TODO" + print + exit 1 +}' diff --git a/workhorse/_support/fake-auth-backend.go b/workhorse/_support/fake-auth-backend.go new file mode 100644 index 00000000000..17dbcb3b35f --- /dev/null +++ b/workhorse/_support/fake-auth-backend.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "net/http" + "os" + + "gitlab.com/gitlab-org/labkit/log" +) + +func main() { + if len(os.Args) == 1 { + fmt.Fprintf(os.Stderr, "Usage: %s /path/to/test-repo.git\n", os.Args[0]) + os.Exit(1) + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, `{"RepoPath":"%s","ArchivePath":"%s"}`, os.Args[1], r.URL.Path) + }) + + log.Fatal(http.ListenAndServe("localhost:8080", nil)) +} diff --git a/workhorse/_support/generate_changelog b/workhorse/_support/generate_changelog new file mode 100755 index 00000000000..a9a8bae5a25 --- /dev/null +++ b/workhorse/_support/generate_changelog @@ -0,0 +1,75 @@ +#!/usr/bin/env ruby +# Generates the changelog from the yaml entries in changelogs/unreleased +# +# Lifted form gitlab-org/gitaly + +require 'yaml' +require 'fileutils' + +class ChangelogEntry + attr_reader :title, :merge_request, :type, :author + + def initialize(file_path) + yaml = YAML.safe_load(File.read(file_path)) + + @title = yaml['title'] + @merge_request = yaml['merge_request'] + @type = yaml['type'] + @author = yaml['author'] + end + + def to_s + str = "" + str << "- #{title}\n" + str << " https://gitlab.com/gitlab-org/gitlab-workhorse/-/merge_requests/#{merge_request}\n" + str << " Contributed by #{author}\n" if author + + str + end +end + +ROOT_DIR = File.expand_path('../..', __FILE__) +UNRELEASED_ENTRIES = File.join(ROOT_DIR, 'changelogs', 'unreleased') +CHANGELOG_FILE = File.join(ROOT_DIR, 'CHANGELOG') + +def main(version) + entries = [] + Dir["#{UNRELEASED_ENTRIES}/*.yml"].each do |yml| + entries << ChangelogEntry.new(yml) + FileUtils.rm(yml) + end + + sections = [] + types = entries.map(&:type).uniq.sort + types.each do |type| + text = '' + text << "### #{type.capitalize}\n" + + entries.each do |e| + next unless e.type == type + + text << e.to_s + end + + sections << text + end + + sections << '- No changes.' if sections.empty? + + new_version_entry = ["## v#{version}\n\n", sections.join("\n"), "\n"].join + + current_changelog = File.read(CHANGELOG_FILE).lines + header = current_changelog.shift(2) + + new_changelog = [header, new_version_entry, current_changelog.join] + + File.write(CHANGELOG_FILE, new_changelog.join) +end + +unless ARGV.count == 1 + warn "Usage: #{$0} VERSION" + warn "Specify version as x.y.z" + abort +end + +main(ARGV.first) diff --git a/workhorse/_support/lint.sh b/workhorse/_support/lint.sh new file mode 100755 index 00000000000..b016f088d80 --- /dev/null +++ b/workhorse/_support/lint.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# Unfortunately, workhorse fails many lint checks which we currently ignore +LINT_RESULT=$(golint "$@"|grep -Ev 'should have|should be|use ALL_CAPS in Go names') + +if [ -n "${LINT_RESULT}" ]; then + echo >&2 "Formatting or imports need fixing: 'make fmt'" + echo ">>${LINT_RESULT}<<" + exit 1 +fi + diff --git a/workhorse/_support/tag.sh b/workhorse/_support/tag.sh new file mode 100644 index 00000000000..639fd141dad --- /dev/null +++ b/workhorse/_support/tag.sh @@ -0,0 +1,45 @@ +set -e + +main() { + version=$1 + set_version + + changelog + + git commit VERSION -m "Update VERSION to $version" + + tag_name="v${version}" + git tag $TAG_OPTS -m "Version ${version}" -a ${tag_name} + git show ${tag_name} + cat <<'EOF' + + Remember to now push your tag, either to gitlab.com (for a + normal release) or dev.gitlab.org (for a security release). +EOF +} + +set_version() { + if ! echo "${version}" | grep -q '^[0-9]\+\.[0-9]\+\.[0-9]\+$' ; then + echo "Invalid VERSION: ${version}" + exit 1 + fi + + if git tag --list | grep -q "^v${version}$" ; then + echo "Tag already exists for ${version}" + exit 1 + fi + + echo "$version" > VERSION +} + +changelog() { + _support/generate_changelog "$version" + + git commit CHANGELOG changelogs/unreleased --file - <<EOF +Update CHANGELOG for ${version} + +[ci skip] +EOF +} + +main "$@" diff --git a/workhorse/_support/validate-formatting.sh b/workhorse/_support/validate-formatting.sh new file mode 100755 index 00000000000..190f646f8df --- /dev/null +++ b/workhorse/_support/validate-formatting.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +IMPORT_RESULT=$(goimports -e -local "gitlab.com/gitlab-org/gitlab-workhorse" -l "$@") + +if [ -n "${IMPORT_RESULT}" ]; then + echo >&2 "Formatting or imports need fixing: 'make fmt'" + echo "${IMPORT_RESULT}" + exit 1 +fi |