Skip to content

Commit 0c08817

Browse files
authored
Add GH workflow for updating external links gallery via issue template
1 parent ea59c76 commit 0c08817

File tree

4 files changed

+268
-1
lines changed

4 files changed

+268
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
name: Update external links gallery
3+
about: Update external links gallery
4+
title: ''
5+
labels: 'external-links-gallery-submission'
6+
---
7+
8+
<!-- Please fill out the template below. -->
9+
10+
Checklist of required entries:
11+
12+
- [ ] `url` is specified
13+
- [ ] `title` is specified
14+
- [ ] `description` is specified
15+
16+
```yaml
17+
---
18+
url: ''
19+
thumbnail: ''
20+
title: ''
21+
description: ''
22+
authors:
23+
- name: ''
24+
affiliation: ''
25+
affiliation_url: ''
26+
email: ''
27+
- name: ''
28+
affiliation: ''
29+
affiliation_url: ''
30+
email: ''
31+
---
32+
33+
```
34+
35+
<!-- Feel free to add comments below this line -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import json
2+
import os
3+
import typing
4+
5+
import frontmatter
6+
import pydantic
7+
from markdown_it import MarkdownIt
8+
9+
10+
class Author(pydantic.BaseModel):
11+
name: str = 'anonymous'
12+
affiliation: str = None
13+
affiliation_url: typing.Union[str, pydantic.HttpUrl] = None
14+
email: typing.Union[str, pydantic.EmailStr] = None
15+
16+
17+
class Submission(pydantic.BaseModel):
18+
title: str
19+
description: str
20+
url: pydantic.HttpUrl
21+
thumbnail: typing.Union[str, pydantic.HttpUrl] = None
22+
authors: typing.List[Author] = None
23+
24+
25+
@pydantic.dataclasses.dataclass
26+
class IssueInfo:
27+
gh_event_path: pydantic.FilePath
28+
submission: Submission = pydantic.Field(default=None)
29+
30+
def __post_init_post_parse__(self):
31+
with open(self.gh_event_path) as f:
32+
self.data = json.load(f)
33+
34+
def create_submission(self):
35+
self._get_inputs()
36+
self._create_submission_input()
37+
return self
38+
39+
def _get_inputs(self):
40+
self.author = self.data['issue']['user']['login']
41+
self.title = self.data['issue']['title']
42+
self.body = self.data['issue']['body']
43+
44+
def _create_submission_input(self):
45+
md = MarkdownIt()
46+
inputs = None
47+
for token in md.parse(self.body):
48+
if token.tag == 'code':
49+
inputs = frontmatter.loads(token.content).metadata
50+
break
51+
name = inputs.get('name')
52+
title = inputs.get('title')
53+
description = inputs.get('description')
54+
url = inputs.get('url')
55+
thumbnail = inputs.get('thumbnail')
56+
_authors = inputs.get('authors', [])
57+
authors = []
58+
if _authors:
59+
for item in _authors:
60+
authors.append(
61+
Author(
62+
name=item.get('name', 'anyonymous'),
63+
affiliation=item.get('affiliation'),
64+
affiliation_url=item.get('affiliation_url'),
65+
email=item.get('email', ''),
66+
)
67+
)
68+
else:
69+
authors = [Author(name='anyonymous')]
70+
71+
self.submission = Submission(
72+
name=name, title=title, description=description, url=url, thumbnail=thumbnail, authors=authors
73+
)
74+
75+
76+
if __name__ == '__main__':
77+
78+
issue = IssueInfo(gh_event_path=os.environ['GITHUB_EVENT_PATH']).create_submission()
79+
inputs = issue.submission.dict()
80+
with open('gallery-submission-input.json', 'w') as f:
81+
json.dump(inputs, f)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
name: Update External Links Gallery
2+
3+
on:
4+
issues:
5+
types:
6+
- opened
7+
- edited
8+
jobs:
9+
validate-user-submission:
10+
if: |
11+
github.repository == 'ProjectPythia/projectpythia.github.io'
12+
&& contains(github.event.issue.labels.*.name, 'external-links-gallery-submission')
13+
runs-on: ubuntu-latest
14+
defaults:
15+
run:
16+
shell: bash -l {0}
17+
steps:
18+
- name: Find Comment
19+
uses: peter-evans/find-comment@v1
20+
id: fc
21+
with:
22+
issue-number: ${{ github.event.issue.number }}
23+
comment-author: 'github-actions[bot]'
24+
body-includes: Thank you for your contribution
25+
- name: Create comment
26+
if: steps.fc.outputs.comment-id == ''
27+
uses: peter-evans/create-or-update-comment@v1
28+
with:
29+
issue-number: ${{ github.event.issue.number }}
30+
body: |
31+
Thank you for your contribution 🎉, @${{ github.actor }}!
32+
33+
We're currently running [validation checks](https://github.com${{ github.repository }}/actions/runs/${{ github.run_id }}) to make sure the contents of your submission are okay. An update will be posted here shortly once the validation checks are passing.
34+
- name: Update comment
35+
if: steps.fc.outputs.comment-id != ''
36+
uses: peter-evans/create-or-update-comment@v1
37+
with:
38+
comment-id: ${{ steps.fc.outputs.comment-id }}
39+
edit-mode: replace
40+
body: |
41+
Thank you for your contribution 🎉, @${{ github.actor }}!
42+
43+
We're currently running [validation checks](https://github.com${{ github.repository }}/actions/runs/${{ github.run_id }}) to make sure the contents of your submission are okay. An update will be posted here shortly once the validation checks are passing.
44+
- uses: actions/checkout@v2
45+
- uses: actions/setup-python@v2
46+
with:
47+
python-version: 3.8
48+
49+
- name: Install dependencies
50+
run: |
51+
python -m pip install pip --upgrade
52+
python -m pip install python-frontmatter markdown-it-py pydantic[email]
53+
54+
- name: Validate input
55+
run: |
56+
python .github/workflows/collect-user-submission.py
57+
exit 0
58+
59+
- uses: actions/upload-artifact@v2
60+
with:
61+
name: submission
62+
path: gallery-submission-input.json
63+
64+
create-pull-request:
65+
needs: validate-user-submission
66+
runs-on: ubuntu-latest
67+
defaults:
68+
run:
69+
shell: bash -l {0}
70+
steps:
71+
- uses: actions/checkout@v2
72+
- uses: actions/setup-python@v2
73+
with:
74+
python-version: 3.8
75+
- uses: actions/download-artifact@v2
76+
with:
77+
name: submission
78+
79+
- name: Display structure of downloaded artifacts
80+
run: |
81+
ls -R
82+
83+
- name: Install dependencies
84+
run: |
85+
python -m pip install pip --upgrade
86+
python -m pip install ruamel.yaml pre-commit
87+
88+
- name: Update gallery
89+
shell: python
90+
run: |
91+
import json
92+
from ruamel.yaml import YAML
93+
94+
yaml = YAML()
95+
submission_file = 'gallery-submission-input.json'
96+
links_file = 'content/links.yaml'
97+
with open(submission_file) as f:
98+
data = json.load(f)
99+
100+
with open(links_file) as f:
101+
links = yaml.load(f)
102+
103+
with open(links_file, 'w') as f:
104+
links.append(data)
105+
yaml.dump(links, f)
106+
107+
- name: Run pre-commit hooks
108+
run: |
109+
python -m pre_commit run --all-files
110+
111+
- name: Create pull request
112+
id: cpr
113+
uses: peter-evans/create-pull-request@v3
114+
with:
115+
commit-message: 'Update external links gallery'
116+
committer: GitHub <noreply@github.com>
117+
author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
118+
signoff: false
119+
branch: external-links-gallery-${{ github.event.issue.number }}
120+
title: 'Update external links gallery'
121+
body: |
122+
Update external links gallery as requested in #${{ github.event.issue.number }}.
123+
124+
- name: Find Comment
125+
uses: peter-evans/find-comment@v1
126+
id: fc
127+
with:
128+
issue-number: ${{ github.event.issue.number }}
129+
comment-author: 'github-actions[bot]'
130+
body-includes: We've created a pull request on your behalf
131+
132+
- name: Create comment
133+
if: steps.fc.outputs.comment-id == ''
134+
uses: peter-evans/create-or-update-comment@v1
135+
with:
136+
issue-number: ${{ github.event.issue.number }}
137+
body: |
138+
@${{ github.actor }}, your submission looks great! We've created a pull request on your behalf using the information you provided.
139+
140+
The pull request can be accessed from this url: ${{ steps.cpr.outputs.pull-request-url }}.
141+
142+
- name: Update comment
143+
if: steps.fc.outputs.comment-id != ''
144+
uses: peter-evans/create-or-update-comment@v1
145+
with:
146+
comment-id: ${{ steps.fc.outputs.comment-id }}
147+
edit-mode: replace
148+
body: |
149+
@${{ github.actor }}, your submission looks great! We've created a pull request on your behalf using the information you provided.
150+
151+
The pull request can be accessed from this url: ${{ steps.cpr.outputs.pull-request-url }}.

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ select = B,C,E,F,W,T4,B9
77

88
[isort]
99
known_first_party=
10-
known_third_party=matplotlib,pandas,yaml
10+
known_third_party=frontmatter,markdown_it,matplotlib,pandas,pydantic,yaml
1111
multi_line_output=3
1212
include_trailing_comma=True
1313
force_grid_wrap=0

0 commit comments

Comments
 (0)