Update repository widget last commits

This commit is contained in:
Svilen Markov 2024-08-29 18:18:38 +01:00
parent c6bfbf4ac1
commit 71ca1753ef
4 changed files with 31 additions and 38 deletions

View File

@ -1216,7 +1216,7 @@ Preview:
| token | string | no | | | token | string | no | |
| pull-requests-limit | integer | no | 3 | | pull-requests-limit | integer | no | 3 |
| issues-limit | integer | no | 3 | | issues-limit | integer | no | 3 |
| commits-limit | integer | no | 3 | | commits-limit | integer | no | -1 |
##### `repository` ##### `repository`
The owner and repository name that will have their information displayed. The owner and repository name that will have their information displayed.
@ -1231,7 +1231,7 @@ The maximum number of latest open pull requests to show. Set to `-1` to not show
The maximum number of latest open issues to show. Set to `-1` to not show any. The maximum number of latest open issues to show. Set to `-1` to not show any.
##### `commits-limit` ##### `commits-limit`
The maximum number of the lastest commit to show. Set to `-1` to not show any. The maximum number of lastest commits to show from the default branch. Set to `-1` to not show any.
### Bookmarks ### Bookmarks
Display a list of links which can be grouped. Display a list of links which can be grouped.

View File

@ -9,16 +9,16 @@
{{ if gt (len .RepositoryDetails.Commits) 0 }} {{ if gt (len .RepositoryDetails.Commits) 0 }}
<hr class="margin-block-10"> <hr class="margin-block-10">
<a class="text-compact" href="https://github.com/{{ $.RepositoryDetails.Name }}/commits" target="_blank" rel="noreferrer">Last {{ .RepositoryDetails.LastCommits | formatNumber }} commits</a> <a class="text-compact" href="https://github.com/{{ $.RepositoryDetails.Name }}/commits" target="_blank" rel="noreferrer">Last {{ .CommitsLimit }} commits</a>
<div class="flex gap-7 size-h5 margin-top-3"> <div class="flex gap-7 size-h5 margin-top-3">
<ul class="list list-gap-2"> <ul class="list list-gap-2">
{{ range .RepositoryDetails.Commits }} {{ range .RepositoryDetails.Commits }}
<li title="{{ .Date | formatTime }}" {{ dynamicRelativeTimeAttrs .Date }}>{{ .Date | relativeTime }}</li> <li {{ dynamicRelativeTimeAttrs .CreatedAt }}></li>
{{ end }} {{ end }}
</ul> </ul>
<ul class="list list-gap-2 min-width-0"> <ul class="list list-gap-2 min-width-0">
{{ range .RepositoryDetails.Commits }} {{ range .RepositoryDetails.Commits }}
<li><a class="color-primary-if-not-visited text-truncate block" title="{{ .Author }}" target="_blank" rel="noreferrer" href="https://github.com/{{ $.RepositoryDetails.Name }}/commits/{{ .Sha }}">{{ .Message }}</a></li> <li><a class="color-primary-if-not-visited text-truncate block" title="{{ .Author }}" target="_blank" rel="noreferrer" href="https://github.com/{{ $.RepositoryDetails.Name }}/commit/{{ .Sha }}">{{ .Message }}</a></li>
{{ end }} {{ end }}
</ul> </ul>
</div> </div>

View File

