Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Winglang version monitor to trigger new test runs in this repo #2

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/feedreader/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Feedreader

This is an example of a WingLang project that demonstrates the usage of cloud services to read a Github Atom feed and trigger a Github Action when a new version is detected.

## Usage

1. Set up a new cloud secret with the name "github-token" and your Github token as the value.
2. Deploy the project.
3. The program will run every hour, checking the Github Atom feed for new versions.
4. If a new version is detected, it will trigger the specified Github Action and store a record in a cloud bucket.
5. Add `workflow_dispatch:` to the Github Action you want to trigger. This allows the action to be run manually from the Github Actions UI or API.

![console](./console.png)

![workflow](./workflow.png)

Binary file added examples/feedreader/console.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions examples/feedreader/feed.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fetch from 'node-fetch';
import { XMLParser, XMLBuilder } from 'fast-xml-parser';

export async function parseAtomFeed() {
const feedUrl = 'https://github.com/winglang/wing/releases.atom';

let feed;
try {
// Make GET request and parse XML data
const response = await fetch(feedUrl);
const text = await response.text();

const parser = new XMLParser();
feed = parser.parse(text);
} catch (error) {
console.error('Error parsing feed:', error);
return null;
}
// Check if feed and its properties exist before extracting data
const feedData = feed ? {
id: feed.feed.id,
link: feed.feed.link && feed.feed.link[0].$ ? feed.feed.link[0].$.href : null,
title: feed.feed.title,
updated: feed.feed.updated,
} : null;

// Check if feed items exist before extracting entries data
const entries = feed && feed.feed.entry ? feed.feed.entry.map(item => ({
id: item.id,
updated: item.updated,
link: item.link && item.link[0].$ && item.link[0].$.rel === 'alternate' ? item.link[0].$.href : null,
title: item.title,
content: item.content,
})) : null;

return {feed: feedData, entries};
}
59 changes: 59 additions & 0 deletions examples/feedreader/main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
bring cloud;
bring util;
bring http;

struct GithubAtomFeed {
id: str;
updated: str;
}
struct GithubAtom {
feed: GithubAtomFeed;
}

class Feedreader {
extern "./feed.mjs" pub static inflight parseAtomFeed(): GithubAtom;
}

let githubToken = new cloud.Secret(name: "github-token");
let bucket = new cloud.Bucket();

bucket.onCreate(inflight () => {
log("File in bucket created");
let token = githubToken.value();
let owner = "winglang";
let repo = "examples";
let workflowId = "wing-sdk.yml";
let result = http.post("https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflowId}/dispatches",
headers: {
"Authorization": "token ${token}",
"Accept": "application/vnd.github.v3+json",
"X-GitHub-Api-Version": "2022-11-28",
},
body: Json.stringify({
"ref": "main",
}),
);

log("Result: ${result.ok} ${result.status} ${result.body}");
});

// This cron schedule runs every hour at minute 0
let schedule = new cloud.Schedule(cron: "0 * * * ?");

let scheduleHandler = inflight () => {
let json: GithubAtom = Feedreader.parseAtomFeed();
let id = util.sha256(json.feed.updated);
if bucket.exists(id) {
log("No new versions");
} else {
log("New versions");
bucket.put(id, "new version ${json.feed.updated}");
}
};

schedule.onTick(scheduleHandler);

test "parse atom feed" {
let json = Feedreader.parseAtomFeed();
log(Json.stringify(json));
}
153 changes: 153 additions & 0 deletions examples/feedreader/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/feedreader/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "feedreader",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fast-xml-parser": "^4.3.2",
"node-fetch": "^3.3.2",
"xml2js": "^0.6.2"
}
}
Binary file added examples/feedreader/workflow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading