-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api.ex
More file actions
161 lines (136 loc) · 4.24 KB
/
Copy pathgithub_api.ex
File metadata and controls
161 lines (136 loc) · 4.24 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# SPDX-License-Identifier: MPL-2.0
# Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
defmodule ScriptManager.GitHubAPI do
@moduledoc """
GitHub API client for repository management using HTTP Capability Gateway
"""
@base_url "https://api.github.com"
@token Application.compile_env(:script_manager, :github)[:token]
alias ScriptManager.SimpleJSON
@doc """
Get all open pull requests for an organization
"""
def get_open_prs(org) do
try do
# Get all repositories for the organization
repos_response = get_repositories(org)
case repos_response do
%{status: 200, body: body} ->
repos = SimpleJSON.decode(body)
# Get open PRs for each repository
Enum.flat_map(repos, fn repo ->
get_open_prs_for_repo(org, repo["name"])
end)
_ ->
IO.puts("Error fetching repositories")
[]
end
rescue
e ->
IO.puts("Error in get_open_prs: #{inspect(e)}")
[]
end
end
defp get_repositories(org) do
url = "#{@base_url}/orgs/#{org}/repos?per_page=100"
headers = [
{"Authorization", "token #{@token}"},
{"Accept", "application/vnd.github.v3+json"},
{"User-Agent", "ElixirScriptManager/1.0"}
]
ScriptManager.HTTPClient.get(url, headers)
end
defp get_open_prs_for_repo(org, repo) do
url = "#{@base_url}/repos/#{org}/#{repo}/pulls?state=open&per_page=100"
headers = [
{"Authorization", "token #{@token}"},
{"Accept", "application/vnd.github.v3+json"},
{"User-Agent", "ElixirScriptManager/1.0"}
]
response = ScriptManager.HTTPClient.get(url, headers)
case response do
%{status: 200, body: body} ->
prs = SimpleJSON.decode(body)
Enum.map(prs, fn pr ->
%{
repo: repo,
number: pr["number"],
title: pr["title"],
url: pr["html_url"],
created_at: pr["created_at"],
updated_at: pr["updated_at"]
}
end)
_ ->
IO.puts("Error fetching PRs for #{repo}")
[]
end
end
@doc """
Add a comment to a pull request
"""
def add_comment(org, repo, pr_number, comment) do
url = "#{@base_url}/repos/#{org}/#{repo}/issues/#{pr_number}/comments"
headers = [
{"Authorization", "token #{@token}"},
{"Accept", "application/vnd.github.v3+json"},
{"User-Agent", "ElixirScriptManager/1.0"}
]
body = SimpleJSON.encode(%{"body" => comment})
response = ScriptManager.HTTPClient.post(url, body, headers)
case response do
%{status: 201} -> :ok
_ -> :error
end
end
@doc """
Apply labels to a pull request
"""
def apply_labels(org, repo, pr_number, labels) do
url = "#{@base_url}/repos/#{org}/#{repo}/issues/#{pr_number}/labels"
headers = [
{"Authorization", "token #{@token}"},
{"Accept", "application/vnd.github.v3+json"},
{"User-Agent", "ElixirScriptManager/1.0"}
]
body = SimpleJSON.encode(%{"labels" => labels})
response = ScriptManager.HTTPClient.post(url, body, headers)
case response do
%{status: 200} -> :ok
_ -> :error
end
end
@doc """
Get repository information
"""
def get_repo_info(org, repo) do
url = "#{@base_url}/repos/#{org}/#{repo}"
headers = [
{"Authorization", "token #{@token}"},
{"Accept", "application/vnd.github.v3+json"},
{"User-Agent", "ElixirScriptManager/1.0"}
]
response = ScriptManager.HTTPClient.get(url, headers)
case response do
%{status: 200, body: body} -> SimpleJSON.decode(body)
_ -> %{}
end
end
@doc """
Create a new issue
"""
def create_issue(org, repo, title, body) do
url = "#{@base_url}/repos/#{org}/#{repo}/issues"
headers = [
{"Authorization", "token #{@token}"},
{"Accept", "application/vnd.github.v3+json"},
{"User-Agent", "ElixirScriptManager/1.0"}
]
request_body = SimpleJSON.encode(%{"title" => title, "body" => body})
response = ScriptManager.HTTPClient.post(url, request_body, headers)
case response do
%{status: 201, body: body} -> SimpleJSON.decode(body)
_ -> :error
end
end
end