From 64b6c07029dec41358957e6cd90165ac830b958f Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 12 Sep 2024 17:11:44 -0400 Subject: [PATCH] add pin and unpin issues --- Makefile | 4 ++-- forgejo/issue.go | 47 +++++++++++++++++++++++++++++++++++++++++++ forgejo/issue_test.go | 16 +++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 1a96408..c94e2a9 100644 --- a/Makefile +++ b/Makefile @@ -90,9 +90,9 @@ test: .PHONY: test-instance test-instance: - rm -f -r ${WORK_DIR}/test 2> /dev/null; \ + rm -f -r ${WORK_DIR}/test/conf ${WORK_DIR}/test/data 2> /dev/null; \ mkdir -p ${WORK_DIR}/test/conf/ ${WORK_DIR}/test/data/ - wget ${FORGEJO_DL} -O ${WORK_DIR}/test/forgejo-main; \ + # wget ${FORGEJO_DL} -O ${WORK_DIR}/test/forgejo-main; \ chmod +x ${WORK_DIR}/test/forgejo-main; \ echo "[security]" > ${WORK_DIR}/test/conf/app.ini; \ echo "INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1NTg4MzY4ODB9.LoKQyK5TN_0kMJFVHWUW0uDAyoGjDP6Mkup4ps2VJN4" >> ${WORK_DIR}/test/conf/app.ini; \ diff --git a/forgejo/issue.go b/forgejo/issue.go index 65e2011..fcaf0c0 100644 --- a/forgejo/issue.go +++ b/forgejo/issue.go @@ -13,6 +13,7 @@ import ( "bytes" "encoding/json" "fmt" + "net/http" "net/url" "strings" "time" @@ -293,6 +294,52 @@ func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) return issue, resp, err } +// PinIssue pins an issue for a given repository +func (c *Client) PinIssue(owner, repo string, index int64) (*Response, error) { + if err := escapeValidatePathSegments(&owner, &repo); err != nil { + return nil, err + } + + status, resp, err := c.getStatusCode("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/pin", owner, repo, index), nil, nil) + if err != nil { + return nil, err + } + + switch status { + case http.StatusNoContent: + return resp, nil + case http.StatusForbidden: + return resp, fmt.Errorf("forbidden") + case http.StatusNotFound: + return resp, fmt.Errorf("not found") + default: + return resp, fmt.Errorf("unexpected status %d", status) + } +} + +// UnpinIssue unpins an issue for a given repository +func (c *Client) UnpinIssue(owner, repo string, index int64) (*Response, error) { + if err := escapeValidatePathSegments(&owner, &repo); err != nil { + return nil, err + } + + status, resp, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/pin", owner, repo, index), nil, nil) + if err != nil { + return nil, err + } + + switch status { + case http.StatusNoContent: + return resp, nil + case http.StatusForbidden: + return resp, fmt.Errorf("forbidden") + case http.StatusNotFound: + return resp, fmt.Errorf("not found") + default: + return resp, fmt.Errorf("unexpected status %d", status) + } +} + // DeleteIssue delete a issue from a repository func (c *Client) DeleteIssue(user, repo string, id int64) (*Response, error) { if err := escapeValidatePathSegments(&user, &repo); err != nil { diff --git a/forgejo/issue_test.go b/forgejo/issue_test.go index 7cb8e93..8d59b05 100644 --- a/forgejo/issue_test.go +++ b/forgejo/issue_test.go @@ -27,6 +27,7 @@ func TestIssue(t *testing.T) { editIssues(t, c) listIssues(t, c) deleteIssue(t, c) + pinIssue(t, c) } func createIssue(t *testing.T, c *Client) { @@ -52,6 +53,21 @@ func createIssue(t *testing.T, c *Client) { createTestIssue(t, c, repo.Name, "", "you never know", nil, nil, mile.ID, nil, true, true) } +func pinIssue(t *testing.T, c *Client) { + log.Println("== TestPinIssues ==") + + user, _, err := c.GetMyUserInfo() + assert.NoError(t, err) + repo, _ := createTestRepo(t, "IssueTestsRepo", c) + + issue := createTestIssue(t, c, repo.Name, "Pinnable Issue", "", nil, nil, 0, nil, false, false) + _, err = c.PinIssue(user.UserName, repo.Name, issue.Index) + assert.NoError(t, err) + + _, err = c.UnpinIssue(user.UserName, repo.Name, issue.Index) + assert.NoError(t, err) +} + func deleteIssue(t *testing.T, c *Client) { log.Println("== TestDeleteIssues ==")