Skip to content

Instantly share code, notes, and snippets.

@CypherpunkSamurai
Last active March 3, 2022 04:23
Show Gist options
  • Select an option

  • Save CypherpunkSamurai/e5985d506287961f4d4ca7eb4742f067 to your computer and use it in GitHub Desktop.

Select an option

Save CypherpunkSamurai/e5985d506287961f4d4ca7eb4742f067 to your computer and use it in GitHub Desktop.
CloudFlare Woker Script
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});
async function handleRequest(request) {
const { pathname } = new URL(request.url);
if (pathname.startsWith("/api")) {
return new Response(JSON.stringify({ pathname }), {
headers: { "Content-Type": "application/json" },
});
}
if (pathname.startsWith("/status")) {
const httpStatusCode = Number(pathname.split("/")[2]);
return Number.isInteger(httpStatusCode)
? fetch("https://http.cat/" + httpStatusCode)
: new Response("That's not a valid HTTP status code.");
}
return fetch("https://welcome.developers.workers.dev");
}
/*
This worker sends a ci trigger to gitlab
This uses formData and fetch to post data to gitlab
*/
// Listen for incomming requests
addEventListener("fetch", (event) => {
// respond with handler, else return 500
event.respondWith(
handleRequest(event.request)
.catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});
// configs
const gitlab_project_id = "12345";
const webhook_url = "https://gitlab.com/api/v4/projects/" + gitlab_project_id + "/trigger/pipeline";
const gitlab_ref = "master";
const gitlab_ci_trigger_token = "";
// const webhook_url = "https://httpbin.org/post";
const webhook_payload2 = new FormData();
webhook_payload2.append("ref", "master");
webhook_payload2.append("token", gitlab_ci_trigger_token);
async function handleRequest(request) {
// send webhook request to trigger the gitlab ci
const request_option = {
method: "POST",
body: webhook_payload2,
}
const response = await fetch(webhook_url, request_option)
// return ok
return new Response( JSON.stringify(response) , {
headers: { "Content-Type": "text/plain" },
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment