Skip to content

Commit

Permalink
Fix autoupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
Froxcey committed Jan 14, 2024
1 parent 59212e7 commit 05d1668
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
32 changes: 32 additions & 0 deletions .github/actions/nightly-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { execSync } = require("child_process");
const { Octokit } = require("@octokit/action");

const dateString = new Date()
.toLocaleDateString("en-GB", { day: "numeric", month: "short" })
.replace(" ", "-");
const branchName = "nightly_update_" + dateString;
execSync(`git checkout -b "${branchName}"`);

// Do the check here
const zigRes = require("./zig-update").default();

if (zigRes == "") process.exit();

execSync(
` git config --global user.name "froxcey";
git config --global user.email "danichen204@gmail.com";
git add -A;
git commit -m "[Autoupdate]: ${branchName}";
git push -f origin ${branchName};`,
);

const octokit = new Octokit();
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
octokit.pulls.create({
owner,
repo,
base: "main",
head: branchName,
title: `Nightly update: ${dateString}`,
body: zigRes,
});
62 changes: 19 additions & 43 deletions .github/actions/zig-update.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
const https = require("https");
const fs = require("fs");
const { execSync } = require("child_process");
const { Octokit } = require("@octokit/action");

https.get("https://ziglang.org/download/index.json", (res) => {
let data = "";
module.exports = new Promise((resolve, reject) => {
https.get("https://ziglang.org/download/index.json", (res) => {
let data = "";

// A chunk of data has been received.
res.on("data", (chunk) => {
data += chunk;
});
// A chunk of data has been received.
res.on("data", (chunk) => {
data += chunk;
});

// The whole response has been received.
res.on("end", () => {
let res = JSON.parse(data);
check(res);
// The whole response has been received.
res.on("end", async () => {
let res = JSON.parse(data);
resolve(await check(res));
});
});
});
})

async function check(data) {
if (!data.master.version.startsWith("0.12")) {
console.error("Zig master is no longer on 0.12, please update");
process.exit(1);
}
const path = "Formula/z/zig-nightly.rb";
const path = "Formula/zig-nightly.rb";
var file = fs.readFileSync(path).toString();
const verMatch = /"[\d.]+-dev\.[+-\w]+"/;
const localVer = file.match(verMatch)[0].replaceAll('"', "");
const remoteVer = data.master.version;
if (localVer == remoteVer) return console.log("Nightly is up to date");

const dateString = new Date()
.toLocaleDateString("en-GB", { day: "numeric", month: "short" })
.replace(" ", "-")
const branchName = "nightly_update_" + dateString;
execSync(`git checkout -b "${branchName}"`);
if (localVer == remoteVer) {
console.log("Nightly is up to date");
return ""
}

file = file.replace(verMatch, `"${remoteVer}"`);

Expand All @@ -52,22 +45,5 @@ async function check(data) {

fs.writeFileSync(path, file);

execSync(
` git config --global user.name "froxcey";
git config --global user.email "danichen204@gmail.com";
git add -A;
git commit -m "[Autoupdate]: Sync zig@0.12 to ${remoteVer}";
git push -f origin ${branchName};`,
);

const octokit = new Octokit();
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
octokit.pulls.create({
owner,
repo,
base: "main",
head: branchName,
title: `Nightly update: ${dateString}`,
body: `- Update Zig@0.12 to ${remoteVer}`,
});
return `- Update zig-nightly to ${remoteVer}\n`;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name: Nightly update
on:
push:
branches:
- main
schedule:
- cron: '16 0 * * *'
- cron: '16 22 * * *'

jobs:
update-zig:
Expand All @@ -12,6 +15,6 @@ jobs:
with:
version: 12
- run: npm install @octokit/action
- run: node .github/actions/zig-update.js
- run: node .github/actions/nightly-update.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit 05d1668

Please sign in to comment.