mirrorer

:)
git clone https://git.sr.ht/~ashymad/mirrorer
Log | Files | Refs | LICENSE

index.ts (2390B)


      1 /**
      2  * Welcome to Cloudflare Workers!
      3  *
      4  * This is a template for a Scheduled Worker: a Worker that can run on a
      5  * configurable interval:
      6  * https://developers.cloudflare.com/workers/platform/triggers/cron-triggers/
      7  *
      8  * - Run `npm run dev` in your terminal to start a development server
      9  * - Run `curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"` to see your Worker in action
     10  * - Run `npm run deploy` to publish your Worker
     11  *
     12  * Bind resources to your Worker in `wrangler.jsonc`. After adding bindings, a type definition for the
     13  * `Env` object can be regenerated with `npm run cf-typegen`.
     14  *
     15  * Learn more at https://developers.cloudflare.com/workers/
     16  */
     17 
     18 export default {
     19     async fetch(req) {
     20         return new Response(`I am just a humble cloudflare worker :)`);
     21     },
     22     async scheduled(event, env, ctx) {
     23         let yml_req = await fetch("https://git.sr.ht/query", {
     24             method: "POST",
     25             headers: {
     26                 "Content-Type": "application/json",
     27                 "Authorization": `Bearer ${env.SRHTTOKEN}`
     28             },
     29             body: JSON.stringify({
     30                 "query": `{
     31                     me {
     32                         repository(name: "mirrorer") {
     33                             path(path: ".build.yml") {
     34                                 object {
     35                                     ... on TextBlob {
     36                                         text
     37                                     }
     38                                 }
     39                             }
     40                         }
     41                     }
     42                 }`
     43             })
     44         });
     45         let yml_json = await yml_req.json();
     46         let yml = yml_json.data.me.repository.path.object.text;
     47         let job_req = await fetch("https://builds.sr.ht/query", {
     48             method: "POST",
     49             headers: {
     50                 "Content-Type": "application/json",
     51                 "Authorization": `Bearer ${env.SRHTTOKEN}`
     52             },
     53             body: JSON.stringify({
     54                 "query": `mutation ($yml: String!) {
     55                     submit(manifest: $yml) {
     56                         id
     57                     }
     58                 }`,
     59                 "variables": {
     60                     "yml": yml
     61                 }})
     62         });
     63         console.log(`Build submitted at ${event.cron}: ${JSON.stringify(await job_req.json())}`);
     64     }
     65 } satisfies ExportedHandler<Env>;