Skip to content

Commit a84c95b

Browse files
authored
🔀 Merge pull request #52 from astariul/action
Use `workflow_dispatch` instead of issues
2 parents fcc0a76 + 64be1ea commit a84c95b

File tree

9 files changed

+223
-166
lines changed

9 files changed

+223
-166
lines changed

.github/ISSUE_TEMPLATE/delete.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

.github/ISSUE_TEMPLATE/register.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

.github/ISSUE_TEMPLATE/update.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/actions.py

Lines changed: 56 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,14 @@
99

1010
INDEX_FILE = "index.html"
1111
TEMPLATE_FILE = "pkg_template.html"
12+
YAML_ACTION_FILES = [".github/workflows/delete.yml", ".github/workflows/update.yml"]
1213

1314

1415
def normalize(name):
1516
""" From PEP503 : https://www.python.org/dev/peps/pep-0503/ """
1617
return re.sub(r"[-_.]+", "-", name).lower()
1718

1819

19-
def parse_issue(issue_ctx):
20-
arguments = {}
21-
22-
parts = issue_ctx['body'].split('- **')[1:] # Ignore the first one : it's the title
23-
for text_arg in parts:
24-
arg_name, arg_value = text_arg.split(':**')
25-
arg_name, arg_value = arg_name.strip(), arg_value.strip()
26-
27-
if arg_name == "Long description":
28-
# Special case, where we have more than 1 line : it contain HTML code
29-
arg_value = arg_value.split('```')[1] if '```' in arg_value else arg_value.split('`')[1]
30-
31-
code_lang = arg_value.split('\n')[0].strip()
32-
if code_lang != 'html':
33-
raise ValueError("The {} argument should contain a HTML code section. But it contain {} code.".format(arg_name, code_lang))
34-
35-
arg_value = "\n".join(arg_value.split('\n')[1:])
36-
else:
37-
if "\n" in arg_value:
38-
raise ValueError("The {} argument should be a single line. Current value : {}".format(arg_name, arg_value))
39-
40-
arguments[arg_name.lower()] = arg_value
41-
return arguments
42-
43-
44-
def print_args(args):
45-
print("\n--- Arguments detected from issue ---\n")
46-
for arg_name, arg_value in args.items():
47-
print("\t{} : {}".format(arg_name, arg_value))
48-
print("\n")
49-
50-
51-
def check_args(args, must_have):
52-
for name in must_have:
53-
if name not in args:
54-
raise ValueError("Couldn't find argument {}".format(name))
55-
if args[name].strip() == "":
56-
raise ValueError("Argument {} is empty. Please specify it".format(name))
57-
58-
5920
def package_exists(soup, package_name):
6021
package_ref = package_name + "/"
6122
for anchor in soup.find_all('a'):
@@ -64,25 +25,23 @@ def package_exists(soup, package_name):
6425
return False
6526

6627

67-
def register(issue_ctx):
68-
args = parse_issue(issue_ctx)
69-
print_args(args)
70-
check_args(args, ['package name', 'version', 'author', 'short description', 'long description', 'homepage', 'link'])
28+
def register(pkg_name, version, author, short_desc, long_desc, homepage, link):
29+
# Read our index first
7130
with open(INDEX_FILE) as html_file:
7231
soup = BeautifulSoup(html_file, "html.parser")
73-
n_package_name = normalize(args['package name'])
32+
norm_pkg_name = normalize(pkg_name)
7433

75-
if package_exists(soup, n_package_name):
76-
raise ValueError("Package {} seems to already exists".format(n_package_name))
34+
if package_exists(soup, norm_pkg_name):
35+
raise ValueError("Package {} seems to already exists".format(norm_pkg_name))
7736

7837
# Create a new anchor element for our new package
7938
last_anchor = soup.find_all('a')[-1] # Copy the last anchor element
8039
new_anchor = copy.copy(last_anchor)
81-
new_anchor['href'] = "{}/".format(n_package_name)
82-
new_anchor.contents[0].replace_with(args['package name'])
40+
new_anchor['href'] = "{}/".format(norm_pkg_name)
41+
new_anchor.contents[0].replace_with(pkg_name)
8342
spans = new_anchor.find_all('span')
84-
spans[1].string = args['version'] # First span contain the version
85-
spans[2].string = args['short description'] # Second span contain the short description
43+
spans[1].string = version # First span contain the version
44+
spans[2].string = short_desc # Second span contain the short description
8645

8746
# Add it to our index and save it
8847
last_anchor.insert_after(new_anchor)
@@ -93,93 +52,97 @@ def register(issue_ctx):
9352
with open(TEMPLATE_FILE) as temp_file:
9453
template = temp_file.read()
9554

96-
template = template.replace("_package_name", args['package name'])
97-
template = template.replace("_version", args['version'])
98-
template = template.replace("_link", "{}#egg={}-{}".format(args['link'], n_package_name, args['version']))
99-
template = template.replace("_homepage", args['homepage'])
100-
template = template.replace("_author", args['author'])
101-
template = template.replace("_long_description", args['long description'])
55+
template = template.replace("_package_name", pkg_name)
56+
template = template.replace("_version", version)
57+
template = template.replace("_link", "{}#egg={}-{}".format(link, norm_pkg_name, version))
58+
template = template.replace("_homepage", homepage)
59+
template = template.replace("_author", author)
60+
template = template.replace("_long_description", long_desc)
10261

103-
os.mkdir(n_package_name)
104-
package_index = os.path.join(n_package_name, INDEX_FILE)
62+
os.mkdir(norm_pkg_name)
63+
package_index = os.path.join(norm_pkg_name, INDEX_FILE)
10564
with open(package_index, "w") as f:
10665
f.write(template)
10766

10867

109-
def update(issue_ctx):
110-
args = parse_issue(issue_ctx)
111-
print_args(args)
112-
check_args(args, ['package name', 'new version', 'link for the new version'])
68+
def update(pkg_name, version, link):
69+
# Read our index first
11370
with open(INDEX_FILE) as html_file:
11471
soup = BeautifulSoup(html_file, "html.parser")
115-
n_package_name = normalize(args['package name'])
72+
norm_pkg_name = normalize(pkg_name)
11673

117-
if not package_exists(soup, n_package_name):
118-
raise ValueError("Package {} seems to not exists".format(n_package_name))
74+
if not package_exists(soup, norm_pkg_name):
75+
raise ValueError("Package {} seems to not exists".format(norm_pkg_name))
11976

12077
# Change the version in the main page
121-
anchor = soup.find('a', attrs={"href": "{}/".format(n_package_name)})
78+
anchor = soup.find('a', attrs={"href": "{}/".format(norm_pkg_name)})
12279
spans = anchor.find_all('span')
123-
spans[1].string = args['new version']
80+
spans[1].string = version
12481
with open(INDEX_FILE, 'wb') as index:
12582
index.write(soup.prettify("utf-8"))
12683

12784
# Change the package page
128-
index_file = os.path.join(n_package_name, INDEX_FILE)
85+
index_file = os.path.join(norm_pkg_name, INDEX_FILE)
12986
with open(index_file) as html_file:
13087
soup = BeautifulSoup(html_file, "html.parser")
13188

13289
# Create a new anchor element for our new version
13390
last_anchor = soup.find_all('a')[-1] # Copy the last anchor element
13491
new_anchor = copy.copy(last_anchor)
135-
new_anchor['href'] = "{}#egg={}-{}".format(args['link for the new version'], n_package_name, args['new version'])
92+
new_anchor['href'] = "{}#egg={}-{}".format(link, norm_pkg_name, version)
13693

13794
# Add it to our index
13895
last_anchor.insert_after(new_anchor)
13996

14097
# Change the latest version
141-
soup.html.body.div.section.find_all('span')[1].contents[0].replace_with(args['new version'])
98+
soup.html.body.div.section.find_all('span')[1].contents[0].replace_with(version)
14299

143100
# Save it
144101
with open(index_file, 'wb') as index:
145102
index.write(soup.prettify("utf-8"))
146103

147104

148-
def delete(issue_ctx):
149-
args = parse_issue(issue_ctx)
150-
print_args(args)
151-
check_args(args, ['package name'])
105+
def delete(pkg_name):
106+
# Read our index first
152107
with open(INDEX_FILE) as html_file:
153108
soup = BeautifulSoup(html_file, "html.parser")
154-
n_package_name = normalize(args['package name'])
109+
norm_pkg_name = normalize(pkg_name)
155110

156-
if not package_exists(soup, n_package_name):
157-
raise ValueError("Package {} seems to not exists".format(n_package_name))
111+
if not package_exists(soup, norm_pkg_name):
112+
raise ValueError("Package {} seems to not exists".format(norm_pkg_name))
158113

159114
# Remove the package directory
160-
shutil.rmtree(n_package_name)
115+
shutil.rmtree(norm_pkg_name)
161116

162117
# Find and remove the anchor corresponding to our package
163-
anchor = soup.find('a', attrs={"href": "{}/".format(n_package_name)})
118+
anchor = soup.find('a', attrs={"href": "{}/".format(norm_pkg_name)})
164119
anchor.extract()
165120
with open(INDEX_FILE, 'wb') as index:
166121
index.write(soup.prettify("utf-8"))
167122

168123

