Skip to content

Commit 8e98c8f

Browse files
RobPasMuejorgepilotoMaxJPRey
authored
Automate milestone creation (#40)
* Upload yml file and dummy python file * Pre-commit * Standarize connection to GitHub * Handling env vars correctly * Codespell check * Reduce strings * Improving docstring * Adapt milestone script and set new location * Missing release date in workflow * Pre-commit checks * Adapting module docstring as per @jorgepiloto's request * Update tools/milestone.py Co-authored-by: Jorge Martínez <[email protected]> * Update tools/milestone.py Co-authored-by: Maxime Rey <[email protected]> Co-authored-by: Jorge Martínez <[email protected]> Co-authored-by: Maxime Rey <[email protected]>
1 parent 54a8de5 commit 8e98c8f

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Create milestones for Ansys Release
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
release-date:
6+
description: 'Milestone release date (format must be YYYY/MM/DD)'
7+
required: true
8+
default: '2023/01/30'
9+
10+
env:
11+
MAIN_PYTHON_VERSION: '3.9'
12+
13+
jobs:
14+
create-milestone:
15+
name: Create a milestone in ${{ matrix.pyansys-projects }}
16+
runs-on: ubuntu-latest
17+
strategy:
18+
matrix:
19+
pyansys-projects:
20+
[
21+
"pyansys/pymapdl",
22+
"pyansys/pymapdl-reader",
23+
"pyansys/pydpf-core",
24+
"pyansys/pydpf-post",
25+
"pyansys/pyfluent",
26+
"pyansys/pyfluent-parametric",
27+
"pyansys/pyfluent-visualization",
28+
"pyansys/pyaedt",
29+
"pyansys/pypim",
30+
"pyansys/grantami-bomanalytics",
31+
"pyansys/openapi-common",
32+
]
33+
steps:
34+
- uses: actions/checkout@v3
35+
36+
- name: Set up Python ${{ env.MAIN_PYTHON_VERSION }}
37+
uses: actions/setup-python@v4
38+
with:
39+
python-version: ${{ env.MAIN_PYTHON_VERSION }}
40+
41+
- name: Linux pip cache
42+
uses: actions/cache@v3
43+
if: ${{ runner.os == 'Linux' }}
44+
with:
45+
path: ~/.cache/pip
46+
key: Python-${{ runner.os }}-create-milestone
47+
48+
- name: Install requirements
49+
run: |
50+
pip install pygithub
51+
52+
- name: Create the milestone
53+
env:
54+
TOKEN: ${{ secrets.CREATE_MILESTONE_TOKEN }}
55+
REPOSITORY: ${{ matrix.pyansys-projects }}
56+
RELEASE_DATE: ${{ github.event.inputs.release-date }}
57+
run: |
58+
python tools/milestone.py
59+

tools/milestone.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""Script for automatically creating a milestone in repositories.
2+
3+
Only specified repositories (via environment variable) will be handled.
4+
5+
The purpose of this code is to connect to a specific repository
6+
and create a milestone. This milestone will be associated to a certain date
7+
provided as an input argument.
8+
9+
"""
10+
11+
import datetime
12+
import os
13+
14+
import github
15+
16+
# Insert your credentials... It should be a PAT. None by default
17+
TOKEN = None
18+
19+
# Provide the repository you want to create a milestone in... None by default
20+
REPOSITORY = None
21+
22+
# Provide the release date to be considered... None by default
23+
# If you provide manual input it should be using:
24+
#
25+
# RELEASE_DATE = datetime.datetime.strptime(date_str,"%Y/%m/%d")
26+
#
27+
# where "date_str" must be a string date of format YYYY/MM/DD
28+
RELEASE_DATE = None
29+
30+
# =============================================================================
31+
# MODIFY WITH CAUTION FROM THIS POINT ONWARDS
32+
# =============================================================================
33+
34+
# Check if a value for TOKEN was provided
35+
if TOKEN is None:
36+
# This probably means that we are creating the milestone automatically
37+
# using our GitHub action: Create milestones for Ansys Release...
38+
# Thus, let us read the GitHub Token or PAT.
39+
print("Reading access token from 'TOKEN' environment variable...")
40+
TOKEN = os.environ.get("TOKEN", default=None)
41+
42+
# Check if a value for REPOSITORY was provided
43+
if REPOSITORY is None:
44+
# This probably means that we are creating the milestone automatically
45+
# using our GitHub action: Create milestones for Ansys Release...
46+
# Thus, let us read the repository name.
47+
print("Reading target repo from 'REPOSITORY' environment variable...")
48+
REPOSITORY = os.environ.get("REPOSITORY", default=None)
49+
50+
# Check if a value for RELEASE_DATE was provided
51+
if RELEASE_DATE is None:
52+
# This probably means that we are creating the milestone automatically
53+
# using our GitHub action: Create milestones for Ansys Release...
54+
# Thus, let us read the release date to be considered.
55+
print("Reading target repo from 'RELEASE_DATE' environment variable...")
56+
env_var = os.environ.get("RELEASE_DATE", default=None)
57+
if env_var is not None:
58+
try:
59+
RELEASE_DATE = datetime.datetime.strptime(RELEASE_DATE, "%Y/%m/%d")
60+
except (TypeError, ValueError, IndexError, KeyError):
61+
raise RuntimeError(
62+
"""Problem parsing input date. It should be a string in format YYYY/MM/DD""" # noqa: E501
63+
)
64+
65+
66+
# If the value for TOKEN, REPOSITORY, RELEASE_DATE is None... throw error!
67+
if TOKEN is None:
68+
raise ValueError("No TOKEN value available. Consider adding it.")
69+
elif REPOSITORY is None:
70+
raise ValueError("No REPOSITORY value available. Consider adding it.")
71+
elif RELEASE_DATE is None:
72+
raise ValueError("No RELEASE_DATE value available. Consider adding it.")
73+
74+
# Create a connection to GitHub
75+
g = github.Github(TOKEN)
76+
77+
# Get the repository we want to create the milestone at
78+
repo = g.get_repo(REPOSITORY)
79+
80+
# Get its last release - assuming semantic versioning (i.e. v0.1.0)
81+
major, minor, _ = repo.get_latest_release().tag_name.replace("v", "").split(".") # noqa: E501
82+
next_release = f"v{major}.{int(minor)+1}.0"
83+
84+
# Get its available milestones
85+
milestones = repo.get_milestones(state="all")
86+
87+
# Check if there is already any milestone whose name matches "next_release"
88+
is_created = False
89+
for milestone in milestones:
90+
if next_release in milestone.title:
91+
# The release is already created!
92+
is_created = True
93+
break
94+
95+
# If the milestone hasn't been created yet... go ahead!
96+
if not is_created:
97+
98+
# Milestone information
99+
desc = f"""This repository is part of an Ansys Release in the Unified Install.
100+
101+
Thus, it is necessary to create a release for the next Dev Complete date.
102+
Please consider releasing by {RELEASE_DATE.strftime("%Y/%m/%d")}"""
103+
104+
# Create a new milestone
105+
repo.create_milestone(
106+
title=next_release,
107+
state="open",
108+
description=desc,
109+
due_on=RELEASE_DATE + datetime.timedelta(hours=12),
110+
)
111+
112+
print(f"Milestone was created at {REPOSITORY} with name {next_release}!")
113+
else:
114+
print(f"Milestone was already available at {REPOSITORY}... Skipping creation!") # noqa: 501

0 commit comments

Comments
 (0)