Skip to content

Commit f238fe0

Browse files
author
ytymchenko9
committed
feat: Add basic func
1 parent ce8bdc7 commit f238fe0

11 files changed

Lines changed: 187 additions & 0 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.github

Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM python:3.9-slim-buster
2+
3+
LABEL "com.github.actions.name"="Comment on Pr with ecs exec"
4+
LABEL "com.github.actions.description"="This is a github action to comment pr"
5+
LABEL "com.github.actions.icon"="airplay"
6+
LABEL "com.github.actions.color"="green"
7+
8+
WORKDIR /
9+
10+
COPY . .
11+
12+
RUN pip3 install --upgrade pip \
13+
&& pip install -r requirements.txt \
14+
&& chmod +x ./entrypoint.sh
15+
16+
ENTRYPOINT ["/entrypoint.sh"]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Comment on Pr with ecs exec

action.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Comment on Pr with ecs exec
2+
author: Yulianna Tymchenko
3+
4+
branding:
5+
color: green
6+
icon: airplay
7+
8+
inputs:
9+
org:
10+
description: Name of the github organisation
11+
repo:
12+
description: Name of the github repo
13+
pr:
14+
description: Github pull request number
15+
cluster:
16+
description: Name of the commits author
17+
task:
18+
description: Name of the docker image to deploy
19+
20+
21+
outputs:
22+
success:
23+
description: Whether action was successful or not
24+
25+
runs:
26+
using: docker
27+
image: Dockerfile
28+
args:
29+
- ${{ inputs.org }}
30+
- ${{ inputs.repo }}
31+
- ${{ inputs.pr }}
32+
- ${{ inputs.cluster }}
33+
- ${{ inputs.task }}

entrypoint.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh -l
2+
set -e
3+
4+
python /src/main.py --org "$1" --repo "$2" --pr "$3" --cluster "$4" --task "$5"
5+
6+
BUILD_RESULT=$?
7+
if [ $BUILD_RESULT -eq 0 ]; then
8+
echo ::set-output name=success::true
9+
exit 0
10+
else
11+
echo ::set-output name=success::false
12+
exit 1
13+
fi
14+

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
boto3==1.34.64
2+
jmespath==1.0.1
3+
requests==2.31.0

src/__init__.py

Whitespace-only changes.

src/ecs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import boto3
2+
import jmespath
3+
4+
5+
def get_ecs_task_description(cluster, task_id):
6+
client = boto3.client('ecs')
7+
return client.describe_tasks(cluster=cluster, tasks=[task_id])
8+
9+
10+
def get_ecs_containers(task_desc):
11+
containers = jmespath.search('tasks[*].containers[*].name[]', task_desc)
12+
return filter(lambda x: x not in ["coralogix", "nginx"], containers)

src/git.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import json
2+
import os
3+
4+
import requests
5+
6+
from utils import setup_custom_logger
7+
8+
LOGGER = setup_custom_logger("root")
9+
10+
def create_comment(org, repo, pr_number, comment):
11+
token = os.environ["GITHUB_TOKEN"]
12+
headers = {
13+
"Authorization": token,
14+
"Accept": "application/vnd.github+json",
15+
"X-GitHub-Api-Version": "2022-11-28"
16+
}
17+
18+
body = json.dumps({"body": comment})
19+
20+
url = f"https://api.github.com/repos/{org}/{repo}/issues/{pr_number}/comments"
21+
res = requests.post(url, body, headers=headers)
22+
LOGGER.info(res.json())
23+
return res.json()

src/main.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import argparse
2+
3+
from ecs import get_ecs_task_description, get_ecs_containers
4+
from git import create_comment
5+
from utils import create_body
6+
7+
8+
def put_comment(org, repo, pr, cluster, task):
9+
task_desc = get_ecs_task_description(cluster, task)
10+
containers = get_ecs_containers(task_desc)
11+
12+
body = create_body(cluster, task, containers)
13+
14+
create_comment(org, repo, pr, body)
15+
16+
17+
def main(command_line=None):
18+
parser = argparse.ArgumentParser(
19+
description=""
20+
)
21+
22+
parser.add_argument("-o", "--org", required=True)
23+
parser.add_argument("-r", "--repo", required=True)
24+
parser.add_argument("-p", "--pr", required=True)
25+
parser.add_argument("-c", "--cluster", required=True)
26+
parser.add_argument("-t", "--task", required=True)
27+
28+
args = parser.parse_args(command_line)
29+
30+
put_comment(
31+
args.org,
32+
args.repo,
33+
args.pr,
34+
args.cluster,
35+
args.task
36+
)
37+
38+
39+
if __name__ == "__main__":
40+
main()

0 commit comments

Comments
 (0)