-
Notifications
You must be signed in to change notification settings - Fork 100
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
flowey: use typestate pattern for gh variables #643
base: main
Are you sure you want to change the base?
Changes from 24 commits
f4f97df
82db3d9
ff116bb
46a3016
65fed09
dcfa810
478329b
cbdd984
db8a9c9
c7f63cf
e360854
ebec5e1
d55fc8a
f778042
b71c81b
bfa2abd
ff200aa
1d8e3ff
06bade7
667fbb5
86f2354
3aa18b6
d88b355
a5b3562
1cd416e
c20f33a
1395a24
7abe491
3f45028
ac30808
9f7afd1
440251b
a293393
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
//! Core types and traits used to read GitHub context variables. | ||
|
||
use crate::node::{user_facing::GhContextVar, ClaimVar, NodeCtx, ReadVar, StepCtx}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. each import on its own line please. the guide has some suggestions on how to configure rust-analyzer to do this for you |
||
use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
use std::collections::BTreeMap; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct Head { | ||
#[serde(rename = "ref")] | ||
pub head_ref: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct GhContextVarReaderEventPullRequest { | ||
pub head: Head, | ||
} | ||
|
||
pub enum Root {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you still want to stuff these ZSTs into their own |
||
pub enum Event {} | ||
|
||
#[derive(Clone)] | ||
pub struct GhVarState { | ||
pub raw_name: String, | ||
pub backing_var: String, | ||
pub is_secret: bool, | ||
pub is_object: bool, | ||
} | ||
|
||
pub struct GhContextVarReader<'a, S> { | ||
pub ctx: NodeCtx<'a>, | ||
pub _state: std::marker::PhantomData<S>, | ||
} | ||
|
||
impl<S> GhContextVarReader<'_, S> { | ||
fn read_var<T: Serialize + DeserializeOwned>( | ||
&self, | ||
var_name: String, | ||
is_secret: bool, | ||
is_object: bool, | ||
) -> ReadVar<T> { | ||
let (var, write_var) = self.ctx.new_maybe_secret_var(is_secret, ""); | ||
let write_var = write_var.claim(&mut StepCtx { | ||
backend: self.ctx.backend.clone(), | ||
}); | ||
let var_state = GhVarState { | ||
raw_name: var_name.clone(), | ||
backing_var: write_var.backing_var, | ||
is_secret: write_var.is_secret, | ||
is_object, | ||
}; | ||
let gh_to_rust = vec![var_state]; | ||
|
||
self.ctx.backend.borrow_mut().on_emit_gh_step( | ||
&format!("🌼 read {}", var_name), | ||
"", | ||
BTreeMap::new(), | ||
None, | ||
BTreeMap::new(), | ||
BTreeMap::new(), | ||
gh_to_rust, | ||
Vec::new(), | ||
); | ||
var | ||
} | ||
} | ||
|
||
impl<'a> GhContextVarReader<'a, Root> { | ||
pub fn global(&self, gh_var: GhContextVar) -> ReadVar<String> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now that you have this the only thing you'll need to tweak is the user-defined secret var handling, but even that should be pretty straightforward, if you simply swap the current type that flow uses (i.e: the |
||
self.read_var(gh_var.as_raw_var_name(), gh_var.is_secret(), false) | ||
} | ||
|
||
pub fn event(self) -> GhContextVarReader<'a, Event> { | ||
GhContextVarReader { | ||
ctx: self.ctx, | ||
_state: std::marker::PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl GhContextVarReader<'_, Event> { | ||
pub fn pull_request(self) -> ReadVar<Option<GhContextVarReaderEventPullRequest>> { | ||
self.read_var("github.event.pull_request".to_string(), false, true) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
//! this crate!** The crate you should be using is called `flowey`, which only | ||
//! exports user-facing types / traits. | ||
|
||
pub mod github_context; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this mod should be a mod under this should also help with some of the visibility issues you might've run into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call, this fixed pretty much all the visibility stuff I was having issues with |
||
pub mod node; | ||
pub mod patch; | ||
pub mod pipeline; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stale comment - not actually true with the introduction of
is_object