pull_request.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Dict, List, Optional
  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(
  38. url=self.url_pr.format(self.repo, number), headers=self.headers
  39. ).get_single()
  40. def get_merged_detail_by_milestone(self, milestone: str) -> List[Dict]:
  41. """Get all merged requests pull request detail by specific milestone.
  42. :param milestone: query by specific milestone.
  43. """
  44. detail = []
  45. numbers = {
  46. pr.get("number") for pr in self.search_merged_by_milestone(milestone)
  47. }
  48. for number in numbers:
  49. pr_dict = RespGet(
  50. url=self.url_pr.format(self.repo, number), headers=self.headers
  51. ).get_single()
  52. detail.append(pr_dict)
  53. return detail
  54. def search_merged_by_milestone(self, milestone: str) -> List[Dict]:
  55. """Get all merged requests pull request by specific milestone.
  56. :param milestone: query by specific milestone.
  57. """
  58. params = {"q": f"repo:{self.repo} is:pr is:merged milestone:{milestone}"}
  59. return RespGet(
  60. url=self.url_search, headers=self.headers, param=params
  61. ).get_total()