diff options
author | Ahmad Sherif <ahmad.m.sherif@gmail.com> | 2017-10-31 15:32:47 +0300 |
---|---|---|
committer | Ahmad Sherif <ahmad.m.sherif@gmail.com> | 2017-10-31 15:32:47 +0300 |
commit | 9ef5784e9a5f95cebb5cfda6a6a7bac7842c2a28 (patch) | |
tree | 1ecb47e1b2ac4bfc4165025a6f823566c84702e2 | |
parent | 25efb0ed0ec89902b4dccd94c1b2a8842af79ac3 (diff) | |
parent | 185e076b02cc22671c63c776dc085cd73a70dfdf (diff) |
Merge branch 'an/gitlab-ce-upgrader' into 'master'
Bug fixes to Gitlab CE update script
See merge request gitlab-org/gitaly!432
-rw-r--r-- | .gitlab-ci.yml | 2 | ||||
-rwxr-xr-x | _support/update-downstream-server-version | 117 | ||||
-rwxr-xr-x | _support/update-downstream-server-version.rb | 61 | ||||
-rw-r--r-- | doc/release.md | 10 |
4 files changed, 128 insertions, 62 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2250c4c2b..397b9c750 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -114,7 +114,7 @@ update-downstream-server-version: only: - tags script: - - ruby _support/update-downstream-server-version.rb + - ruby _support/update-downstream-server-version allow_failure: true # Ensure that gitlab-git vendoring from gitlab-ce is functioning diff --git a/_support/update-downstream-server-version b/_support/update-downstream-server-version new file mode 100755 index 000000000..ee74e7936 --- /dev/null +++ b/_support/update-downstream-server-version @@ -0,0 +1,117 @@ +#!/usr/bin/env ruby + +require 'net/http' +require 'uri' +require 'json' +require_relative 'run.rb' + +def gitlab_api(url, body=nil) + uri = URI.parse(url) + + header = { + 'Content-Type': 'application/json', + 'PRIVATE-TOKEN': ENV['GITLAB_TOKEN'] + } + + # Create the HTTP objects + Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| + if body + request = Net::HTTP::Post.new(uri.request_uri, header) + request.body = body.to_json + else + request = Net::HTTP::Get.new(uri.request_uri, header) + end + + response = http.request(request) + raise "Request to #{url} failed: #{response.body}" unless Integer(response.code) < 400 + + JSON.parse(response.body) + end +end + +def find_user_id + return ENV["GITLAB_USER_ID"] if ENV["GITLAB_USER_ID"] + + response = gitlab_api("https://gitlab.com/api/v4/user") + response['id'] +end + +def find_project_id(project_name) + encoded_project_name = URI.encode_www_form_component(project_name) + response = gitlab_api("https://gitlab.com/api/v4/projects/#{encoded_project_name}") + + response['id'] +end + +def find_tag + return ENV["CI_COMMIT_TAG"] if ENV["CI_COMMIT_TAG"] + + capture!(%w[git tag --points-at HEAD]).chomp +end + +def update_gitaly_version(project_id, tag_version) + version = tag_version.sub(/^v/, "") + + server_version_commit = { + "branch": "gitaly-version-#{tag_version}", + "start_branch": "master", + "commit_message": "Update Gitaly version to #{tag_version}", + "actions": [{ + "action": "update", + "file_path": "GITALY_SERVER_VERSION", + "content": "#{version}\n" + }] + } + + gitlab_api("https://gitlab.com/api/v4/projects/#{project_id}/repository/commits", server_version_commit) +end + +def create_mr(project_id, tag_version, assignee_id) + merge_request = { + "source_branch": "gitaly-version-#{tag_version}", + "target_branch": "master", + "title": "Upgrade Gitaly to #{tag_version}", + "assignee_id": assignee_id, + "description": "Upgrade Gitaly to #{tag_version}", + "labels": "Gitaly", + "remove_source_branch": true, + "squash": true + } + + gitlab_api("https://gitlab.com/api/v4/projects/#{project_id}/merge_requests", merge_request) +end + +def add_changelog(project_id, tag_version, merge_request_number) + changelog_commit = { + "branch": "gitaly-version-#{tag_version}", + "start_branch": "gitaly-version-#{tag_version}", + "commit_message": "Add changelog [skip ci]", + "actions": [{ + "action": "create", + "file_path": "changelogs/unreleased/gitaly-version-#{tag_version}.yml", + "content": <<~HEREDOC + --- + title: Upgrade to Gitaly #{tag_version} + merge_request: #{merge_request_number} + author: + type: changed + HEREDOC + }] + } + + gitlab_api("https://gitlab.com/api/v4/projects/#{project_id}/repository/commits", changelog_commit) +end + +abort "Please set GITLAB_TOKEN env var" unless ENV['GITLAB_TOKEN'] + +project_id = find_project_id("gitlab-org/gitlab-ce") +tag_version = find_tag +assignee_id = find_user_id + +abort "Unable to determine tag for current HEAD" unless tag_version + +update_gitaly_version(project_id, tag_version) +merge_request = create_mr(project_id, tag_version, assignee_id) +add_changelog(project_id, tag_version, merge_request['iid']) + +puts merge_request['web_url'] diff --git a/_support/update-downstream-server-version.rb b/_support/update-downstream-server-version.rb deleted file mode 100755 index 7ad9eaec2..000000000 --- a/_support/update-downstream-server-version.rb +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env ruby - -require 'net/http' -require 'uri' -require 'json' - -def gitlab_api(url, body) - uri = URI.parse(url) - - header = { - 'Content-Type': 'application/json', - 'PRIVATE-TOKEN': ENV['PRIVATE_TOKEN'] - } - - # Create the HTTP objects - Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| - request = Net::HTTP::Post.new(uri.request_uri, header) - request.body = body.to_json - - response = http.request(request) - raise "Request to #{url} failed: #{response.body}" unless Integer(response.code) < 400 - response.body - end -end - -def update_tag(project_id, tag_version) - commit = { - "branch": "gitaly-version-#{tag_version}", - "start_branch": "master", - "commit_message": "Update Gitaly version to #{tag_version}", - "actions": [{ - "action": "update", - "file_path": "GITALY_SERVER_VERSION", - "content": tag_version.to_s - }] - } - - gitlab_api("https://gitlab.com/api/v4/projects/#{project_id}/repository/commits", commit) -end - -def create_mr(project_id, tag_version, assignee_id) - merge_request = { - "source_branch": "gitaly-version-#{tag_version}", - "target_branch": "master", - "title": "Upgrade Gitaly to #{tag_version}", - "assignee_id": assignee_id, - "description": "Upgrade Gitaly to #{tag_version}", - "labels": "Gitaly", - "remove_source_branch": true, - "squash": true - } - - gitlab_api("https://gitlab.com/api/v4/projects/#{project_id}/merge_requests", merge_request) -end - -project_id = ENV["GITLAB_CE_PROJECT_ID"] -tag_version = ENV["CI_COMMIT_TAG"] -assignee_id = ENV["GITLAB_USER_ID"] - -update_tag(project_id, tag_version) -create_mr(project_id, tag_version, assignee_id) diff --git a/doc/release.md b/doc/release.md index 63e496385..7986d21cf 100644 --- a/doc/release.md +++ b/doc/release.md @@ -6,6 +6,16 @@ Where x.y.z is a semver-compliant version number. +- To automatically create a merge-request in Gitlab CE to update that + project to the latest tag, run + +```shell +GITLAB_TOKEN=$(cat /path/to/gitlab-token) _support/update-downstream-server-version +``` + +- This will create a merge-request (with changelog) and assign it to you. Once the build has + completed successfully, assign it to a maintainer for review. + ## Experimental builds Push the release tag to dev.gitlab.org/gitlab/gitaly. After the |