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

V2.2.0 #47

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
bc666a7
3 new actions
MarlonHeiber May 23, 2022
91b6566
Update CHANGES.md
MarlonHeiber May 23, 2022
67620f0
more two actions
MarlonHeiber May 25, 2022
3cd58ed
Merge branch 'repositoryActions' of github.com:MarlonHeiber/stackstor…
MarlonHeiber May 25, 2022
394f93f
create/get/delete branches :)
pimguilherme Jun 21, 2022
929a018
documentation
pimguilherme Jun 21, 2022
a55d1ee
fixes
pimguilherme Jun 22, 2022
a376c7a
adding missing repository actions
pimguilherme Jun 24, 2022
966853e
pack
MarlonHeiber Jul 12, 2022
59abe03
pack 2.2.0
MarlonHeiber Jul 12, 2022
69cf6d4
Merge branch 'v2.1.2' into repositoryActions
MarlonHeiber Jul 18, 2022
223c48f
Merge pull request #1 from MarlonHeiber/repositoryActions
MarlonHeiber Jul 18, 2022
a4c4c7b
Merge pull request #2 from MarlonHeiber/create-branch-action
MarlonHeiber Jul 18, 2022
348721e
Merge branch 'master' into v2.2.0
MarlonHeiber Jul 18, 2022
a6092a7
changeloh
MarlonHeiber Jul 18, 2022
7788189
Squashed 'lint-configs/' content from commit 4696bad
MarlonHeiber Jul 18, 2022
f35fe21
Merge commit '7788189fe660bd4b8e6eea0e85d69d29efc32c1d' as 'lint-conf…
MarlonHeiber Jul 18, 2022
c5b0295
lint_flake8
MarlonHeiber Jul 18, 2022
629bb61
lint
MarlonHeiber Jul 18, 2022
be2b512
variable not used
MarlonHeiber Jul 19, 2022
343458f
pylint test
MarlonHeiber Jul 19, 2022
5bc19a1
pylint
MarlonHeiber Jul 19, 2022
26fa095
update readme
MarlonHeiber Jul 21, 2022
9aa47de
adding missing actions :)
pimguilherme Aug 2, 2022
b4a100a
Merge pull request #5 from MarlonHeiber/fix/add-missing-actions
pimguilherme Aug 2, 2022
a9410ee
adding missing actions :)
pimguilherme Aug 2, 2022
df509a2
Merge pull request #6 from MarlonHeiber/fix/add-missing-action
pimguilherme Aug 2, 2022
9476365
minor merge adjustments
pimguilherme Aug 2, 2022
8626229
Merge branch 'master' into v2.2.0
pimguilherme Aug 2, 2022
5830938
adding environment creation functionality
pimguilherme Aug 11, 2022
0a8f5ea
removing unnecessary lint-config changes
pimguilherme Nov 10, 2022
50ba270
Update CHANGES.md
pimguilherme Nov 10, 2022
980ec06
adjusting add/update environment documentation
pimguilherme Nov 10, 2022
96030fd
adjusting typos
pimguilherme Nov 10, 2022
38e6238
fixing typo
pimguilherme Nov 10, 2022
5e12799
making github_type usage/doc uniform
pimguilherme Nov 10, 2022
734e098
Merge branch 'v2.2.0' of github.com:MarlonHeiber/stackstorm-github in…
pimguilherme Nov 10, 2022
6e253b5
fixing typos :)
pimguilherme Nov 10, 2022
190b097
Merge branch 'master' into v2.2.0
pimguilherme Nov 11, 2022
c22ec01
adding missing action to doc
pimguilherme Nov 11, 2022
bf07568
fixing tpos
pimguilherme Nov 11, 2022
8591b30
typo
pimguilherme Nov 11, 2022
eb49fa1
description
pimguilherme Nov 11, 2022
f95d241
adjusting text
pimguilherme Nov 11, 2022
0b91d7e
adjusting doc
pimguilherme Nov 11, 2022
84e5c1d
adjusting action
pimguilherme Nov 11, 2022
a43734a
Merge branch 'v2.2.0' of github.com:MarlonHeiber/stackstorm-github in…
pimguilherme Nov 11, 2022
89fb9a6
typo
pimguilherme Nov 11, 2022
4187e34
adjusting docs
pimguilherme Nov 11, 2022
343e7f5
update changelog
pimguilherme Nov 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## 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.
* Add new ``github.create_organization_repository`` action which allows user to create an organization repository.
* Add new ``github.create_repository_authenticated_user`` action which allows user to create an user repository.
* Add new ``github.create_repository_from_template`` action which allows user to create a repository from template.
* Add new ``github.add_update_repository_environment`` action which allows user to create a repository deployment environment.
* Bug fix on ``github.store_oauth_token.`` to api save the token correctly so that it can be read later.
* Segure improvement on ``github.store_oauth_token.`` to encrypt de github token in web interface.
pimguilherme marked this conversation as resolved.
Show resolved Hide resolved
* Add new ``github.create_branch``, ``github.get_branch``, ``github.delete_branch`` actions which allows user to create/get/delete a branch.
* Add token to ``github.create_file``, ``github.create_pull``, ``github.update_file``.

