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

Improving usability for private registry, outside of github.com #250

Merged
merged 4 commits into from
Jun 10, 2020
Merged
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
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RegistryCI"
uuid = "0c95cc5f-2f7e-43fe-82dd-79dbcba86b32"
authors = ["Dilum Aluthge <[email protected]>", "Fredrik Ekre <[email protected]>"]
version = "2.4.0"
version = "2.5.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -15,7 +15,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"

[compat]
GitHub = "5.1.1"
GitHub = "5.1.6"
HTTP = "0.8"
JSON = "0.19, 0.20, 0.21"
RegistryTools = "1.2"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ Note that commenting on a pull request will automatically disable automerging on

For instructions on how to run the RegistryCI.jl integration tests on your local machine, see [`INTEGRATION_TESTS.md`](INTEGRATION_TESTS.md).

## TeamCity support

There is support also for TeamCity, but it does not work out-of the box, it requires Pull Request build feature to be added, and passing few build variables as environment variables.

To make it work in TeamCity, see [this Kotlin DSL snippet](teamcity_settings.kts) and put following parts into your DSL/add them in GUI.

## Acknowledgements

Dilum Aluthge would like to acknowledge the following:
Expand Down
27 changes: 15 additions & 12 deletions bin/remember_to_update_registryci.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ end
return nothing
end

