Skip to content

Commit

Permalink
Merge pull request #12 from udzura/releases
Browse files Browse the repository at this point in the history
Releases
  • Loading branch information
udzura authored Jul 1, 2021
2 parents a506dd9 + 38a2a4e commit 47bf86d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "octx"
version = "0.4.3"
version = "0.4.4"
authors = ["Uchio Kondo <[email protected]>"]
repository = "https://github.com/udzura/octx"
keywords = ["github", "cli", "etl"]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod commits;
pub mod events;
pub mod issues;
pub mod labels;
pub mod releases;
pub mod users;
pub mod users_detailed;

Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::io;
extern crate octx;
use octx::{
comments::CommentFetcher, commits::CommitFetcher, events::IssueEventFetcher,
issues::IssueFetcher, labels::LabelFetcher, users::UserFetcher,
issues::IssueFetcher, labels::LabelFetcher, releases::ReleaseFetcher, users::UserFetcher,
users_detailed::UserDetailedFetcher,
};

Expand Down Expand Up @@ -39,6 +39,9 @@ struct Command {
/// Extract issue labels
#[structopt(long = "labels")]
target_labels: bool,
/// Extract releases
#[structopt(long = "releases")]
target_releases: bool,
/// Extract users with detailed info - owner/name is not required. this option takes some more minutes
#[structopt(long = "users-detailed")]
target_users_detailed: bool,
Expand Down Expand Up @@ -123,6 +126,10 @@ async fn main() -> octocrab::Result<()> {
info!("Target: labels");
let runner = LabelFetcher::new(owner, name, octocrab);
runner.fetch(wtr).await?;
} else if args.target_releases {
info!("Target: releases");
let runner = ReleaseFetcher::new(owner, name, octocrab);
runner.fetch(wtr).await?;
} else {
error!("No target specified");
}
Expand Down
119 changes: 119 additions & 0 deletions src/releases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use chrono::{DateTime, Utc};
use octocrab::models::repos::*;
use reqwest::Url;
use serde::*;

use crate::*;

#[derive(Serialize, Debug)]
pub struct ReleaseRec {
pub url: Url,
pub html_url: Url,
pub assets_url: Url,
pub upload_url: Url,
pub tarball_url: Url,
pub zipball_url: Url,
pub id: i64,
pub node_id: String,
pub tag_name: String,
pub target_commitish: String,
pub name: Option<String>,
pub body: Option<String>,
pub draft: bool,
pub prerelease: bool,
pub created_at: DateTime<Utc>,
pub published_at: DateTime<Utc>,
pub author_id: i64,
pub assets: String,

pub sdc_repository: String,
}

impl From<Release> for ReleaseRec {
fn from(from: Release) -> Self {
Self {
url: from.url,
html_url: from.html_url,
assets_url: from.assets_url,
upload_url: from.upload_url,
tarball_url: from.tarball_url,
zipball_url: from.zipball_url,
id: from.id,
node_id: from.node_id,
tag_name: from.tag_name,
target_commitish: from.target_commitish,
name: from.name,
body: from.body,
draft: from.draft,
prerelease: from.prerelease,
created_at: from.created_at,
published_at: from.published_at,
author_id: from.author.id,
assets: from
.assets
.iter()
.map(|v| format!("{};{};{}", v.id, v.name, v.browser_download_url))
.collect::<Vec<String>>()
.join(","),

sdc_repository: String::default(),
}
}
}

impl RepositryAware for ReleaseRec {
fn set_repository(&mut self, name: String) {
self.sdc_repository = name;
}
}

pub struct ReleaseFetcher {
owner: String,
name: String,
octocrab: octocrab::Octocrab,
}

impl ReleaseFetcher {
pub fn new(owner: String, name: String, octocrab: octocrab::Octocrab) -> Self {
Self {
owner,
name,
octocrab,
}
}
}

impl UrlConstructor for ReleaseFetcher {
fn reponame(&self) -> String {
format!("{}/{}", self.owner, self.name)
}

fn entrypoint(&self) -> Option<Url> {
let param = Params::default();

let route = format!(
"repos/{owner}/{repo}/releases?{query}",
owner = &self.owner,
repo = &self.name,
query = param.to_query(),
);
self.octocrab.absolute_url(route).ok()
}
}

impl LoopWriter for ReleaseFetcher {
type Model = Release;
type Record = ReleaseRec;
}

impl ReleaseFetcher {
pub async fn fetch<T: std::io::Write>(&self, mut wtr: csv::Writer<T>) -> octocrab::Result<()> {
let mut next: Option<Url> = self.entrypoint();

while let Some(page) = self.octocrab.get_page(&next).await? {
next = self.write_and_continue(page, &mut wtr);
}

Ok(())
}
}

0 comments on commit 47bf86d

Please sign in to comment.