-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelog2jira.exs
111 lines (93 loc) · 3.45 KB
/
changelog2jira.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# Extracts JIRA IDs from CHANGELOG (using Conventional Commits) and call JIRA Webhook Automation
#
# CLI arguments:
# - `version` - version you want to extract JIRA IDs from
# - `path` - location of your project that includes CHANGELOG.md
#
# Usage:
# export JIRA_WEBHOOK_URL=https://automation.atlassian.com/pro/hooks/secret
# elixir changelog2jira.exs --version=0.1.0 --path=../your/project
#
Mix.install([
{:earmark_parser, "~> 1.4"},
{:httpoison, "~> 1.8"}
])
# configuration
jira_webhook_url = System.fetch_env!("JIRA_WEBHOOK_URL")
# get CLI args
case OptionParser.parse(System.argv(), strict: [version: :string, path: :string]) do
{[path: path, version: version], [], []} ->
case File.read(Path.join(path, "CHANGELOG.md")) do
{:ok, markdown} ->
{:ok, markdown_ast, []} = EarmarkParser.as_ast(markdown)
# find version changes and parse JIRA IDs from it
jira_ids =
markdown_ast
|> Enum.reduce_while({false, []}, fn
{"h" <> _, [], [{"a", [{"href", _compare_url}], [current_version], %{}}, _date], %{}},
{false, acc} ->
case version do
^current_version ->
{:cont, {true, acc}}
"latest" ->
{:cont, {true, acc}}
_not_matching ->
{:cont, {false, acc}}
end
{"h" <> _, [], [{"a", [{"href", _compare_url}], [_other_version], %{}}, _date], %{}},
{true, acc} ->
{:halt, {false, acc}}
other, {true, acc} ->
{:cont, {true, [other | acc]}}
_other, {false, acc} ->
{:cont, {false, acc}}
end)
|> elem(1)
|> Enum.reverse()
|> Enum.reduce([], fn
{"ul", [], children, %{}}, acc ->
Enum.reduce(children, acc, fn
{"li", [], [{"strong", [], [jira_id], %{}} | _rest], %{}}, acc ->
jira_id = jira_id |> String.replace(":", "") |> String.upcase()
if String.contains?(jira_id, "-") do
String.split(jira_id, ",") ++ acc
else
acc
end
_other, acc ->
acc
end)
_other, acc ->
acc
end)
if jira_ids != [] do
case HTTPoison.post(jira_webhook_url, "{\"issues\": #{inspect(jira_ids)}}", [
{"Content-Type", "application/json"}
]) do
{:ok, %{status_code: 200}} ->
IO.puts("Issues #{inspect(jira_ids)} marked as released")
System.stop(0)
{:ok, %{status_code: status_code}} ->
IO.puts(
"Unexpected response code #{status_code} while marking issues #{inspect(jira_ids)}"
)
System.stop(1)
{:error, reason} ->
IO.puts("Error #{inspect(reason)} while marking issues #{inspect(jira_ids)}")
System.stop(1)
end
else
IO.puts("No JIRA issues for version #{version} found.")
System.stop(0)
end
{:error, reason} ->
IO.puts(
"Unable to read the version #{version} from the CHANGELOG file #{path} due to error #{inspect(reason)}"
)
System.stop(1)
end
_other ->
IO.puts("Usage: elixir changelog2jira.ex --path=<your_project_changelog> --version=latest")
System.stop(1)
end