Skip to content
Open
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ArtifactUtils"
uuid = "8b73e784-e7d8-4ea5-973d-377fed4e3bce"
authors = ["Simeon Schaub <simeondavidschaub99@gmail.com> and contributors"]
version = "0.2.5"
authors = ["Simeon Schaub <simeondavidschaub99@gmail.com> and contributors"]

[deps]
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,35 @@ file. You can also call `add_artifact!` with the `gist` result object.
```julia
julia> add_artifact!("Artifacts.toml", "hello_world", gist)
```

### Archive a directory and upload it as release

You can also create an artifact from a directory using `artifact_from_directory` and
then upload it as a tagged release with `upload_to_release`. Note that `upload_to_release`
also requires login with `gh`.

```julia
julia> using ArtifactUtils

julia> tempdir = mktempdir();

julia> write(joinpath(tempdir, "file"), "hello");

julia> artifact_id = artifact_from_directory(tempdir)
SHA1("538e83d637ab07ada6d841aa2454e0d5af4e52b3")

julia> release = upload_to_release(artifact_id)
[ Info: Creating release with tag artifacts-latest
https://github.com/JuliaPackaging/ArtifactUtils.jl/releases/tag/artifacts-latest
┌ Info: Uploading files to JuliaPackaging/ArtifactUtils.jl with tag artifacts-latest
└ filepath = "/tmp/jl_ccmwEn/538e83d637ab07ada6d841aa2454e0d5af4e52b3.tar.gz"
Successfully uploaded 1 asset to artifacts-latest
ArtifactUtils.ReleaseUploadResult(SHA1("538e83d637ab07ada6d841aa2454e0d5af4e52b3"), "538e83d637ab07ada6d841aa2454e0d5af4e52b3.tar.gz", "/tmp/jl_ccmwEn/538e83d637ab07ada6d841aa2454e0d5af4e52b3.tar.gz", "https://github.com/JuliaPackaging/ArtifactUtils.jl/releases/download/artifacts-latest/538e83d637ab07ada6d841aa2454e0d5af4e52b3.tar.gz", "62c63d3d365e40cbb163731b53c992b1c048a94914bf624e4f2081e774ad43c4", "artifacts-latest")

```

Simply call the `add_artifact!` with the `release` result object.

```julia
julia> add_artifact!("Artifacts.toml", "hello_world", release)
```
5 changes: 4 additions & 1 deletion src/ArtifactUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ using Base: SHA1
using SHA
using Downloads: download

export add_artifact!, artifact_from_directory, upload_to_gist, upload_all_to_gist!
export add_artifact!, artifact_from_directory
export upload_to_gist, upload_all_to_gist!, upload_to_release

