diff --git a/Cargo.lock b/Cargo.lock index aaa4cdb..4d1d33f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -628,6 +628,15 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28988d872ab76095a6e6ac88d99b54fd267702734fd7ffe610ca27f533ddb95a" +[[package]] +name = "openssl-src" +version = "111.20.0+1.1.1o" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92892c4f87d56e376e469ace79f1128fdaded07646ddf73aa0be4706ff712dec" +dependencies = [ + "cc", +] + [[package]] name = "openssl-sys" version = "0.9.73" @@ -637,6 +646,7 @@ dependencies = [ "autocfg", "cc", "libc", + "openssl-src", "pkg-config", "vcpkg", ] @@ -910,6 +920,7 @@ dependencies = [ "chrono", "feed-rs", "html2md", + "openssl", "regex", "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index f7ea97e..d454a96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +openssl = { version = "0.10.32", features = ["vendored"] } serde = { version = "1", features = ["derive"] } serde_json="1.0" tokio = { version = "1.18.2", features = ["rt-multi-thread","macros"] } diff --git a/Dockerfile b/Dockerfile index 8fd9f52..ec21694 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,11 @@ FROM rust:latest AS builder +RUN rustup target add x86_64-unknown-linux-musl +RUN apt update && apt install -y musl-tools musl-dev RUN update-ca-certificates # Create appuser -ENV USER=rel +ENV USER=pr ENV UID=10001 RUN adduser \ @@ -16,28 +18,21 @@ RUN adduser \ "${USER}" -WORKDIR /kube - COPY src src -COPY Cargo.toml Cargo.lock ./ - -RUN cargo build --release +COPY Cargo.toml Cargo.toml +RUN cargo build --target x86_64-unknown-linux-musl --release -#################################################################################################### -## Final image -#################################################################################################### -FROM gcr.io/distroless/cc +FROM scratch -# Import from builder. COPY --from=builder /etc/passwd /etc/passwd COPY --from=builder /etc/group /etc/group -WORKDIR /kube +WORKDIR /pr + +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ -# Copy our build -COPY --from=builder /kube/target/release/release_notifier ./ +COPY --from=builder /target/x86_64-unknown-linux-musl/release/release_notifier ./ -# Use an unprivileged user. -USER rel:rel +USER pr:pr -CMD ["/kube/release_notifier"] \ No newline at end of file +ENTRYPOINT [ "/pr/release_notifier" ] diff --git a/README.md b/README.md index 2f952b7..c542ca0 100644 --- a/README.md +++ b/README.md @@ -30,20 +30,22 @@ jobs: runs-on: ubuntu-latest steps: - name: release-notifier-gh - uses: maheshrayas/action-release-notifier@v2.0 + uses: maheshrayas/action-release-notifier@v2.1 with: github_token: '${{ secrets.GITHUB_TOKEN }}' type: Github repo: 'https://github.com/kubernetes/kubernetes,https://github.com/kubernetes-sigs/kustomize,https://github.com/helm/helm,https://github.com/istio/istio' days: 1 #optional field, default 1 day, , make sure you set the cron appropriately, Example if you want to check for release once in 7 days, set days: 7 and cron schedule to run once in 7 days + labels: 'release' - name: release-notifier-rss - uses: maheshrayas/action-release-notifier@v2.0 + uses: maheshrayas/action-release-notifier@v2.1 with: github_token: '${{ secrets.GITHUB_TOKEN }}' type: Rss repo: 'https://cloud.google.com/feeds/anthosconfig-release-notes.xml' days: 1 #optional field, default 1 day, , make sure you set the cron appropriately, Example if you want to check for release once in 7 days, set days: 7 and cron schedule to run once in 7 days + labels: 'release,google' ``` ## Notification diff --git a/action.yml b/action.yml index 7c6f50c..b1788aa 100644 --- a/action.yml +++ b/action.yml @@ -13,10 +13,12 @@ inputs: type: description: 'release type: Github or Rss' required: true + labels: + description: 'add desired label to GH Issue' runs: using: 'docker' - image: 'docker://maheshrayas/release-notifier:v2.0' + image: 'docker://maheshrayas/release-notifier:v2.1' branding: icon: 'zoom-in' color: 'purple' \ No newline at end of file diff --git a/src/gh.rs b/src/gh.rs index 80f1b1e..31062e2 100644 --- a/src/gh.rs +++ b/src/gh.rs @@ -2,7 +2,8 @@ use anyhow::{anyhow, Result}; use regex::{Captures, Regex}; use reqwest::{header::HeaderMap, Client}; use serde::Deserialize; -use std::{collections::HashMap, env}; +use serde_json::{Map, Value}; +use std::env; use tracing::info; #[derive(Deserialize, Debug, Default)] @@ -38,6 +39,7 @@ pub(crate) struct Issue<'a> { pub(crate) release_body: &'a str, pub(crate) repo_name: &'a str, pub(crate) release_version: &'a str, + pub(crate) labels: &'a Option, } impl<'a> Issue<'a> { @@ -48,6 +50,7 @@ impl<'a> Issue<'a> { release_body: &'a str, repo_name: &'a str, release_version: &'a str, + labels: &'a Option, ) -> Self { Issue { headers, @@ -56,42 +59,51 @@ impl<'a> Issue<'a> { release_body, repo_name, release_version, + labels, } } pub async fn create_issue(&self) -> Result<()> { - let mut map = HashMap::new(); + let mut map = Map::new(); let he = &self.headers.to_owned(); let github_org: &str = &env::var("GITHUB_REPOSITORY").expect("Missing input parameter: repo"); let current_repo: Vec<&str> = github_org.split('/').collect(); map.insert( - "title", - format!( + "title".to_string(), + Value::String(format!( "New version of {} {} available", &self.repo_name, &self.release_version - ), + )), ); map.insert( - "body", - format!( + "body".to_string(), + Value::String(format!( " Upstream new release {} available at {} \n\n **Release Details:** \n {}", &self.repo_name, &self.release_url, &self.release_body - ), + )), ); + if let Some(labels) = &self.labels { + let m: Vec = labels + .split(',') + .map(|s| Value::String(s.to_string())) + .collect(); + map.insert("labels".to_string(), Value::Array(m)); + } + let url = format!( "https://api.github.com/repos/{}/{}/issues", current_repo[0], current_repo[1] ); + let issue_response = self .client .post(url) .headers(he.to_owned()) - .json(&map) + .json(&serde_json::json!(&map)) .send() .await?; - println!("issue_response.status() {}", issue_response.status()); if issue_response.status() == 201 { info!( "Successfully issue created with details for repo: {}", diff --git a/src/main.rs b/src/main.rs index 9d26c6d..4403590 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,8 +27,13 @@ async fn process() -> Result<()> { ); let input_type = env::var("INPUT_TYPE").expect("Missing input parameter: type"); + let labels: Option = match env::var("INPUT_LABELS") { + Ok(labels) => Some(labels), + Err(_) => None, + }; + // intialize the struct - let input = Input::new(input_type, token, repo, days); + let input = Input::new(input_type, token, repo, days, labels); let m = match input.input_type { InputType::Github => input.gh().await?, @@ -37,8 +42,6 @@ async fn process() -> Result<()> { Ok(m) } - - #[tokio::test] async fn test_gh() { use std::time::Instant; @@ -48,7 +51,7 @@ async fn test_gh() { // env::set_var("INPUT_DAYS", "4"); env::set_var("GITHUB_REPOSITORY", "maheshrayas/action-release-notifier"); env::set_var("INPUT_TYPE", "Github"); - + env::set_var("INPUT_LABELS", "release,google"); env::set_var("INPUT_GITHUB_TOKEN", gh_token); if let Err(_) = process().await { panic!("Failed",); @@ -57,17 +60,19 @@ async fn test_gh() { println!("Time taken for execution is: {:?}", duration); } - #[tokio::test] async fn test_rss() { use std::time::Instant; let start = Instant::now(); let gh_token = &env::var("TOKEN").unwrap(); - env::set_var("INPUT_REPO", "https://cloud.google.com/feeds/anthosconfig-release-notes.xml"); + env::set_var( + "INPUT_REPO", + "https://cloud.google.com/feeds/anthosconfig-release-notes.xml", + ); // env::set_var("INPUT_DAYS", "9"); env::set_var("GITHUB_REPOSITORY", "maheshrayas/action-release-notifier"); env::set_var("INPUT_TYPE", "Rss"); - + env::set_var("INPUT_DAYS", "7"); env::set_var("INPUT_GITHUB_TOKEN", gh_token); if let Err(_) = process().await { panic!("Failed",); diff --git a/src/util.rs b/src/util.rs index 72fb8e5..c33762c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -32,16 +32,18 @@ pub struct Input { pub input_type: InputType, pub repo: String, pub days: i64, + pub labels: Option, } impl Input { - pub fn new(ty: String, token: String, repo: String, days: i64) -> Self { + pub fn new(ty: String, token: String, repo: String, days: i64, labels: Option) -> Self { let input_type = InputType::from_str(&ty).unwrap(); Input { input_type, token, repo, days, + labels, } } @@ -98,6 +100,7 @@ impl Input { &rbody.body, parsed_repo, &rbody.tag_name, + &self.labels, ); issue.create_issue().await?; info!("New release found and github issue created"); @@ -160,6 +163,7 @@ impl Input { &body, &title, "", + &self.labels, ); issue.create_issue().await? } else {