169124
def main():
170-
# Get the context from the environment variable
171-
context = json.loads(os.environ['GITHUB_CONTEXT'])
172-
issue_ctx = context['event']['issue']
173-
title = issue_ctx['title']
174-
175-
if title.startswith("🟢"):
176-
register(issue_ctx)
177-
178-
if title.startswith("🔵"):
179-
update(issue_ctx)
180-
181-
if title.startswith("🔴"):
182-
delete(issue_ctx)
125+
# Call the right method, with the right arguments
126+
action = os.environ["PKG_ACTION"]
127+
128+
if action == "REGISTER":
129+
register(
130+
pkg_name=os.environ["PKG_NAME"],
131+
version=os.environ["PKG_VERSION"],
132+
author=os.environ["PKG_AUTHOR"],
133+
short_desc=os.environ["PKG_SHORT_DESC"],
134+
long_desc=os.environ["PKG_LONG_DESC"],
135+
homepage=os.environ["PKG_HOMEPAGE"],
136+
link=os.environ["PKG_LINK"],
137+
)
138+
elif action == "DELETE":
139+
delete(pkg_name=os.environ["PKG_NAME"])
140+
elif action == "UPDATE":
141+
update(
142+
pkg_name=os.environ["PKG_NAME"],
143+
version=os.environ["PKG_VERSION"],
144+
link=os.environ["PKG_LINK"],
145+
)
183146

184147

185148
if __name__ == "__main__":

.github/workflows/delete.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: delete
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
package_name:
7+
description: Package name
8+
required: true
9+
type: string
10+
11+
jobs:
12+
delete:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
python-version: [3.6]
17+
18+
steps:
19+
- uses: actions/checkout@v2
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v1
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
- name: Run Action
25+
env:
26+
PKG_ACTION: DELETE
27+
PKG_NAME: ${{ inputs.package_name }}
28+
run: |
29+
pip install beautifulsoup4
30+
python .github/actions.py
31+
- name: Create Pull Request
32+
uses: peter-evans/create-pull-request@v3
33+
with:
34+
commit-message: ':package: [:robot:] Delete package from PyPi index'
35+
title: '[🤖] Delete `${{ inputs.package_name }}` from PyPi index'
36+
body: Automatically generated PR, deleting `${{ inputs.package_name }}` from
37+
PyPi index.
38+
branch-suffix: random
39+
delete-branch: true

.github/workflows/register.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: register
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
package_name:
7+
description: 'Package name'
8+
required: true
9+
type: string
10+
version:
11+
description: 'Version of the package'
12+
required: true
13+
type: string
14+
author:
15+
description: 'Author(s) of the package'
16+
required: true
17+
type: string
18+
short_desc:
19+
description: 'A short description of the package'
20+
required: true
21+
type: string
22+
long_desc:
23+
description: 'A longer description of the package (HTML)'
24+
required: true
25+
type: string
26+
homepage:
27+
description: 'Homepage of the package (link to the github repository)'
28+
required: true
29+
type: string
30+
link:
31+
description: 'Link used for `pip`. For example, for a github-hosted package refered by the tag `v3.0.2`, it would be : git+https://github.com/huggingface/[email protected]'
32+
required: true
33+
type: string
34+
35+
jobs:
36+
update:
37+
runs-on: ubuntu-latest
38+
strategy:
39+
matrix:
40+
python-version: [3.6]
41+
42+
steps:
43+
- uses: actions/checkout@v2
44+
- name: Set up Python ${{ matrix.python-version }}
45+
uses: actions/setup-python@v1
46+
with:
47+
python-version: ${{ matrix.python-version }}
48+
- name: Run Action
49+
env:
50+
PKG_ACTION: REGISTER
51+
PKG_NAME: ${{ inputs.package_name }}
52+
PKG_VERSION: ${{ inputs.version }}
53+
PKG_AUTHOR: ${{ inputs.author }}
54+
PKG_SHORT_DESC: ${{ inputs.short_desc }}
55+
PKG_LONG_DESC: ${{ inputs.long_desc }}
56+
PKG_HOMEPAGE: ${{ inputs.homepage }}
57+
PKG_LINK: ${{ inputs.link }}
58+
run: |
59+
pip install beautifulsoup4
60+
python .github/actions.py
61+
- name: Create Pull Request
62+
uses: peter-evans/create-pull-request@v3
63+
with:
64+
commit-message: ':package: [:robot:] Register package in PyPi index'
65+
title: '[🤖] Register `${{ inputs.package_name }}` in PyPi index'
66+
body: Automatically generated PR, registering `${{ inputs.package_name }}` in PyPi
67+
index.
68+
branch-suffix: random
69+
delete-branch: true

0 commit comments

Comments
 (0)