include("utils.jl")
include("gistutils.jl")
Expand Down Expand Up @@ -306,6 +307,8 @@ function print_artifact_entry(
TOML.print(io, dict; sorted = true)
end

include("upload_to_release.jl")

function Base.show(io::IO, ::MIME"text/plain", gist::GistUploadResult)
print(io, upload_to_gist, "(")
show(io, gist.artifact_id)
Expand Down
35 changes: 35 additions & 0 deletions src/release_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Uses `gh` to get the name of this repo, in the form owner/name.
Note that this will return the owner/name set in `gh repo set-default`.
"""
function get_repo_name(; gh=gh_cli_jll.gh())
readchomp(`$gh repo view --json nameWithOwner -q .nameWithOwner`)
end

"""
Creates a github release and uploads files.
Essentially, it just runs:
`gh release create \$tag --repo \$repo`,
`gh release upload \$tag \$filepath --repo \$repo --clobber`.
Returns the url of the release.
"""
function release_from_file(
filepath::AbstractString;
tag::AbstractString,
gh=gh_cli_jll.gh(),
repo::AbstractString=get_repo_name(; gh),
title=tag,
notes="",
)
@assert isfile(filepath)
# Create release if it doesn't exist yet
@info "Creating release with tag $tag"
try
run(`$gh release create $tag --repo $repo --title $title --notes $notes`)
catch
@info "Release $tag already exists, continuing..."
end
@info("Uploading files to $repo with tag $tag", filepath)
run(`$gh release upload $tag $filepath --repo $repo --clobber`)
return "https://github.com/$(repo)/releases/download/$(tag)/$(basename(filepath))"
end
125 changes: 125 additions & 0 deletions src/upload_to_release.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
struct ReleaseUploadResult
artifact_id::SHA1
filename::String
localpath::Union{String,Nothing}
url::String
sha256::String
tag::String
end

include("release_utils.jl")

"""
upload_to_release(
artifact_id::SHA1,
[tarball];
tag::AbstractString="artifacts-latest",
honor_overrides = false,
# Following options are aviailable only when `tarball` is not specified:
name::AbstractString = "\$artifact_id.tar.gz",
extension::AbstractString = ".tar.gz",
) -> release

Create an artifact archive at path `tarball` (or in a temporary location) and upload it to
release. The returned value `release` can be passed to `add_artifact!`.

# Extended help

## Examples
```julia
using ArtifactUtils
add_artifact!("Artifact.toml", "name", upload_to_release(artifact_from_directory("source")))
```

creates an artifact from files in the `"source"` directory, uploads it to a release, and then
adds it to `"Artifact.toml"` with the name `"name"`.

## Keyword Arguments
- `tag`: the tag of the release to upload to
- `name`: name of the archive file, including file extension
- `extension`: file extension of the tarball. It can be used for specifying the compression
method.
- `honor_overrides`: see `Pkg.Artifacts.archive_artifact`
"""
function upload_to_release end

function upload_to_release(
artifact_id::SHA1,
tarball::AbstractString;
tag::AbstractString="artifacts-latest",
title::AbstractString=tag,
notes::AbstractString="",
gh=gh_cli_jll.gh(),
archive_options...,
)
mkpath(dirname(tarball))
archive_artifact(artifact_id, tarball; archive_options...)
sha256 = sha256sum(tarball)
url = release_from_file(
tarball;
tag=tag,
title=title,
notes=notes,
gh=gh,
)
return ReleaseUploadResult(
artifact_id,
basename(tarball),
abspath(tarball),
url,
sha256,
tag,
)
end

function upload_to_release(
artifact_id::SHA1;
name::Union{AbstractString,Nothing}=nothing,
extension::Union{AbstractString,Nothing}=nothing,
gh=gh_cli_jll.gh(),
options...,
)
if name !== nothing && extension !== nothing
error(
"Options `name` and `extension` are mutually exclusive. Got: name = ",
name,
" extension = ",
extension,
)
end

tarball = if name === nothing
string(artifact_id, something(extension, ".tar.gz"))
else
name
end

return mktempdir() do dir
upload_to_release(artifact_id, joinpath(dir, tarball); gh=gh, options...)
end
end

"""
add_artifact!(
artifacts_toml::String,
name::String,
release::ReleaseUploadResult;
options...,
)

Extends the `add_artifact!` function to `ReleaseUploadResult`.
"""
function add_artifact!(
artifacts_toml::String,
name::String,
release::ReleaseUploadResult;
options...,
)
bind_artifact!(
artifacts_toml,
name,
release.artifact_id;
download_info=[(release.url, release.sha256)],
options...,
)
end
42 changes: 42 additions & 0 deletions test/fake_gh.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# fake gh CLI for testing release functionality without hitting GitHub.
# simulates repo view, release create, release upload
# uses FAKE_GH_RELEASE_DIR env var to be set to a writable directory.

release_dir = get(ENV, "FAKE_GH_RELEASE_DIR", "")
if isempty(release_dir)
error("FAKE_GH_RELEASE_DIR not set")
end

if length(ARGS) < 2
error("fake_gh: need at least 2 args")
end

cmd = join(ARGS[1:2], " ")

if cmd == "repo view"
# we tried to get the repo name most likey,
# so just return a fake
print("test-owner/ArtifactUtils.jl")

elseif cmd == "release create"
# tried to create a new release,
# so here we just make new subdirectory
# by the name of what would be the tag
mkpath(joinpath(release_dir, ARGS[3]))

elseif cmd == "release upload"
# tried to upload file to a release,
# so here we just copy that file into the release directory
# of the same tag.
# we DO NOT make the path, as gh doesnt automatically
# create a release when you try upload,
# you must first create the release.
tag, filepath = ARGS[3], ARGS[4]
if !ispath(joinpath(release_dir, tag))
error("fake_gh: release $tag not found")
end
cp(filepath, joinpath(release_dir, tag, basename(filepath)), force=true)

else
error("fake_gh: unknown command: $(join(ARGS, " "))")
end
101 changes: 101 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
using ArtifactUtils, Pkg.Artifacts
using Base: SHA1
using Downloads
using Test
using TOML
import Pkg
import Git

# returns a run command that can be interpolated into
# julia run commands, and acts like a fake, local-only gh
fake_gh(release_dir) = addenv(
`$(Base.julia_cmd()) $(joinpath(@__DIR__, "fake_gh.jl"))`,
"FAKE_GH_RELEASE_DIR" => release_dir,
)

function _artifact(name, loc)
eval(Expr(:macrocall, Symbol("@artifact_str"), LineNumberNode(1, Symbol(loc)), name))
end
Expand Down Expand Up @@ -140,3 +148,96 @@ end
@test all(==(1), hits)
end
end

@testset "fake_gh" begin
mktempdir() do release_dir
gh = fake_gh(release_dir)

@testset "repo view" begin
@test readchomp(`$gh repo view --json nameWithOwner -q .nameWithOwner`) == "test-owner/ArtifactUtils.jl"
end

@testset "release create" begin
run(`$gh release create v1.0`)
@test isdir(joinpath(release_dir, "v1.0"))
end

@testset "release upload" begin
src = joinpath(release_dir, "artifact.tar.gz")
write(src, "test content")
run(`$gh release upload v1.0 $src --repo owner/repo --clobber`)
@test read(joinpath(release_dir, "v1.0", "artifact.tar.gz"), String) == "test content"
# we never created v1.1, should error
@test_throws ProcessFailedException run(`$gh release upload v1.1 $src --repo owner/repo --clobber`)

end
end
end

@testset "release utils" begin
@testset "get_repo_name" begin
mktempdir() do tmpdir
gh = fake_gh(tmpdir)
@test ArtifactUtils.get_repo_name(; gh=gh) == "test-owner/ArtifactUtils.jl"
end
end

@testset "release_from_file" begin
mktempdir() do tmpdir
# create a small test file
# use a random int to ensure its always overwriting the test file
filepath = joinpath(tmpdir, "test.txt")
random_int = string(rand(Int))
write(filepath, random_int)

release_dir = joinpath(tmpdir, "releases")
mkpath(release_dir)
gh = fake_gh(release_dir)

tag = "test"
url = ArtifactUtils.release_from_file(filepath; tag=tag, gh=gh)
# verify the file was "uploaded" to the fake release dir
@test read(joinpath(release_dir, tag, basename(filepath)), String) == random_int
# verify the returned URL has the expected GitHub format
@test url == "https://github.com/test-owner/ArtifactUtils.jl/releases/download/$tag/$(basename(filepath))"

end
end
end

@testset "upload_to_release" begin
mktempdir() do tmp_dir
# Create a small artifact
src_dir = joinpath(tmp_dir, "src")
mkpath(src_dir)
filepath = joinpath(src_dir, "test.txt")
random_int = string(rand(Int))
write(filepath, random_int)
artifact_id = artifact_from_directory(src_dir)

release_dir = joinpath(tmp_dir, "releases")
mkpath(release_dir)
gh = fake_gh(release_dir)

tag = "test"
result = upload_to_release(artifact_id; tag=tag, gh=gh)

# Check the result struct
@test result.artifact_id == artifact_id
@test result.tag == tag
@test endswith(result.filename, ".tar.gz")
@test startswith(result.url, "https://github.com/")
@test !isempty(result.sha256)

# Verify the tarball was "uploaded" to the fake release dir
@test isfile(joinpath(release_dir, tag, result.filename))

# make sure we can now add it as an artifact
artifact_file = joinpath(tmp_dir, "Artifacts.toml")
add_artifact!(artifact_file, "test_gh_release_artifact", result)
artifacts = TOML.parsefile(artifact_file)
# Verify the artifact was bound in the TOML
@test haskey(artifacts, "test_gh_release_artifact")
@test artifacts["test_gh_release_artifact"]["git-tree-sha1"] == bytes2hex(result.artifact_id.bytes)
end
end
Loading