Skip to content

Commit

Permalink
internal/task: upload generated package extension to release asset
Browse files Browse the repository at this point in the history
Add a new method UploadReleaseAsset to GitHubClient interface. The
method can upload a file in form of fs.File to a GitHub release as a
release asset.

A local relui screenshot is at golang/vscode-go#3500 (comment)

For golang/vscode-go#3500

Change-Id: I16f48573fbb2e3c76d8c5c91bc80ab09c7653e25
Reviewed-on: https://go-review.googlesource.com/c/build/+/613596
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Auto-Submit: Hongxiang Jiang <[email protected]>
  • Loading branch information
h9jiang authored and gopherbot committed Sep 17, 2024
1 parent 591f2af commit 31c4176
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
4 changes: 4 additions & 0 deletions internal/task/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,10 @@ func (f *FakeGitHub) FetchMilestoneIssues(_ context.Context, owner, repo string,
return issueLabels, nil
}

func (*FakeGitHub) UploadReleaseAsset(ctx context.Context, owner, repo string, releaseID int64, fileName string, file fs.File) (*github.ReleaseAsset, error) {
return nil, nil
}

func (*FakeGitHub) CreateRelease(ctx context.Context, owner, repo string, release *github.RepositoryRelease) (*github.RepositoryRelease, error) {
return nil, nil
}
Expand Down
40 changes: 40 additions & 0 deletions internal/task/milestones.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package task

import (
"context"
"errors"
"fmt"
"io/fs"
"net/url"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -318,6 +321,18 @@ type GitHubClientInterface interface {

// See github.Client.Repositories.CreateRelease.
CreateRelease(ctx context.Context, owner, repo string, release *github.RepositoryRelease) (*github.RepositoryRelease, error)

// UploadReleaseAsset uploads an fs.File to a GitHub release as a release
// asset.
// It uses NewUploadRequest as github.Client.Repositories.UploadReleaseAsset
// only supports uploading from an os.File.
// Parameters:
// - owner: The account owner of the repository.
// - repo: The name of the repository.
// - releaseID: The ID of the github release.
// - fileName: The name of the asset as it will appear in the release.
// - file: The content of the file to upload.
UploadReleaseAsset(ctx context.Context, owner, repo string, releaseID int64, fileName string, file fs.File) (*github.ReleaseAsset, error)
}

type GitHubClient struct {
Expand All @@ -344,6 +359,31 @@ func (c *GitHubClient) FetchMilestone(ctx context.Context, owner, repo, name str
return *m.Number, nil
}

func (c *GitHubClient) UploadReleaseAsset(ctx context.Context, owner, repo string, releaseID int64, fileName string, file fs.File) (*github.ReleaseAsset, error) {
// Query parameter "name" is used to determine the asset name.
// See details https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#upload-a-release-asset
u := fmt.Sprintf("repos/%s/%s/releases/%d/assets?name=%s", owner, repo, releaseID, url.QueryEscape(fileName))

stat, err := file.Stat()
if err != nil {
return nil, err
}
if stat.IsDir() {
return nil, errors.New("the asset to upload can't be a directory")
}

req, err := c.V3.NewUploadRequest(u, file, stat.Size(), "")
if err != nil {
return nil, err
}

asset := new(github.ReleaseAsset)
if _, err = c.V3.Do(ctx, req, asset); err != nil {
return nil, err
}
return asset, nil
}

func (c *GitHubClient) CreateRelease(ctx context.Context, owner, repo string, release *github.RepositoryRelease) (*github.RepositoryRelease, error) {
release, _, err := c.V3.Repositories.CreateRelease(ctx, owner, repo, release)
return release, err
Expand Down
34 changes: 28 additions & 6 deletions internal/task/releasevscodego.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,12 @@ go run tools/release/release.go package &> go-output.log
cat npm-output.log
cat go-output.log
`

versionString := release.String()
if prerelease != "" {
versionString += "-" + prerelease
}
// The version inside of vsix does not have prefix "v".
vsix := fmt.Sprintf("go-%s.vsix", versionString[1:])

saveScript := cloudBuildClientScriptPrefix
for _, file := range []string{"npm-output.log", "go-output.log", vsix} {
for _, file := range []string{"npm-output.log", "go-output.log", vsixFileName(release, prerelease)} {
saveScript += fmt.Sprintf("gsutil cp %s %s/%s\n", file, resultURL, file)
}
return []*cloudbuildpb.BuildStep{
Expand Down Expand Up @@ -406,6 +402,15 @@ func (r *ReleaseVSCodeGoTasks) nextPrereleaseVersion(ctx *wf.TaskContext, releas
return fmt.Sprintf("rc.%v", pre+1), nil
}

func vsixFileName(release releaseVersion, prerelease string) string {
versionString := release.String()
if prerelease != "" {
versionString += "-" + prerelease
}
// The version inside of vsix does not have prefix "v".
return fmt.Sprintf("go-%s.vsix", versionString[1:])
}

func (r *ReleaseVSCodeGoTasks) createGitHubReleaseAsDraft(ctx *wf.TaskContext, release releaseVersion, prerelease string, build CloudBuild) error {
tags, err := r.Gerrit.ListTags(ctx, "vscode-go")
if err != nil {
Expand Down Expand Up @@ -438,7 +443,24 @@ func (r *ReleaseVSCodeGoTasks) createGitHubReleaseAsDraft(ctx *wf.TaskContext, r
if err != nil {
return err
}
ctx.Printf("Find the draft release note in %s", draft.GetHTMLURL())
ctx.Printf("Created the draft release note in %s", draft.GetHTMLURL())

outFS, err := r.CloudBuild.ResultFS(ctx, build)
if err != nil {
return err
}

file, err := outFS.Open(vsixFileName(release, prerelease))
if err != nil {
return err
}

asset, err := r.GitHub.UploadReleaseAsset(ctx, "golang", "vscode-go", draft.GetID(), vsixFileName(release, prerelease), file)
if err != nil {
return err
}

ctx.Printf("Uploaded asset %s to release %v as asset ID %v", asset.GetName(), draft.GetID(), asset.GetID())
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/task/template/vscode-go-pre-release-note.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
**Milestone**: https://github.com/golang/vscode-go/issues?q=milestone%%3A%s

* How to test prerelease
* Download the .vsix file from this Releases page.
* Navigate to the Extensions view in VS Code (Ctrl+Shift+X). Click on the "..." in the top-right corner, select "Install from VSIX", and select the .vsix file you downloaded. Alternatively, you can run code --install-extension Go-latest.vsix or open the Command Palette and run the "Extensions: Install from VSIX..." command.
* Download the `.vsix` file from this Releases page.
* Navigate to the Extensions view in VS Code (`Ctrl+Shift+X`). Click on the "..." in the top-right corner, select "Install from VSIX", and select the `.vsix` file you downloaded. Alternatively, you can run `code --install-extension Go-latest.vsix` or open the Command Palette and run the "Extensions: Install from VSIX..." command.
* If prompted, reload VS Code.

0 comments on commit 31c4176

Please sign in to comment.