forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
61 lines (50 loc) · 1.31 KB
/
main.go
File metadata and controls
61 lines (50 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2025 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The uploadreleaseassetfromrelease example demonstrates how to upload
// a release asset using the UploadReleaseAssetFromRelease helper.
package main
import (
"bytes"
"context"
"fmt"
"log"
"os"
"github.com/google/go-github/v81/github"
)
func main() {
token := os.Getenv("GITHUB_AUTH_TOKEN")
if token == "" {
log.Fatal("GITHUB_AUTH_TOKEN not set")
}
ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
owner := "OWNER"
repo := "REPO"
releaseID := int64(1)
// Fetch the release (UploadURL is populated by the API)
release, _, err := client.Repositories.GetRelease(ctx, owner, repo, releaseID)
if err != nil {
log.Fatalf("GetRelease failed: %v", err)
}
// Asset content
data := []byte("Hello from go-github!\n")
reader := bytes.NewReader(data)
size := int64(len(data))
opts := &github.UploadOptions{
Name: "example.txt",
Label: "Example asset",
}
asset, _, err := client.Repositories.UploadReleaseAssetFromRelease(
ctx,
release,
opts,
reader,
size,
)
if err != nil {
log.Fatalf("UploadReleaseAssetFromRelease failed: %v", err)
}
fmt.Printf("Uploaded asset ID: %v\n", asset.GetID())
}