import os
import requests
owner = os.environ["OWNER"]
repo = os.environ["REPO"]
token = os.environ["GITHUB_TOKEN"]
url = f"https://api.github.com/repos/{owner}/{repo}/git/refs"
headers = {"Authorization": f"Bearer {token}"}
data = {"ref": "refs/heads/feature-branch", "sha": "abc123def456"}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
print(response.json())
import os
import httpx
owner = os.environ["OWNER"]
repo = os.environ["REPO"]
token = os.environ["GITHUB_TOKEN"]
url = f"https://api.github.com/repos/{owner}/{repo}/git/refs"
headers = {"Authorization": f"Bearer {token}"}
data = {"ref": "refs/heads/feature-branch", "sha": "abc123def456"}
with httpx.Client() as client:
response = client.post(url, headers=headers, json=data)
response.raise_for_status()
print(response.json())
import { Buffer } from 'buffer';
import process from 'process';
const owner = process.env.OWNER;
const repo = process.env.REPO;
const token = process.env.GITHUB_TOKEN;
const url = `https://api.github.com/repos/${owner}/${repo}/git/refs`;
const headers = {
'Authorization': `Bearer ${token}`
};
const body = JSON.stringify({
ref: 'refs/heads/feature-branch',
sha: 'abc123def456'
});
fetch(url, {
method: 'POST',
headers,
body
})
.then(response => {
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
return response.json();
})
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
import axios from 'axios';
import process from 'process';
const owner = process.env.OWNER;
const repo = process.env.REPO;
const token = process.env.GITHUB_TOKEN;
const url = `https://api.github.com/repos/${owner}/${repo}/git/refs`;
const headers = {
'Authorization': `Bearer ${token}`
};
const data = {
ref: 'refs/heads/feature-branch',
sha: 'abc123def456'
};
axios.post(url, data, { headers })
.then(response => {
console.log(response.data);
})
.catch(error => {
if (error.response) {
console.error('Error:', error.response.status, error.response.data);
} else {
console.error('Error:', error.message);
}
});
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
type Ref struct {
Ref string `json:"ref"`
Sha string `json:"sha"`
}
func main() {
owner := os.Getenv("OWNER")
repo := os.Getenv("REPO")
token := os.Getenv("GITHUB_TOKEN")
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/git/refs", owner, repo)
data := Ref{
Ref: "refs/heads/feature-branch",
Sha: "abc123def456",
}
jsonData, _ := json.Marshal(data)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Fatalf("Request failed with status: %s", resp.Status)
}
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
fmt.Println(result)
}
<?php
$owner = getenv('OWNER');
$repo = getenv('REPO');
$token = getenv('GITHUB_TOKEN');
$url = "https://api.github.com/repos/{$owner}/{$repo}/git/refs";
$data = [
'ref' => 'refs/heads/feature-branch',
'sha' => 'abc123def456'
];
$options = [
'http' => [
'header' => "Authorization: Bearer {$token}\r\n",
'method' => 'POST',
'content' => json_encode($data),
'ignore_errors' => true
]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
throw new Exception("Request failed");
}
$response = json_decode($result, true);
print_r($response);
require 'net/http'
require 'json'
require 'uri'
owner = ENV['OWNER']
repo = ENV['REPO']
token = ENV['GITHUB_TOKEN']
uri = URI.parse("https://api.github.com/repos/#{owner}/#{repo}/git/refs")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request['Authorization'] = "Bearer #{token}"
request.body = { ref: 'refs/heads/feature-branch', sha: 'abc123def456' }.to_json
request['Content-Type'] = 'application/json'
response = http.request(request)
unless response.is_a?(Net::HTTPSuccess)
raise "Request failed: #{response.code} #{response.message}"
end
puts JSON.parse(response.body)
import Foundation
let owner = ProcessInfo.processInfo.environment["OWNER"] ?? ""
let repo = ProcessInfo.processInfo.environment["REPO"] ?? ""
let token = ProcessInfo.processInfo.environment["GITHUB_TOKEN"] ?? ""
let url = URL(string: "https://api.github.com/repos/" + owner + "/" + repo + "/git/refs")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Bearer " + token, forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let body: [String: Any] = [
"ref": "refs/heads/feature-branch",
"sha": "abc123def456"
]
request.httpBody = try? JSONSerialization.data(withJSONObject: body)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("Error: \(error)")
return
}
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
print("Request failed")
return
}
if let data = data, let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
print(json)
}
}
task.resume()
RunLoop.main.run()
use std::env;
use reqwest::blocking::Client;
use serde_json::json;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let owner = env::var("OWNER")?;
let repo = env::var("REPO")?;
let token = env::var("GITHUB_TOKEN")?;
let url = format!("https://api.github.com/repos/{}/{}/git/refs", owner, repo);
let client = Client::new();
let body = json!({
"ref": "refs/heads/feature-branch",
"sha": "abc123def456"
});
let response = client
.post(&url)
.header("Authorization", format!("Bearer {}", token))
.json(&body)
.send()?;
if !response.status().is_success() {
return Err(format!("Request failed: {}", response.status()).into());
}
let json: serde_json::Value = response.json()?;
println!("{}", json);
Ok(())
}
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Text.Json;
class Program
{
static async Task Main()
{
var owner = Environment.GetEnvironmentVariable("OWNER");
var repo = Environment.GetEnvironmentVariable("REPO");
var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN");
var url = $"https://api.github.com/repos/{owner}/{repo}/git/refs";
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
request.Content = new StringContent(JsonSerializer.Serialize(new {
@ref = "refs/heads/feature-branch",
sha = "abc123def456"
}));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
throw new Exception($"Request failed: {response.StatusCode}");
}
var json = await response.Content.ReadAsStringAsync();
Console.WriteLine(json);
}
}