|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2018-2023 contributors to the Marquez project |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +from github import Github |
| 7 | +import rich_click as click |
| 8 | +from datetime import date |
| 9 | +from typing import TYPE_CHECKING |
| 10 | +if TYPE_CHECKING: |
| 11 | + from github.PullRequest import PullRequest |
| 12 | + |
| 13 | +class GetChanges: |
| 14 | + |
| 15 | + def __init__(self, github_token: str, previous: str, current: str, path: str): |
| 16 | + self.github_token = github_token |
| 17 | + self.previous = previous |
| 18 | + self.current = current |
| 19 | + self.path = path |
| 20 | + self.pulls: list[PullRequest] = [] |
| 21 | + self.rel_title_str: str = '' |
| 22 | + self.text: list[str] = [] |
| 23 | + self.new_contributors: dict[str:str] = {} |
| 24 | + |
| 25 | + def get_pulls(self): |
| 26 | + print('Working on it...') |
| 27 | + g = Github(self.github_token) |
| 28 | + repo = g.get_repo("MarquezProject/marquez") |
| 29 | + prev_date = repo.get_release(self.previous).created_at |
| 30 | + commits = repo.get_commits(since=prev_date) |
| 31 | + self.pulls = [pull for commit in commits for pull in commit.get_pulls()] |
| 32 | + |
| 33 | + def write_title(self): |
| 34 | + self.rel_title_str = f'## [{self.current}](https://github.com/MarquezProject/marquez/compare/{self.previous}...{self.current}) - {date.today()}' |
| 35 | + |
| 36 | + def describe_changes(self): |
| 37 | + for pull in self.pulls: |
| 38 | + |
| 39 | + """ Assembles change description with PR and user URLs """ |
| 40 | + entry = [] |
| 41 | + if pull.user.login != 'dependabot[bot]': |
| 42 | + labels = [] |
| 43 | + for label in pull.labels: |
| 44 | + if label.name != 'documentation': |
| 45 | + labels.append(label.name) |
| 46 | + change_str = f'* **{labels[0]}: {pull.title}** [`#{pull.number}`]({pull.html_url}) [@{pull.user.login}]({pull.user.html_url}) ' |
| 47 | + |
| 48 | + """ Extracts one-line description if present """ |
| 49 | + beg = pull.body.find('One-line summary:') + 18 |
| 50 | + if beg == 17: |
| 51 | + change_descrip_str = ' **' |
| 52 | + else: |
| 53 | + test = pull.body.find('### Checklist') |
| 54 | + if test == -1: |
| 55 | + end = beg + 75 |
| 56 | + else: |
| 57 | + end = test - 1 |
| 58 | + descrip = pull.body[beg:end].split() |
| 59 | + descrip_str = ' '.join(descrip) |
| 60 | + change_descrip_str = f' *{descrip_str}*' |
| 61 | + |
| 62 | + entry.append(change_str + '\n') |
| 63 | + entry.append(change_descrip_str + '\n') |
| 64 | + self.text.append(entry) |
| 65 | + |
| 66 | + def get_new_contributors(self): |
| 67 | + for pull in self.pulls: |
| 68 | + comments = pull.get_issue_comments() |
| 69 | + for comment in comments: |
| 70 | + if 'Thanks for opening your' in comment.body: |
| 71 | + self.new_contributors[pull.user.login] = pull.user.url |
| 72 | + if self.new_contributors: |
| 73 | + print('New contributors:') |
| 74 | + for k, v in self.new_contributors.items(): |
| 75 | + print(f'@{k}: {v}') |
| 76 | + else: |
| 77 | + print('Note: no new contributors were found.') |
| 78 | + |
| 79 | + def update_changelog(self): |
| 80 | + f = open('changes.txt', 'w+') |
| 81 | + f = open('changes.txt', 'a') |
| 82 | + f.write(self.rel_title_str + '\n') |
| 83 | + for entry in self.text: |
| 84 | + for line in entry: |
| 85 | + f.write(line) |
| 86 | + f.close() |
| 87 | + |
| 88 | + with open('changes.txt', 'r+') as f: |
| 89 | + new_changes = f.read() |
| 90 | + with open(self.path, 'r') as contents: |
| 91 | + save = contents.read() |
| 92 | + with open(self.path, 'w') as contents: |
| 93 | + contents.write(new_changes) |
| 94 | + with open(self.path, 'a') as contents: |
| 95 | + contents.write(save) |
| 96 | + |
| 97 | +@click.command() |
| 98 | +@click.option( |
| 99 | + '--github_token', type=str, default='' |
| 100 | +) |
| 101 | +@click.option( |
| 102 | + "--previous", type=str, default='' |
| 103 | +) |
| 104 | +@click.option( |
| 105 | + "--current", type=str, default='' |
| 106 | +) |
| 107 | +@click.option( |
| 108 | + "--path", |
| 109 | + type=str, |
| 110 | + default='', |
| 111 | + help='absolute path to changelog', |
| 112 | +) |
| 113 | + |
| 114 | +def main( |
| 115 | + github_token: str, |
| 116 | + previous: str, |
| 117 | + current: str, |
| 118 | + path: str, |
| 119 | +): |
| 120 | + c = GetChanges( |
| 121 | + github_token=github_token, |
| 122 | + previous=previous, |
| 123 | + current=current, |
| 124 | + path=path |
| 125 | + ) |
| 126 | + c.get_pulls() |
| 127 | + c.describe_changes() |
| 128 | + c.write_title() |
| 129 | + c.update_changelog() |
| 130 | + c.get_new_contributors() |
| 131 | + print('...done!') |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + main() |
| 135 | + |
0 commit comments