Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2247 linkedin update metrics from posts in notion content calendar update to notion not working #2248

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 141 additions & 22 deletions GitHub/GitHub_Connect_from_Naas_Chat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"tags": []
},
"source": [
"**Description:** This notebook integrates your GitHub token into the Naas secret manager under the variable 'GITHUB_TOKEN' from Naas Chat. By doing so, you can conveniently utilize this secret across all templates within the Awesome Notebooks catalog."
"**Description:** This notebook integrates your GitHub token into the Naas secret manager under the variable 'GITHUB_TOKEN' from Naas Chat and connect to your GitHub organization."
]
},
{
Expand Down Expand Up @@ -105,15 +105,24 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 1,
"id": "cc038b10-2679-42bc-909e-09a298339df4",
"metadata": {
"execution": {
"iopub.execute_input": "2023-10-03T16:35:13.322269Z",
"iopub.status.busy": "2023-10-03T16:35:13.321816Z",
"iopub.status.idle": "2023-10-03T16:35:26.658817Z",
"shell.execute_reply": "2023-10-03T16:35:26.658088Z",
"shell.execute_reply.started": "2023-10-03T16:35:13.322185Z"
},
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"import naas"
"import naas\n",
"import requests\n",
"import os"
]
},
{
Expand All @@ -126,14 +135,22 @@
"source": [
"### Setup variables\n",
"- `github_token`: GitHub token to be add as secret.\n",
"- `github_url`: GitHub organization html URL.\n",
"- `body`: This variable stores the body to be send by the webhook."
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 2,
"id": "8d86b8fb-2576-426d-a305-50043c24c23e",
"metadata": {
"execution": {
"iopub.execute_input": "2023-10-03T16:35:26.664479Z",
"iopub.status.busy": "2023-10-03T16:35:26.662561Z",
"iopub.status.idle": "2023-10-03T16:35:26.673515Z",
"shell.execute_reply": "2023-10-03T16:35:26.672958Z",
"shell.execute_reply.started": "2023-10-03T16:35:26.664443Z"
},
"papermill": {},
"tags": [
"parameters"
Expand All @@ -142,6 +159,7 @@
"outputs": [],
"source": [
"github_token = None\n",
"github_url = \"https://github.com/jupyter-naas/\"\n",
"body = {}"
]
},
Expand Down Expand Up @@ -170,9 +188,16 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 3,
"id": "42d27d66-3076-4fc6-a5dc-c1533bd47bd0",
"metadata": {
"execution": {
"iopub.execute_input": "2023-10-03T16:35:26.675708Z",
"iopub.status.busy": "2023-10-03T16:35:26.675471Z",
"iopub.status.idle": "2023-10-03T16:35:26.771503Z",
"shell.execute_reply": "2023-10-03T16:35:26.770935Z",
"shell.execute_reply.started": "2023-10-03T16:35:26.675686Z"
},
"papermill": {},
"tags": [
"parameters"
Expand All @@ -182,7 +207,8 @@
"source": [
"# Parameters\n",
"if len(body) > 0:\n",
" github_token = body.get(\"github_token\") "
" github_token = body.get(\"github_token\")\n",
" github_url = body.get(\"github_url\")"
]
},
{
Expand All @@ -204,15 +230,21 @@
"tags": []
},
"source": [
"### Add or Update Secret"
"### Connect to GitHub"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "fc6771cb-a9d8-431e-acd9-ebf19f26f362",
"execution_count": 8,
"id": "e235794a-6907-4f0e-ab2b-5aa6668a8d3b",
"metadata": {
"papermill": {},
"execution": {
"iopub.execute_input": "2023-10-03T16:36:50.322904Z",
"iopub.status.busy": "2023-10-03T16:36:50.322667Z",
"iopub.status.idle": "2023-10-03T16:36:50.843416Z",
"shell.execute_reply": "2023-10-03T16:36:50.842826Z",
"shell.execute_reply.started": "2023-10-03T16:36:50.322881Z"
},
"tags": []
},
"outputs": [],
Expand All @@ -221,21 +253,69 @@
" secret_name,\n",
" secret_value\n",
"):\n",
" # Init\n",
" value = naas.secret.get(secret_name)\n",
" \n",
" # Add secret\n",
" if secret_value and value != secret_value:\n",
" value = secret_value\n",
" return value\n",
"\n",
"def get_github_org(\n",
" token,\n",
" html_url\n",
"):\n",
" # Init\n",
" data = {}\n",
" base_url = \"https://api.github.com/orgs/\"\n",
" \n",
" # Split URL\n",
" org = html_url.split(\"https://github.com/\")[-1].split(\"/\")[0]\n",
" \n",
" # Create URL\n",
" url = os.path.join(base_url, org)\n",
" \n",
" # Set the request headers\n",
" headers = {\n",
" \"Authorization\": f\"token {token}\",\n",
" \"Content-Type\": \"application/json\"\n",
" }\n",
" \n",
" # Send the POST request\n",
" res = requests.get(url, headers=headers)\n",
" if res.status_code == 200:\n",
" data = res.json()\n",
" return data\n",
"\n",
"def connect_github(\n",
" secret_name,\n",
" secret_value,\n",
" github_url,\n",
"):\n",
" # Init\n",
" status = \"ok\"\n",
" try:\n",
" if not secret_value:\n",
" message = f\"🔐❌ No secret added.\"\n",
" elif naas.secret.get(secret_name) != secret_value:\n",
" naas.secret.add(secret_name, secret_value)\n",
" message = f\"🔐✅ Secret '{secret_name}' added.\"\n",
" # Add GitHub token to naas secret\n",
" token = add_or_update_secret(secret_name, secret_value)\n",
" \n",
" # Connect to GitHub org\n",
" data = get_github_org(token, github_url)\n",
" if len(data):\n",
" org_name = data.get(\"login\")\n",
" description = data.get(\"description\")\n",
" public_repos = data.get(\"public_repos\")\n",
" total_private_repos = data.get(\"total_private_repos\")\n",
" github_org = add_or_update_secret(\"GITHUB_ORG\", org_name)\n",
" message = f\"Connexion to GitHub successfull. Organization: {org_name}, Description: {description}, Public repos: {public_repos}, Private repos: {total_private_repos}\"\n",
" else:\n",
" message = f\"🔐 Secret '{secret_name}' already exists.\"\n",
" status = \"ko\"\n",
" message = \"Impossible to retrieve data from your organization please retry to connect!\"\n",
" except Exception as e:\n",
" status = \"ko\"\n",
" message = e\n",
" message = f\"Template error: {e}\"\n",
" return status, message\n",
" \n",
"status, message = add_or_update_secret(\"GITHUB_TOKEN\", github_token)"
"\n",
"status, message = connect_github(\"GITHUB_TOKEN\", github_token, github_url)"
]
},
{
Expand Down Expand Up @@ -270,13 +350,52 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 9,
"id": "7432dbf6-f47b-4f6c-8b56-4ad5b215e657",
"metadata": {
"execution": {
"iopub.execute_input": "2023-10-03T16:36:54.067012Z",
"iopub.status.busy": "2023-10-03T16:36:54.066786Z",
"iopub.status.idle": "2023-10-03T16:36:54.074323Z",
"shell.execute_reply": "2023-10-03T16:36:54.073636Z",
"shell.execute_reply.started": "2023-10-03T16:36:54.066990Z"
},
"papermill": {},
"tags": []
},
"outputs": [],
"outputs": [
{
"data": {
"text/markdown": [
"Response Set as JSON, preview below: "
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/json": {
"message": "Connexion to GitHub successfull. Organization: jupyter-naas, Description: ⚡️The universal open source data platform, based on @jupyter, Public repos: 45, Private repos: 52",
"status": "ok"
},
"text/plain": [
"<IPython.core.display.JSON object>"
]
},
"metadata": {
"application/json": {
"expanded": false,
"naas_api": true,
"root": "root"
}
},
"output_type": "display_data"
}
],
"source": [
"naas.webhook.respond_json(\n",
" {\n",
Expand Down Expand Up @@ -336,4 +455,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
5 changes: 3 additions & 2 deletions GitHub/GitHub_Create_Issue_from_Naas_Chat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@
"outputs": [],
"source": [
"import naas\n",
"import requests"
"import requests\n",
"import json"
]
},
{
Expand Down Expand Up @@ -393,4 +394,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
24 changes: 10 additions & 14 deletions GitHub/GitHub_Create_plugin_with_commands.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"**Mandatory**\n",
"- `name`: The name of the plugin.\n",
"- `system_prompt`: The system prompt for the plugin.\n",
"- `input_dir`: The path where the webhooks should be stored. By default, you can access all templates in your Naas Lab in \"/home/ftp/__templates__/\"\n",
"\n",
"**Optional**\n",
"- `body`: This variable stores the body to be send by the webhook.\n",
Expand All @@ -151,6 +152,7 @@
"# Mandatory\n",
"name = \"GitHub Agent\"\n",
"system_prompt = f\"You are a GitHub assistant. Start presenting yourself and tell the user the commands they could use: [COMMANDS]\"\n",
"input_dir = \"/home/ftp/__templates__\"\n",
"\n",
"# Optional\n",
"body = {}\n",
Expand Down Expand Up @@ -192,8 +194,8 @@
},
"outputs": [],
"source": [
"webhook_url1 = naas.webhook.add(\"GitHub_Connect_from_Naas_Chat.ipynb\", params={\"inline\": True})\n",
"webhook_url2 = naas.webhook.add(\"GitHub_Create_Issue_from_Naas_Chat.ipynb\", params={\"inline\": True})"
"webhook_url1 = naas.webhook.add(f\"{input_dir}/GitHub/GitHub_Connect_from_Naas_Chat.ipynb\", params={\"inline\": True})\n",
"webhook_url2 = naas.webhook.add(f\"{input_dir}/GitHub/GitHub_Create_Issue_from_Naas_Chat.ipynb\", params={\"inline\": True})"
]
},
{
Expand Down Expand Up @@ -230,6 +232,11 @@
" \"description\": \"GitHub token to be add as secret\",\n",
" \"default\": \"\"\n",
" },\n",
" \"github_url\": {\n",
" \"type\": \"str\",\n",
" \"description\": \"GitHub organization html URL\",\n",
" \"default\": \"\"\n",
" },\n",
" }\n",
" }\n",
" },\n",
Expand Down Expand Up @@ -373,17 +380,6 @@
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7614c1e5-ac31-41cd-a9f9-f087b9115187",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down Expand Up @@ -424,4 +420,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
Loading
Loading