@ -17,7 +17,6 @@ type githubReleaseLatestResponseJson struct {
} `json:"reactions"` } `json:"reactions"`
} }
func fetchLatestGithubRelease(request *ReleaseRequest) (*AppRelease, error) { func fetchLatestGithubRelease(request *ReleaseRequest) (*AppRelease, error) {
httpRequest, err := http.NewRequest( httpRequest, err := http.NewRequest(
"GET", "GET",
@ -70,7 +69,7 @@ type RepositoryDetails struct {
OpenIssues int OpenIssues int
Issues []GithubTicket Issues []GithubTicket
LastCommits int LastCommits int
Commits []CommitsDetails Commits []CommitDetails
} }
type githubRepositoryDetailsResponseJson struct { type githubRepositoryDetailsResponseJson struct {
@ -88,30 +87,24 @@ type githubTicketResponseJson struct {
} `json:"items"` } `json:"items"`
} }
type CommitsDetails struct { type CommitDetails struct {
Sha string Sha string
Author string Author string
Email string CreatedAt time.Time
Date time.Time Message string
Message string
} }
type Author struct { type gitHubCommitResponseJson struct {
Name string `json:"name"`
Email string `json:"email"`
Date string `json:"date"`
}
type Commit struct {
Sha string `json:"sha"` Sha string `json:"sha"`
Commit struct { Commit struct {
Author Author `json:"author"` Author struct {
Name string `json:"name"`
Date string `json:"date"`
} `json:"author"`
Message string `json:"message"` Message string `json:"message"`
} `json:"commit"` } `json:"commit"`
} }
type githubCommitsResponseJson []Commit
func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs int, maxIssues int, maxCommits int) (RepositoryDetails, error) { func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs int, maxIssues int, maxCommits int) (RepositoryDetails, error) {
repositoryRequest, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s", repository), nil) repositoryRequest, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s", repository), nil)
if err != nil { if err != nil {
@ -136,7 +129,7 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in
var PRsErr error var PRsErr error
var issuesResponse githubTicketResponseJson var issuesResponse githubTicketResponseJson
var issuesErr error var issuesErr error
var CommitsResponse githubCommitsResponseJson var commitsResponse []gitHubCommitResponseJson
var CommitsErr error var CommitsErr error
var wg sync.WaitGroup var wg sync.WaitGroup
@ -166,9 +159,10 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in
wg.Add(1) wg.Add(1)
go (func() { go (func() {
defer wg.Done() defer wg.Done()
CommitsResponse, CommitsErr = decodeJsonFromRequest[githubCommitsResponseJson](defaultClient, CommitsRequest) commitsResponse, CommitsErr = decodeJsonFromRequest[[]gitHubCommitResponseJson](defaultClient, CommitsRequest)
})() })()
} }
wg.Wait() wg.Wait()
if detailsErr != nil { if detailsErr != nil {
@ -181,7 +175,7 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in
Forks: detailsResponse.Forks, Forks: detailsResponse.Forks,
PullRequests: make([]GithubTicket, 0, len(PRsResponse.Tickets)), PullRequests: make([]GithubTicket, 0, len(PRsResponse.Tickets)),
Issues: make([]GithubTicket, 0, len(issuesResponse.Tickets)), Issues: make([]GithubTicket, 0, len(issuesResponse.Tickets)),
Commits: make([]CommitsDetails, 0, len(CommitsResponse)), Commits: make([]CommitDetails, 0, len(commitsResponse)),
} }
err = nil err = nil
@ -223,17 +217,16 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in
if CommitsErr != nil { if CommitsErr != nil {
err = fmt.Errorf("%w: could not get issues: %s", ErrPartialContent, CommitsErr) err = fmt.Errorf("%w: could not get issues: %s", ErrPartialContent, CommitsErr)
} else { } else {
for _, commit := range CommitsResponse { for i := range commitsResponse {
details.LastCommits++ details.Commits = append(details.Commits, CommitDetails{
details.Commits = append(details.Commits, CommitsDetails{ Sha: commitsResponse[i].Sha,
Sha: commit.Sha, Author: commitsResponse[i].Commit.Author.Name,
Author: commit.Commit.Author.Name, CreatedAt: parseRFC3339Time(commitsResponse[i].Commit.Author.Date),
Email: commit.Commit.Author.Email, Message: strings.SplitN(commitsResponse[i].Commit.Message, "\n\n", 2)[0],
Date: parseGithubTime(commit.Commit.Author.Date),
Message: strings.SplitN(commit.Commit.Message, "\n\n", 2)[0],
}) })
} }
} }
} }
return details, err return details, err
} }

View File

@ -15,7 +15,7 @@ type Repository struct {
Token OptionalEnvString `yaml:"token"` Token OptionalEnvString `yaml:"token"`
PullRequestsLimit int `yaml:"pull-requests-limit"` PullRequestsLimit int `yaml:"pull-requests-limit"`
IssuesLimit int `yaml:"issues-limit"` IssuesLimit int `yaml:"issues-limit"`
CommitsLimits int `yaml:"commits-limit"` CommitsLimit int `yaml:"commits-limit"`
RepositoryDetails feed.RepositoryDetails RepositoryDetails feed.RepositoryDetails
} }
@ -30,8 +30,8 @@ func (widget *Repository) Initialize() error {
widget.IssuesLimit = 3 widget.IssuesLimit = 3
} }
if widget.CommitsLimits == 0 || widget.CommitsLimits < -1 { if widget.CommitsLimit == 0 || widget.CommitsLimit < -1 {
widget.CommitsLimits = 3 widget.CommitsLimit = -1
} }
return nil return nil
@ -43,7 +43,7 @@ func (widget *Repository) Update(ctx context.Context) {
string(widget.Token), string(widget.Token),
widget.PullRequestsLimit, widget.PullRequestsLimit,
widget.IssuesLimit, widget.IssuesLimit,
widget.CommitsLimits, widget.CommitsLimit,
) )
if !widget.canContinueUpdateAfterHandlingErr(err) { if !widget.canContinueUpdateAfterHandlingErr(err) {