Skip to content

Commit 615d7ef

Browse files
feat: docs agent api integration (#95)
1 parent 813f222 commit 615d7ef

File tree

10 files changed

+707
-14
lines changed

10 files changed

+707
-14
lines changed

.env.sample

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ LOGIN_USER=username
33
LOGIN_PASSWORD=strongpassword
44
DEFAULT_GITHUB_ORG=Git-Commit-Show
55
ONE_CLA_PER_ORG=true
6+
API_POST_GITHUB_COMMENT=http://localhost:3000/api/comment #Put your webhook proxy for PR/issue comment here
7+
DOCS_AGENT_API_URL=docs_agent_api_base_url
8+
DOCS_AGENT_API_KEY=docs_agent_api_key_here
9+
DOCS_AGENT_API_REVIEW_URL=#full url to review endpoint of docs agent e.g. http://localhost:3001/review, overrides `DOCS_AGENT_API_URL` base url config
10+
DOCS_AGENT_API_PRIORITIZE_URL=#full url
11+
DOCS_AGENT_API_EDIT_URL=
12+
DOCS_AGENT_API_LINK_URL=
13+
DOCS_AGENT_API_AUDIT_URL=
14+
DOCS_REPOS= #repos separated by comma
615
SLACK_DEFAULT_MESSAGE_CHANNEL_WEBHOOK_URL=https://hooks.slack.com/services/T05487DUMMY/B59DUMMY1U/htdsEdsdf7CNeDUMMY
716
GITHUB_BOT_USERS=dependabot[bot],devops-github-rudderstack
817
GITHUB_ORG_MEMBERS=

README.md

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,23 @@ A Node.js server for GitHub app to assist external contributors and save maintai
77
- [x] When an external contributor (not the internal team) raises a PR, post a comment to sign CLA and label PR `Pending CLA`
88
- [x] On signing CLA, remove `Pending CLA` label from all the PRs of that user. Never ask that user to sign the CLA on any of our repo in future
99
- [x] On `rudder-transformer` PR merge, post a comment to raise PR in `integrations-config`
10-
- [ ] On `integrations-config` PR merge, psot a comment to join Slack's product-releases channel to get notified when that integration goes live
10+
- [ ] On `integrations-config` PR merge, post a comment to join Slack's product-releases channel to get notified when that integration goes live
1111
- [ ] On `integrations-config` PR merge, post a comment to raise PR in `rudder-docs`
1212
- [x] List of open PRs by external contributors
13+
- [x] Notify on Slack when `product review` label is added to a PR
14+
- [ ] Analyze merged PRs and suggest next actions
15+
- [x] Analyze docs pages using AI on PR labelled with `docs review`
16+
17+
## Features
18+
19+
### Next Actions Feature
20+
21+
The Next Actions feature automatically analyzes merged pull requests from external contributors and suggests next actions based on the code changes. Here's how it works:
22+
23+
1. **Triggers**: Listens to `pull_request.closed` events and checks if the PR was merged
24+
2. **Analysis**: Extracts production code changes (excludes test files)
25+
3. **External API**: Sends changes to services such as DocsAgent
26+
4. **Comments**: Posts the API response as a comment on the PR
1327

1428
## Requirements
1529

@@ -26,7 +40,7 @@ A Node.js server for GitHub app to assist external contributors and save maintai
2640
2. Create a `.env` file similar to `.env.example` and set actual values. If you are using GitHub Enterprise Server, also include a `ENTERPRISE_HOSTNAME` variable and set the value to the name of your GitHub Enterprise Server instance.
2741
3. Install dependencies with `npm install`.
2842
4. Start the server with `npm run server`.
29-
5. Ensure your server is reachable from the internet.
43+
5. Ensure your server is reachable from the internet. This is necessary for GitHub to send webhook events to your local server.
3044
- If you're using `smee`, run `smee -u <smee_url> -t http://localhost:3000/api/webhook`.
3145
6. Ensure your GitHub App includes at least one repository on its installations.
3246

@@ -35,15 +49,33 @@ A Node.js server for GitHub app to assist external contributors and save maintai
3549
### Using `Docker`
3650

3751
1. [Register a GitHub app](https://docs.github.com/en/apps/creating-github-apps/registering-a-github-app/registering-a-github-app) for your GitHub organization. Make sure to activate the webhook with webhook url `https://YOUR_WEBSITE/api/webhook` in your app with a secret. Enable Permissions & Events as you may need, at minimum pull_request and issue related events should be enabled.
38-
2. Install your GitHub app in all the repos where you need this app.
52+
2. Install your GitHub app in all the repos where you need this app.
3953
3. Clone this repo OR download the [`build/docker-compose.yml`](./build/docker-compose.yml) to install via dockerhub image
40-
4. Update `docker-compose.yml` environment variables with the details received from the step 2
41-
> To convert GitHub App's private key to base64, use this command - `openssl base64 -in /path/to/original-private-key.pem -out ./base64EncodedKey.txt -A`
42-
5. Run `docker-componse build` to build the service
43-
6. Run `docker-compose up` to create and start the container
44-
7. Test by visiting `http://localhost:3000` OR whatever `WEBSITE_ADDRESS` environment variable you've configured
54+
4. Update the `docker-compose.yml` file with the environment variables obtained from step 2. Make sure to replace placeholder values with your actual GitHub App details.
55+
5. To convert GitHub App's private key to base64, use this command:
56+
```
57+
openssl base64 -in /path/to/original-private-key.pem -out ./base64EncodedKey.txt -A
58+
```
59+
6. Run `docker-compose build` to build the service
60+
7. Run `docker-compose up` to create and start the container
61+
8. Test by visiting `http://localhost:3000` OR whatever `WEBSITE_ADDRESS` environment variable you've configured
62+
63+
## Advanced Features Setup
64+
65+
### Docs Agent Setup
66+
67+
To set up the Docs Agent feature:
68+
69+
1. Locate your Docs Agent API project. This is a separate service that analyzes documentation and provides suggestions.
70+
2. In the Docs Agent API project's environment configuration, add the following URL to the `ALLOWED_WEBHOOK_URLS` variable:
71+
```
72+
https://your-github-app-host.com/api/comment
73+
```
74+
Replace `your-github-app-host.com` with the actual hostname where your GitHub App is deployed.
75+
76+
This setup allows the Docs Agent to send webhook requests to your GitHub App.
4577

46-
## Usage
78+
## How It Works
4779

4880
With your server running, you can now create a pull request on any repository that
4981
your app can access. GitHub will emit a `pull_request.opened` event and will deliver
@@ -67,4 +99,4 @@ etc.
6799
## References
68100

69101
- [Docs - octokit.rest.* methods](https://github.com/octokit/plugin-rest-endpoint-methods.js/tree/main/docs)
70-
- [Docs - GitHub API](https://docs.github.com/en/rest)
102+
- [Docs - GitHub API](https://docs.github.com/en/rest)

app.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
isMessageAfterMergeRequired,
1414
getWebsiteAddress,
1515
} from "./src/helpers.js";
16+
import DocsAgent from "./src/services/DocsAgent.js";
1617

1718
try {
1819
const packageJson = await import("./package.json", {
@@ -112,6 +113,49 @@ GitHub.app.webhooks.on("pull_request.labeled", async ({ octokit, payload }) => {
112113
const message = `:mag: <${pull_request.html_url}|#${pull_request.number}: ${pull_request.title}> by ${pull_request.user?.login}`;
113114
await Slack.sendMessage(message);
114115
}
116+
if(label.name?.toLowerCase() === "docs review") {
117+
console.log("Processing docs review for this PR");
118+
try {
119+
const DOCS_REPOS = process.env.DOCS_REPOS?.split(",")?.map((item) => item?.trim()) || [];
120+
if(DOCS_REPOS?.length > 0 && !DOCS_REPOS.includes(repository.name)) {
121+
throw new Error("Docs agent review is not available for this repository");
122+
}
123+
if(!DocsAgent.isConfigured()) {
124+
throw new Error("Docs agent service is not configured");
125+
}
126+
console.log("Going to analyze the docs pages in this PR");
127+
// Get PR changes
128+
const prChanges = await GitHub.getPRChanges(
129+
repository.owner.login,
130+
repository.name,
131+
pull_request.number
132+
);
133+
const docsFiles = prChanges.files.filter((file) => file.filename.endsWith(".md"));
134+
console.log(`Found ${docsFiles.length} docs files being changed`);
135+
if(docsFiles.length === 0) {
136+
throw new Error("No docs files being changed in this PR");
137+
}
138+
for(const file of docsFiles) {
139+
const content = file.content;
140+
// Convert relative file path to full remote github file path using PR head commit SHA https://raw.githubusercontent.com/gitcommitshow/rudder-github-app/e14433e76d74dc680b8cf9102d39f31970e8b794/.codesandbox/tasks.json
141+
const relativePath = file.filename;
142+
const fullPath = `https://raw.githubusercontent.com/${repository.owner.login}/${repository.name}/${prChanges.headCommit}/${relativePath}`;
143+
const webhookUrl = process.env.API_POST_GITHUB_COMMENT || (getWebsiteAddress() + "/api/comment");//TODO: add this url to `ALLOWED_WEBHOOK_URLS` env of docs-agent project
144+
DocsAgent.reviewDocs(content, fullPath, {
145+
webhookUrl: webhookUrl,
146+
webhookMetadata: {
147+
issue_number: pull_request.number,
148+
repo: repository.name,
149+
owner: repository.owner.login,
150+
},
151+
});
152+
console.log(`Successfully started docs review for ${fullPath}, results will be handled by webhook: ${webhookUrl}`);
153+
}
154+
console.log(`Successfully started all necessary docs reviews for PR ${repository.name} #${pull_request.number}`);
155+
} catch (error) {
156+
console.error(error);
157+
}
158+
}
115159
} catch (error) {
116160
if (error.response) {
117161
console.error(
@@ -216,6 +260,9 @@ const server = http
216260
case "POST /api/webhook":
217261
githubWebhookRequestHandler(req, res);
218262
break;
263+
case "POST /api/comment":
264+
routes.addCommentToGitHubIssueOrPR(req, res);
265+
break;
219266
case "GET /":
220267
routes.home(req, res);
221268
break;

0 commit comments

Comments
 (0)