feat: add endpoint to enable email enrollment

This commit is contained in:
Patrick 2024-07-14 00:57:03 +02:00
parent 255a6f1459
commit 46ab80a7ac
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F
4 changed files with 243 additions and 185 deletions

View file

@ -5,7 +5,7 @@
name = "pr-tracker"
version = "1.2.0"
authors = ["Alyssa Ross <hi@alyssa.is>"]
edition = "2018"
edition = "2021"
license = "AGPL-3.0-or-later WITH GPL-3.0-linking-exception"
[build-dependencies]

View file

@ -23,6 +23,7 @@ use futures_util::future::join_all;
use http_types::mime;
use once_cell::sync::Lazy;
use serde::Deserialize;
use serde_json::json;
use structopt::StructOpt;
use tide::{Request, Response};
@ -71,8 +72,10 @@ static GITHUB_TOKEN: Lazy<OsString> = Lazy::new(|| {
struct PageTemplate {
error: Option<String>,
pr_number: Option<String>,
email: Option<String>,
pr_title: Option<String>,
closed: bool,
subscribed: bool,
tree: Option<Tree>,
source_url: String,
}
@ -80,14 +83,10 @@ struct PageTemplate {
#[derive(Debug, Deserialize)]
struct Query {
pr: Option<String>,
email: Option<String>,
}
async fn track_pr(pr_number: Option<String>, status: &mut u16, page: &mut PageTemplate) {
let pr_number = match pr_number {
Some(pr_number) => pr_number,
None => return,
};
async fn track_pr(pr_number: String, status: &mut u16, page: &mut PageTemplate) {
let pr_number_i64 = match pr_number.parse() {
Ok(n) => n,
Err(_) => {
@ -146,8 +145,30 @@ async fn handle_request<S>(request: Request<S>) -> http_types::Result<Response>
};
let pr_number = request.query::<Query>()?.pr;
let email = request.query::<Query>()?.email;
page.email = email.clone();
track_pr(pr_number, &mut status, &mut page).await;
match pr_number.clone() {
Some(pr_number) => track_pr(pr_number, &mut status, &mut page).await,
None => {}
};
match email {
Some(email) => {
if let Some(ref tree) = page.tree {
let mut v = Vec::new();
let remaining = tree.collect_branches(&mut v);
if !remaining {
page.error = Some("There are no branches remaining to be tracked".to_string())
} else {
page.subscribed = true;
let folder = format!("data/{}", pr_number.unwrap());
std::fs::create_dir_all(folder.clone())?;
std::fs::write(format!("{folder}/{email}"), json!(v).to_string())?;
}
}
}
None => {}
}
Ok(Response::builder(status)
.content_type(mime::HTML)

View file

@ -39,6 +39,18 @@ impl Tree {
children: nexts,
}
}
pub fn collect_branches(&self, vec: &mut Vec<String>) -> bool {
let mut res = false;
if let Some(true) = self.accepted {
vec.push(self.branch_name.clone());
} else {
res = true
}
for c in &self.children {
res |= c.collect_branches(vec);
}
res
}
fn fill_accepted(&mut self, branches: &BTreeSet<OsString>, missing_means_absent: bool) {
self.accepted = match branches.contains(OsStr::new(&self.branch_name)) {

View file

@ -4,6 +4,7 @@
<!doctype html>
<html lang="en">
<head>
{% match pr_number %}
{%- when Some with (pr_number) -%}
@ -55,7 +56,8 @@
margin: 0;
}
ol, ul {
ol,
ul {
list-style: none;
padding: 0;
}
@ -114,6 +116,17 @@
content: "✔";
}
.state-subscribed {
background: #00C42D;
margin-bottom: 1em;
max-width: 800px;
margin-left: auto;
margin-right: auto;
padding: 1em;
border-radius: 10px;
border: 1px solid black;
}
span.state-rejected::after {
background: #c40000;
content: "❌︎";
@ -138,7 +151,8 @@
position: relative;
}
ol::before, ul::before {
ol::before,
ul::before {
background: #7A877D;
content: "";
display: block;
@ -155,14 +169,24 @@
<header>
<h1>Nixpkgs Pull Request Tracker</h1>
{%- if subscribed -%}
<div class="state-subscribed">You will be notified be by mail when this PR reaches a new ???</div>
{%- endif -%}
<form>
<label for="pr">PR number: </label>
<input id="pr" name="pr" type="text" pattern="[1-9][0-9]*"
value="{%- match pr_number -%}
<input id="pr" name="pr" type="text" pattern="[1-9][0-9]*" value="{%- match pr_number -%}
{%- when Some with (pr_number) -%}
{{- pr_number -}}
{%- else -%}
{%- endmatch -%}">
<br>
<label for="email">Email: </label>
<input id="email" name="email" type="email" value="{%- match email -%}
{%- when Some with (email) -%}
{{- email -}}
{%- else -%}
{%- endmatch -%}">
<br>
<button type="submit">Track</button>
</form>
</header>
@ -212,4 +236,5 @@
<p><a href="{{ source_url }}">Source code</a></p>
</footer>
</body>
</html>