-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentrypoint.py
More file actions
193 lines (153 loc) · 5.54 KB
/
Copy pathentrypoint.py
File metadata and controls
193 lines (153 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import json
import os
import requests
from datetime import datetime
from github import Github
def read_json(filepath):
with open(filepath, "r") as f:
return json.load(f)
def get_actions_input(input_name):
return os.getenv("INPUT_{}".format(input_name).upper())
def load_template(filename):
template_path = os.path.join(".github/workflows", filename)
with open(template_path, "r") as f:
return f.read()
def get_changed_files_dump(sha, repo):
changed_files = []
commit = repo.get_commit(sha)
# Loop through the changed files in the commit
for file in commit.files:
if (
file.filename.startswith("docs/") or "marketing/src/blog" in file.filename
): # Filter by docs/** folder
changed_files.append(file.filename)
return json.dumps({"changed_files": changed_files})
def find_pr_by_sha(repo, sha):
"""Find a PR by commit SHA.
Parameters:
- repo: The repository object from PyGithub.
- sha: The commit SHA to search for in PRs.
Returns:
- The pull request object if found, otherwise None.
"""
prs = repo.get_pulls(state="all")
for pr in prs:
if pr.merge_commit_sha == sha:
return pr
return None
def main():
graphql_endpoint = "https://api.management.inkeep.com/graphql"
# Your GraphQL mutation
create_source_sync_job_mutation = """
mutation CreateSourceSyncJob($sourceId: ID!, $type: SourceSyncJobType!) {
createSourceSyncJob(input: {sourceId: $sourceId, type: $type origin: GITHUB_ACTION}) {
job{
id
}
success
}
}
"""
create_indexing_job_mutation = """
mutation CreateIndexingJob($indexId: ID! $sourceSyncJobId: ID! $statusMessage: String! $startTime: DateTime!){
createIndexingJob(input:{
indexId: $indexId
sourceSyncJobId: $sourceSyncJobId
job: {
startTime: $startTime
status: QUEUED
statusMessage: $statusMessage
}
}){
success
}
}
"""
get_source_query = """
query source($sourceId: ID!) {
source(sourceId: $sourceId) {
displayName
indexes {
id
}
}
}
"""
gh = Github(os.getenv("GITHUB_TOKEN"))
event = read_json(os.getenv("GITHUB_EVENT_PATH"))
repo = gh.get_repo(event["repository"]["full_name"])
sha = event.get("after") # The commit SHA from the push event
files_changed_str = get_changed_files_dump(sha, repo)
source_id = get_actions_input("sourceId")
api_key = get_actions_input("apiKey")
source_id = get_actions_input("sourceId")
api_key = get_actions_input("apiKey")
# Prepare the JSON payload
create_source_sync_job_payload = {
"query": create_source_sync_job_mutation,
"variables": {"sourceId": source_id, "type": "INCREMENTAL"},
}
get_source_payload = {
"query": get_source_query,
"variables": {"sourceId": source_id},
}
# Headers including the Authorization token
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
# Make the GraphQL request
create_source_sync_job_muation_result = requests.post(
graphql_endpoint, headers=headers, json=create_source_sync_job_payload
)
create_source_sync_job_mutation_result = (
create_source_sync_job_muation_result.json()
)
source_sync_job_id = create_source_sync_job_mutation_result["data"][
"createSourceSyncJob"
]["job"]["id"]
query_response = requests.post(
graphql_endpoint, headers=headers, json=get_source_payload
)
display_name = query_response.json()["data"]["source"]["displayName"]
indexes = query_response.json()["data"]["source"]["indexes"]
if not indexes:
raise Exception("No indexes found.")
print(files_changed_str)
index_id = query_response.json()["data"]["source"]["indexes"][0]["id"]
create_indexing_job_payload = {
"query": create_indexing_job_mutation,
"variables": {
"indexId": index_id,
"statusMessage": files_changed_str,
"startTime": datetime.utcnow().replace(microsecond=0).isoformat() + "Z",
"sourceSyncJobId": source_sync_job_id,
},
}
create_indexing_job_response = requests.post(
graphql_endpoint, headers=headers, json=create_indexing_job_payload
)
print(
"Indexing Job Created: ",
create_indexing_job_response.json()["data"]["createIndexingJob"]["success"],
)
if (
"data" in create_source_sync_job_mutation_result.keys()
and "createSourceSyncJob"
in create_source_sync_job_mutation_result["data"].keys()
and create_source_sync_job_mutation_result["data"]["createSourceSyncJob"][
"success"
]
is True
):
pr = find_pr_by_sha(repo, sha)
if pr is None:
print(f"No PR found for commit {sha}.")
return
new_comment = f""":mag_right::speech_balloon: [Inkeep](https://inkeep.com) AI search and chat service is syncing content for source '{display_name}'"""
# Check for duplicated comment
old_comments = [c.body for c in pr.get_issue_comments()]
if new_comment in old_comments:
print("This pull request already has a duplicated comment.")
return
# Add the comment
pr.create_issue_comment(new_comment)
if __name__ == "__main__":
main()