0

I'm trying to write a rust script that submits an issue with given name, label and body to a specific repository using github access token. I've found github-rs and octocrab, but can't figure out how this exact function works.

UPDATE Here's the code using rust-curl:

use std::io::Read;
use curl::easy::{Easy, List};

fn main() {
    let mut data = r#"{
        "title": "Found a bug",
        "body": "I'm having a problem with this.",
        "labels": [
          "bug"
        ]
      }"#.as_bytes();
    let mut easy = Easy::new();
    easy.url("https://api.github.com").unwrap();

    let mut list = List::new();
    list.append("Authorization: token TOKEN_HERE").unwrap();
    easy.http_headers(list).unwrap();
    easy.perform().unwrap();
    easy.post(true).unwrap();
    easy.post_field_size(data.len() as u64).unwrap();
    let mut transfer = easy.transfer();
    transfer.read_function(|buf| {
        Ok(data.read(buf).unwrap_or(0))
    }).unwrap();
    transfer.perform().unwrap();
}
swanux
  • 67
  • 7

1 Answers1

0

If those librairies don't include an easy way, you could fall back to alexcrichton/curl-rust

It does let you add custom headers, including the "Authorization: token MY_TOKEN_NUMBERS" you can see used here, which will authenticate you.

From there, your curl call (in rust) can use the GitHub API "create issue" endpoint:

POST /repos/:owner/:repo/issues

with the data:

{
  "title": "Found a bug",
  "body": "I'm having a problem with this.",
  "assignees": [
    "octocat"
  ],
  "milestone": 1,
  "labels": [
    "bug"
  ]
}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for the answer! This workaround may be the best way to do it then. Excuse my stupid question, but how should I do it exactly? I mean, I know how to do it in curl, and the custom header thing seems also okay, I'm just unsure regarding where to put the post request and data (and in what format and how). – swanux Aug 16 '20 at 08:20
  • @swanux You can see an example in grunt-contrib-imagemin/issues/228: the data is a JSON body. – VonC Aug 16 '20 at 09:26
  • I've edited my question with the current code, as I have no idea where to put post method inside of this (checked the issue which you've mentioned, but it didn't really help in my case). – swanux Aug 16 '20 at 10:12
  • btw, here's a working cURL command ```curl -u "MY_USER":"TOKEN" https://api.github.com/repos/OWNER/REPO/issues -d $'{"title": "test issue", "body": "test body", "labels": ["bug"]}'``` I've tried to use reqwest also: program runs and builds, no errors but still doesn't work. – swanux Aug 16 '20 at 13:06
  • @swanux Sorry, I meant https://github.com/alexcrichton/curl-rust/issues/241#issuecomment-451185439 or https://github.com/alexcrichton/curl-rust/issues/155#issuecomment-300543194 as examples. – VonC Aug 16 '20 at 15:26