Skip to content

Commit

Permalink
WIP, tox and black
Browse files Browse the repository at this point in the history
Signed-off-by: Or Shoval <oshoval@redhat.com>
  • Loading branch information
oshoval committed Nov 4, 2021
1 parent f0c0c43 commit 42e1c1a
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 91 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__
secret.txt
secret.txt
.tox/
11 changes: 7 additions & 4 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import os
import sys


def get_envvar(name, default_value=None):
value = os.getenv(name, default_value)
if value == None:
if value is None:
print(f"Error: cant find {name}, exiting")
sys.exit(1)
return value



def main():
print("common self test")
get_envvar('JIRA_TOKEN')
get_envvar("JIRA_TOKEN")


if __name__ == '__main__':
if __name__ == "__main__":
main()
51 changes: 28 additions & 23 deletions create_ticket.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python3

import os
import json
import sys
import argparse

Expand All @@ -16,31 +14,36 @@
# how many tickets can be opened on each cycle
flood_protection_limit = 3


def process_issue(jira, github, issue, dryrun):
html_url = issue["html_url"]

if "pull" in html_url:
return False

if github.check_labels(issue) == False:
if not github.check_labels(issue):
return False

issue_id = issue["number"]
if jira.issue_exists(github.repo, issue_id) == False:
if github.check_time(issue, max_delta_weeks) == False:
if not jira.issue_exists(github.repo, issue_id):
if not github.check_time(issue, max_delta_weeks):
return False

if dryrun == False:
created_issue = jira.create_issue(github.repo, issue_id, issue["title"], html_url)
print(f'Created issue {jira.server}/browse/{created_issue} for {html_url}')
if not dryrun:
created_issue = jira.create_issue(
github.repo, issue_id, issue["title"], html_url
)
issue_url = f"{jira.server}/browse/{created_issue}"
print(f"Created issue {issue_url} for {html_url}")
else:
print(f'Dry Run Created issue for {html_url}')
print(f"Dry Run Created issue for {html_url}")
return True
else:
print("Issue for", html_url, "already exists")

return False


def loop(jira, github, dryrun):
issues_created = 0

Expand All @@ -51,27 +54,28 @@ def loop(jira, github, dryrun):

for issue in issues:
res = process_issue(jira, github, issue, dryrun)
if res == True:
if res:
issues_created += 1
if issues_created == flood_protection_limit:
print("Flood protection limit reached, exiting")
sys.exit(0)


def main():
server = get_envvar('JIRA_SERVER')
username = get_envvar('JIRA_USERNAME')
token = get_envvar('JIRA_TOKEN')
project = get_envvar('JIRA_PROJECT')
project_id = get_envvar('JIRA_PROJECT_ID')
jira_component = get_envvar('JIRA_COMPONENT')

github_token = get_envvar('GITHUB_TOKEN')
github_owner = get_envvar('GITHUB_OWNER')
github_repo = get_envvar('GITHUB_REPO')
github_label = get_envvar('GITHUB_LABEL')
server = get_envvar("JIRA_SERVER")
username = get_envvar("JIRA_USERNAME")
token = get_envvar("JIRA_TOKEN")
project = get_envvar("JIRA_PROJECT")
project_id = get_envvar("JIRA_PROJECT_ID")
jira_component = get_envvar("JIRA_COMPONENT")

github_token = get_envvar("GITHUB_TOKEN")
github_owner = get_envvar("GITHUB_OWNER")
github_repo = get_envvar("GITHUB_REPO")
github_label = get_envvar("GITHUB_LABEL")

parser = argparse.ArgumentParser()
parser.add_argument('--dryrun', default=False, action='store_true')
parser.add_argument("--dryrun", default=False, action="store_true")
args = parser.parse_args()

if args.dryrun:
Expand All @@ -82,5 +86,6 @@ def main():

loop(jira, github, args.dryrun)

if __name__ == '__main__':

if __name__ == "__main__":
main()
26 changes: 14 additions & 12 deletions githublib.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env python3

import os
import sys
import requests
import json
import time
Expand All @@ -11,18 +9,17 @@

SECONDS_PER_WEEK = 604800


class Github:
def __init__(self, token, owner, repo, expected_label):
self.owner = owner
self.repo = repo
self.expected_label = expected_label
self.query_url = f"https://api.github.com/repos/{owner}/{repo}/issues"
self.headers = {'Authorization': f'token {token}'}
self.headers = {"Authorization": f"token {token}"}

def get_issues(self, page):
params = {
"state": "open", "page" : page, "per_page": "100"
}
params = {"state": "open", "page": page, "per_page": "100"}

r = requests.get(self.query_url, headers=self.headers, params=params)
data = r.json()
Expand All @@ -38,19 +35,24 @@ def check_labels(self, issue):

def check_time(self, issue, max_delta):
epoch_time_now = int(time.time())
epoch = int(datetime.strptime(issue["created_at"], "%Y-%m-%dT%H:%M:%SZ").timestamp())
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
epoch = int(
datetime.strptime(issue["created_at"], TIME_FORMAT).timestamp()
)
return (epoch_time_now - epoch) < (max_delta * SECONDS_PER_WEEK)