## 2.1.1

* Bug fix (#43) where the sensor will throw an exception if no events are returned from the GitHub api.
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,27 @@ StackStorm webhook handler.
## Actions

* ``add_comment`` - Add comment to the provided issue / pull request.
* ``add_repository_collaborator`` - Add a collaborator to repository.
* ``add_status`` - Add commit status to the provided commit.
* ``add_update_repository_team`` - Add/Update a team to repository.
* ``create_branch`` - Create new branch.
* ``create_file`` - Create new file.
* ``create_issue`` - Create a new issue.
* ``create_repository_authenticated_user`` - Create an user repository.
* ``create_repository_from_template`` - Create a repository from template.
* ``create_organization_repository`` - Create an organization repository.
* ``create_pull`` - Create a new Pull Request.
* ``check_team_permissions_for_repository`` - Check if a team has access to repository.
* ``check_user_repository_collaborator`` - Check if an user is a collaborator's repository.
* ``delete_branch`` - Remove branch.
* ``delete_branch_protection`` - Remove branch protection settings.
* ``get_branch`` - Get branch.
* ``get_branch_protection`` - Get branch protection settings.
* ``get_contents`` - Get repository or file contents.
* ``get_issue`` - Retrieve information about a particular issue. Note: You
only need to specify authentication token in the config if you use this
action with a private repository.
* ``get_repository_collaborators`` - List the collaborators of repository.
* ``list_issues`` - List all the issues for a particular repo (includes pull
requests since pull requests are just a special type of issues).
* ``list_pulls`` - List all pull requests for a particular repo.
Expand Down
26 changes: 26 additions & 0 deletions actions/add_repository_collaborator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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
44 changes: 44 additions & 0 deletions actions/add_repository_collaborator.yaml
Original file line number Diff line number Diff line change
@@ -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.."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description: "The account owner of the repository. The name is not case sensitive.."
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"
60 changes: 60 additions & 0 deletions actions/add_update_repository_environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from lib.base import BaseGithubAction

__all__ = [
'AddUpdateRepositoryEnvironmentAction'
]


class AddUpdateRepositoryEnvironmentAction(BaseGithubAction):

def _get_team_id(self, enterprise, org, name):
self.logger.debug("Getting team ID for name [%s]", name)
response = self._request("GET",
f"/orgs/{org}/teams/{name}",
None,
self.token,
enterprise)
self.logger.debug("Found ID [%d] for name [%s]", response["id"], name)
return response["id"]

def run(self, api_user, environment,
owner, repo, github_type, reviewers, wait_timer, deployment_branch_policy):

enterprise = self._is_enterprise(github_type)

if api_user:
self.token = self._get_user_token(api_user, enterprise)

# Transforming team slug names in IDs
for reviewer in reviewers:
type = reviewer.get("type", None)
name = reviewer.get("name", None)
if type == "Team" and name:
del reviewer["name"]
reviewer["id"] = self._get_team_id(enterprise, owner, name)
elif type == "User" and name:
raise NotImplementedError("Providing reviewer of type user without ID is not implemented!")

payload = {
"wait_timer": int(wait_timer),
"reviewers": reviewers,
"deployment_branch_policy": deployment_branch_policy
}

self.logger.info(
"Adding/Updating environment [%s] with parameters [%s] for repo [%s/%s] with user [%s]",
environment, payload, owner, repo, api_user)

try:
response = self._request("PUT",
f"/repos/{owner}/{repo}/environments/{environment}",
payload,
self.token,
enterprise)
results = {'response': response}
return results
except Exception as e:
self.logger.error("Could not add/update environment, error: %s", repr(e))
return (False, "Could not add/update environment, error: %s" % repr(e))

return (False, "Could not add/update environment for unknown reason!")
73 changes: 73 additions & 0 deletions actions/add_update_repository_environment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
name: add_update_repository_environment
# https://docs.github.com/en/[email protected]/rest/deployments/environments
runner_type: python-script
pack: github
description: >
Add or update a repository environment.
Example:
st2 run github.add_update_repository_environment organization="organization" owner="owner" repo="reponame" reviewers="< array of reviewers >" api_user="token_name"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see a parameter organization in the yaml. It looks like in the python code, that owner is being passed as organisation to the get_team_id - so that parameters in the example and the yaml don't seem to match.
Also yaml says environment is required parameter with no default, yet its not used in the example.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! I have rewritten this and tested the call, it seems to be okay now :)

