Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code scanning alerts #2227

Merged
merged 23 commits into from Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4aaa777
ADD code scanning alerts
eric-nieuwland Sep 24, 2021
5b6c479
ADD example of code scanning alerts
eric-nieuwland Sep 27, 2021
e0f202f
FIX typo
eric-nieuwland Oct 11, 2021
92c1de3
Merge branch 'PyGithub:master' into master
eric-nieuwland Oct 20, 2021
c6871ba
ADD test
eric-nieuwland Oct 20, 2021
05af558
FIX __repr__s
eric-nieuwland Oct 20, 2021
41ccb3a
RENAME to satisfy Sphinx
eric-nieuwland Oct 21, 2021
f4606a5
Merge branch 'PyGithub:master' into master
eric-nieuwland Oct 22, 2021
12b0db2
ADD more details to test
eric-nieuwland Oct 23, 2021
90806d5
Merge branch 'master' of https://github.com/eric-nieuwland/PyGithub
eric-nieuwland Oct 23, 2021
b06e5f6
FIX property/method documentation to name correct class
eric-nieuwland Oct 23, 2021
5aa8c03
Merge branch 'PyGithub:master' into master
eric-nieuwland Oct 24, 2021
49be9a2
Merge branch 'PyGithub:master' into master
eric-nieuwland Dec 23, 2021
edb0996
Add copyright
eric-nieuwland Apr 29, 2022
b712626
Add copyright
eric-nieuwland Apr 29, 2022
0dfb95f
Add copyright
eric-nieuwland Apr 29, 2022
97f15ca
Merge remote-tracking branch 'source/master' into xmaster
eric-nieuwland Oct 28, 2022
da9cdd1
FIX .pyi files
eric-nieuwland Oct 28, 2022
5f59587
Make black happy
eric-nieuwland Oct 28, 2022
fc7358d
Make black happy
eric-nieuwland Oct 28, 2022
56dad77
Merge branch 'master' into master
eric-nieuwland Nov 2, 2022
b4cd2d2
Merge branch 'master' into master
eric-nieuwland Nov 8, 2022
aad01ff
Update Repository.rst
eric-nieuwland Nov 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions doc/examples/Repository.rst
Expand Up @@ -35,6 +35,31 @@ Get list of open issues
Issue(title="Is suspended_users for github enterprise implemented in NamedUser?", number=900)
Issue(title="Adding migration api wrapper", number=899)

Get list of code scanning alerts
eric-nieuwland marked this conversation as resolved.
Show resolved Hide resolved
--------------------------------

.. code-block:: python

>>> repo = g.get_repo("PyGithub/PyGithub")
>>> codescan_alerts = repo.get_codescan_alerts()
>>> for alert in codescan_alerts:
... print(alert.number, alert.created_at, alert.dismissed_at)
... print(" ", alert.tool.name, alert.tool.version, alert.tool.guid)
... print(" ", alert.rule.name alert.rule.security_severity_level alert.rule.severity)
... print(" ", alert.rule.description)
... print(" ", alert.most_recent_instance.ref, alert.most_recent_instance.state)
... print(" ", alert.most_recent_instance.location)
... print(" ", alert.most_recent_instance.message['text'])
...
3 1984-02-29 12:34:56 None
CodeQL 2.6.1 None
py/weak-sensitive-data-hashing high warning
Use of a broken or weak cryptographic hashing algorithm on sensitive data
refs/heads/master | open
src/secrets/rats.py @ l42:c13-l42:c69
Sensitive data (password) is used in a hashing algorithm (SHA1) that is insecure⤶
for password hashing, since it is not a computationally expensive hash function.

Get all the labels of the repository
------------------------------------

Expand Down
191 changes: 191 additions & 0 deletions github/CodeScanAlert.py
@@ -0,0 +1,191 @@
############################ Copyrights and license ############################
# #
# Copyright 2022 Eric Nieuwland <eric.nieuwland@gmail.com> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################

import github.CodeScanAlertInstance
import github.CodeScanRule
import github.CodeScanTool
import github.GithubObject
import github.NamedUser
import github.PaginatedList


class CodeScanAlert(github.GithubObject.NonCompletableGithubObject):
"""
This class represents alerts from code scanning.
The reference can be found here https://docs.github.com/en/rest/reference/code-scanning.
"""

def __repr__(self):
return self.get__repr__({"number": self.number})

@property
def number(self):
"""
:type: int
"""
return self._number.value

@property
def rule(self):
"""
:type: :class: `github.CodeScanRule.CodeScanRule`
"""
return self._rule.value

@property
def tool(self):
"""
:type: :class: `github.CodeScanTool.CodeScanTool`
"""
return self._tool.value

@property
def created_at(self):
"""
:type: datetime
"""
return self._created_at.value

