Skip to content

Commit

Permalink
Merge pull request #1272 from thc202/update-flathub
Browse files Browse the repository at this point in the history
Update Flathub on main releases
  • Loading branch information
kingthorin authored Jan 14, 2025
2 parents 9f7a789 + e0e9c77 commit 5b19049
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/handle-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ jobs:
persist-credentials: false
path: zaproxy-website
fetch-depth: 0
- name: Checkout org.zaproxy.ZAP
uses: actions/checkout@v4
with:
repository: flathub/org.zaproxy.ZAP
persist-credentials: false
path: org.zaproxy.ZAP
fetch-depth: 0
- name: Setup Java
uses: actions/setup-java@v4
with:
Expand Down
32 changes: 32 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.zaproxy.gradle.HandleWeeklyRelease
import org.zaproxy.gradle.UpdateAddOnZapVersionsEntries
import org.zaproxy.gradle.UpdateAndCreatePullRequestAddOnRelease
import org.zaproxy.gradle.UpdateDailyZapVersionsEntries
import org.zaproxy.gradle.UpdateFlathubData
import org.zaproxy.gradle.UpdateGettingStartedWebsitePage
import org.zaproxy.gradle.UpdateMainZapVersionsEntries
import org.zaproxy.gradle.UpdateZapVersionWebsiteData
Expand Down Expand Up @@ -375,8 +376,39 @@ val handleMainRelease by tasks.registering(HandleMainRelease::class) {
eventType.set("release-main-docker")
}

val flathubRepo = GitHubRepo("flathub", "org.zaproxy.ZAP", file("$rootDir/../org.zaproxy.ZAP"))

val updateFlathubData by tasks.registering(UpdateFlathubData::class) {
releaseState.set(releaseStateData)

zapVersions.set(latestZapVersions)
baseDirectory.set(flathubRepo.dir)
}

val updateFlathub by tasks.registering(CreatePullRequest::class) {
dependsOn(updateFlathubData)

user.set(ghUser)
repo.set(flathubRepo)
baseBranchName.set("master")
branchName.set("update-zap")

commitSummary.set("Update ZAP version")
commitDescription.set(
provider {
"""
From:
$adminRepo@${headCommit(adminRepo.dir)}
""".trimIndent()
},
)

mustRunAfter(handleMainRelease)
}

tasks.register("handleRelease") {
dependsOn(updateWebsite)
dependsOn(handleWeeklyRelease)
dependsOn(handleMainRelease)
dependsOn(updateFlathub)
}
119 changes: 119 additions & 0 deletions buildSrc/src/main/java/org/zaproxy/gradle/UpdateFlathubData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Zed Attack Proxy (ZAP) and its related class files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Copyright 2025 The ZAP Development Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.zaproxy.gradle;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.LocalDate;
import java.util.Iterator;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.configuration.XMLConfiguration;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.DefaultTask;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
import org.zaproxy.gradle.ReleaseState.VersionChange;

public abstract class UpdateFlathubData extends DefaultTask {

@InputFile
public abstract RegularFileProperty getReleaseState();

@InputFile
public abstract RegularFileProperty getZapVersions();

@Input
public abstract Property<File> getBaseDirectory();

@TaskAction
void executeTasks() throws Exception {
ReleaseState releaseState = ReleaseState.read(getReleaseState().getAsFile().get());
if (!isNewMainRelease(releaseState)) {
return;
}

XMLConfiguration zapVersionsXml = new CustomXmlConfiguration();
zapVersionsXml.load(getZapVersions().get().getAsFile());

String version = zapVersionsXml.getString("core.version");
String url = zapVersionsXml.getString("core.linux.url");
String hash = zapVersionsXml.getString("core.linux.hash").split(":", 2)[1];

Path repo = getBaseDirectory().get().toPath();

updateAppData(repo, version);
updateJson(repo, url, hash);
}

private static boolean isNewMainRelease(ReleaseState releaseState) {
VersionChange mainRelease = releaseState.getMainRelease();
return mainRelease != null && mainRelease.isNewVersion();
}

private static void updateAppData(Path dir, String version) throws IOException {
Path file = dir.resolve("org.zaproxy.ZAP.appdata.xml");
String contents = Files.readString(file);

Matcher matcher = Pattern.compile("<release version=\"([^\\\"]+)\" date=\"([^\\\"]+)\" />").matcher(contents);
if (!matcher.find()) {
throw new IOException("The XML entry release was not found in: " + file);
}
StringBuilder sb = new StringBuilder();
matcher.appendReplacement(sb, "<release version=\"" + version + "\" date=\"" + LocalDate.now() + "\" />");
matcher.appendTail(sb);

Files.writeString(file, sb.toString());
}

private static void updateJson(Path dir, String url, String hash) throws IOException {
Path file = dir.resolve("org.zaproxy.ZAP.json");
String contents = Files.readString(file);

Matcher matcher = Pattern.compile("url\": \"([^\\\"]+)\"").matcher(contents);
if (!matcher.find()) {
throw new IOException("The JSON url property was not found in: " + file);
}
StringBuilder sb = new StringBuilder();
matcher.appendReplacement(sb, "url\": \"" + url + "\"");
matcher.appendTail(sb);

matcher = Pattern.compile("sha256\": \"([^\\\"]+)\"").matcher(sb.toString());
if (!matcher.find()) {
throw new IOException("The JSON sha256 property was not found in: " + file);
}
sb.setLength(0);
matcher.appendReplacement(sb, "sha256\": \"" + hash + "\"");
matcher.appendTail(sb);

Files.writeString(file, sb.toString());
}

}

0 comments on commit 5b19049

Please sign in to comment.