Skip to content

Commit

Permalink
feat: Support labels
Browse files Browse the repository at this point in the history
  • Loading branch information
rayasm1 authored and maheshrayas committed May 31, 2022
1 parent 5f3f9bf commit 4611836
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 38 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
29 changes: 12 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand All @@ -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"]
ENTRYPOINT [ "/pr/release_notifier" ]
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
32 changes: 22 additions & 10 deletions src/gh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<String>,
}

impl<'a> Issue<'a> {
Expand All @@ -48,6 +50,7 @@ impl<'a> Issue<'a> {
release_body: &'a str,
repo_name: &'a str,
release_version: &'a str,
labels: &'a Option<String>,
) -> Self {
Issue {
headers,
Expand All @@ -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<Value> = 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: {}",
Expand Down
19 changes: 12 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ async fn process() -> Result<()> {
);
let input_type = env::var("INPUT_TYPE").expect("Missing input parameter: type");

let labels: Option<String> = 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?,
Expand All @@ -37,8 +42,6 @@ async fn process() -> Result<()> {
Ok(m)
}



#[tokio::test]
async fn test_gh() {
use std::time::Instant;
Expand All @@ -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",);
Expand All @@ -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",);
Expand Down
6 changes: 5 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ pub struct Input {
pub input_type: InputType,
pub repo: String,
pub days: i64,
pub labels: Option<String>,
}

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<String>) -> Self {
let input_type = InputType::from_str(&ty).unwrap();
Input {
input_type,
token,
repo,
days,
labels,
}
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -160,6 +163,7 @@ impl Input {
&body,
&title,
"",
&self.labels,
);
issue.create_issue().await?
} else {
Expand Down

0 comments on commit 4611836

Please sign in to comment.