Skip to content

Commit

Permalink
build(updater): setup autoupdater (#219)
Browse files Browse the repository at this point in the history
* build(updater): setup autoupdater
  • Loading branch information
juancgalvis authored Oct 3, 2024
1 parent 4046158 commit 52a2cbe
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- 'ide_extension/**'
- '.github/workflows/intellij-build.yml'
- '.github/workflows/intellij-release.yml'
- '.github/workflows/intellij-updater.yml'

pull_request:
branches:
Expand All @@ -17,6 +18,7 @@ on:
- 'ide_extension/**'
- '.github/workflows/intellij-build.yml'
- '.github/workflows/intellij-release.yml'
- '.github/workflows/intellij-updater.yml'

jobs:
build:
Expand Down
60 changes: 60 additions & 0 deletions .github/workflows/intellij-updater.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: updater
on:
workflow_dispatch:
schedule:
- cron: '0 12 * * 1' # every Monday at 07:00 Colombia Time
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit

- name: Generate a token
id: generate_token
uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a # v2.1.0
with:
app_id: ${{ secrets.APP_ID_ADMIN_GITHUB }}
private_key: ${{ secrets.APP_PRIVATE_KEY_ADMIN_GITHUB }}
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
ref: trunk
token: ${{ steps.generate_token.outputs.token }}
- name: Set up JDK 17
uses: actions/setup-java@2dfa2011c5b2a0f1489bf9e433881c92c1631f88 # v4.3.0
with:
distribution: 'temurin'
java-version: 17
- name: Set up NodeJS Latest
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 #v4.0.4
with:
node-version: 'latest'
- name: Check for updates
working-directory: ./ide_extension/intellij
run: node ide_extension/intellij/updater/index.js
- name: Check for changes
id: git_changes
run: |
git diff --name-only
if [[ $(git diff --name-only) ]]; then
echo "Changes detected!"
echo "HAS_CHANGES=true" >> $GITHUB_ENV
else
echo "No changes detected!"
echo "HAS_CHANGES=false" >> $GITHUB_ENV
fi
- name: Create Pull Request
if: env.HAS_CHANGES == 'true'
uses: peter-evans/create-pull-request@5e914681df9dc83aa4e4905692ca88beb2f9e91f # v7.0.5
with:
token: ${{ steps.generate_token.outputs.token }}
committer: Dependencies Bot <[email protected]>
commit-message: 'fix(deps): update dependencies'
title: 'fix(deps): update dependencies'
body: 'This PR updates dependencies to latest versions'
branch: 'feature/autoupdate-deps'
base: 'trunk'
labels: 'dependencies'
reviewers: 'juancgalvis,santitigaga'
11 changes: 7 additions & 4 deletions ide_extension/intellij/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ fun environment(key: String) = providers.environmentVariable(key)
plugins {
id("java")
id("jacoco")
alias(libs.plugins.intelliJPlatform) // IntelliJ Platform Gradle Plugin
alias(libs.plugins.changelog) // Gradle Changelog Plugin
alias(libs.plugins.sonar) // Gradle Sonar Plugin
// IntelliJ Platform Gradle Plugin
id("org.jetbrains.intellij.platform") version "2.0.1"
// Gradle Changelog Plugin
id("org.jetbrains.changelog") version "2.2.1"
// Gradle Sonar Plugin
id("org.sonarqube") version "5.1.0.4882"
}

group = properties("pluginGroup").get()
Expand All @@ -29,7 +32,7 @@ repositories {
}

dependencies {
testImplementation(libs.junit)
testImplementation("junit:junit:4.13.2")

// IntelliJ Platform Gradle Plugin Dependencies Extension - read more: https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-dependencies-extension.html
intellijPlatform {
Expand Down
16 changes: 0 additions & 16 deletions ide_extension/intellij/gradle/libs.versions.toml

This file was deleted.

Binary file modified ide_extension/intellij/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
163 changes: 163 additions & 0 deletions ide_extension/intellij/updater/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
const { exec } = require('child_process');
const fs = require('fs');

// Regular expression to match Gradle dependencies
const dependencyRegex = /(\w[\w.-]*:\w[\w.-]*):(\d+\.\d+\.\d+)/;
// Regular expression to match Gradle plugins
const pluginRegex = /id\("(.+?)"\) version "(\d+(\.\d+)+)"/;

async function fetchDependencyLatestVersion(dependency) {
const [groupId, artifactId] = dependency.split(':');
const url = `https://search.maven.org/solrsearch/select?q=g:${groupId}+AND+a:${artifactId}&rows=10&wt=json&core=gav`;
console.log(`Fetching version for ${dependency} from ${url}`);

try {
const response = await fetch(url);
if (response.ok) {
const body = await response.json();
const stableVersions = body.response.docs
.map(doc => doc.v)
.filter(version => !/-(alpha|beta|rc|m|snapshot|cr|dev)/i.test(version));
return stableVersions.length > 0 ? stableVersions[0] : null;
}
} catch (error) {
console.error(`Error fetching version for ${dependency}: ${error.message}`);
}
return null;
}

async function fetchLatestPluginVersion(pluginId) {
const pluginIdPath = pluginId.replace(/\./g, '/');
const url = `https://plugins.gradle.org/m2/${pluginIdPath}/${pluginId}.gradle.plugin/maven-metadata.xml`;

console.log(`Fetching plugin version for ${pluginId} from ${url}`);

try {
const response = await fetch(url);
if (response.ok) {
const body = await response.text();
const match = body.match(/<version>(.+?)<\/version>/);
if (match) {
console.log(`Fetched plugin version for ${pluginId}:${match[1]}`);
return match[1];
}
}
} catch (error) {
console.error(`Error fetching plugin version for ${pluginId}: ${error.message}`);
}
return null;
}

async function updateDependencyLine(line) {
let updatedLine = line;

const match = line.match(dependencyRegex);
if (match) {
const dependency = match[1];
const currentVersion = match[2];
console.log(`Found dependency: ${dependency}:${currentVersion}`);

const latestVersion = await fetchDependencyLatestVersion(dependency);
if (latestVersion) {
console.log(`Updating ${dependency} from ${currentVersion} to ${latestVersion}`);
updatedLine = line.replace(currentVersion, latestVersion);
}
} else {
const pluginMatch = line.match(pluginRegex);
if (pluginMatch) {
const pluginId = pluginMatch[1];
const currentVersion = pluginMatch[2];
console.log(`Found plugin: ${pluginId}:${currentVersion}`);

const latestPluginVersion = await fetchLatestPluginVersion(pluginId);
if (latestPluginVersion) {
console.log(`Updating ${pluginId} from ${currentVersion} to ${latestPluginVersion}`);
updatedLine = line.replace(currentVersion, latestPluginVersion);
}
}
}
return updatedLine;
}

async function updateFileByLines(filePath, processLine) {
const fileContent = fs.readFileSync(filePath, 'utf8');
const lines = fileContent.split('\n');
const updatedLines = [];

for (const line of lines) {
updatedLines.push(await processLine(line));
}

fs.writeFileSync(filePath, updatedLines.join('\n'), 'utf8');
console.log(`File ${filePath} updated successfully!`);
}

function wildCardBuild(build) {
return build.split(".")[0] + ".*";
}

function extractICVersion(data) {
const latestRelease = data.filter((item) => item.code === "IIU")[0].releases.filter((release) => release.type === "release")[0];
return { version: latestRelease.version, build: wildCardBuild(latestRelease.build) };
}

async function getIntellijCommunityVersion() {
const response = await fetch("https://data.services.jetbrains.com/products?code=IIU%2CIIC&release.type=release");
if (response.ok) {
const data = await response.json();
return extractICVersion(data);
}
throw new Error("Failed to fetch IntelliJ Community version", response.status);
}

async function getGradleVersion() {
const response = await fetch("https://services.gradle.org/versions/current");
if (response.ok) {
const data = await response.json();
return data.version;
}
throw new Error("Failed to fetch latest gradle version", response.status);
}

function updateGradleProperties({ intellijVersion, gradleVersion }) {
return async function (line) {
let updatedLine = line;
if (line.startsWith("platformVersion")) {
updatedLine = `platformVersion=${intellijVersion.version}`;
} else if (line.startsWith("pluginUntilBuild")) {
updatedLine = `pluginUntilBuild=${intellijVersion.build}`;
} else if (line.startsWith("gradleVersion")) {
updatedLine = `gradleVersion=${gradleVersion}`;
}
return updatedLine;
}
}

function gradleWrapper() {
exec('./gradlew wrapper', (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(stdout);
});
}

async function update() {
updateFileByLines('build.gradle.kts', updateDependencyLine)
.catch(console.error);
const intellijVersion = await getIntellijCommunityVersion();
console.log(`IntelliJ Community version: ${intellijVersion.version} (${intellijVersion.build})`);
const gradleVersion = await getGradleVersion();
console.log(`Gradle version: ${gradleVersion}`);
updateFileByLines('gradle.properties', updateGradleProperties({ intellijVersion, gradleVersion }))
.catch(console.error);
console.log("Updating gradle wrapper");
gradleWrapper();
}

update();

0 comments on commit 52a2cbe

Please sign in to comment.