feat: add endpoint to enable email enrollment
This commit is contained in:
parent
255a6f1459
commit
46ab80a7ac
|
@ -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]
|
||||
|
|
35
src/main.rs
35
src/main.rs
|
@ -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)
|
||||
|
|
12
src/tree.rs
12
src/tree.rs
|
@ -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)) {
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
|
||||
<head>
|
||||
{% match pr_number %}
|
||||
{%- when Some with (pr_number) -%}
|
||||
{% match pr_title %}
|
||||
|
@ -27,7 +28,7 @@
|
|||
text-align: center;
|
||||
}
|
||||
|
||||
body > header {
|
||||
body>header {
|
||||
margin-bottom: 2em;
|
||||
}
|
||||
|
||||
|
@ -37,7 +38,7 @@
|
|||
text-align: center;
|
||||
}
|
||||
|
||||
body > section {
|
||||
body>section {
|
||||
background: #c4b0b0;
|
||||
padding: 0 1em;
|
||||
margin: 1em auto;
|
||||
|
@ -45,27 +46,28 @@
|
|||
max-width: 50ch;
|
||||
}
|
||||
|
||||
body > main {
|
||||
body>main {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
body > main > ol {
|
||||
body>main>ol {
|
||||
text-align: left;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
ol, ul {
|
||||
ol,
|
||||
ul {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
ul > li {
|
||||
ul>li {
|
||||
margin-left: 2em;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
ul > li:last-child {
|
||||
ul>li:last-child {
|
||||
margin-left: 0;
|
||||
position: static;
|
||||
}
|
||||
|
@ -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: "❌︎";
|
||||
|
@ -130,7 +143,7 @@
|
|||
background: #7A877D;
|
||||
}
|
||||
|
||||
ul > li:last-child > span::before {
|
||||
ul>li:last-child>span::before {
|
||||
content: none;
|
||||
}
|
||||
|
||||
|
@ -138,7 +151,8 @@
|
|||
position: relative;
|
||||
}
|
||||
|
||||
ol::before, ul::before {
|
||||
ol::before,
|
||||
ul::before {
|
||||
background: #7A877D;
|
||||
content: "";
|
||||
display: block;
|
||||
|
@ -149,20 +163,30 @@
|
|||
position: absolute;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<body>
|
||||
<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>
|
||||
|
@ -211,5 +235,6 @@
|
|||
|
||||
<p><a href="{{ source_url }}">Source code</a></p>
|
||||
</footer>
|
||||
</body>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue