diff --git a/api.go b/api.go index 50ca187..0bbdb7f 100644 --- a/api.go +++ b/api.go @@ -47,7 +47,10 @@ type MarkdownRequest struct { // repos gets a list of Git repositories in this organisation. func (this *Application) repos(ctx context.Context) ([]Repo, error) { - this.apiSem.Acquire(ctx, 1) + err := this.apiSem.Acquire(ctx, 1) + if err != nil { + return nil, err // e.g. ctx closed + } defer this.apiSem.Release(1) req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/orgs/`+url.PathEscape(this.cfg.Gitea.Org)+`/repos`, nil) @@ -77,7 +80,10 @@ func (this *Application) repos(ctx context.Context) ([]Repo, error) { // repoFile gets a single file from the default branch of the git repository // Usually the default branch is `master`. func (this *Application) repoFile(ctx context.Context, repo, filename string) ([]byte, error) { - this.apiSem.Acquire(ctx, 1) + err := this.apiSem.Acquire(ctx, 1) + if err != nil { + return nil, err // e.g. ctx closed + } defer this.apiSem.Release(1) req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/contents/`+url.PathEscape(filename), nil) @@ -105,7 +111,10 @@ func (this *Application) repoFile(ctx context.Context, repo, filename string) ([ } func (this *Application) filesInDirectory(ctx context.Context, repo, dir string) ([]ReaddirEntry, error) { - this.apiSem.Acquire(ctx, 1) + err := this.apiSem.Acquire(ctx, 1) + if err != nil { + return nil, err // e.g. ctx closed + } defer this.apiSem.Release(1) req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/contents/`+dir, nil) // n.b. $dir param not escaped @@ -168,7 +177,10 @@ func (this *Application) imageFilesForRepo(ctx context.Context, repo string) ([] } func (this *Application) topicsForRepo(ctx context.Context, repo string) ([]string, error) { - this.apiSem.Acquire(ctx, 1) + err := this.apiSem.Acquire(ctx, 1) + if err != nil { + return nil, err // e.g. ctx closed + } defer this.apiSem.Release(1) req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/topics`, nil) @@ -197,7 +209,10 @@ func (this *Application) topicsForRepo(ctx context.Context, repo string) ([]stri // renderMarkdown calls the remote Gitea server's own markdown renderer. func (this *Application) renderMarkdown(ctx context.Context, repoName string, body string) ([]byte, error) { - this.apiSem.Acquire(ctx, 1) + err := this.apiSem.Acquire(ctx, 1) + if err != nil { + return nil, err // e.g. ctx closed + } defer this.apiSem.Release(1) jb, err := json.Marshal(MarkdownRequest{ @@ -231,7 +246,10 @@ func (this *Application) renderMarkdown(ctx context.Context, repoName string, bo // renderMarkdownRaw calls the remote Gitea server's own markdown renderer. func (this *Application) renderMarkdownRaw(ctx context.Context, body []byte) ([]byte, error) { - this.apiSem.Acquire(ctx, 1) + err := this.apiSem.Acquire(ctx, 1) + if err != nil { + return nil, err // e.g. ctx closed + } defer this.apiSem.Release(1) req, err := http.NewRequestWithContext(ctx, http.MethodPost, this.cfg.Gitea.URL+`api/v1/markdown/raw`, bytes.NewReader(body))