-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
repos.py
168 lines (138 loc) · 6.3 KB
/
repos.py
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
162
163
164
165
166
167
168
from typing import Any, List
from prefect import Task
from prefect.utilities.tasks import defaults_from_attrs
class GetRepoInfo(Task):
"""
Task for retrieving GitHub repository information using the v3 version of the GitHub REST API.
Args:
- repo (str, optional): the name of the repository to open the issue in; must be
provided in the form `organization/repo_name` or `user/repo_name`; can also be
provided to the `run` method
- info_keys (List[str], optional): a list of repo attributes to pull (e.g.,
`["stargazers_count", "subscribers_count"]`). A full list of available keys can be
found in the official [GitHub
documentation](https://developer.github.com/v3/repos/)
- **kwargs (Any, optional): additional keyword arguments to pass to the standard Task
init method
"""
def __init__(self, repo: str = None, info_keys: List[str] = None, **kwargs: Any):
self.repo = repo
self.info_keys = info_keys or []
super().__init__(**kwargs)
@defaults_from_attrs("repo", "info_keys")
def run(
self, repo: str = None, info_keys: List[str] = None, token: str = None
) -> None:
"""
Run method for this Task. Invoked by calling this Task after initialization within a
Flow context, or by using `Task.bind`.
Args:
- repo (str, optional): the name of the repository to open the issue in; must be
provided in the form `organization/repo_name`; defaults to the one provided at
initialization
- info_keys (List[str], optional): a list of repo attributes to pull (e.g.,
`["stargazers_count", "subscribers_count"]`). A full list of available keys
can be found in the official [GitHub
documentation](https://developer.github.com/v3/repos/)
- token (str): a GitHub access token
Raises:
- ValueError: if a `repo` was never provided
- HTTPError: if the GET request returns a non-200 status code
Returns:
- dict: dictionary of the requested information
"""
if repo is None:
raise ValueError("A GitHub repository must be provided.")
# 'import requests' is expensive time-wise, we should do this just-in-time to keep
# the 'import prefect' time low
import requests
url = "https://api.github.com/repos/{}".format(repo)
headers = {
"AUTHORIZATION": "token {}".format(token),
"Accept": "application/vnd.github.v3+json",
}
# send the request
resp = requests.get(url, headers=headers)
resp.raise_for_status()
data = resp.json()
return {key: data[key] for key in info_keys}
class CreateBranch(Task):
"""
Task for creating new branches using the v3 version of the GitHub REST API.
Args:
- repo (str, optional): the name of the repository to create the branch in; must be
provided in the form `organization/repo_name` or `user/repo_name`; can also be
provided to the `run` method
- base (str, optional): the name of the branch you want to branch off; can also be
provided to the `run` method. Defaults to "master".
- branch_name (str, optional): the name of the new branch; can also be provided to the
`run` method
- **kwargs (Any, optional): additional keyword arguments to pass to the standard Task
init method
"""
def __init__(
self,
repo: str = None,
base: str = "master",
branch_name: str = None,
**kwargs: Any
):
self.repo = repo
self.base = base
self.branch_name = branch_name
super().__init__(**kwargs)
@defaults_from_attrs("repo", "base", "branch_name")
def run(
self,
repo: str = None,
base: str = None,
branch_name: str = None,
token: str = None,
) -> dict:
"""
Run method for this Task. Invoked by calling this Task after initialization within a
Flow context, or by using `Task.bind`.
Args:
- repo (str, optional): the name of the repository to open the issue in; must be
provided in the form `organization/repo_name`; defaults to the one provided at
initialization
- base (str, optional): the name of the branch you want to branch off; if not
provided here, defaults to the one set at initialization
- branch_name (str, optional): the name of the new branch; if not provided here,
defaults to the one set at initialization
- token (str): a GitHub access token
Raises:
- ValueError: if a `repo` or `branch_name` was never provided, or if the base
branch wasn't found
- HTTPError: if the GET request returns a non-200 status code
Returns:
- dict: dictionary of the response (includes commit hash, etc.)
"""
if branch_name is None:
raise ValueError("A branch name must be provided.")
if repo is None:
raise ValueError("A GitHub repository must be provided.")
# 'import requests' is expensive time-wise, we should do this just-in-time to keep
# the 'import prefect' time low
import requests
url = "https://api.github.com/repos/{}/git/refs".format(repo)
headers = {
"AUTHORIZATION": "token {}".format(token),
"Accept": "application/vnd.github.v3+json",
}
# gather branch information
resp = requests.get(url + "/heads", headers=headers)
resp.raise_for_status()
branch_data = resp.json()
commit_sha = None
for branch in branch_data:
if branch.get("ref") == "refs/heads/{}".format(base):
commit_sha = branch.get("object", {}).get("sha")
break
if commit_sha is None:
raise ValueError("Base branch {} not found.".format(base))
# create new branch
new_branch = {"ref": "refs/heads/{}".format(branch_name), "sha": commit_sha}
resp = requests.post(url, headers=headers, json=new_branch)
resp.raise_for_status()
return resp.json()