2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121# THE SOFTWARE.
2222
23+ """An utility to automatically apply the 'hacktoberfest' label to open issues
24+ marked as 'good first issue', during DigitalOcean's/GitHub's Hacktoberfest
25+ event.
26+ """
27+
2328import argparse
2429import datetime
2530import requests
3338 "--remove-label" ,
3439 action = "store_true" ,
3540 help = "Option to remove Hacktoberfest labels, instead of adding them." ,
36- dest = "remove_label" ,
41+ dest = "remove_labels" ,
42+ )
43+ cli_args .add_argument (
44+ "--dry-run" ,
45+ action = "store_true" ,
46+ help = "Option to remove Hacktoberfest labels, instead of adding them." ,
47+ dest = "dry_run" ,
3748)
3849
3950
@@ -56,7 +67,7 @@ def is_hacktober_season():
5667 ]
5768 if add_range [0 ] <= today <= add_range [1 ]:
5869 return True , "add"
59- elif remove_range [0 ] <= today <= remove_range [1 ]:
70+ if remove_range [0 ] <= today <= remove_range [1 ]:
6071 return True , "remove"
6172
6273 return False , None
@@ -70,7 +81,7 @@ def get_open_issues(repo):
7081 }
7182 response = github .get ("/repos/" + repo ["full_name" ] + "/issues" , params = params )
7283 if not response .ok :
73- print ("Failed to retrieve issues for '{}'" . format ( repo [" name" ]) )
84+ print (f "Failed to retrieve issues for '{ repo [' name' ] } '" )
7485 return False
7586
7687 issues = []
@@ -79,33 +90,21 @@ def get_open_issues(repo):
7990 [issue for issue in response .json () if "pull_request" not in issue ]
8091 )
8192
82- try :
83- links = response .headers ["Link" ]
84- except KeyError :
85- break
86- next_url = None
87- for link in links .split ("," ):
88- link , rel = link .split (";" )
89- link = link .strip (" <>" )
90- rel = rel .strip ()
91- if rel == 'rel="next"' :
92- next_url = link
93- break
94- if not next_url :
93+ if response .links .get ("next" ):
94+ response = requests .get (response .links ["next" ]["url" ])
95+ else :
9596 break
9697
97- response = requests .get (link , timeout = 30 )
98-
9998 return issues
10099
101100
102- def ensure_hacktober_label_exists (repo ):
101+ def ensure_hacktober_label_exists (repo , dry_run = False ):
103102 """Checks if the 'Hacktoberfest' label exists on the repo.
104103 If not, creates the label.
105104 """
106- response = github .get ("/repos/" + repo [" full_name" ] + " /labels" )
105+ response = github .get (f "/repos/{ repo [' full_name' ] } /labels" )
107106 if not response .ok :
108- print ("Failed to retrieve labels for '{}'" . format ( repo [" name" ]) )
107+ print (f "Failed to retrieve labels for '{ repo [' name' ] } '" )
109108 return False
110109
111110 repo_labels = [label ["name" ] for label in response .json ()]
@@ -117,17 +116,16 @@ def ensure_hacktober_label_exists(repo):
117116 "color" : "f2b36f" ,
118117 "description" : "DigitalOcean's Hacktoberfest" ,
119118 }
120- result = github .post ("/repos/" + repo ["full_name" ] + "/labels" , json = params )
121- if not result .status_code == 201 :
122- print (
123- "Failed to create new Hacktoberfest label for: {}" .format (repo ["name" ])
124- )
125- return False
119+ if not dry_run :
120+ result = github .post (f"/repos/{ repo ['full_name' ]} /labels" , json = params )
121+ if not result .status_code == 201 :
122+ print (f"Failed to create new Hacktoberfest label for: { repo ['name' ]} " )
123+ return False
126124
127125 return True
128126
129127
130- def assign_hacktoberfest (repo , issues = None , remove_labels = False ):
128+ def assign_hacktoberfest (repo , issues = None , remove_labels = False , dry_run = False ):
131129 """Gathers open issues on a repo, and assigns the 'Hacktoberfest' label
132130 to each issue if its not already assigned.
133131 """
@@ -150,50 +148,54 @@ def assign_hacktoberfest(repo, issues=None, remove_labels=False):
150148 update_issue = True
151149 else :
152150 if has_good_first and not has_hacktober :
153- label_exists = ensure_hacktober_label_exists (repo )
151+ label_exists = ensure_hacktober_label_exists (repo , dry_run )
154152 if not label_exists :
155153 continue
156154 update_issue = True
157155
158156 if update_issue :
159157 params = {"labels" : label_names }
160- result = github .patch (
161- "/repos/" + repo ["full_name" ] + "/issues/" + str (issue ["number" ]),
162- json = params ,
163- )
164-
165- if result .ok :
166- labels_changed += 1
158+ if not dry_run :
159+ result = github .patch (
160+ f"/repos/{ repo ['full_name' ]} /issues/{ str (issue ['number' ])} " ,
161+ json = params ,
162+ )
163+
164+ if result .ok :
165+ labels_changed += 1
166+ else :
167+ # sadly, GitHub will only silently ignore labels that are
168+ # not added and return a 200. so this will most likely only
169+ # trigger on endpoint/connection failures.
170+ print (f"Failed to add Hacktoberfest label to: { issue ['url' ]} " )
167171 else :
168- # sadly, GitHub will only silently ignore labels that are
169- # not added and return a 200. so this will most likely only
170- # trigger on endpoint/connection failures.
171- print ("Failed to add Hacktoberfest label to: {}" .format (issue ["url" ]))
172+ labels_changed += 1
172173
173174 return labels_changed
174175
175176
176- def process_hacktoberfest (repo , issues = None , remove_labels = False ):
177- result = assign_hacktoberfest (repo , issues , remove_labels )
177+ def process_hacktoberfest (repo , issues = None , remove_labels = False , dry_run = False ):
178+ """Run hacktoberfest functions and return the result."""
179+ result = assign_hacktoberfest (repo , issues , remove_labels , dry_run )
178180 return result
179181
180182
181183if __name__ == "__main__" :
182- labels_assigned = 0
184+ LABELS_ASSIGNED = 0
183185 args = cli_args .parse_args ()
184186
185- remove_labels = args .remove_label
186-
187- if not remove_labels :
187+ if not args .remove_labels :
188188 print ("Checking for open issues to assign the Hacktoberfest label to..." )
189189 else :
190190 print ("Checking for open issues to remove the Hacktoberfest label from..." )
191191
192192 repos = common_funcs .list_repos ()
193- for repo in repos :
194- labels_assigned += process_hacktoberfest (repo , remove_labels )
193+ for repository in repos :
194+ LABELS_ASSIGNED += process_hacktoberfest (
195+ repository , remove_labels = args .remove_labels , dry_run = args .dry_run
196+ )
195197
196- if not remove_labels :
197- print ("Added the Hacktoberfest label to {} issues." . format ( labels_assigned ) )
198+ if not args . remove_labels :
199+ print (f "Added the Hacktoberfest label to { LABELS_ASSIGNED } issues." )
198200 else :
199- print ("Removed the Hacktoberfest label from {} issues." . format ( labels_assigned ) )
201+ print (f "Removed the Hacktoberfest label from { LABELS_ASSIGNED } issues." )
0 commit comments