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

feat: Add basic func #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.github
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.9-slim-buster

LABEL "com.github.actions.name"="Comment on Pr with ecs exec"
LABEL "com.github.actions.description"="This is a github action to comment pr"
LABEL "com.github.actions.icon"="airplay"
LABEL "com.github.actions.color"="green"

WORKDIR /

COPY . .

RUN pip3 install --upgrade pip \
&& pip install -r requirements.txt \
&& chmod +x ./entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Comment on Pr with ecs exec
33 changes: 33 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Comment on Pr with ecs exec
author: Yulianna Tymchenko

branding:
color: green
icon: airplay

inputs:
org:
description: Name of the github organisation
repo:
description: Name of the github repo
pr:
description: Github pull request number
cluster:
description: Name of the commits author
task:
description: Name of the docker image to deploy


outputs:
success:
description: Whether action was successful or not

runs:
using: docker
image: Dockerfile
args:
- ${{ inputs.org }}
- ${{ inputs.repo }}
- ${{ inputs.pr }}
- ${{ inputs.cluster }}
- ${{ inputs.task }}
14 changes: 14 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh -l
set -e

python /src/main.py --org "$1" --repo "$2" --pr "$3" --cluster "$4" --task "$5"

BUILD_RESULT=$?
if [ $BUILD_RESULT -eq 0 ]; then
echo ::set-output name=success::true
exit 0
else
echo ::set-output name=success::false
exit 1
fi

3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
boto3==1.34.64
jmespath==1.0.1
requests==2.31.0
Empty file added src/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions src/ecs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import boto3
import jmespath


def get_ecs_task_description(cluster, task_id):
client = boto3.client('ecs')
return client.describe_tasks(cluster=cluster, tasks=[task_id])


def get_ecs_containers(task_desc):
containers = jmespath.search('tasks[*].containers[*].name[]', task_desc)
return filter(lambda x: x not in ["coralogix", "nginx"], containers)
23 changes: 23 additions & 0 deletions src/git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import json
import os

import requests

from utils import setup_custom_logger

LOGGER = setup_custom_logger("root")

def create_comment(org, repo, pr_number, comment):
token = os.environ["GITHUB_TOKEN"]
headers = {
"Authorization": "Bearer " + token,
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28"
}

body = json.dumps({"body": comment})

url = f"https://api.github.com/repos/{org}/{repo}/issues/{pr_number}/comments"
res = requests.post(url, body, headers=headers)
LOGGER.info(res.json())
return res.json()
40 changes: 40 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import argparse

from ecs import get_ecs_task_description, get_ecs_containers
from git import create_comment
from utils import create_body


def put_comment(org, repo, pr, cluster, task):
task_desc = get_ecs_task_description(cluster, task)
containers = get_ecs_containers(task_desc)

body = create_body(cluster, task, containers)

create_comment(org, repo, pr, body)


def main(command_line=None):
parser = argparse.ArgumentParser(
description=""
)

parser.add_argument("-o", "--org", required=True)
parser.add_argument("-r", "--repo", required=True)
parser.add_argument("-p", "--pr", required=True)
parser.add_argument("-c", "--cluster", required=True)
parser.add_argument("-t", "--task", required=True)

args = parser.parse_args(command_line)

put_comment(
args.org,
args.repo,
args.pr,
args.cluster,
args.task
)


if __name__ == "__main__":
main()
44 changes: 44 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging


LOGGER = logging.getLogger("root")
ENV_FILE = ".env_vars"


def create_container_body(cluster, task_id, container):
return f""" To connect to __{container}__ container please copy the following command into your terminal:

aws ecs execute-command \\
--region us-east-1 \\
--cluster {cluster} \\
--task {task_id} \\
--container {container} \\
--command "/bin/bash" \\
--profile $AWS_PROFILE \\
--interactive \n\n"""


def create_body(cluster, task_id, containers):
comment = ""
for container in containers:
comment += create_container_body(cluster, task_id, container)
return comment


def export_env(key, value):
env_file = open(ENV_FILE, "a")
env = "{}={}".format(key, value)
print(env, file=env_file)
env_file.close()


def setup_custom_logger(name):
formatter = logging.Formatter(fmt="[%(levelname)s] %(message)s")

handler = logging.StreamHandler()
handler.setFormatter(formatter)

logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
logger.addHandler(handler)
return logger