Skip to content

Commit

Permalink
Merge pull request #113 from figsoda/diff
Browse files Browse the repository at this point in the history
Add diff feature
  • Loading branch information
Mic92 authored Nov 27, 2022
2 parents e774fdb + 8eed069 commit 64f5cc5
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 13 deletions.
2 changes: 2 additions & 0 deletions nix_update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def format_commit_message(package: Package) -> str:
):
new_version = new_version[1:]
msg = f"{package.attribute}: {package.old_version} -> {new_version}"
if package.diff_url:
msg += f"\n\nDiff: {package.diff_url}"
if package.changelog:
msg += f"\n\nChangelog: {package.changelog}"
return msg
Expand Down
8 changes: 8 additions & 0 deletions nix_update/eval.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from dataclasses import InitVar, dataclass, field
from typing import Any, Dict, List, Optional
from urllib.parse import ParseResult, urlparse

from .errors import UpdateError
from .options import Options
Expand All @@ -24,6 +25,7 @@ class Package:
line: int
urls: Optional[List[str]]
url: Optional[str]
src_homepage: Optional[str]
changelog: Optional[str]
rev: str
hash: Optional[str]
Expand All @@ -35,10 +37,15 @@ class Package:

raw_version_position: InitVar[Optional[Dict[str, Any]]]

parsed_url: Optional[ParseResult] = None
new_version: Optional[Version] = None
version_position: Optional[Position] = field(init=False)
diff_url: Optional[str] = None