function get_all_pull_requests(repo::GitHub.Repo,
function get_all_pull_requests(api::GitHub.GitHubAPI,
repo::GitHub.Repo,
state::String;
auth::GitHub.Authorization,
per_page::Integer = 100,
Expand All @@ -24,13 +25,13 @@ function get_all_pull_requests(repo::GitHub.Repo,
myparams = Dict("state" => state,
"per_page" => per_page,
"page" => 1)
prs, page_data = GitHub.pull_requests(repo;
prs, page_data = GitHub.pull_requests(api, repo;
auth=auth,
params = myparams,
page_limit = page_limit)
append!(all_pull_requests, prs)
while haskey(page_data, "next")
prs, page_data = GitHub.pull_requests(repo;
prs, page_data = GitHub.pull_requests(api, repo;
auth=auth,
page_limit = page_limit,
start_page = page_data["next"])
Expand Down Expand Up @@ -83,7 +84,8 @@ function only_my_pull_requests(pr_list::Vector{GitHub.PullRequest}; my_username:
return my_pr_list
end

function create_new_pull_request(repo::GitHub.Repo;
function create_new_pull_request(api::GitHub.GitHubAPI,
repo::GitHub.Repo;
base_branch::String,
head_branch::String,
title::String,
Expand All @@ -94,7 +96,7 @@ function create_new_pull_request(repo::GitHub.Repo;
params["head"] = head_branch
params["base"] = base_branch
params["body"] = body
result = GitHub.create_pull_request(repo; params = params, auth = auth)
result = GitHub.create_pull_request(api, repo; params = params, auth = auth)
return result
end

Expand Down Expand Up @@ -128,7 +130,8 @@ function set_git_identity(username, email)
return nothing
end

function create_new_pull_request(repo::GitHub.Repo;
function create_new_pull_request(api::GitHub.GitHubAPI,
repo::GitHub.Repo;
base_branch::String,
head_branch::String,
title::String,
Expand All @@ -139,7 +142,7 @@ function create_new_pull_request(repo::GitHub.Repo;
params["head"] = head_branch
params["base"] = base_branch
params["body"] = body
result = GitHub.create_pull_request(repo; params = params, auth = auth)
result = GitHub.create_pull_request(api, repo; params = params, auth = auth)
return result
end

Expand All @@ -154,15 +157,15 @@ function main(relative_path;
my_email = "41898282+github-actions[bot]@users.noreply.github.com")
original_project = Base.active_project()
original_directory = pwd()

api = GitHubWebAPI(HTTP.URI("https://api.github.com"))
tmp_dir = mktempdir()
atexit(() -> rm(tmp_dir; force = true, recursive = true))
cd(tmp_dir)

auth = GitHub.authenticate(github_token)
my_repo = GitHub.repo(registry; auth = auth)
auth = GitHub.authenticate(api, github_token)
my_repo = GitHub.repo(api, registry; auth = auth)
registry_url_with_auth = "https://x-access-token:$(github_token)@github.com/$(registry)"
_all_open_prs = get_all_pull_requests(my_repo, "open"; auth = auth)
_all_open_prs = get_all_pull_requests(api, my_repo, "open"; auth = auth)
_nonforked_prs = exclude_pull_requests_from_forks(my_repo, _all_open_prs)
pr_list = only_my_pull_requests(_nonforked_prs; my_username = my_username)
pr_titles = Vector{String}(undef, length(pr_list))
Expand Down Expand Up @@ -209,7 +212,7 @@ function main(relative_path;
"`.ci/Manifest.toml` file.\n\n",
username_mentions_text))
_new_pr_body = convert(String, strip(new_pr_body))
create_new_pull_request(my_repo;
create_new_pull_request(api, my_repo;
base_branch = master_branch,
head_branch = pr_branch,
title = pr_title,
Expand Down
14 changes: 8 additions & 6 deletions src/AutoMerge/automerge_comment.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
function update_automerge_comment!(repo::GitHub.Repo,
function update_automerge_comment!(api::GitHub.GitHubAPI,
repo::GitHub.Repo,
pr::GitHub.PullRequest;
body::AbstractString,
auth::GitHub.Authorization,
whoami)::Nothing
my_comments = my_retry(() -> get_all_my_pull_request_comments(repo,
my_comments = my_retry(() -> get_all_my_pull_request_comments(api,
repo,
pr;
auth = auth,
whoami = whoami))
Expand All @@ -18,7 +20,7 @@ function update_automerge_comment!(repo::GitHub.Repo,
for i = 2:num_comments
comment_to_delete = my_comments[i]
try
my_retry(() -> delete_comment!(repo,
my_retry(() -> delete_comment!(api, repo,
pr,
comment_to_delete;
auth = auth))
Expand All @@ -28,7 +30,7 @@ function update_automerge_comment!(repo::GitHub.Repo,
end
comment_to_update = my_comments[1]
if strip(comment_to_update.body) != strip(_body)
my_retry(() -> edit_comment!(repo,
my_retry(() -> edit_comment!(api, repo,
pr,
comment_to_update,
_body;
Expand All @@ -37,15 +39,15 @@ function update_automerge_comment!(repo::GitHub.Repo,
elseif num_comments == 1
comment_to_update = my_comments[1]
if strip(comment_to_update.body) != strip(_body)
my_retry(() -> edit_comment!(repo,
my_retry(() -> edit_comment!(api, repo,
pr,
comment_to_update,
_body;
auth = auth))
end
else
always_assert(num_comments < 1)
my_retry(() -> post_comment!(repo,
my_retry(() -> post_comment!(api, repo,
pr,
_body;
auth = auth))
Expand Down
5 changes: 3 additions & 2 deletions src/AutoMerge/changed_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function allowed_changed_files(::NewVersion, pkg::String)
return result
end

function pr_only_changes_allowed_files(t::Union{NewPackage, NewVersion},
function pr_only_changes_allowed_files(api::GitHub.GitHubAPI,
t::Union{NewPackage, NewVersion},
registry::GitHub.Repo,
pr::GitHub.PullRequest,
pkg::String;
Expand All @@ -29,7 +30,7 @@ function pr_only_changes_allowed_files(t::Union{NewPackage, NewVersion},
m0 = "This PR is allowed to modify at most $(_num_allowed_changed_files) files, but it actually modified $(this_pr_num_changed_files) files."
return g0, m0
else
this_pr_changed_files = get_changed_filenames(registry, pr; auth = auth)
this_pr_changed_files = get_changed_filenames(api, registry, pr; auth = auth)
if length(this_pr_changed_files) != this_pr_num_changed_files
g0 = false
m0 = "Something weird happened when I tried to get the list of changed files"
Expand Down
48 changes: 45 additions & 3 deletions src/AutoMerge/ciservice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ end
function directory_of_cloned_registry(cfg::TravisCI; env=ENV, kwargs...)
return env["TRAVIS_BUILD_DIR"]
end
function username(cfg::TravisCI; auth)
return username(auth) # use /user endpoint
function username(api::GitHub.GitHubAPI, cfg::TravisCI; auth)
return username(api, auth) # use /user endpoint
end

####################
Expand Down Expand Up @@ -74,12 +74,52 @@ end
function directory_of_cloned_registry(cfg::GitHubActions; env=ENV, kwargs...)
return get(env, "GITHUB_WORKSPACE", nothing)
end
function username(cfg::GitHubActions; auth)
function username(api::GitHub.GitHubAPI, cfg::GitHubActions; auth)
# /user endpoint of GitHub API not available
# with the GITHUB_TOKEN authentication
return "github-actions[bot]"
end

##############
## TeamCity ##
##############
struct TeamCity <: CIService
end

function conditions_met_for_pr_build(cfg::TeamCity; env=ENV, kwargs...)
pr_number_ok = tryparse(Int, get(env, "teamcity_pullRequest_number", "")) !== nothing
return haskey(env, "teamcity_pullRequest_title") && pr_number_ok
end

function conditions_met_for_merge_build(cfg::TeamCity; env=ENV, master_branch, kwargs...)
pr_number_ok = tryparse(Int, get(env, "teamcity_pullRequest_number", "")) !== nothing
haskey(env, "teamcity_pullRequest_title") && pr_number_ok && return false
## Check that we are on the correct branch
m = match(r"^refs\/heads\/(.*)$", get(env, "vcsroot_branch", ""))
branch_ok = m !== nothing && m.captures[1] == master_branch
return branch_ok
end

function pull_request_number(cfg::TeamCity; env=ENV, kwargs...)
pr_number = tryparse(Int, get(env, "teamcity_pullRequest_number", ""))
always_assert(pr_number !== nothing)
return pr_number
end

function current_pr_head_commit_sha(cfg::TeamCity; env=ENV, kwargs...)
# black magic relying on TC build parameter teamcity.git.fetchAllHeads=true
tc_pr_branch_name = get(env, "teamcity_pullRequest_source_branch", nothing)
always_assert(!isnothing(tc_pr_branch_name))
git_info = read(pipeline(`git show-ref`, `grep $tc_pr_branch_name`), String)
git_info_row = split(git_info, "\n")[1]
pr_sha = split(git_info_row, " ")[1]
return string(pr_sha)
end

directory_of_cloned_registry(cfg::TeamCity; env=ENV, kwargs...) = get(env, "PWD", nothing)

username(api::GitHub.GitHubAPI, cfg::TeamCity; auth) = "svc-aivision-reg"

####################
## Auto detection ##
####################
Expand All @@ -88,6 +128,8 @@ function auto_detect_ci_service(; env=ENV)
return TravisCI()
elseif haskey(env, "GITHUB_REPOSITORY")
return GitHubActions()
elseif haskey(env, "TEAMCITY_PROJECT_NAME")
return TeamCity()
else
error("Could not detect system.")
end
Expand Down
Loading