2
0
Fork 0

Example config and check against repository issues to avoid duplicates

This commit is contained in:
Carlos Galindo 2023-09-07 17:40:13 +02:00
commit e0d09e5015
2 changed files with 149 additions and 4 deletions

View file

@ -29,6 +29,22 @@ class IssuePoster:
'''Post a issue warning that version of software name has been published at link.'''
raise NotImplementedError()
def issue_exists(self, name: str, version: str) -> bool:
'''Checks whether the issue for the given name/version pair exists.'''
expected_title = IssuePoster.title(name, version)
get = requests.get(self.url,
headers = self.auth_headers,
timeout = TIMEOUT,
params = self.search_params(expected_title))
if not IssuePoster.check_req(get, name):
return False
data = get.json()
return type(data) == list and any(issue['title'] == expected_title for issue in data)
def search_params(self, expected_title: str) -> dict[str, Any]:
'''A parameter dictionary to query issues in different services.'''
raise NotImplementedError()
@staticmethod
def describe(name: str, version: str, link: str) -> str:
'''Generate a description for the issue.'''
@ -65,6 +81,7 @@ class GitlabPoster(IssuePoster):
'''
super().__init__(project, token, assignee, labels)
self.api_url = f"{instance}/api/v4"
self.url = f"{instance}/api/v4/projects/{project}/issues"
self.auth_headers = { 'PRIVATE-TOKEN': self.token }
def post_issue(self, name: str, version: str, link: str) -> bool:
@ -75,12 +92,15 @@ class GitlabPoster(IssuePoster):
}
if self.assignee:
payload['assignee_id'] = self.assignee
post = requests.post(f"{self.api_url}/projects/{self.project}/issues",
post = requests.post(self.url,
params = payload,
headers = self.auth_headers,
timeout = TIMEOUT)
return IssuePoster.check_req(post, name)
def search_params(self, expected_title: str) -> bool:
return { 'per_page': 1, 'search': expected_title, 'sort': 'asc', 'order_by': 'created_at' }
class ForgejoPoster(IssuePoster):
'''Forgejo (git.cgj.es instance) issue poster'''
@ -101,6 +121,7 @@ class ForgejoPoster(IssuePoster):
super().__init__(project, token, assignee, labels)
self.auth_headers = { 'Authorization': f"token {self.token}" }
self.api_url = f"{instance}/api/v1"
self.url = f"{instance}/api/v1/repos/{project}/issues"
def post_issue(self, name: str, version: str, link: str) -> bool:
payload = {
@ -109,12 +130,15 @@ class ForgejoPoster(IssuePoster):
"title": IssuePoster.title(name, version),
"labels": self.labels if self.labels else [],
}
post = requests.post(f"{self.api_url}/repos/{self.project}/issues",
post = requests.post(self.url,
json = payload,
headers = self.auth_headers,
timeout = TIMEOUT)
return IssuePoster.check_req(post, name)
def search_params(self, expected_title: str) -> bool:
return { 'q': expected_title, 'state': 'all' }
def list_labels(self) -> dict[int, str] | None:
'''Lists the labels and their IDs for this repository.'''
get = requests.get(f"{self.api_url}/repos/{self.project}/labels",
@ -166,6 +190,7 @@ class FeedReader:
def match_post_save(self, version: str, _id: str, link: str) -> bool:
'''Checks if a version is new, posts an issue and saves it as such.
If the version is not new, or the posting fails, the method stops and returns False.'''
# Match 1: with local file
try:
with open(CONFIG_DIR + self.name, encoding="utf-8") as file:
match = file.readline().strip("\n") == str(_id)
@ -173,8 +198,12 @@ class FeedReader:
match = False
if match:
return False
if not self.target.post_issue(self.name, version, link):
return False
# Match 2: with repository issues
if not self.target.issue_exists(self.name, version):
# Post the issue
if not self.target.post_issue(self.name, version, link):
return False
# Save to disk
if not os.path.isdir(CONFIG_DIR):
os.makedirs(CONFIG_DIR)
with open(CONFIG_DIR + self.name, mode="w", encoding="utf-8") as file: