|
| 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