Skip to content

Commit be4eb63

Browse files
committed
Add support for latest-[pre|all] special values
1 parent 697e583 commit be4eb63

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

src/updateinformation/GithubReleasesZsyncUpdateInformation.cpp

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,38 @@ namespace appimage::update::updateinformation {
1717
auto filename = _updateInformationComponents[4];
1818

1919
std::stringstream url;
20-
url << "https://api.github.com/repos/" << username << "/" << repository << "/releases/";
21-
22-
if (tag.find("latest") != std::string::npos) {
20+
url << "https://api.github.com/repos/" << username << "/" << repository << "/releases";
21+
22+
bool parseListResponse = false;
23+
bool usePrereleases = false;
24+
bool useReleases = true;
25+
26+
// TODO: this snippet does not support pagination
27+
// it is more reliable for "known" releases ("latest" and named ones, e.g., "continuous") to query them directly
28+
// we expect paginated responses to be very unlikely
29+
if (tag == "latest-pre") {
30+
usePrereleases = true;
31+
useReleases = false;
32+
parseListResponse = true;
33+
} else if (tag == "latest-all") {
34+
usePrereleases = true;
35+
parseListResponse = true;
36+
} else if (tag == "latest") {
2337
issueStatusMessage("Fetching latest release information from GitHub API");
24-
url << "latest";
38+
url << "/latest";
2539
} else {
2640
std::ostringstream oss;
2741
oss << "Fetching release information for tag \"" << tag << "\" from GitHub API.";
2842
issueStatusMessage(oss.str());
29-
url << "tags/" << tag;
43+
url << "/tags/" << tag;
44+
}
45+
46+
if (parseListResponse) {
47+
issueStatusMessage("Fetching releases list from GitHub API");
3048
}
3149

32-
auto response = cpr::Get(cpr::Url{url.str()});
50+
auto urlStr = url.str();
51+
auto response = cpr::Get(cpr::Url{urlStr});
3352

3453
nlohmann::json json;
3554

@@ -46,6 +65,32 @@ namespace appimage::update::updateinformation {
4665
throw UpdateInformationError(oss.str());
4766
}
4867

68+
if (parseListResponse) {
69+
bool found = false;
70+
71+
for (auto& item : json) {
72+
if (item["prerelease"].get<bool>() && usePrereleases) {
73+
json = item;
74+
found = true;
75+
break;
76+
}
77+
78+
if (!useReleases) {
79+
continue;
80+
}
81+
82+
json = item;
83+
found = true;
84+
break;
85+
}
86+
87+
if (!found) {
88+
throw UpdateInformationError(std::string("Failed to find suitable release"));
89+
}
90+
91+
issueStatusMessage("Found matching release: " + json["name"].get<std::string>());
92+
}
93+
4994
// not ideal, but allows for returning a match for the entire line
5095
auto pattern = "*" + filename;
5196

0 commit comments

Comments
 (0)