still pending to push the code though

enabled: true
entry_point: add_update_repository_environment.py
parameters:
# Repository parameters
owner:
type: "string"
description: "The account owner of the repository. The name is not case sensitive.."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description: "The account owner of the repository. The name is not case sensitive.."
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
# Authentication parameters
api_user:
type: "string"
description: "The API user"
default: "{{action_context.api_user|default(None)}}"
github_type:
type: "string"
description: "The type of github installation to target, if unset will use the configured default."
enum:
- "online"
- "enterprise"
default: "enterprise"
# Call-specific parameters :)
environment:
type: string
description: "The name of the environment"
required: true
wait_timer:
type: number
description: "The amount of time to delay a job after the job is initially triggered. The time (in minutes) must be an integer between 0 and 43,200 (30 days)."
required: false
default: 0
reviewers:
type: array
description: "The people or teams that may review jobs that reference the environment. You can list up to six users or teams as reviewers. The reviewers must have at least read access to the repository. Only one of the required reviewers needs to approve the job for it to proceed."
required: false
items:
type: object
properties:
type:
type: string
required: true
id:
type: number
required: false
name:
type: string
required: false
deployment_branch_policy:
type: object
description: "The type of deployment branch policy for this environment. To allow all branches to deploy, set to null."
required: false
default: null
properties:
protected_branches:
type: boolean
required: true
description: Whether only branches with branch protection rules can deploy to this environment. If protected_branches is true, custom_branch_policies must be false; if protected_branches is false, custom_branch_policies must be true.
custom_branch_policies:
type: boolean
required: true
description: Whether only branches that match the specified name patterns can deploy to this environment. If custom_branch_policies is true, protected_branches must be false; if custom_branch_policies is false, protected_branches must be true.
27 changes: 27 additions & 0 deletions actions/add_update_repository_team.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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
48 changes: 48 additions & 0 deletions actions/add_update_repository_team.yaml
Original file line number Diff line number Diff line change
@@ -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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example uses a parameter called organization, but the yaml states that the parameter is called org.

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: "pull"
35 changes: 35 additions & 0 deletions actions/check_team_permissions_for_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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:
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
Loading