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

feat apilinks.json generator #153

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

feat apilinks.json generator #153

wants to merge 1 commit into from

Conversation

flakey5
Copy link
Member

@flakey5 flakey5 commented Nov 29, 2024

Closes #152

Opening this as a draft currently to get feedback on the approach since it's a bit non-trivial relative to the other generators. The apilinks.json file maps things exported by modules to their source locations on Github.

Example:

{
  "SomeClass.prototype.var": "github.com/nodejs/node/tree/<hash>/lib/file.js#L100"
}

This means we need to parse the module's javascript source in addition to its markdown.

What the current approach does is doing:

  • Adds another loading & parsing step for the js source files
    • acorn is used for parsing the source files
    • This is dependent on the Markdown source parsing since it uses the source_link metadata in the docs
  • Exposes the parsed js ast to other generators by adding the ast-js generator
  • api-links generator is based off of the ast-js result

With the current approach, the generator is almost there. Some todos remain though:

  • I think some exported functions aren't making it
  • Git methods are kinda broken
  • Some cleanup & docs
  • Add tests (when we agree on an approach)

Closes #152

Signed-off-by: flakey5 <[email protected]>
@flakey5 flakey5 requested a review from a team as a code owner November 29, 2024 07:59
@flakey5 flakey5 marked this pull request as draft November 29, 2024 07:59
*/
export function getGitRepository(directory) {
try {
const trackingRemote = execSync(`cd ${directory} && git remote`);

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This string concatenation which depends on
library input
is later used in a
shell command
.

Copilot Autofix AI 1 day ago

To fix the problem, we should avoid using execSync with unsanitized input. Instead, we can use execFileSync from the child_process module, which allows us to pass arguments as an array, avoiding shell interpretation. This change ensures that the input is treated as a literal string and not as a part of the shell command.

  1. Replace execSync with execFileSync in the getGitRepository and getGitTag functions.
  2. Modify the commands to use execFileSync with arguments passed as an array.
Suggested changeset 1
src/utils/git.mjs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/utils/git.mjs b/src/utils/git.mjs
--- a/src/utils/git.mjs
+++ b/src/utils/git.mjs
@@ -2,3 +2,3 @@
 
-import { execSync } from 'child_process';
+import { execFileSync } from 'child_process';
 
@@ -14,6 +14,4 @@
   try {
-    const trackingRemote = execSync(`cd ${directory} && git remote`);
-    const remoteUrl = execSync(
-      `cd ${directory} && git remote get-url ${trackingRemote}`
-    );
+    const trackingRemote = execFileSync('git', ['-C', directory, 'remote']).toString().trim();
+    const remoteUrl = execFileSync('git', ['-C', directory, 'remote', 'get-url', trackingRemote]).toString().trim();
 
@@ -40,7 +38,5 @@
     const hash =
-      execSync(`cd ${directory} && git log -1 --pretty=%H`) || 'main';
+      execFileSync('git', ['-C', directory, 'log', '-1', '--pretty=%H']).toString().trim() || 'main';
     const tag =
-      execSync(`cd ${directory} && git describe --contains ${hash}`).split(
-        '\n'
-      )[0] || hash;
+      execFileSync('git', ['-C', directory, 'describe', '--contains', hash]).toString().split('\n')[0] || hash;
 
EOF
@@ -2,3 +2,3 @@

import { execSync } from 'child_process';
import { execFileSync } from 'child_process';

@@ -14,6 +14,4 @@
try {
const trackingRemote = execSync(`cd ${directory} && git remote`);
const remoteUrl = execSync(
`cd ${directory} && git remote get-url ${trackingRemote}`
);
const trackingRemote = execFileSync('git', ['-C', directory, 'remote']).toString().trim();
const remoteUrl = execFileSync('git', ['-C', directory, 'remote', 'get-url', trackingRemote]).toString().trim();

@@ -40,7 +38,5 @@
const hash =
execSync(`cd ${directory} && git log -1 --pretty=%H`) || 'main';
execFileSync('git', ['-C', directory, 'log', '-1', '--pretty=%H']).toString().trim() || 'main';
const tag =
execSync(`cd ${directory} && git describe --contains ${hash}`).split(
'\n'
)[0] || hash;
execFileSync('git', ['-C', directory, 'describe', '--contains', hash]).toString().split('\n')[0] || hash;

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
try {
const trackingRemote = execSync(`cd ${directory} && git remote`);
const remoteUrl = execSync(
`cd ${directory} && git remote get-url ${trackingRemote}`

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This string concatenation which depends on
library input
is later used in a
shell command
.
export function getGitTag(directory) {
try {
const hash =
execSync(`cd ${directory} && git log -1 --pretty=%H`) || 'main';

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This string concatenation which depends on
library input
is later used in a
shell command
.
const hash =
execSync(`cd ${directory} && git log -1 --pretty=%H`) || 'main';
const tag =
execSync(`cd ${directory} && git describe --contains ${hash}`).split(

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This string concatenation which depends on
library input
is later used in a
shell command
.
@@ -0,0 +1,52 @@
'use strict';

import { execSync } from 'child_process';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { execSync } from 'child_process';
import { execSync } from 'node:child_process';

@@ -1,10 +1,12 @@
// @ts-check
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// @ts-check

use actualy didn't use type-checking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

urgent: feat: add apilinks.json generator
2 participants