pull_request.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from typing import Optional, List, Dict
  18. from github.resp_get import RespGet
  19. class PullRequest:
  20. """Pull request to filter the by specific condition.
  21. :param token: token to request GitHub API entrypoint.
  22. :param repo: GitHub repository identify, use `user/repo` or `org/repo`.
  23. """
  24. url_search = "https://api.github.com/search/issues"
  25. url_pr = "https://api.github.com/repos/{}/pulls/{}"
  26. def __init__(self, token: str, repo: Optional[str] = "apache/dolphinscheduler"):
  27. self.token = token
  28. self.repo = repo
  29. self.headers = {
  30. "Accept": "application/vnd.github+json",
  31. "Authorization": f"token {token}",
  32. }
  33. def get_merged_detail(self, number: str) -> Dict:
  34. """Get all merged pull requests detail by pr number.
  35. :param number: pull requests number you want to get detail.
  36. """
  37. return RespGet(url=self.url_pr.format(self.repo, number), headers=self.headers).get_single()
  38. def get_merged_detail_by_milestone(self, milestone: str) -> List[Dict]:
  39. """Get all merged requests pull request detail by specific milestone.
  40. :param milestone: query by specific milestone.
  41. """
  42. detail = []
  43. numbers = {pr.get("number") for pr in self.search_merged_by_milestone(milestone)}
  44. for number in numbers:
  45. pr_dict = RespGet(url=self.url_pr.format(self.repo, number), headers=self.headers).get_single()
  46. detail.append(pr_dict)
  47. return detail
  48. def search_merged_by_milestone(self, milestone: str) -> List[Dict]:
  49. """Get all merged requests pull request by specific milestone.
  50. :param milestone: query by specific milestone.
  51. """
  52. params = {"q": f"repo:{self.repo} is:pr is:merged milestone:{milestone}"}
  53. return RespGet(url=self.url_search, headers=self.headers, param=params).get_total()