-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reduce duplicate efforts while loading repository (#133)
* refactor: do not load workflows by default * fix: write version info to commands * refactor: decouple repo info, ref and source * refactor: do not pull same info twice
- Loading branch information
Showing
17 changed files
with
394 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,46 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"dagger.io/dagger" | ||
|
||
"github.com/cli/go-gh/v2" | ||
) | ||
|
||
// Repository represents a GitHub repository | ||
type Repository struct { | ||
ID string | ||
Name string | ||
NameWithOwner string | ||
URL string | ||
Owner RepositoryOwner | ||
DefaultBranchRef RepositoryBranchRef | ||
GitRef *RepositoryGitRef | ||
ID string `json:"id" env:"GALE_REPO_ID" container_env:"true"` | ||
Name string `json:"name" env:"GALE_REPO_NAME" container_env:"true"` | ||
NameWithOwner string `json:"name_with_owner" env:"GALE_REPO_NAME_WITH_OWNER" container_env:"true"` | ||
URL string `json:"url" env:"GALE_REPO_URL" container_env:"true"` | ||
Owner RepositoryOwner `json:"owner"` | ||
DefaultBranchRef RepositoryBranchRef `json:"default_branch_ref"` | ||
} | ||
|
||
// RepositoryOwner represents a GitHub repository owner | ||
type RepositoryOwner struct { | ||
ID string | ||
Login string | ||
ID string `json:"id" env:"GALE_REPO_OWNER_ID" container_env:"true"` | ||
Login string `json:"login" env:"GALE_REPO_OWNER_LOGIN" container_env:"true"` | ||
} | ||
|
||
// RepositoryBranchRef represents a GitHub repository branch ref | ||
type RepositoryBranchRef struct { | ||
Name string | ||
} | ||
|
||
// GetRepositoryOpts represents the options for refs used to get a repository. Only one of the | ||
// options can be set. | ||
// | ||
// If none of the options are set, the default branch will be used. Default branch or remote repositories is configured | ||
// in the GitHub repository settings. For local repositories, it's the branch that is currently checked out. | ||
// | ||
// If multiple options are set, the precedence is as follows: tag, branch. | ||
type GetRepositoryOpts struct { | ||
Branch string | ||
Tag string | ||
} | ||
|
||
// GetCurrentRepository returns current repository information. This is a wrapper around GetRepository with empty name. | ||
func GetCurrentRepository(ctx context.Context, client *dagger.Client, opts ...GetRepositoryOpts) (*Repository, error) { | ||
return GetRepository(ctx, client, "", opts...) | ||
Name string `json:"name" env:"GALE_REPO_BRANCH_NAME" container_env:"true"` | ||
} | ||
|
||
// GetRepository returns repository information. If name is empty, the current repository will be used. | ||
func GetRepository(ctx context.Context, client *dagger.Client, name string, opts ...GetRepositoryOpts) (*Repository, error) { | ||
func GetRepository(name string) (Repository, error) { | ||
var repo Repository | ||
|
||
stdout, stderr, err := gh.Exec("repo", "view", name, "--json", "id,name,owner,nameWithOwner,url,defaultBranchRef") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get current repository: %w stderr: %s", err, stderr.String()) | ||
return repo, fmt.Errorf("failed to get current repository: %w stderr: %s", err, stderr.String()) | ||
} | ||
|
||
err = json.Unmarshal(stdout.Bytes(), &repo) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal current repository: %s err: %w", stdout.String(), err) | ||
} | ||
|
||
opt := GetRepositoryOpts{} | ||
if len(opts) > 0 { | ||
opt = opts[0] | ||
} | ||
|
||
// load repo tree based on the options precedence | ||
switch { | ||
case opt.Tag != "": | ||
repo.GitRef, err = GetRepositoryGitRef(ctx, client, repo.URL, RefTypeTag, opt.Tag) | ||
if err != nil { | ||
return nil, err | ||
} | ||
case opt.Branch != "": | ||
repo.GitRef, err = GetRepositoryGitRef(ctx, client, repo.URL, RefTypeBranch, opt.Branch) | ||
if err != nil { | ||
return nil, err | ||
} | ||
case name != "": | ||
repo.GitRef, err = GetRepositoryGitRef(ctx, client, repo.URL, RefTypeBranch, repo.DefaultBranchRef.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
default: | ||
// TODO: current directory could be a subdirectory of the repository. Should we handle this case? | ||
repo.GitRef, err = GetRepositoryRefFromDir(ctx, client, client.Host().Directory(".")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return repo, fmt.Errorf("failed to unmarshal current repository: %s err: %w", stdout.String(), err) | ||
} | ||
|
||
return &repo, nil | ||
return repo, nil | ||
} |
Oops, something went wrong.