Skip to content

Instantly share code, notes, and snippets.

@tkf tkf/pr_project.jl
Last active Jun 2, 2019

Embed
What would you like to do?
const USAGE = """
julia $PROGRAM_FILE PATH/TO/gen_project.jl REMOTE PROJECT [PROJECT...]
REMOTE
A Git remote repository to be pushed (e.g., origin). It is
assumed to be the same for all PROJECTs.
PROJECT
A path to the Git repository of a Julia package.
"""
using DefaultApplication
using Setfield
using URIParser
function assert_clean_repo(path)
dirty = read(setenv(`git status --short`; dir=path), String)
if !isempty(dirty)
error("""
Git repository $path is not clean. Following files are not committed:
$dirty
""")
end
end
function guess_repo(path, remote)
cmd = setenv(`git config --get remote.$remote.url`; dir=path)
m = match(r"github\.com[:/](.*?)(\.git)?$", read(cmd, String))
return m.captures[1]
end
function github_branch(path, remote, branch)
cmd = setenv(`git config --get remote.$remote.url`; dir=path)
m = match(r"github\.com[:/](.*?)/(.*?)(\.git)?$", read(cmd, String))
user, = m.captures
return "$user:$branch"
end
github_new_pr_uri(path, remote, branch; query...) =
_github_new_pr_uri(
guess_repo(path, "origin"),
github_branch(path, remote, branch);
expand="1",
query...)
escape_query_params(query) =
join([
string(key, "=", escape_form(value)) for (key, value) in query
], "&")
function _github_new_pr_uri(repo, branch; query...)
url = URI("https://github.com/$repo/compare/master...$branch")
return @set url.query = escape_query_params(query)
end
"""
github_new_pr(local_repo, remote_name, remote_branch; query...)
For the full set of queries supported by GitHub, see:
https://help.github.com/en/articles/about-automation-for-issues-and-pull-requests-with-query-parameters
"""
function github_new_pr(args...; query...)
url = github_new_pr_uri(args...; query...)
@info "Open: $url"
DefaultApplication.open(url)
end
function pr_project(gen_project,
remote,
project_paths,
branch = "project-toml")
for path in project_paths
assert_clean_repo(path)
end
for path in project_paths
run(setenv(`git fetch origin`; dir=path))
run(setenv(`git checkout -b $branch origin/master`; dir=path))
end
cmd = `$(Base.julia_cmd()) $gen_project $project_paths`
@info "Run: $cmd"
run(cmd)
title = "Add Project.toml"
body = """
This patch adds `Project.toml` generated by Pkg.jl's `gen_project.jl` script
and removes `REQUIRE` file.
"""
msg = """
$title
$body
"""
for path in project_paths
rm(joinpath(path, "REQUIRE"))
rm(joinpath(path, "test", "REQUIRE"), force=true)
run(setenv(`git add .`; dir=path))
run(setenv(`git commit -m $msg`; dir=path))
run(setenv(`git push -u $remote $branch`; dir=path))
end
for path in project_paths
github_new_pr(
path,
remote,
branch;
title = title,
body = """
$body
This PR is auto-generated by https://gist.github.com/tkf/eee56c7a989530fdaa87471333d0c06a
""",
)
end
end
function main(args = ARGS; )
if length(args) < 3
println("Usage: $USAGE")
return
end
gen_project, remote, = args
project_paths = args[3:end]
pr_project(gen_project, remote, project_paths)
end
if abspath(PROGRAM_FILE) == @__FILE__
main()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.