Skip to content

Commit

Permalink
merge with action
Browse files Browse the repository at this point in the history
  • Loading branch information
miles-kt-inkeep committed Feb 14, 2024
1 parent 46946f3 commit 72067b4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 20 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM python:3.7.5-slim

RUN pip install PyGithub
RUN pip install requests
COPY entrypoint.py /entrypoint.py

ENTRYPOINT python /entrypoint.py
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ branding:
icon: 'git-pull-request'
color: 'purple'
description: Comment on pull requests
inputs:
api-key:
description: 'API key to authenticate the request'
required: true
source-id:
description: 'The source id for which the source sync job needs to be created.'
required: true
source-name:
description: 'The source name for which the source sync job needs to be created.'
required: true
runs:
using: 'docker'
image: 'Dockerfile'
88 changes: 68 additions & 20 deletions entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import requests

from github import Github

Expand Down Expand Up @@ -37,26 +38,73 @@ def find_pr_by_sha(repo, sha):


def main():
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

pr = find_pr_by_sha(repo, sha)
if pr is None:
print(f"No PR found for commit {sha}")
return

new_comment = "Inkeep sync has been started."

# 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)

graphql_endpoint = "https://api.management.inkeep.com/graphql"

# Your GraphQL mutation
graphql_mutation = """
mutation CreateSourceSyncJob($sourceId: ID!, $type: SourceSyncJobType!) {
createSourceSyncJob(input: {sourceId: $sourceId, type: $type}) {
success
}
}
"""

graphql_query = """
query source($sourceId: ID!) {
source(sourceId: $sourceId) {
displayName
}
}
"""

source_id = get_actions_input("source-id")
api_key = get_actions_input("api-key")

# Prepare the JSON payload
json_payload = {
"query": graphql_mutation,
"variables": {"sourceId": source_id, "type": "INCREMENTAL"},
}

# query_payload = {
# "query": graphql_query,
# "variables": {"sourceId": source_id},
# }

# Headers including the Authorization token
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}

# Make the GraphQL request
mutation_response = requests.post(
graphql_endpoint, headers=headers, json=json_payload
)

# query_response = requests.post(
# graphql_endpoint, headers=headers, json=query_payload
# )

if mutation_response.status_code == 200:
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

pr = find_pr_by_sha(repo, sha)
if pr is None:
print(f"No PR found for commit {sha}")
return

new_comment = f"![Inkeep Logo](https://storage.googleapis.com/public_inkeep_assetts/inkeep_logo_16h.png) [Inkeep](https://inkeep.com) AI search and chat service is syncing content for source '{source_id}'"

# 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__":
Expand Down

0 comments on commit 72067b4

Please sign in to comment.