def main():
print("githublib self test")

github_token = get_envvar('GITHUB_TOKEN')
github_owner = get_envvar('GITHUB_OWNER')
github_repo = get_envvar('GITHUB_REPO')
github_token = get_envvar("GITHUB_TOKEN")
github_owner = get_envvar("GITHUB_OWNER")
github_repo = get_envvar("GITHUB_REPO")

github = Github(github_token, github_owner, github_repo, "")
data = github.get_issues(1)
print(json.dumps(data[0], sort_keys=True, indent=4))

if __name__ == '__main__':

if __name__ == "__main__":
main()
55 changes: 30 additions & 25 deletions jiralib.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
#!/usr/bin/env python3

import os
import sys

from jira import JIRA

from common import get_envvar


class Jira:
def __init__(self, server, username, token, project, project_id, component):
def __init__(self,
server,
username,
token,
project,
project_id,
component):
self.project = project
self.project_id = project_id
self.server = server
self.component = component
jiraOptions = {'server': self.server}
self.jira = JIRA(options = jiraOptions, basic_auth = (username, token))

jiraOptions = {"server": self.server}
self.jira = JIRA(options=jiraOptions, basic_auth=(username, token))

def issue_exists(self, repo, id):
query = f'project={self.project} AND text ~ "GITHUB:{repo}-{id}"'
issues = self.jira.search_issues(query)
return len(issues) != 0

def create_issue(self, repo, issue_id, title, body):
issue_dict=dict()
issue_dict['project']=dict({'id':self.project_id})
issue_dict['summary']=f"[GITHUB:{repo}-{issue_id}] {title}"
issue_dict['description']=body
issue_dict['issuetype']=dict({'name':'Task'})
if self.component != '':
issue_dict['components']=[dict({'name':self.component})]
issue_dict = dict()
issue_dict["project"] = dict({"id": self.project_id})
issue_dict["summary"] = f"[GITHUB:{repo}-{issue_id}] {title}"
issue_dict["description"] = body
issue_dict["issuetype"] = dict({"name": "Task"})
if self.component != "":
issue_dict["components"] = [dict({"name": self.component})]

issue = self.jira.create_issue(issue_dict)
return issue



def main():
print("jiralib self test")

server = get_envvar('JIRA_SERVER')
username = get_envvar('JIRA_USERNAME')
token = get_envvar('JIRA_TOKEN')
project = get_envvar('JIRA_PROJECT')
project_id = get_envvar('JIRA_PROJECT_ID')
jira_component = get_envvar('JIRA_COMPONENT', "")
server = get_envvar("JIRA_SERVER")
username = get_envvar("JIRA_USERNAME")
token = get_envvar("JIRA_TOKEN")
project = get_envvar("JIRA_PROJECT")
project_id = get_envvar("JIRA_PROJECT_ID")
jira_component = get_envvar("JIRA_COMPONENT", "")

print(jira_component)
jira = Jira(server, username, token, project, project_id, jira_component)
res = jira.issue_exists('kubevirt', '123')
res = jira.issue_exists("kubevirt", "123")
if not res:
raise AssertionError()
print('OK')
print("OK")


if __name__ == '__main__':
if __name__ == "__main__":
main()
27 changes: 14 additions & 13 deletions manifests/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
apiVersion: batch/v1
kind: CronJob
metadata:
Expand All @@ -11,19 +12,19 @@ spec:
template:
spec:
containers:
- name: github
image: quay.io/oshoval/github:latest
command:
- /bin/sh
- -ce
- |
source /app/secret.txt
git clone https://github.com/oshoval/github2jira.git
./github2jira/create_ticket.py
volumeMounts:
- name: configs
mountPath: /app/secret.txt
subPath: secret.txt
- name: github
image: quay.io/oshoval/github:latest
command:
- /bin/sh
- -ce
- |
source /app/secret.txt
git clone https://github.com/oshoval/github2jira.git
./github2jira/create_ticket.py
volumeMounts:
- name: configs
mountPath: /app/secret.txt
subPath: secret.txt
restartPolicy: Never
volumes:
- name: configs
Expand Down
27 changes: 14 additions & 13 deletions manifests/gitpod.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
---
apiVersion: v1
kind: Pod
metadata:
name: github
namespace: default
spec:
containers:
- image: quay.io/oshoval/github:latest
name: github
command:
- /bin/bash
- -c
- sleep infinity
volumeMounts:
- name: configs
mountPath: /app/secret.txt
subPath: secret.txt
- image: quay.io/oshoval/github:latest
name: github
command:
- /bin/bash
- -c
- sleep infinity
volumeMounts:
- name: configs
mountPath: /app/secret.txt
subPath: secret.txt
volumes:
- name: configs
configMap:
name: git-token
- name: configs
configMap:
name: git-token
Loading

0 comments on commit 42e1c1a

Please sign in to comment.