def __post_init__(self, raw_version_position: Optional[Dict[str, Any]]) -> None:
url = self.url or (self.urls[0] if self.urls else None)
if url:
self.parsed_url = urlparse(url)
if raw_version_position is None:
self.version_position = None
else:
Expand Down Expand Up @@ -74,6 +81,7 @@ def eval_expression(import_path: str, attr: str) -> str:
cargo_deps = (pkg.cargoDeps or null).outputHash or null;
npm_deps = (pkg.npmDeps or null).outputHash or null;
tests = builtins.attrNames (pkg.passthru.tests or {{}});
src_homepage = pkg.src.meta.homepage or null;
changelog = pkg.meta.changelog or null;
}})"""

Expand Down
20 changes: 12 additions & 8 deletions nix_update/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .options import Options
from .utils import info, run
from .version import fetch_latest_version
from .version.gitlab import GITLAB_API
from .version.version import Version, VersionPreference


Expand Down Expand Up @@ -135,13 +136,8 @@ def update_version(
if preference == VersionPreference.FIXED:
new_version = Version(version)
else:
if not package.url:
if package.urls:
package.url = package.urls[0]
else:
raise UpdateError(
"Could not find a url in the derivations src attribute"
)
if not package.parsed_url:
raise UpdateError("Could not find a url in the derivations src attribute")
if preference != VersionPreference.BRANCH:
branch = None
elif version == "branch":
Expand All @@ -151,7 +147,7 @@ def update_version(
assert version.startswith("branch=")
branch = version[7:]
new_version = fetch_latest_version(
package.url, preference, version_regex, branch
package.parsed_url, preference, version_regex, branch
)
package.new_version = new_version
position = package.version_position
Expand All @@ -162,6 +158,14 @@ def update_version(
if recovered_version:
package.old_version = recovered_version
return False

if package.parsed_url:
if package.parsed_url.netloc == "github.com":
_, owner, repo, *_ = package.parsed_url.path.split("/")
package.diff_url = f"https://github.com/{owner}/{repo}/compare/{package.rev}...{new_version.rev or new_version.number}"
elif GITLAB_API.match(package.parsed_url.geturl()) and package.src_homepage:
package.diff_url = f"{package.src_homepage}-/compare/{package.rev}...{new_version.rev or new_version.number}"

return replace_version(package)


Expand Down
6 changes: 2 additions & 4 deletions nix_update/version/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from functools import partial
from typing import Callable, List, Optional
from urllib.parse import ParseResult, urlparse
from urllib.parse import ParseResult

from ..errors import VersionError
from .crate import fetch_crate_versions
Expand Down Expand Up @@ -56,13 +56,11 @@ def is_unstable(version: Version, extracted: str) -> bool:


def fetch_latest_version(
url_str: str,
url: ParseResult,
preference: VersionPreference,
version_regex: str,
branch: Optional[str] = None,
) -> Version:
url = urlparse(url_str)

unstable: List[str] = []
filtered: List[str] = []
used_fetchers = fetchers
Expand Down
3 changes: 2 additions & 1 deletion tests/test_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest.mock
from pathlib import Path
from typing import BinaryIO
from urllib.parse import urlparse

import conftest

Expand All @@ -19,7 +20,7 @@ def test_branch(helpers: conftest.Helpers) -> None:
with unittest.mock.patch("urllib.request.urlopen", fake_urlopen):
assert (
fetch_latest_version(
"https://github.com/Mic92/nix-update",
urlparse("https://github.com/Mic92/nix-update"),
VersionPreference.BRANCH,
"(.*)",
"master",
Expand Down
36 changes: 36 additions & 0 deletions tests/test_github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import subprocess

import conftest

from nix_update import main


def test_main(helpers: conftest.Helpers) -> None:
with helpers.testpkgs(init_git=True) as path:
main(["--file", str(path), "--commit", "github"])
version = subprocess.run(
[
"nix",
"eval",
"--raw",
"--extra-experimental-features",
"nix-command",
"-f",
path,
"github.version",
],
check=True,
text=True,
stdout=subprocess.PIPE,
).stdout.strip()
assert version >= "8.5.2"
commit = subprocess.run(
["git", "-C", path, "log", "-1"],
text=True,
stdout=subprocess.PIPE,
check=True,
).stdout.strip()
print(commit)
assert version in commit
assert "github" in commit
assert "https://github.com/sharkdp/fd/compare/v8.0.0...v" in commit
38 changes: 38 additions & 0 deletions tests/test_gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import subprocess

import conftest

from nix_update import main


def test_main(helpers: conftest.Helpers) -> None:
with helpers.testpkgs(init_git=True) as path:
main(["--file", str(path), "--commit", "gitlab"])
version = subprocess.run(
[
"nix",
"eval",
"--raw",
"--extra-experimental-features",
"nix-command",
"-f",
path,
"gitlab.version",
],
check=True,
text=True,
stdout=subprocess.PIPE,
).stdout.strip()
assert version >= "0.22.0"
commit = subprocess.run(
["git", "-C", path, "log", "-1"],
text=True,
stdout=subprocess.PIPE,
check=True,
).stdout.strip()
print(commit)
assert version in commit
assert "gitlab" in commit
assert (
"https://gitlab.gnome.org/world/phosh/phosh/-/compare/v0.20.0...v" in commit
)
2 changes: 2 additions & 0 deletions tests/testpkgs/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{ pkgs ? import <nixpkgs> {} }:
{
crate = pkgs.callPackage ./crate.nix {};
github = pkgs.callPackage ./github.nix {};
gitlab = pkgs.callPackage ./gitlab.nix {};
pypi = pkgs.python3.pkgs.callPackage ./pypi.nix {};
sourcehut = pkgs.python3.pkgs.callPackage ./sourcehut.nix {};
savanna = pkgs.python3.pkgs.callPackage ./savanna.nix {};
Expand Down
13 changes: 13 additions & 0 deletions tests/testpkgs/github.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ stdenv, fetchFromGitHub }:

stdenv.mkDerivation rec {
pname = "fd";
version = "8.0.0";

src = fetchFromGitHub {
owner = "sharkdp";
repo = pname;
rev = "v${version}";
sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
};
}
15 changes: 15 additions & 0 deletions tests/testpkgs/gitlab.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{ stdenv, fetchFromGitLab }:

stdenv.mkDerivation rec {
pname = "phosh";
version = "0.20.0";

src = fetchFromGitLab {
domain = "gitlab.gnome.org";
group = "world";
owner = "phosh";
repo = pname;
rev = "v${version}";
sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
};
}

0 comments on commit 64f5cc5

Please sign in to comment.