def job_marketplace(event_type, event_id, data):

    owner = data["marketplace_purchase"]["account"]["login"]
    account_type = data["marketplace_purchase"]["account"]["type"]
    try:
        installation = github_app.get_client().get_installation(
            owner, account_type=account_type
        )
    except exceptions.MergifyNotInstalled:
        return

    r = utils.get_redis_for_cache()
    r.delete("subscription-cache-%s" % installation["id"])
    subscription = sub_utils.get_subscription(r, installation["id"])

    LOG.info(
        "Marketplace event",
        event_type=event_type,
        event_id=event_id,
        install_id=installation["id"],
        sender=data["sender"]["login"],
        gh_owner=owner,
        subscription_active=subscription["subscription_active"],
        subscription_reason=subscription["subscription_reason"],
    )
Beispiel #2
0
def job_marketplace(event_type, event_id, data):

    owner = data["marketplace_purchase"]["account"]["login"]
    account_type = data["marketplace_purchase"]["account"]["type"]
    try:
        installation_id = github_app.get_client().get_installation_id(
            owner, account_type=account_type
        )
    except httpx.HTTPClientSideError as e:
        LOG.warning("mergify not installed", gh_owner=owner, error=str(e))
        installation_id = None
        subscription = {
            "subscription_active": "Unknown",
            "subscription_reason": "No",
            "tokens": None,
        }
    else:
        r = utils.get_redis_for_cache()
        r.delete("subscription-cache-%s" % installation_id)
        subscription = sub_utils.get_subscription(r, installation_id)

    LOG.info(
        "Marketplace event",
        event_type=event_type,
        event_id=event_id,
        install_id=installation_id,
        sender=data["sender"]["login"],
        gh_owner=owner,
        subscription_active=subscription["subscription_active"],
        subscription_reason=subscription["subscription_reason"],
    )
Beispiel #3
0
 def get_access_token(self):
     now = datetime.utcnow()
     if self._access_token is None or self._access_token_expiration <= now:
         r = github_app.get_client().post(
             f"/app/installations/{self.installation_id}/access_tokens", )
         self._access_token = r.json()["token"]
         self._access_token_expiration = datetime.fromisoformat(
             r.json()["expires_at"][:-1])  # Remove the Z
     return self._access_token
Beispiel #4
0
 def __init__(self, owner, repo, installation_id=None):
     self.owner = owner
     self.repo = repo
     self.installation_id = installation_id or github_app.get_client(
     ).get_installation_id(owner, repo)
     super().__init__(
         base_url=
         f"https://api.{config.GITHUB_DOMAIN}/repos/{owner}/{repo}/",
         auth=GithubInstallationAuth(self.installation_id),
         **common.DEFAULT_CLIENT_OPTIONS,
     )
Beispiel #5
0
def get_installation(owner, repo, installation_id=None):
    installation = github_app.get_client().get_installation(owner, repo)
    if installation_id is not None and installation["id"] != installation_id:
        LOG.error(
            "installation id for repository diff from event installation id",
            gh_owner=owner,
            gh_repo=repo,
            installation_id=installation["id"],
            expected_installation_id=installation_id,
        )
    return installation
Beispiel #6
0
 def get_access_token(self):
     now = datetime.utcnow()
     if self._cached_token is None or self._cached_token.expiration <= now:
         r = github_app.get_client().post(
             f"/app/installations/{self.installation_id}/access_tokens",
         )
         self._cached_token = CachedToken(
             self.installation_id,
             r.json()["token"],
             datetime.fromisoformat(r.json()["expires_at"][:-1]),  # Remove the Z
         )
         LOG.info(
             "New token acquired",
             gh_owner=self.owner,
             gh_repo=self.repo,
             expire_at=self._cached_token.expiration,
         )
     return self._cached_token.token
Beispiel #7
0
def get_installation_by_id(installation_id):
    return github_app.get_client().get_installation_by_id(installation_id)