Skip to content

Allow to specify branch for repository widget #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/glance/templates/repository.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

{{ if gt (len .Repository.Commits) 0 }}
<hr class="margin-block-8">
<a class="text-compact" href="https://github.com/{{ $.Repository.Name }}/commits" target="_blank" rel="noreferrer">Last {{ .CommitsLimit }} commits</a>
<a class="text-compact" href="https://github.com/{{ $.Repository.Name }}/commits/{{ .RepositoryBranch}}" target="_blank" rel="noreferrer">Last {{ .CommitsLimit }} commits <span class="color-primary">{{ .RepositoryBranch }}</span></a>
<div class="flex gap-7 size-h5 margin-top-3">
<ul class="list list-gap-2">
{{ range .Repository.Commits }}
Expand Down
14 changes: 10 additions & 4 deletions internal/glance/widget-repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var repositoryWidgetTemplate = mustParseTemplate("repository.html", "widget-base
type repositoryWidget struct {
widgetBase `yaml:",inline"`
RequestedRepository string `yaml:"repository"`
RepositoryBranch string `yaml:"branch"`
Token string `yaml:"token"`
PullRequestsLimit int `yaml:"pull-requests-limit"`
IssuesLimit int `yaml:"issues-limit"`
Expand Down Expand Up @@ -43,6 +44,7 @@ func (widget *repositoryWidget) initialize() error {
func (widget *repositoryWidget) update(ctx context.Context) {
details, err := fetchRepositoryDetailsFromGithub(
widget.RequestedRepository,
widget.RepositoryBranch,
string(widget.Token),
widget.PullRequestsLimit,
widget.IssuesLimit,
Expand Down Expand Up @@ -111,16 +113,20 @@ type gitHubCommitResponseJson struct {
} `json:"commit"`
}

func fetchRepositoryDetailsFromGithub(repo string, token string, maxPRs int, maxIssues int, maxCommits int) (repository, error) {
func fetchRepositoryDetailsFromGithub(repo string, branch string, token string, maxPRs int, maxIssues int, maxCommits int) (repository, error) {
repositoryRequest, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s", repo), nil)
if err != nil {
return repository{}, fmt.Errorf("%w: could not create request with repository: %v", errNoContent, err)
}

var repoUrl string
PRsRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/search/issues?q=is:pr+is:open+repo:%s&per_page=%d", repo, maxPRs), nil)
issuesRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/search/issues?q=is:issue+is:open+repo:%s&per_page=%d", repo, maxIssues), nil)
CommitsRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s/commits?per_page=%d", repo, maxCommits), nil)

if branch != "" {
repoUrl = fmt.Sprintf("https://api.github.com/repos/%s/commits?sha=%s&per_page=%d", repo, branch, maxCommits)
} else {
repoUrl = fmt.Sprintf("https://api.github.com/repos/%s/commits?per_page=%d", repo, maxCommits)
}
CommitsRequest, _ := http.NewRequest("GET", repoUrl, nil)
if token != "" {
token = fmt.Sprintf("Bearer %s", token)
repositoryRequest.Header.Add("Authorization", token)
Expand Down