Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit daadf5a

Browse files
committed
support using as in action
1 parent 7fd687a commit daadf5a

9 files changed

Lines changed: 235 additions & 30 deletions

File tree

.github/workflows/release.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Release
2+
on:
3+
push:
4+
tags:
5+
- '**'
6+
7+
permissions:
8+
contents: write
9+
packages: write
10+
11+
jobs:
12+
goreleaser:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
with:
17+
fetch-depth: 0
18+
- run: git fetch --force --tags
19+
- uses: actions/setup-go@v3
20+
with:
21+
go-version: '1.20.x'
22+
cache: true
23+
- uses: goreleaser/goreleaser-action@v4
24+
with:
25+
distribution: goreleaser
26+
version: latest
27+
args: release --clean
28+
env:
29+
# Custom secret here since we need to access to j178/homebrew-tap and j178/scoop-bucket repo.
30+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/

.goreleaser.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
before:
2+
hooks:
3+
- go mod tidy
4+
builds:
5+
- main: ./cmd/github-s3
6+
env:
7+
- CGO_ENABLED=0
8+
goos:
9+
- linux
10+
- windows
11+
- darwin
12+
goarch:
13+
- amd64
14+
- arm64
15+
flags:
16+
- -v
17+
- -trimpath
18+
ldflags:
19+
- -s
20+
- -w
21+
22+
archives:
23+
- format: tar.gz
24+
name_template: >-
25+
{{ .ProjectName }}_
26+
{{- title .Os }}_
27+
{{- if eq .Arch "amd64" }}x86_64
28+
{{- else if eq .Arch "386" }}i386
29+
{{- else }}{{ .Arch }}{{ end }}
30+
{{- if .Arm }}v{{ .Arm }}{{ end }}
31+
format_overrides:
32+
- goos: windows
33+
format: zip
34+
checksum:
35+
name_template: 'checksums.txt'
36+
snapshot:
37+
name_template: "{{ incpatch .Version }}-next"
38+
changelog:
39+
sort: asc
40+
filters:
41+
exclude:
42+
- '^docs:'
43+
- '^test:'
44+
brews:
45+
- repository:
46+
owner: j178
47+
name: homebrew-tap
48+
commit_author:
49+
name: goreleaserbot
50+
email: bot@goreleaser.com
51+
homepage: https://github.com/j178/github-s3
52+
description: Use GitHub as a file server.
53+
license: MIT
54+
55+
scoops:
56+
- repository:
57+
owner: j178
58+
name: scoop-bucket
59+
commit_author:
60+
name: goreleaserbot
61+
email: bot@goreleaser.com
62+
folder: bucket
63+
homepage: https://github.com/j178/github-s3
64+
description: Use GitHub as a file server.
65+
license: MIT
66+
67+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
68+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ go install github.com/j178/github-s3/cmd/github-s3-auto@latest
2424
github-s3-auto <path-to-file>
2525
```
2626
27+
### Use in GitHub Actions
28+
29+
```yaml
30+
jobs:
31+
build:
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: Upload files to GitHub
35+
uses: j178/github-s3@master
36+
env:
37+
GITHUB_USER_SESSION: ${{ secrets.GITHUB_USER_SESSION }}
38+
with:
39+
files: <list-of-paths-to-file>
40+
- name: Use uploaded files
41+
run: |
42+
# Use the uploaded files
43+
with:
44+
files: ${{ steps.upload.outputs.links }}
45+
```
46+
2747
## Disclaimer
2848
2949
Please note that this project relies on an unpublicized API of GitHub, and its usage may be subject to changes in GitHub's policies or API. Use it responsibly and ensure compliance with GitHub's terms of service.

action.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: github-s3
2+
description: Uploads a file to GitHub using attachments API
3+
inputs:
4+
repo:
5+
description: 'The repository to upload to, defaults to the current repository'
6+
required: false
7+
default: ${{ github.repository }}
8+
session:
9+
description: 'The GitHub user session cookie'
10+
required: true
11+
files:
12+
description: 'The files to upload'
13+
required: true
14+
outputs:
15+
links:
16+
description: 'The links to the uploaded files'
17+
value: ${{ steps.upload.outputs.links }}
18+
runs:
19+
using: composite
20+
steps:
21+
- name: Install github-s3
22+
shell: bash
23+
run: |
24+
curl -sSL --output /tmp/github-s3.tar.gz https://github.com/j178/github-s3/releases/latest/download/github-s3_Linux_x86_64.tar.gz && \
25+
tar -xvf /tmp/github-s3.tar.gz -C /tmp && \
26+
chmod +x /tmp/github-s3
27+
- name: Upload
28+
id: upload
29+
shell: bash
30+
run: |
31+
links=$(/tmp/github-s3 upload -repo ${{ inputs.repo }} ${{ inputs.files }})
32+
echo "links=$links" >> $GITHUB_OUTPUT
33+
env:
34+
GITHUB_SESSION: ${{ inputs.session }}

cmd/github-s3-auto/go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/j178/github-s3/cmd/github-s3-auto
22

3-
go 1.20
3+
go 1.21
4+
5+
toolchain go1.21.0
46

57
require (
68
github.com/j178/github-s3 v0.0.0-20230806042728-2f82dd2f6817

cmd/github-s3-auto/main.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"os"
67

78
githubs3 "github.com/j178/github-s3"
89
"github.com/j178/kooky"
9-
10-
_ "github.com/j178/kooky/browser/chrome"
11-
_ "github.com/j178/kooky/browser/edge"
12-
_ "github.com/j178/kooky/browser/firefox"
13-
_ "github.com/j178/kooky/browser/safari"
1410
)
1511

12+
var repo = flag.String("repo", "", "github repo name")
13+
1614
func main() {
1715
if len(os.Args) < 2 {
18-
fmt.Println("Usage: github-s3-auto <file-path>")
16+
fmt.Println("Usage: github-s3 <file-path>")
1917
os.Exit(1)
2018
}
19+
flag.Parse()
20+
if *repo == "" {
21+
*repo = "cli/cli"
22+
}
2123

2224
cookies := kooky.ReadCookies(kooky.Domain("github.com"), kooky.Name("user_session"))
2325
if len(cookies) == 0 {
2426
fmt.Println("No github cookies found")
2527
os.Exit(1)
2628
}
2729

28-
gh := githubs3.New(cookies[0].Value)
29-
loc, err := gh.UploadFromPath(os.Args[1])
30-
if err != nil {
31-
fmt.Println(err)
32-
os.Exit(1)
30+
gh := githubs3.New(cookies[0].Value, githubs3.WithRepo(*repo))
31+
32+
for _, path := range os.Args[1:] {
33+
res, err := gh.UploadFromPath(path)
34+
if err != nil {
35+
fmt.Println(err)
36+
}
37+
fmt.Println(res.GithubLink)
3338
}
34-
fmt.Println(loc.GithubLink)
3539
}

cmd/github-s3/main.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"os"
67

78
githubs3 "github.com/j178/github-s3"
89
)
910

11+
var repo = flag.String("repo", "", "github repo name")
12+
1013
func main() {
11-
if len(os.Args) < 3 {
12-
fmt.Println("Usage: github-s3 <github-user-session> <file-path>")
14+
if len(os.Args) < 2 {
15+
fmt.Println("Usage: github-s3 <file-path>")
1316
os.Exit(1)
1417
}
18+
flag.Parse()
19+
if *repo == "" {
20+
*repo = "cli/cli"
21+
}
1522

16-
gh := githubs3.New(os.Args[1])
17-
loc, err := gh.UploadFromPath(os.Args[2])
18-
if err != nil {
19-
fmt.Println(err)
23+
session := os.Getenv("GITHUB_SESSION")
24+
if session == "" {
25+
fmt.Println("GITHUB_SESSION env var is required")
2026
os.Exit(1)
2127
}
22-
fmt.Println(loc.GithubLink)
28+
gh := githubs3.New(session, githubs3.WithRepo(*repo))
29+
30+
for _, path := range os.Args[1:] {
31+
res, err := gh.UploadFromPath(path)
32+
if err != nil {
33+
fmt.Println(err)
34+
}
35+
fmt.Println(res.GithubLink)
36+
}
2337
}

github.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@ import (
1616

1717
type GitHub struct {
1818
c *resty.Client
19+
repo string
20+
repoId int
1921
authenticityToken string
20-
repositoryId string
2122
}
2223

23-
func New(userSession string) *GitHub {
24+
type Option func(*GitHub)
25+
26+
func WithRepo(repo string) Option {
27+
return func(g *GitHub) {
28+
g.repo = repo
29+
}
30+
}
31+
32+
func New(userSession string, opts ...Option) *GitHub {
33+
g := &GitHub{}
34+
for _, opt := range opts {
35+
opt(g)
36+
}
37+
2438
c := resty.New()
2539
u, _ := url.Parse("https://github.com")
2640
// Set cookies to jar avoid leaking to other sites
@@ -42,18 +56,15 @@ func New(userSession string) *GitHub {
4256
c.SetRedirectPolicy(resty.NoRedirectPolicy())
4357
c.SetContentLength(true)
4458
c.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36")
45-
c.SetHeader("Referer", "https://github.com/cli/cli/issues/7797")
46-
return &GitHub{
47-
c: c,
48-
// repositoryId doesn't matter, use cli/cli as default
49-
repositoryId: "212613049",
50-
}
59+
g.c = c
60+
61+
return g
5162
}
5263

5364
var tokenPattern = regexp.MustCompile(`<file-attachment class="js-upload-markdown-image.*?<input type="hidden" value="([^{"]+?)" data-csrf="true"`)
5465

5566
func (g *GitHub) fetchAuthenticityToken() (string, error) {
56-
resp, err := g.c.R().Get("https://github.com/cli/cli/issues/new?assignees=&labels=bug&projects=&template=bug_report.md")
67+
resp, err := g.c.R().Get(fmt.Sprintf("https://github.com/%s/issues/new", g.repo))
5768
if err != nil {
5869
return "", err
5970
}
@@ -68,6 +79,20 @@ func (g *GitHub) fetchAuthenticityToken() (string, error) {
6879
return matches[1], nil
6980
}
7081

82+
func (g *GitHub) getRepoId() (int, error) {
83+
var result struct {
84+
ID int `json:"id"`
85+
}
86+
resp, err := g.c.R().SetResult(&result).Get("https://api.github.com/repos/" + g.repo)
87+
if err != nil {
88+
return 0, err
89+
}
90+
if !resp.IsSuccess() {
91+
return 0, fmt.Errorf("failed to get repo id: %s", resp.Status())
92+
}
93+
return result.ID, nil
94+
}
95+
7196
type preUploadResult struct {
7297
UploadUrl string `json:"upload_url"`
7398
UploadAuthenticityToken string `json:"upload_authenticity_token"`
@@ -94,12 +119,19 @@ func (g *GitHub) preUpload(name string, size int, contentType string) (*preUploa
94119
}
95120
g.authenticityToken = token
96121
}
122+
if g.repoId == 0 {
123+
repoId, err := g.getRepoId()
124+
if err != nil {
125+
return nil, err
126+
}
127+
g.repoId = repoId
128+
}
97129

98130
var result preUploadResult
99131
resp, err := g.c.R().
100132
SetMultipartFormData(map[string]string{
101133
"authenticity_token": g.authenticityToken,
102-
"repository_id": g.repositoryId,
134+
"repository_id": strconv.Itoa(g.repoId),
103135
"name": name,
104136
"size": strconv.Itoa(size),
105137
"content_type": contentType,

0 commit comments

Comments
 (0)