Skip to content

Commit dec1813

Browse files
committed
add new search_issues graphql query
1 parent b9cd68e commit dec1813

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

dvcurator/github.py

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ def check_repo(key=None, repo=None):
5454

5555
# Search if tickets already exist in the repo for this project
5656
# True = tickets exist, False = they don't exist
57-
def search_existing(project_name, key=None, repo=None):
57+
def search_existing(project_name, token=None, repo=None):
5858
"""
5959
Check if a project has existing github tickets
6060
6161
:param project_name: Project name to check for existing tickets
6262
:type project_name: String
63-
:param key: Github API key, or None for public repository
64-
:type key: String or None
63+
:param token: Github token key, or None for public repository
64+
:type token: String or None
6565
:param repo: Github repository path (e.g. QualitativeDataRepository/dvcurator-python), or None for default
6666
:type repo: String or None
6767
:return: Whether or not there are any existing tickets with the specified project name
@@ -70,17 +70,19 @@ def search_existing(project_name, key=None, repo=None):
7070
import json, requests, dvcurator.hosts
7171

7272
repo = dvcurator.hosts.curation_repo if not repo else repo
73-
project_url = github_api + "/repos/" + repo + "/issues"
73+
project_url = dvcurator.hosts.github_api + "/repos/" + repo + "/issues"
7474

7575
# Ideally we would use the project API endpoint here.
7676
# We can't, because it requires an OAuth token for all calls
7777
# even on public repositories. So we go directly to the issues
78-
if (not key):
78+
if (not token):
7979
projects = requests.get(project_url + "?per_page=100")
8080
else:
81-
key = {'Authorization': "token " + key.strip()}
82-
projects = requests.get(project_url + "?per_page=100", headers=key)
83-
81+
token = {'Authorization': "token " + token.strip()}
82+
projects = requests.get(project_url + "?per_page=100", headers=token)
83+
84+
projects.raise_for_status()
85+
8486
# Take the first three words ("lastname - first-of-title") to search
8587
project_name = ' '.join(project_name.split()[:3])
8688

@@ -94,10 +96,52 @@ def search_existing(project_name, key=None, repo=None):
9496
# Return false if nothing was found
9597
return False
9698

99+
def search_issues(search_string, repo, token):
100+
url = "https://api.github.com/graphql"
101+
repo_owner, repo_name = repo.split('/')
102+
103+
headers = {
104+
"Authorization": f"Bearer {token}",
105+
"Content-Type": "application/json"
106+
}
107+
query = """
108+
query($repoOwner: String!, $repoName: String!, $searchString: String!) {
109+
repository(owner: $repoOwner, name: $repoName) {
110+
issues(first: 100, filterBy: {states: OPEN}, query: $searchString) {
111+
edges {
112+
node {
113+
title
114+
url
115+
}
116+
}
117+
}
118+
}
119+
}
120+
"""
121+
122+
variables = {
123+
"repoOwner": repo_owner,
124+
"repoName": repo_name,
125+
"searchString": se
126+
}
127+
response = requests.post(url, headers=headers, json={"query": query, "variables": variables})
128+
response.raise_for_status()
129+
return response.json()
130+
97131
def create_project(project_name, token, repo=None):
98-
import requests, dvcurator.hosts
132+
import requests, dvcurator.hosts, dvcurator.dataverse
99133
repo = dvcurator.hosts.curation_repo if not repo else repo
100134

135+
if (search_existing(project_name, token, repo)):
136+
print("Project already exists")
137+
return
138+
139+
doi = dv['data']['latestVersion']['datasetPersistentId']
140+
link = dvcurator.hosts.qdr_doi_path + doi
141+
contact_info = 'Depositor: ' + dvcurator.dataverse.get_citation(dv)['depositor'] + '\n'
142+
contact_info += 'DV link: ' + link
143+
144+
101145
url = "https://api.github.com/repos/" + repo + "/dispatches"
102146
headers = {
103147
"Accept": "application/vnd.github.everest-preview+json",
@@ -107,7 +151,9 @@ def create_project(project_name, token, repo=None):
107151
data = {
108152
"event_type": "trigger-event",
109153
"client_payload": {
110-
"title": project_name
154+
"title": project_name,
155+
"desc": link,
156+
"readme": contact_info
111157
}
112158
}
113159

0 commit comments

Comments
 (0)