From 90d718c1866207f75a0c9d750df67ab1e9099ffe Mon Sep 17 00:00:00 2001 From: Anthony Harivel Date: Sat, 18 May 2024 11:51:44 +0200 Subject: [PATCH 1/2] Add last Github commits Signed-off-by: Anthony Harivel --- docs/configuration.md | 5 ++ internal/assets/templates/repository.html | 17 +++++++ internal/feed/github.go | 60 +++++++++++++++++++++-- internal/widget/repository-overview.go | 6 +++ 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index e698fdd..ecb4566 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -836,6 +836,7 @@ Example: repository: glanceapp/glance pull-requests-limit: 5 issues-limit: 3 + commits-limit: 3 ``` Preview: @@ -850,6 +851,7 @@ Preview: | token | string | no | | | pull-requests-limit | integer | no | 3 | | issues-limit | integer | no | 3 | +| commits-limit | integer | no | 3 | ##### `repository` The owner and repository name that will have their information displayed. @@ -863,6 +865,9 @@ The maximum number of latest open pull requests to show. Set to `-1` to not show ##### `issues-limit` The maximum number of latest open issues to show. Set to `-1` to not show any. +##### `commits-limit` +The maximum number of the lastest commit to show. Set to `-1` to not show any. + ### Bookmarks Display a list of links which can be grouped. diff --git a/internal/assets/templates/repository.html b/internal/assets/templates/repository.html index 9122a8e..705376a 100644 --- a/internal/assets/templates/repository.html +++ b/internal/assets/templates/repository.html @@ -7,6 +7,23 @@
  • {{ .RepositoryDetails.Forks | formatNumber }} forks
  • +{{ if gt (len .RepositoryDetails.Commits) 0 }} +
    +Last {{ .RepositoryDetails.LastCommits | formatNumber }} commits +
    +
      + {{ range .RepositoryDetails.Commits }} +
    • {{ .Date | relativeTime }}
    • + {{ end }} +
    +
      + {{ range .RepositoryDetails.Commits }} +
    • {{ .Message }}
    • + {{ end }} +
    +
    +{{ end }} + {{ if gt (len .RepositoryDetails.PullRequests) 0 }}
    Open pull requests ({{ .RepositoryDetails.OpenPullRequests | formatNumber }} total) diff --git a/internal/feed/github.go b/internal/feed/github.go index 43a2459..799e61e 100644 --- a/internal/feed/github.go +++ b/internal/feed/github.go @@ -4,6 +4,7 @@ import ( "fmt" "log/slog" "net/http" + "strings" "sync" "time" ) @@ -21,7 +22,6 @@ type githubReleaseResponseJson struct { func parseGithubTime(t string) time.Time { parsedTime, err := time.Parse("2006-01-02T15:04:05Z", t) - if err != nil { return time.Now() } @@ -51,7 +51,6 @@ func FetchLatestReleasesFromGithub(repositories []string, token string) (AppRele task := decodeJsonFromRequestTask[[]githubReleaseResponseJson](defaultClient) job := newJob(task, requests).withWorkers(15) responses, errs, err := workerPoolDo(job) - if err != nil { return nil, err } @@ -131,6 +130,8 @@ type RepositoryDetails struct { PullRequests []GithubTicket OpenIssues int Issues []GithubTicket + LastCommits int + Commits []CommitsDetails } type githubRepositoryDetailsResponseJson struct { @@ -148,21 +149,46 @@ type githubTicketResponseJson struct { } `json:"items"` } -func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs int, maxIssues int) (RepositoryDetails, error) { - repositoryRequest, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s", repository), nil) +type CommitsDetails struct { + Sha string + Author string + Email string + Date time.Time + Message string +} +type Author struct { + Name string `json:"name"` + Email string `json:"email"` + Date string `json:"date"` +} + +type Commit struct { + Sha string `json:"sha"` + Commit struct { + Author Author `json:"author"` + Message string `json:"message"` + } `json:"commit"` +} + +type githubCommitsResponseJson []Commit + +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) if err != nil { return RepositoryDetails{}, fmt.Errorf("%w: could not create request with repository: %v", ErrNoContent, err) } PRsRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/search/issues?q=is:pr+is:open+repo:%s&per_page=%d", repository, maxPRs), nil) issuesRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/search/issues?q=is:issue+is:open+repo:%s&per_page=%d", repository, maxIssues), nil) + CommitsRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s/commits?per_page=%d", repository, maxCommits), nil) if token != "" { token = fmt.Sprintf("Bearer %s", token) repositoryRequest.Header.Add("Authorization", token) PRsRequest.Header.Add("Authorization", token) issuesRequest.Header.Add("Authorization", token) + CommitsRequest.Header.Add("Authorization", token) } var detailsResponse githubRepositoryDetailsResponseJson @@ -171,6 +197,8 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in var PRsErr error var issuesResponse githubTicketResponseJson var issuesErr error + var CommitsResponse githubCommitsResponseJson + var CommitsErr error var wg sync.WaitGroup wg.Add(1) @@ -195,6 +223,13 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in })() } + if maxCommits > 0 { + wg.Add(1) + go (func() { + defer wg.Done() + CommitsResponse, CommitsErr = decodeJsonFromRequest[githubCommitsResponseJson](defaultClient, CommitsRequest) + })() + } wg.Wait() if detailsErr != nil { @@ -207,6 +242,7 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in Forks: detailsResponse.Forks, PullRequests: make([]GithubTicket, 0, len(PRsResponse.Tickets)), Issues: make([]GithubTicket, 0, len(issuesResponse.Tickets)), + Commits: make([]CommitsDetails, 0, len(CommitsResponse)), } err = nil @@ -244,5 +280,21 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in } } + if maxCommits > 0 { + if CommitsErr != nil { + err = fmt.Errorf("%w: could not get issues: %s", ErrPartialContent, CommitsErr) + } else { + for _, commit := range CommitsResponse { + details.LastCommits++ + details.Commits = append(details.Commits, CommitsDetails{ + Sha: commit.Sha, + Author: commit.Commit.Author.Name, + Email: commit.Commit.Author.Email, + Date: parseGithubTime(commit.Commit.Author.Date), + Message: strings.SplitN(commit.Commit.Message, "\n\n", 2)[0], + }) + } + } + } return details, err } diff --git a/internal/widget/repository-overview.go b/internal/widget/repository-overview.go index 85a896c..0edbcc1 100644 --- a/internal/widget/repository-overview.go +++ b/internal/widget/repository-overview.go @@ -15,6 +15,7 @@ type Repository struct { Token OptionalEnvString `yaml:"token"` PullRequestsLimit int `yaml:"pull-requests-limit"` IssuesLimit int `yaml:"issues-limit"` + CommitsLimits int `yaml:"commits-limit"` RepositoryDetails feed.RepositoryDetails } @@ -29,6 +30,10 @@ func (widget *Repository) Initialize() error { widget.IssuesLimit = 3 } + if widget.CommitsLimits == 0 || widget.CommitsLimits < -1 { + widget.CommitsLimits = 3 + } + return nil } @@ -38,6 +43,7 @@ func (widget *Repository) Update(ctx context.Context) { string(widget.Token), widget.PullRequestsLimit, widget.IssuesLimit, + widget.CommitsLimits, ) if !widget.canContinueUpdateAfterHandlingErr(err) { From 71ca1753ef70659c2e40243438d5ea824f2ed885 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Thu, 29 Aug 2024 18:18:38 +0100 Subject: [PATCH 2/2] Update repository widget last commits --- docs/configuration.md | 4 +- internal/assets/templates/repository.html | 6 +-- internal/feed/github.go | 51 ++++++++++------------- internal/widget/repository-overview.go | 8 ++-- 4 files changed, 31 insertions(+), 38 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 1cd13af..1b02af4 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1216,7 +1216,7 @@ Preview: | token | string | no | | | pull-requests-limit | integer | no | 3 | | issues-limit | integer | no | 3 | -| commits-limit | integer | no | 3 | +| commits-limit | integer | no | -1 | ##### `repository` 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. ##### `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 Display a list of links which can be grouped. diff --git a/internal/assets/templates/repository.html b/internal/assets/templates/repository.html index 7f4bedc..53b6617 100644 --- a/internal/assets/templates/repository.html +++ b/internal/assets/templates/repository.html @@ -9,16 +9,16 @@ {{ if gt (len .RepositoryDetails.Commits) 0 }}
    -Last {{ .RepositoryDetails.LastCommits | formatNumber }} commits +Last {{ .CommitsLimit }} commits
      {{ range .RepositoryDetails.Commits }} -
    • {{ .Date | relativeTime }}
    • +
    • {{ end }}
    diff --git a/internal/feed/github.go b/internal/feed/github.go index f81b6d2..782d612 100644 --- a/internal/feed/github.go +++ b/internal/feed/github.go @@ -17,7 +17,6 @@ type githubReleaseLatestResponseJson struct { } `json:"reactions"` } - func fetchLatestGithubRelease(request *ReleaseRequest) (*AppRelease, error) { httpRequest, err := http.NewRequest( "GET", @@ -70,7 +69,7 @@ type RepositoryDetails struct { OpenIssues int Issues []GithubTicket LastCommits int - Commits []CommitsDetails + Commits []CommitDetails } type githubRepositoryDetailsResponseJson struct { @@ -88,30 +87,24 @@ type githubTicketResponseJson struct { } `json:"items"` } -type CommitsDetails struct { - Sha string - Author string - Email string - Date time.Time - Message string +type CommitDetails struct { + Sha string + Author string + CreatedAt time.Time + Message string } -type Author struct { - Name string `json:"name"` - Email string `json:"email"` - Date string `json:"date"` -} - -type Commit struct { +type gitHubCommitResponseJson struct { Sha string `json:"sha"` Commit struct { - Author Author `json:"author"` + Author struct { + Name string `json:"name"` + Date string `json:"date"` + } `json:"author"` Message string `json:"message"` } `json:"commit"` } -type githubCommitsResponseJson []Commit - 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) if err != nil { @@ -136,7 +129,7 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in var PRsErr error var issuesResponse githubTicketResponseJson var issuesErr error - var CommitsResponse githubCommitsResponseJson + var commitsResponse []gitHubCommitResponseJson var CommitsErr error var wg sync.WaitGroup @@ -166,9 +159,10 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in wg.Add(1) go (func() { defer wg.Done() - CommitsResponse, CommitsErr = decodeJsonFromRequest[githubCommitsResponseJson](defaultClient, CommitsRequest) + commitsResponse, CommitsErr = decodeJsonFromRequest[[]gitHubCommitResponseJson](defaultClient, CommitsRequest) })() } + wg.Wait() if detailsErr != nil { @@ -181,7 +175,7 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in Forks: detailsResponse.Forks, PullRequests: make([]GithubTicket, 0, len(PRsResponse.Tickets)), Issues: make([]GithubTicket, 0, len(issuesResponse.Tickets)), - Commits: make([]CommitsDetails, 0, len(CommitsResponse)), + Commits: make([]CommitDetails, 0, len(commitsResponse)), } err = nil @@ -223,17 +217,16 @@ func FetchRepositoryDetailsFromGithub(repository string, token string, maxPRs in if CommitsErr != nil { err = fmt.Errorf("%w: could not get issues: %s", ErrPartialContent, CommitsErr) } else { - for _, commit := range CommitsResponse { - details.LastCommits++ - details.Commits = append(details.Commits, CommitsDetails{ - Sha: commit.Sha, - Author: commit.Commit.Author.Name, - Email: commit.Commit.Author.Email, - Date: parseGithubTime(commit.Commit.Author.Date), - Message: strings.SplitN(commit.Commit.Message, "\n\n", 2)[0], + for i := range commitsResponse { + details.Commits = append(details.Commits, CommitDetails{ + Sha: commitsResponse[i].Sha, + Author: commitsResponse[i].Commit.Author.Name, + CreatedAt: parseRFC3339Time(commitsResponse[i].Commit.Author.Date), + Message: strings.SplitN(commitsResponse[i].Commit.Message, "\n\n", 2)[0], }) } } } + return details, err } diff --git a/internal/widget/repository-overview.go b/internal/widget/repository-overview.go index 0edbcc1..9d4cab3 100644 --- a/internal/widget/repository-overview.go +++ b/internal/widget/repository-overview.go @@ -15,7 +15,7 @@ type Repository struct { Token OptionalEnvString `yaml:"token"` PullRequestsLimit int `yaml:"pull-requests-limit"` IssuesLimit int `yaml:"issues-limit"` - CommitsLimits int `yaml:"commits-limit"` + CommitsLimit int `yaml:"commits-limit"` RepositoryDetails feed.RepositoryDetails } @@ -30,8 +30,8 @@ func (widget *Repository) Initialize() error { widget.IssuesLimit = 3 } - if widget.CommitsLimits == 0 || widget.CommitsLimits < -1 { - widget.CommitsLimits = 3 + if widget.CommitsLimit == 0 || widget.CommitsLimit < -1 { + widget.CommitsLimit = -1 } return nil @@ -43,7 +43,7 @@ func (widget *Repository) Update(ctx context.Context) { string(widget.Token), widget.PullRequestsLimit, widget.IssuesLimit, - widget.CommitsLimits, + widget.CommitsLimit, ) if !widget.canContinueUpdateAfterHandlingErr(err) {