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