From bc666a7805ae1d3045e89332d129ff2fa1170aa7 Mon Sep 17 00:00:00 2001 From: MarlonHeiber Date: Mon, 23 May 2022 10:26:35 -0300 Subject: [PATCH 1/5] 3 new actions --- CHANGES.md | 6 +++ actions/add_repository_collaborator.py | 29 ++++++++++++ actions/add_repository_collaborator.yaml | 44 ++++++++++++++++++ actions/check_user_repository_collaborator.py | 36 +++++++++++++++ .../check_user_repository_collaborator.yaml | 34 ++++++++++++++ actions/get_repository_collaborators.py | 31 +++++++++++++ actions/get_repository_collaborators.yaml | 46 +++++++++++++++++++ 7 files changed, 226 insertions(+) create mode 100644 actions/add_repository_collaborator.py create mode 100644 actions/add_repository_collaborator.yaml create mode 100644 actions/check_user_repository_collaborator.py create mode 100644 actions/check_user_repository_collaborator.yaml create mode 100644 actions/get_repository_collaborators.py create mode 100644 actions/get_repository_collaborators.yaml diff --git a/CHANGES.md b/CHANGES.md index 2bbbfad..f96b6c0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ # Changelog +## 2.1.2 + +* Add new ``github.add_repository_collaborator`` action which allows user to add a collaborator to repository. +* Add new ``github.check_user_repository_collaborator`` action which allows user to check if an user is a collaborator's repository. +* Add new ``github.get_repository_collaborators`` action which allows user to list the collaborators of repository. + ## 2.1.1 * Bug fix (#43) where the sensor will throw an exception if no events are returned from the GitHub api. diff --git a/actions/add_repository_collaborator.py b/actions/add_repository_collaborator.py new file mode 100644 index 0000000..e5174ad --- /dev/null +++ b/actions/add_repository_collaborator.py @@ -0,0 +1,29 @@ +import time +import datetime + + +from lib.base import BaseGithubAction + +__all__ = [ + 'AddRepositoryCollaboratorAction' +] + +class AddRepositoryCollaboratorAction(BaseGithubAction): + def run(self, api_user, owner, repo, username, github_type, permission ): + + enterprise = self._is_enterprise(github_type) + + if api_user: + self.token = self._get_user_token(api_user, enterprise) + + payload = { "permission": permission } + + response = self._request("PUT", + "/repos/{}/{}/collaborators/{}".format(owner,repo,username ), + payload, + self.token, + enterprise) + + results = {'response': response} + + return results diff --git a/actions/add_repository_collaborator.yaml b/actions/add_repository_collaborator.yaml new file mode 100644 index 0000000..76b6808 --- /dev/null +++ b/actions/add_repository_collaborator.yaml @@ -0,0 +1,44 @@ +--- +name: add_repository_collaborator +runner_type: python-script +pack: github +description: > + Add a repository collaborator. + Example: + st2 run github.add_repository_collaborator owner="organization" repo="reponame" username="collaborator" api_user="token_name" +enabled: true +entry_point: add_repository_collaborator.py +parameters: + api_user: + type: "string" + description: "The API user" + default: "{{action_context.api_user|default(None)}}" + owner: + type: "string" + description: "The account owner of the repository. The name is not case sensitive.." + required: true + repo: + type: "string" + description: "The name of the repository. The name is not case sensitive." + required: true + username: + type: "string" + description: "The handle for the GitHub user account." + required: true + github_type: + type: "string" + description: "The type of github installation to target, if unset will use the configured default." + enum: + - "online" + - "enterprise" + default: "enterprise" + permission: + type: "string" + description: "The permission to grant the collaborator. Only valid on organization-owned repositories. In addition to the enumerated values, you can also specify a custom repository role name, if the owning organization has defined any." + enum: + - "pull" + - "push" + - "admin" + - "maintain" + - "triage" + default: "push" \ No newline at end of file diff --git a/actions/check_user_repository_collaborator.py b/actions/check_user_repository_collaborator.py new file mode 100644 index 0000000..1cea121 --- /dev/null +++ b/actions/check_user_repository_collaborator.py @@ -0,0 +1,36 @@ +import time +import datetime +import json + +from lib.base import BaseGithubAction + +__all__ = [ + 'CheckIfUserIsRepositoryCollaborator' +] + +class CheckIfUserIsRepositoryCollaborator(BaseGithubAction): + def run(self, api_user, owner, repo, username, github_type ): + + enterprise = self._is_enterprise(github_type) + + if api_user: + self.token = self._get_user_token(api_user, enterprise) + + try: + response = self._request("GET", + "/repos/{}/{}/collaborators/{}".format(owner,repo,username ), + {}, + self.token, + enterprise) + results = {'response': "The user {} is a Collaborator".format(username)} + except OSError as err: + raise err + except ValueError as err: + raise err + except Exception as err: + if str(err).find("404"): + results = {'response': "is not a Collaborator or not found"} + else: + raise err + + return results diff --git a/actions/check_user_repository_collaborator.yaml b/actions/check_user_repository_collaborator.yaml new file mode 100644 index 0000000..7d14c81 --- /dev/null +++ b/actions/check_user_repository_collaborator.yaml @@ -0,0 +1,34 @@ +--- +name: check_user_repository_collaborator +runner_type: python-script +pack: github +description: > + Check if a user is a repository collaborator. + Example: + st2 run github.check_user_repository_collaborator owner="organization" repo="reponame" username="collaborator" api_user="token_name" +enabled: true +entry_point: check_user_repository_collaborator.py +parameters: + api_user: + type: "string" + description: "The API user" + default: "{{action_context.api_user|default(None)}}" + owner: + type: "string" + description: "The account owner of the repository. The name is not case sensitive." + required: true + repo: + type: "string" + description: "The name of the repository. The name is not case sensitive." + required: true + username: + type: "string" + description: "The handle for the GitHub user account." + required: true + github_type: + type: "string" + description: "The type of github installation to target, if unset will use the configured default." + enum: + - "online" + - "enterprise" + default: "enterprise" \ No newline at end of file diff --git a/actions/get_repository_collaborators.py b/actions/get_repository_collaborators.py new file mode 100644 index 0000000..65988d7 --- /dev/null +++ b/actions/get_repository_collaborators.py @@ -0,0 +1,31 @@ +import time +import datetime + + +from lib.base import BaseGithubAction + +__all__ = [ + 'GetRepositoryCollaborators' +] + +class GetRepositoryCollaborators(BaseGithubAction): + def run(self, api_user, owner, repo, affiliation, per_page, page, github_type ): + + enterprise = self._is_enterprise(github_type) + + if api_user: + self.token = self._get_user_token(api_user, enterprise) + + payload = { "affiliation": affiliation, + "per_page": per_page, + "page": page } + + response = self._request("GET", + "/repos/{}/{}/collaborators".format(owner,repo), + payload, + self.token, + enterprise) + + results = {'response': response} + + return results diff --git a/actions/get_repository_collaborators.yaml b/actions/get_repository_collaborators.yaml new file mode 100644 index 0000000..455b9a3 --- /dev/null +++ b/actions/get_repository_collaborators.yaml @@ -0,0 +1,46 @@ +--- +name: get_repository_collaborators +runner_type: python-script +pack: github +description: > + List repository collaborators. + Example: + st2 run github.get_repository_collaborators owner="organization" repo="reponame" api_user="token_name" +enabled: true +entry_point: get_repository_collaborators.py +parameters: + api_user: + type: "string" + description: "The API user" + default: "{{action_context.api_user|default(None)}}" + owner: + type: "string" + description: "The account owner of the repository. The name is not case sensitive." + required: true + repo: + type: "string" + description: "The name of the repository. The name is not case sensitive." + required: true + affiliation: + type: "string" + description: "Filter collaborators returned by their affiliation. outside means all outside collaborators of an organization-owned repository. direct means all collaborators with permissions to an organization-owned repository, regardless of organization membership status. all means all collaborators the authenticated user can see." + enum: + - "outside" + - "direct" + - "all" + default: "all" + per_page: + type: "integer" + description: "The number of results per page (max 100)." + default: 30 + page: + type: "integer" + description: "Page number of the results to fetch." + default: 1 + github_type: + type: "string" + description: "The type of github installation to target, if unset will use the configured default." + enum: + - "online" + - "enterprise" + default: "enterprise" \ No newline at end of file From 91b65669d1211ad73d4911c482770d3d24c642be Mon Sep 17 00:00:00 2001 From: Marlon Augusto Heiber <55984212+MarlonHeiber@users.noreply.github.com> Date: Mon, 23 May 2022 18:26:02 -0300 Subject: [PATCH 2/5] Update CHANGES.md Co-authored-by: Eugen C. <1533818+armab@users.noreply.github.com> --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index f96b6c0..2488d6a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,6 @@ # Changelog -## 2.1.2 +## 2.2.0 * Add new ``github.add_repository_collaborator`` action which allows user to add a collaborator to repository. * Add new ``github.check_user_repository_collaborator`` action which allows user to check if an user is a collaborator's repository. From 67620f03b99e2994b0668e7e783340066b1df9ec Mon Sep 17 00:00:00 2001 From: MarlonHeiber Date: Wed, 25 May 2022 09:37:50 -0300 Subject: [PATCH 3/5] more two actions --- CHANGES.md | 4 +- actions/add_update_repository_team.py | 29 +++++++++++ actions/add_update_repository_team.yaml | 48 +++++++++++++++++++ .../check_team_permissions_for_repository.py | 37 ++++++++++++++ ...check_team_permissions_for_repository.yaml | 38 +++++++++++++++ 5 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 actions/add_update_repository_team.py create mode 100644 actions/add_update_repository_team.yaml create mode 100644 actions/check_team_permissions_for_repository.py create mode 100644 actions/check_team_permissions_for_repository.yaml diff --git a/CHANGES.md b/CHANGES.md index f96b6c0..50427e6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,10 +1,12 @@ # Changelog -## 2.1.2 +## 2.2.0 * Add new ``github.add_repository_collaborator`` action which allows user to add a collaborator to repository. * Add new ``github.check_user_repository_collaborator`` action which allows user to check if an user is a collaborator's repository. * Add new ``github.get_repository_collaborators`` action which allows user to list the collaborators of repository. +* Add new ``github.add_update_repository_team`` action which allows user to add a team to repository. +* Add new ``github.check_team_permissions_for_repository`` action which allows user to check if a team has access to repository. ## 2.1.1 diff --git a/actions/add_update_repository_team.py b/actions/add_update_repository_team.py new file mode 100644 index 0000000..93f8e14 --- /dev/null +++ b/actions/add_update_repository_team.py @@ -0,0 +1,29 @@ +import time +import datetime + + +from lib.base import BaseGithubAction + +__all__ = [ + 'AddUpdateRepositoryTeamAction' +] + +class AddUpdateRepositoryTeamAction(BaseGithubAction): + def run(self, api_user, org, team_slug, owner, repo, github_type, permission ): + + enterprise = self._is_enterprise(github_type) + + if api_user: + self.token = self._get_user_token(api_user, enterprise) + + payload = { "permission": permission } + + response = self._request("PUT", + "/orgs/{}/teams/{}/repos/{}/{}".format(org,team_slug,owner,repo ), + payload, + self.token, + enterprise) + + results = {'response': response} + + return results diff --git a/actions/add_update_repository_team.yaml b/actions/add_update_repository_team.yaml new file mode 100644 index 0000000..fd5fef0 --- /dev/null +++ b/actions/add_update_repository_team.yaml @@ -0,0 +1,48 @@ +--- +name: add_update_repository_team +runner_type: python-script +pack: github +description: > + Add or update repository team. + Example: + st2 run github.add_update_repository_team organization="organization" owner="owner" repo="reponame" team_slug="team_id" api_user="token_name" +enabled: true +entry_point: add_update_repository_team.py +parameters: + api_user: + type: "string" + description: "The API user" + default: "{{action_context.api_user|default(None)}}" + org: + type: "string" + description: "The organization name. The name is not case sensitive." + required: true + team_slug: + type: "string" + description: "The slug of the team name." + required: true + owner: + type: "string" + description: "The account owner of the repository. The name is not case sensitive." + required: true + repo: + type: "string" + description: "The name of the repository. The name is not case sensitive." + required: true + github_type: + type: "string" + description: "The type of github installation to target, if unset will use the configured default." + enum: + - "online" + - "enterprise" + default: "enterprise" + permission: + type: "string" + description: "The permission to grant the team on this repository. In addition to the enumerated values, you can also specify a custom repository role name, if the owning organization has defined any. If no permission is specified, the team's permission attribute will be used to determine what permission to grant the team on this repository." + enum: + - "pull" + - "push" + - "admin" + - "maintain" + - "triage" + default: "push" \ No newline at end of file diff --git a/actions/check_team_permissions_for_repository.py b/actions/check_team_permissions_for_repository.py new file mode 100644 index 0000000..81fd33f --- /dev/null +++ b/actions/check_team_permissions_for_repository.py @@ -0,0 +1,37 @@ +import time +import datetime + + +from lib.base import BaseGithubAction + +__all__ = [ + 'CheckTeamPermissionsForRepository' +] + +class CheckTeamPermissionsForRepository(BaseGithubAction): + def run(self, api_user, org, team_slug, owner, repo, github_type ): + + enterprise = self._is_enterprise(github_type) + + if api_user: + self.token = self._get_user_token(api_user, enterprise) + + try: + response = self._request("GET", + "/orgs/{}/teams/{}/repos/{}/{}".format(org,team_slug,owner,repo ), + {}, + self.token, + enterprise) + + results = {'response': "The team {} has access to the repository {}".format(team_slug, repo)} + except OSError as err: + raise err + except ValueError as err: + raise err + except Exception as err: + if str(err).find("404"): + results = {'response': "The team don't have access to the repository or not found"} + else: + raise err + + return results diff --git a/actions/check_team_permissions_for_repository.yaml b/actions/check_team_permissions_for_repository.yaml new file mode 100644 index 0000000..7802865 --- /dev/null +++ b/actions/check_team_permissions_for_repository.yaml @@ -0,0 +1,38 @@ +--- +name: check_team_permissions_for_repository +runner_type: python-script +pack: github +description: > + Check team permissions for a repository. + Example: + st2 run github.check_team_permissions_for_repository organization="organization" owner="owner" repo="reponame" team_slug="team_id" api_user="token_name" +enabled: true +entry_point: check_team_permissions_for_repository.py +parameters: + api_user: + type: "string" + description: "The API user" + default: "{{action_context.api_user|default(None)}}" + org: + type: "string" + description: "The organization name. The name is not case sensitive." + required: true + team_slug: + type: "string" + description: "The slug of the team name." + required: true + owner: + type: "string" + description: "The account owner of the repository. The name is not case sensitive." + required: true + repo: + type: "string" + description: "The name of the repository. The name is not case sensitive." + required: true + github_type: + type: "string" + description: "The type of github installation to target, if unset will use the configured default." + enum: + - "online" + - "enterprise" + default: "enterprise" \ No newline at end of file From 966853ee47d70afd514603b834333ff49c6f1b65 Mon Sep 17 00:00:00 2001 From: MarlonHeiber Date: Tue, 12 Jul 2022 15:30:12 -0300 Subject: [PATCH 4/5] pack --- pack.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pack.yaml b/pack.yaml index 53013e4..6138ecd 100644 --- a/pack.yaml +++ b/pack.yaml @@ -8,7 +8,7 @@ keywords: - git - scm - serverless -version: 2.1.1 +version: 2.1.2 python_versions: - "3" author : StackStorm, Inc. From 59abe03862551adc12ea5f2865639630fdaf327a Mon Sep 17 00:00:00 2001 From: MarlonHeiber Date: Tue, 12 Jul 2022 15:31:53 -0300 Subject: [PATCH 5/5] pack 2.2.0 --- pack.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pack.yaml b/pack.yaml index 6138ecd..b991709 100644 --- a/pack.yaml +++ b/pack.yaml @@ -8,7 +8,7 @@ keywords: - git - scm - serverless -version: 2.1.2 +version: 2.2.0 python_versions: - "3" author : StackStorm, Inc.