Skip to content

Instantly share code, notes, and snippets.

@wozulong
Last active October 15, 2024 16:39
Show Gist options
  • Save wozulong/f3c5962cfee7e62341546906b4844204 to your computer and use it in GitHub Desktop.
Save wozulong/f3c5962cfee7e62341546906b4844204 to your computer and use it in GitHub Desktop.
检查issues里的白嫖者,自动关闭+加标签+锁定
import requests
issue_labels = ['no respect']
github_repo = '<owner>/<repo>'
github_token = '<github token>'
headers = {
'Authorization': 'Bearer ' + github_token,
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
}
def get_stargazers(repo):
page = 1
_stargazers = {}
while True:
queries = {
'per_page': 100,
'page': page,
}
url = 'https://api.github.com/repos/{}/stargazers?'.format(repo)
resp = requests.get(url, headers=headers, params=queries)
if resp.status_code != 200:
raise Exception('Error get stargazers: ' + resp.text)
data = resp.json()
if not data:
break
for stargazer in data:
_stargazers[stargazer['login']] = True
page += 1
print('list stargazers done, total: ' + str(len(_stargazers)))
return _stargazers
def get_issues(repo):
page = 1
_issues = []
while True:
queries = {
'state': 'open',
'sort': 'created',
'direction': 'desc',
'per_page': 100,
'page': page,
}
url = 'https://api.github.com/repos/{}/issues?'.format(repo)
resp = requests.get(url, headers=headers, params=queries)
if resp.status_code != 200:
raise Exception('Error get issues: ' + resp.text)
data = resp.json()
if not data:
break
_issues += data
page += 1
print('list issues done, total: ' + str(len(_issues)))
return _issues
def close_issue(repo, issue_number):
url = 'https://api.github.com/repos/{}/issues/{}'.format(repo, issue_number)
data = {
'state': 'closed',
'state_reason': 'not_planned',
'labels': issue_labels,
}
resp = requests.patch(url, headers=headers, json=data)
if resp.status_code != 200:
raise Exception('Error close issue: ' + resp.text)
print('issue: {} closed'.format(issue_number))
def lock_issue(repo, issue_number):
url = 'https://api.github.com/repos/{}/issues/{}/lock'.format(repo, issue_number)
data = {
'lock_reason': 'spam',
}
resp = requests.put(url, headers=headers, json=data)
if resp.status_code != 204:
raise Exception('Error lock issue: ' + resp.text)
print('issue: {} locked'.format(issue_number))
if '__main__' == __name__:
stargazers = get_stargazers(github_repo)
issues = get_issues(github_repo)
for issue in issues:
if 'pull_request' in issue:
continue
login = issue['user']['login']
if login not in stargazers:
print('issue: {}, login: {} not in stargazers'.format(issue['number'], login))
close_issue(github_repo, issue['number'])
lock_issue(github_repo, issue['number'])
print('done')
@14790897
Copy link

适合大项目,我是用不着了

@katorly
Copy link

katorly commented May 7, 2024

适合不怕骂的大项目😆(参考某必须star才能看文档的“开源”项目)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment