|
| 1 | +"""This module contains all the classes relating to GitHub Actions secrets.""" |
| 2 | + |
| 3 | +from .. import models |
| 4 | + |
| 5 | + |
| 6 | +class PublicKey(models.GitHubCore): |
| 7 | + |
| 8 | + """Object representing a Public Key for GitHub Actions secrets. |
| 9 | +
|
| 10 | + See https://docs.github.com/en/rest/actions/secrets for more details. |
| 11 | +
|
| 12 | + .. attribute:: key_id |
| 13 | +
|
| 14 | + The ID of the public key |
| 15 | +
|
| 16 | + .. attribute:: key |
| 17 | +
|
| 18 | + The actual public key as a string |
| 19 | + """ |
| 20 | + |
| 21 | + def _update_attributes(self, publickey): |
| 22 | + self.key_id = publickey["key_id"] |
| 23 | + self.key = publickey["key"] |
| 24 | + |
| 25 | + def _repr(self): |
| 26 | + return f"<PublicKey [{self.key_id}]>" |
| 27 | + |
| 28 | + def __str__(self): |
| 29 | + return self.key |
| 30 | + |
| 31 | + |
| 32 | +class _Secret(models.GitHubCore): |
| 33 | + |
| 34 | + """Base class for all secrets for GitHub Actions. |
| 35 | +
|
| 36 | + See https://docs.github.com/en/rest/actions/secrets for more details. |
| 37 | + GitHub never reveals the secret value through its API, it is only accessible |
| 38 | + from within actions. Therefore, this object represents the secret's metadata |
| 39 | + but not its actual value. |
| 40 | + """ |
| 41 | + |
| 42 | + class_name = "_Secret" |
| 43 | + |
| 44 | + def _repr(self): |
| 45 | + return f"<{self.class_name} [{self.name}]>" |
| 46 | + |
| 47 | + def __str__(self): |
| 48 | + return self.name |
| 49 | + |
| 50 | + def _update_attributes(self, secret): |
| 51 | + self.name = secret["name"] |
| 52 | + self.created_at = self._strptime(secret["created_at"]) |
| 53 | + self.updated_at = self._strptime(secret["updated_at"]) |
| 54 | + |
| 55 | + |
| 56 | +class RepositorySecret(_Secret): |
| 57 | + """An object representing a repository secret for GitHub Actions. |
| 58 | +
|
| 59 | + See https://docs.github.com/en/rest/actions/secrets for more details. |
| 60 | + GitHub never reveals the secret value through its API, it is only accessible |
| 61 | + from within actions. Therefore, this object represents the secret's metadata |
| 62 | + but not its actual value. |
| 63 | +
|
| 64 | + .. attribute:: name |
| 65 | +
|
| 66 | + The name of the secret |
| 67 | +
|
| 68 | + .. attribute:: created_at |
| 69 | +
|
| 70 | + The timestamp of when the secret was created |
| 71 | +
|
| 72 | + .. attribute:: updated_at |
| 73 | +
|
| 74 | + The timestamp of when the secret was last updated |
| 75 | + """ |
| 76 | + |
| 77 | + class_name = "RepositorySecret" |
| 78 | + |
| 79 | + |
| 80 | +class SharedOrganizationSecret(_Secret): |
| 81 | + """An object representing an organization secret for GitHub Actions that is |
| 82 | + shared with the repository. |
| 83 | +
|
| 84 | + See https://docs.github.com/en/rest/actions/secrets for more details. |
| 85 | + GitHub never reveals the secret value through its API, it is only accessible |
| 86 | + from within actions. Therefore, this object represents the secret's metadata |
| 87 | + but not its actual value. |
| 88 | +
|
| 89 | + .. attribute:: name |
| 90 | +
|
| 91 | + The name of the secret |
| 92 | +
|
| 93 | + .. attribute:: created_at |
| 94 | +
|
| 95 | + The timestamp of when the secret was created |
| 96 | +
|
| 97 | + .. attribute:: updated_at |
| 98 | +
|
| 99 | + The timestamp of when the secret was last updated |
| 100 | + """ |
| 101 | + |
| 102 | + class_name = "SharedOrganizationSecret" |
| 103 | + |
| 104 | + |
| 105 | +class OrganizationSecret(_Secret): |
| 106 | + """An object representing am organization secret for GitHub Actions. |
| 107 | +
|
| 108 | + See https://docs.github.com/en/rest/actions/secrets for more details. |
| 109 | + GitHub never reveals the secret value through its API, it is only accessible |
| 110 | + from within actions. Therefore, this object represents the secret's metadata |
| 111 | + but not its actual value. |
| 112 | +
|
| 113 | + .. attribute:: name |
| 114 | +
|
| 115 | + The name of the secret |
| 116 | +
|
| 117 | + .. attribute:: created_at |
| 118 | +
|
| 119 | + The timestamp of when the secret was created |
| 120 | +
|
| 121 | + .. attribute:: updated_at |
| 122 | +
|
| 123 | + The timestamp of when the secret was last updated |
| 124 | + """ |
| 125 | + |
| 126 | + class_name = "OrganizationSecret" |
| 127 | + |
| 128 | + def _update_attributes(self, secret): |
| 129 | + super()._update_attributes(secret) |
| 130 | + self.visibility = secret["visibility"] |
| 131 | + if self.visibility == "selected": |
| 132 | + self._selected_repos_url = secret["selected_repositories_url"] |
| 133 | + |
| 134 | + def selected_repositories(self, number=-1, etag=""): |
| 135 | + """Iterates over all repositories this secret is visible to. |
| 136 | +
|
| 137 | + :param int number: |
| 138 | + (optional), number of repositories to return. |
| 139 | + Default: -1 returns all selected repositories. |
| 140 | + :param str etag: |
| 141 | + (optional), ETag from a previous request to the same endpoint |
| 142 | + :returns: |
| 143 | + Generator of selected repositories or None if the visibility of this |
| 144 | + secret is not set to 'selected'. |
| 145 | + :rtype: |
| 146 | + :class:`~github3.repos.ShortRepository` |
| 147 | + """ |
| 148 | + from .. import repos |
| 149 | + |
| 150 | + if self.visibility != "selected": |
| 151 | + return None |
| 152 | + |
| 153 | + return self._iter( |
| 154 | + int(number), |
| 155 | + self._selected_repos_url, |
| 156 | + repos.ShortRepository, |
| 157 | + etag=etag, |
| 158 | + list_key="repositories", |
| 159 | + ) |
| 160 | + |
| 161 | + def set_selected_repositories(self, repository_ids: list[int]): |
| 162 | + """Sets the selected repositories this secret is visible to. |
| 163 | +
|
| 164 | + :param list[int] repository_ids: |
| 165 | + A list of repository IDs which this secret should be visible to. |
| 166 | + :returns: |
| 167 | + A boolean indicating whether the update was successful. |
| 168 | + :rtype: |
| 169 | + bool |
| 170 | + """ |
| 171 | + if self.visibility != "selected": |
| 172 | + raise ValueError( |
| 173 | + """cannot set a list of selected repositories when visibility |
| 174 | + is not 'selected'""" |
| 175 | + ) |
| 176 | + |
| 177 | + data = {"selected_repository_ids": repository_ids} |
| 178 | + |
| 179 | + return self._boolean( |
| 180 | + self._put(self._selected_repos_url, json=data), 204, 404 |
| 181 | + ) |
| 182 | + |
| 183 | + def add_selected_repository(self, repository_id: int): |
| 184 | + """Adds a repository to the list of repositories this secret is |
| 185 | + visible to. |
| 186 | +
|
| 187 | + :param int repository_id: |
| 188 | + The IDs of a repository this secret should be visible to. |
| 189 | + :raises: |
| 190 | + A ValueError if the visibility of this secret is not 'selected'. |
| 191 | + :returns: |
| 192 | + A boolean indicating if the repository was successfully added to |
| 193 | + the visible list. |
| 194 | + :rtype: |
| 195 | + bool |
| 196 | + """ |
| 197 | + if self.visibility != "selected": |
| 198 | + raise ValueError( |
| 199 | + "cannot add a repository when visibility is not 'selected'" |
| 200 | + ) |
| 201 | + |
| 202 | + url = "/".join([self._selected_repos_url, str(repository_id)]) |
| 203 | + return self._boolean(self._put(url), 204, 409) |
| 204 | + |
| 205 | + def delete_selected_repository(self, repository_id: int): |
| 206 | + """Deletes a repository from the list of repositories this secret is |
| 207 | + visible to. |
| 208 | +
|
| 209 | + :param int repository_id: |
| 210 | + The IDs of the repository this secret should no longer be |
| 211 | + visible to. |
| 212 | + :raises: |
| 213 | + A ValueError if the visibility of this secret is not 'selected'. |
| 214 | + :returns: |
| 215 | + A boolean indicating if the repository was successfully removed |
| 216 | + from the visible list. |
| 217 | + :rtype: |
| 218 | + bool |
| 219 | + """ |
| 220 | + if self.visibility != "selected": |
| 221 | + raise ValueError( |
| 222 | + "cannot delete a repository when visibility is not 'selected'" |
| 223 | + ) |
| 224 | + |
| 225 | + url = "/".join([self._selected_repos_url, str(repository_id)]) |
| 226 | + return self._boolean(self._delete(url), 204, 409) |
0 commit comments