Skip to content

Commit

Permalink
Linkify commit hashes (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre authored Apr 15, 2024
1 parent 98c2c4f commit 010cc8d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- ## [Unreleased] -->

## [v1.1.0] - 2023-11-13
### Added
- Links of the form `[<commit hash>]`, where `<commit hash>` is a commit hash
of length 7 or 40, are now linkified. ([#4])

## [v1.0.0] - 2023-11-13
First release. See
[README.md](https://github.com/JuliaDocs/Changelog.jl/blob/master/README.md)
Expand All @@ -16,3 +21,5 @@ for currently supported functionality.
<!-- Links generated by Changelog.jl -->

[v1.0.0]: https://github.com/JuliaDocs/Changelog.jl/releases/tag/v1.0.0
[v1.1.0]: https://github.com/JuliaDocs/Changelog.jl/releases/tag/v1.1.0
[#4]: https://github.com/JuliaDocs/Changelog.jl/issues/4
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Changelog"
uuid = "5217a498-cd5d-4ec6-b8c2-9b85a09b6e3e"
version = "1.0.0"
version = "1.1.0"

[compat]
julia = "1.6"
Expand Down
29 changes: 26 additions & 3 deletions src/Changelog.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ function collect_links(inputfile::String, repo::String)
linkmap[m["token"]] = "https://github.com/$(repo)/releases/tag/$(m["tag"])"
end

# Rule: [<commit hash>] -> https://github.com/url/commit/<commit hash>
# Description: Replace short (7) or long (40) commit hash with a link to the commit
# Example: [98c2c4f] -> https://github.com/JuliaDocs/Changelog.jl/commit/98c2c4f
for m in eachmatch(r"(?<!\])\[(?<commit>[a-f0-9]{7}|[a-f0-9]{40})\](?![\[\(])", content)
linkmap[m.match] = "https://github.com/$(repo)/commit/$(m["commit"])"
end

return linkmap
end

Expand All @@ -74,6 +81,10 @@ The following modifications and replacements are performed:
`[#123](https://github.com/JuliaDocs/Changelog.jl/issues/123)` (with
`repo = "JuliaDocs/Changelog.jl"`).
- `[<commit hash>]` (where `<commit hash>` is either of length 7 or 40) is replaced with
`[<commit hash>](https://github.com/\$repo/commit/<commit hash>)` where `repo` is the
input keyword argument.
- `[abc#XYZ]` is replaced with `[abc#XYZ](https://github.com/abc/issues/XYZ)`. For example,
`[JuliaLang/julia#265]` becomes
`[JuliaLang/julia#265](https://github.com/JuliaLang/julia/issues/265)`.
Expand Down Expand Up @@ -186,6 +197,10 @@ The following link tokens are discovered:
`[#123]: https://github.com/JuliaDocs/Changelog.jl/issues/123` to the list (with
`repo = "JuliaDocs/Changelog.jl"`).
- `[<commit hash>]` (where `<commit hash>` is either of length 7 or 40) results in the link
`[<commit hash>]: https://github.com/\$repo/commit/<commit hash>` where `repo` is the
input keyword argument.
- `[abc#XYZ]` results in the link `[abc#XYZ]: https://github.com/abc/issues/XYZ`. For
example, `[JuliaLang/julia#265]` adds
`[JuliaLang/julia#265]: https://github.com/JuliaLang/julia/issues/265` to the list.
Expand All @@ -207,7 +222,12 @@ function generate(
# Get the map of token to full URL
linkmap = collect(collect_links(inputfile, repo))

# Sort releases first, then own issues, then external issues, then other things
# Sorting order:
# 1. Releases by version
# 2. Own issues by issue number
# 3. Own commits by hash
# 4. External issues by issue number
# 5. Other things by link token
sort!(linkmap; by = function(x)
k, v = x
if occursin("/releases/tag/", v)
Expand All @@ -217,13 +237,16 @@ function generate(
# Sort issues by number
n = parse(Int, match(r"\[\#(?<id>\d+)\]", k)["id"])
return (2, n)
elseif occursin("github.com/$(repo)/commit/", v)
# Sort commit by hash (url)
return (3, v)
elseif occursin(r"github\.com/.*/issues/", v)
# Sort by repo name, then issues by number
m = match(r"\[(?<repo>.*)\#(?<id>\d+)\]", k)
n = parse(Int, m["id"])
return (3, m["repo"], n)
return (4, m["repo"], n)
else
return (4,)
return (5, k)
end
end)

Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ using Changelog, Test
const CHANGELOG = """
## Version [v1.2.3] - link to GitHub release
- Link to issue/pull request in own repository: [#123]
- Link to short commit in own repository: [abcd123]
- Link to long commit in own repository: [abcdef0123456789abcdef0123456789abcdef12]
- Link to issue/pull request in another GitHub repository:
[JuliaLang/julia#123], [JuliaDocs/Documenter.jl#123]
- Link to GitHub user: [@octocat]
Expand All @@ -22,6 +24,8 @@ EditURL = "https://github.com/JuliaDocs/Changelog.jl/blob/master/CHANGELOG.md"
## Version [v1.2.3](https://github.com/JuliaDocs/Changelog.jl/releases/tag/v1.2.3) - link to GitHub release
- Link to issue/pull request in own repository: [#123](https://github.com/JuliaDocs/Changelog.jl/issues/123)
- Link to short commit in own repository: [abcd123](https://github.com/JuliaDocs/Changelog.jl/commit/abcd123)
- Link to long commit in own repository: [abcdef0123456789abcdef0123456789abcdef12](https://github.com/JuliaDocs/Changelog.jl/commit/abcdef0123456789abcdef0123456789abcdef12)
- Link to issue/pull request in another GitHub repository:
[JuliaLang/julia#123](https://github.com/JuliaLang/julia/issues/123), [JuliaDocs/Documenter.jl#123](https://github.com/JuliaDocs/Documenter.jl/issues/123)
- Link to GitHub user: [@octocat](https://github.com/octocat)
Expand All @@ -35,6 +39,8 @@ EditURL = "https://github.com/JuliaDocs/Changelog.jl/blob/master/CHANGELOG.md"
const GITHUB_OUTPUT = """
## Version [v1.2.3] - link to GitHub release
- Link to issue/pull request in own repository: [#123]
- Link to short commit in own repository: [abcd123]
- Link to long commit in own repository: [abcdef0123456789abcdef0123456789abcdef12]
- Link to issue/pull request in another GitHub repository:
[JuliaLang/julia#123], [JuliaDocs/Documenter.jl#123]
- Link to GitHub user: [@octocat]
Expand All @@ -51,6 +57,8 @@ $(Changelog.CHANGELOG_LINK_SEPARATOR)
[v1.2.3]: https://github.com/JuliaDocs/Changelog.jl/releases/tag/v1.2.3
[#123]: https://github.com/JuliaDocs/Changelog.jl/issues/123
[abcd123]: https://github.com/JuliaDocs/Changelog.jl/commit/abcd123
[abcdef0123456789abcdef0123456789abcdef12]: https://github.com/JuliaDocs/Changelog.jl/commit/abcdef0123456789abcdef0123456789abcdef12
[JuliaDocs/Documenter.jl#123]: https://github.com/JuliaDocs/Documenter.jl/issues/123
[JuliaLang/julia#123]: https://github.com/JuliaLang/julia/issues/123
[@octocat]: https://github.com/octocat
Expand Down

2 comments on commit 010cc8d

@fredrikekre
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/104965

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.0 -m "<description of version>" 010cc8de63a8e8f1ea68271f954cf59999fc3f67
git push origin v1.1.0

Please sign in to comment.