Skip to content

Commit

Permalink
Merge pull request #29 from depends-on/js
Browse files Browse the repository at this point in the history
add javascript support
  • Loading branch information
fredericlepied authored Oct 19, 2023
2 parents f5fa69f + f50a46a commit c281c7e
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ For a Go lang change, the action adds `replace` directives for the dependencies

The action replaces entries in `requirements.txt` for a Python change with a `-e <local change>` or the equivalent for `pyproject.toml`.

### Javascript

The action replaces entries in `package.json` for Javascript change with `file:<local change>`.

### Container

The action auto-detects if a container is present and injects the changes in a compatible way if this is the case.
Expand Down Expand Up @@ -103,10 +107,10 @@ When the action is called with the `check-unmerged-pr: true` setting, stages 1 a
- [x] [stage 3: python poetry support](https://github.com/depends-on/depends-on-action/issues/18)
- [x] [stage 3: python subdir support](https://github.com/depends-on/depends-on-action/issues/19)
- [x] [stage 3: Container support](https://github.com/depends-on/depends-on-action/issues/17)
- [x] [stage 3: javascript support](https://github.com/depends-on/depends-on-action/issues/12)
- [ ] [stage 3: custom injection](https://github.com/depends-on/depends-on-action/issues/4)
- [ ] [stage 3: Github action support](https://github.com/depends-on/depends-on-action/issues/5)
- [ ] [stage 2: gerrit support for software-factory.io](https://github.com/depends-on/depends-on-action/issues/6)
- [ ] [stage 2: extract private PR](https://github.com/depends-on/depends-on-action/issues/7)
- [ ] [stage 3: ansible support](https://github.com/depends-on/depends-on-action/issues/9)
- [ ] [stage 3: rust support](https://github.com/depends-on/depends-on-action/issues/11)
- [ ] [stage 3: javascript support](https://github.com/depends-on/depends-on-action/issues/12)
2 changes: 1 addition & 1 deletion golang.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"golang specific code for stage 2."
"golang specific code for stage 3."

import os
import re
Expand Down
111 changes: 111 additions & 0 deletions javascript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"javascript specific code for stage 3."

import glob
import json
import os
import sys


def load_package_json(package_json_path):
"""Load package.json file."""
with open(package_json_path, "r", encoding="UTF-8") as package_json:
return json.load(package_json)


def local_dependencies(dirs):
"""Return a dictionary of local dependencies with the name as key
and the directory as value. It also looks for workspaces in package.json."""
deps = {}
for local_dir in dirs:
if "subdir" in dirs[local_dir]:
local_path = os.path.join(
dirs[local_dir]["path"], dirs[local_dir]["subdir"]
)
else:
local_path = dirs[local_dir]["path"]
package_json_path = os.path.join(local_path, "package.json")
if os.path.exists(package_json_path):
package = load_package_json(package_json_path)
deps[package["name"]] = dirs[local_dir]
if "workspaces" in package and "packages" in package["workspaces"]:
print("detected workspaces", file=sys.stderr)
for workspace_glob in package["workspaces"]["packages"]:
print(f"processing {workspace_glob}", file=sys.stderr)
for workspace_dir in glob.glob(
os.path.join(local_path, workspace_glob)
):
print(f"found subdir={workspace_dir}", file=sys.stderr)
workspace_path = os.path.join(local_path, workspace_dir)
print(
f"checking workspace {workspace_path} for package.json",
file=sys.stderr,
)
workspace_package_json_path = os.path.join(
workspace_path, "package.json"
)
if os.path.exists(workspace_package_json_path):
workspace_package = load_package_json(
workspace_package_json_path
)
print(
f"found package {workspace_package['name']}",
file=sys.stderr,
)
deps[workspace_package["name"]] = {
"path": workspace_path,
"subdir": workspace_dir[len(local_path) + 1 :],
"fork_url": dirs[local_dir]["fork_url"],
"branch": dirs[local_dir]["branch"],
}
return deps


def process_dependencies(dependencies, dirs, container_mode, package_json_path):
"""Process dependencies in package.json and replace local dependencies"""
local_deps = local_dependencies(dirs)
print(f"Found {len(local_deps)} local dependencies: {local_deps=}", file=sys.stderr)
count = 0
for dependency in dependencies:
if dependency in local_deps:
if container_mode:
info = local_deps[dependency]
repo = "git+" + info["fork_url"] + "#" + info["branch"]
print(
f"Replacing {dependency} with remote version from {repo} in package.json",
file=sys.stderr,
)
dependencies[dependency] = repo
else:
print(
f"Replacing {dependency} with local version from {local_deps[dependency]['path']} in package.json",
file=sys.stderr,
)
dependencies[dependency] = "file:" + local_deps[dependency]["path"]
count += 1
return count


def process_javascript(main_dir, dirs, container_mode):
"""Use changes from PR in package.json if present"""
package_json_path = os.path.join(main_dir, "package.json")
if not os.path.exists(package_json_path):
return False
print(
"Detected package.json file, checking for local dependencies", file=sys.stderr
)
package = load_package_json(package_json_path)
if "dependencies" not in package:
return False
dependencies = package["dependencies"]
count = process_dependencies(dependencies, dirs, container_mode, package_json_path)
if "devDependencies" in package:
dependencies = package["devDependencies"]
count += process_dependencies(
dependencies, dirs, container_mode, package_json_path
)
with open(package_json_path, "w", encoding="UTF-8") as package_json:
json.dump(package, package_json, indent=2)
return count > 0


# javascript.py ends here
2 changes: 1 addition & 1 deletion python.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"Python specific code for stage 2."
"Python specific code for stage 3."

import os
import re
Expand Down
2 changes: 2 additions & 0 deletions stage3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys

from golang import process_golang
from javascript import process_javascript
from python import process_python


Expand Down Expand Up @@ -80,6 +81,7 @@ def main():
print(f"{container_mode=}", file=sys.stderr)
process_golang(main_dir, dirs, container_mode)
process_python(main_dir, dirs, container_mode)
process_javascript(main_dir, dirs, container_mode)


if __name__ == "__main__":
Expand Down

0 comments on commit c281c7e

Please sign in to comment.