@property
def dismissed_at(self):
"""
:type: datetime
"""
return self._dismissed_at.value

@property
def dismissed_by(self):
"""
:type: :class: `github.NamedUser.NamedUser`
"""
return self._dismissed_by.value

@property
def dismissed_reason(self):
"""
:type: str
"""
return self._dismissed_reason.value

@property
def url(self):
"""
:type: string
"""
return self._url.value

@property
def html_url(self):
"""
:type: string
"""
return self._html_url.value

@property
def instances_url(self):
"""
:type: string
"""
return self._instances_url.value

@property
def most_recent_instance(self):
"""
:type: :class: github.CodeScanAlertInstance.CodeScanAlertInstance
"""
return self._most_recent_instance.value

@property
def state(self):
"""
:type: str
"""
return self._state.value

def get_instances(self):
"""
:calls: `GET` on the URL for instances as provided by Github
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.CodeScanAlertInstance.CodeScanAlertInstance`
"""
return github.PaginatedList.PaginatedList(
github.CodeScanAlertInstance.CodeScanAlertInstance,
self._requester,
self.instances_url,
None,
)

def _initAttributes(self):
self._number = github.GithubObject.NotSet
self._rule = github.GithubObject.NotSet
self._tool = github.GithubObject.NotSet

self._created_at = github.GithubObject.NotSet
self._dismissed_at = github.GithubObject.NotSet
self._dismissed_by = github.GithubObject.NotSet
self._dismissed_reason = github.GithubObject.NotSet

self._url = github.GithubObject.NotSet
self._html_url = github.GithubObject.NotSet
self._instances_url = github.GithubObject.NotSet

self._most_recent_instance = github.GithubObject.NotSet
self._state = github.GithubObject.NotSet

def _useAttributes(self, attributes):
if "number" in attributes: # pragma no branch
self._number = self._makeIntAttribute(attributes["number"])
if "rule" in attributes: # pragma no branch
self._rule = self._makeClassAttribute(
github.CodeScanRule.CodeScanRule, attributes["rule"]
)
if "tool" in attributes: # pragma no branch
self._tool = self._makeClassAttribute(
github.CodeScanTool.CodeScanTool, attributes["tool"]
)

if "created_at" in attributes: # pragma no branch
self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
if "dismissed_at" in attributes: # pragma no branch
self._dismissed_at = self._makeDatetimeAttribute(attributes["dismissed_at"])
if "dismissed_by" in attributes: # pragma no branch
self._dismissed_by = self._makeClassAttribute(
github.NamedUser.NamedUser, attributes["dismissed_by"]
)
if "dismissed_reason" in attributes: # pragma no branch
self._dismissed_reason = self._makeStringAttribute(
attributes["dismissed_reason"]
)

if "url" in attributes: # pragma no branch
self._url = self._makeStringAttribute(attributes["url"])
if "html_url" in attributes: # pragma no branch
self._html_url = self._makeStringAttribute(attributes["html_url"])
if "instances_url" in attributes: # pragma no branch
self._instances_url = self._makeStringAttribute(attributes["instances_url"])

if "most_recent_instance" in attributes: # pragma no branch
self._most_recent_instance = self._makeClassAttribute(
github.CodeScanAlertInstance.CodeScanAlertInstance,
attributes["most_recent_instance"],
)
if "state" in attributes: # pragma no branch
self._state = self._makeStringAttribute(attributes["state"])
62 changes: 62 additions & 0 deletions github/CodeScanAlert.pyi
@@ -0,0 +1,62 @@
############################ Copyrights and license ############################
# #
# Copyright 2022 Eric Nieuwland <eric.nieuwland@gmail.com> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################

from typing import Any, Dict
from datetime import datetime

import github.GithubObject
import github.PaginatedList
import github.CodeScanRule
import github.CodeScanTool
import github.CodeScanAlertInstance

class CodeScanAlert(github.GithubObject.NonCompletableGithubObject):
def __repr__(self) -> str: ...
@property
def number(self) -> int: ...
@property
def rule(self) -> github.CodeScanRule.CodeScanRule: ...
@property
def tool(self) -> github.CodeScanTool.CodeScanTool: ...
@property
def created_at(self) -> datetime: ...
@property
def dismissed_at(self) -> datetime: ...
@property
def dismissed_by(self) -> dict: ...
@property
def dismissed_reason(self) -> str: ...
@property
def url(self) -> str: ...
@property
def html_url(self) -> str: ...
@property
def instances_url(self) -> str: ...
@property
def most_recent_instance(
self,
) -> github.CodeScanAlertInstance.CodeScanAlertInstance: ...
@property
def state(self) -> str: ...
def get_instances(self) -> github.PaginatedList.PaginatedList: ...
def _initAttributes(self) -> None: ...
def _useAttributes(self, attributes: Dict[str, Any]) -> None: ...