├── .typos.toml ├── .github ├── workflows │ ├── godoc.yml │ ├── codeql.yml │ └── ci.yml ├── PULL_REQUEST_TEMPLATE.md ├── ISSUE_TEMPLATE │ ├── config.yml │ ├── question.yml │ ├── suggestion.yml │ └── bug.yml ├── CONTRIBUTING.md ├── dependabot.yml ├── images │ ├── godoc.svg │ ├── license.svg │ └── card.svg └── CODE_OF_CONDUCT.md ├── go.mod ├── SECURITY.md ├── go.sum ├── auth.go ├── README.md ├── Makefile ├── utils.go ├── confluence_test.go ├── team_calandars.go ├── LICENSE ├── api.go └── confluence.go /.typos.toml: -------------------------------------------------------------------------------- 1 | [files] 2 | extend-exclude = ["go.sum"] 3 | 4 | [default.extend-identifiers] 5 | SanboxEventTypeReminders = "SanboxEventTypeReminders" 6 | sanboxEventTypeReminders = "sanboxEventTypeReminders" 7 | -------------------------------------------------------------------------------- /.github/workflows/godoc.yml: -------------------------------------------------------------------------------- 1 | name: GoDoc 2 | 3 | on: 4 | create 5 | 6 | jobs: 7 | GoDoc: 8 | name: Generate docs 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Trigger GoSumDB and PkgGoDev 13 | uses: essentialkaos/godoc-action@v1 14 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/essentialkaos/go-confluence/v6 2 | 3 | go 1.23.6 4 | 5 | require ( 6 | github.com/essentialkaos/check v1.4.1 7 | github.com/valyala/fasthttp v1.61.0 8 | ) 9 | 10 | require ( 11 | github.com/andybalholm/brotli v1.1.1 // indirect 12 | github.com/klauspost/compress v1.18.0 // indirect 13 | github.com/kr/pretty v0.3.1 // indirect 14 | github.com/kr/text v0.2.0 // indirect 15 | github.com/rogpeppe/go-internal v1.13.1 // indirect 16 | github.com/valyala/bytebufferpool v1.0.0 // indirect 17 | ) 18 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### What did you implement: 2 | 3 | Closes #XXXXX 4 | 5 | ### How did you implement it: 6 | 7 | ... 8 | 9 | ### How can we verify it: 10 | 11 | ... 12 | 13 | ### TODO's: 14 | 15 | - [ ] Write tests 16 | - [ ] Write documentation 17 | - [ ] Check that there aren't other open pull requests for the same issue/feature 18 | - [ ] Format your source code by `make fmt` 19 | - [ ] Provide verification config / commands 20 | - [ ] Enable "Allow edits from maintainers" for this PR 21 | - [ ] Update the messages below 22 | 23 | **Is this ready for review?:** No 24 | **Is it a breaking change?:** No 25 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | 3 | contact_links: 4 | - name: Security Policies and Procedures 5 | url: https://github.com/essentialkaos/.github/blob/master/SECURITY.md 6 | about: Security procedures and general policies for all ESSENTIAL KAOS projects. 7 | 8 | - name: Contributing Guidelines 9 | url: https://github.com/essentialkaos/contributing-guidelines/blob/master/CONTRIBUTING.md 10 | about: Contributing Guidelines for all ESSENTIAL KAOS projects 11 | 12 | - name: Go Version Support Policy 13 | url: https://github.com/essentialkaos/.github/blob/master/GO-VERSION-SUPPORT.md 14 | about: Information about supported Go versions 15 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | branches: [master, develop] 6 | pull_request: 7 | branches: [master] 8 | schedule: 9 | - cron: '0 3 * * */2' 10 | 11 | permissions: 12 | security-events: write 13 | actions: read 14 | contents: read 15 | 16 | jobs: 17 | analyse: 18 | name: Analyse 19 | runs-on: ubuntu-latest 20 | 21 | steps: 22 | - name: Checkout repository 23 | uses: actions/checkout@v4 24 | with: 25 | fetch-depth: 2 26 | 27 | - name: Initialize CodeQL 28 | uses: github/codeql-action/init@v3 29 | with: 30 | languages: go 31 | 32 | - name: Perform CodeQL Analysis 33 | uses: github/codeql-action/analyze@v3 34 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | **IMPORTANT! Contribute your code only if you have an excellent understanding of project idea and all existing code base. Otherwise, a nicely formatted issue will be more helpful to us.** 4 | 5 | ### Issues 6 | 7 | 1. Provide product version where the problem was found; 8 | 2. Provide info about your environment; 9 | 3. Provide detailed info about your problem; 10 | 4. Provide steps to reproduce the problem; 11 | 5. Provide actual and expected results. 12 | 13 | ### Code 14 | 15 | 1. Check your code **before** creating pull request; 16 | 2. If tests are present in a project, add tests for your code; 17 | 3. Add inline documentation for your code; 18 | 4. Apply code style used throughout the project; 19 | 5. Create your pull request to `develop` branch (_pull requests to other branches are not allowed_). 20 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | updates: 4 | - package-ecosystem: "gomod" 5 | directory: "/" 6 | target-branch: "develop" 7 | schedule: 8 | interval: "daily" 9 | timezone: "Etc/UTC" 10 | time: "03:00" 11 | labels: 12 | - "PR • MAINTENANCE" 13 | assignees: 14 | - "andyone" 15 | reviewers: 16 | - "andyone" 17 | groups: 18 | all: 19 | applies-to: version-updates 20 | update-types: 21 | - "minor" 22 | - "patch" 23 | 24 | - package-ecosystem: "github-actions" 25 | directory: "/" 26 | target-branch: "develop" 27 | schedule: 28 | interval: "daily" 29 | timezone: "Etc/UTC" 30 | time: "03:00" 31 | labels: 32 | - "PR • MAINTENANCE" 33 | assignees: 34 | - "andyone" 35 | reviewers: 36 | - "andyone" 37 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/question.yml: -------------------------------------------------------------------------------- 1 | name: ❓ Question 2 | description: Question about application, configuration or code 3 | title: "[Question]: " 4 | labels: ["issue • question"] 5 | assignees: 6 | - andyone 7 | 8 | body: 9 | - type: markdown 10 | attributes: 11 | value: | 12 | > [!IMPORTANT] 13 | > Before you open an issue, search GitHub Issues for a similar question. If so, please add a 👍 reaction to the existing issue. 14 | 15 | - type: textarea 16 | attributes: 17 | label: Question 18 | description: Detailed question 19 | validations: 20 | required: true 21 | 22 | - type: textarea 23 | attributes: 24 | label: Module version info 25 | description: Output of `grep 'github.com/essentialkaos/go-confluence' go.sum` command 26 | render: shell 27 | validations: 28 | required: true 29 | -------------------------------------------------------------------------------- /.github/images/godoc.svg: -------------------------------------------------------------------------------- 1 | go: referencegoreference -------------------------------------------------------------------------------- /.github/images/license.svg: -------------------------------------------------------------------------------- 1 | license: Apache-2.0licenseApache-2.0 -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/suggestion.yml: -------------------------------------------------------------------------------- 1 | name: ➕ Suggestion 2 | description: Suggest new feature or improvement 3 | title: "[Suggestion]: " 4 | labels: ["issue • suggestion"] 5 | assignees: 6 | - andyone 7 | 8 | body: 9 | - type: markdown 10 | attributes: 11 | value: | 12 | > [!IMPORTANT] 13 | > Before you open an issue, search GitHub Issues for a similar feature requests. If so, please add a 👍 reaction to the existing issue. 14 | > 15 | > Opening a feature request kicks off a discussion. Requests may be closed if we're not actively planning to work on them. 16 | 17 | - type: textarea 18 | attributes: 19 | label: Proposal 20 | description: Description of the feature 21 | validations: 22 | required: true 23 | 24 | - type: textarea 25 | attributes: 26 | label: Current behavior 27 | description: What currently happens 28 | validations: 29 | required: true 30 | 31 | - type: textarea 32 | attributes: 33 | label: Desired behavior 34 | description: What you would like to happen 35 | validations: 36 | required: true 37 | 38 | - type: textarea 39 | attributes: 40 | label: Use case 41 | description: Why is this important (helps with prioritizing requests) 42 | validations: 43 | required: true 44 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug.yml: -------------------------------------------------------------------------------- 1 | name: ❗ Bug Report 2 | description: File a bug report 3 | title: "[Bug]: " 4 | labels: ["issue • bug"] 5 | assignees: 6 | - andyone 7 | 8 | body: 9 | - type: markdown 10 | attributes: 11 | value: | 12 | > [!IMPORTANT] 13 | > Before you open an issue, search GitHub Issues for a similar bug reports. If so, please add a 👍 reaction to the existing issue. 14 | 15 | - type: textarea 16 | attributes: 17 | label: Module version info 18 | description: Output of `grep 'github.com/essentialkaos/go-confluence' go.sum` command 19 | render: shell 20 | validations: 21 | required: true 22 | 23 | - type: textarea 24 | attributes: 25 | label: Steps to reproduce 26 | description: Short guide on how to reproduce this problem on our site 27 | placeholder: | 28 | 1. [First Step] 29 | 2. [Second Step] 30 | 3. [and so on...] 31 | validations: 32 | required: true 33 | 34 | - type: textarea 35 | attributes: 36 | label: Expected behavior 37 | description: What you expected to happen 38 | validations: 39 | required: true 40 | 41 | - type: textarea 42 | attributes: 43 | label: Actual behavior 44 | description: What actually happened 45 | validations: 46 | required: true 47 | 48 | - type: textarea 49 | attributes: 50 | label: Additional info 51 | description: Include gist of relevant config, logs, etc. 52 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policies and Procedures 2 | 3 | This document outlines security procedures and general policies for all 4 | ESSENTIAL KAOS projects. 5 | 6 | * [Reporting a Bug](#reporting-a-bug) 7 | * [Disclosure Policy](#disclosure-policy) 8 | 9 | ## Reporting a Bug 10 | 11 | The ESSENTIAL KAOS team and community take all security bugs in our projects 12 | very seriously. Thank you for improving the security of our project. We 13 | appreciate your efforts and responsible disclosure and will make every effort 14 | to acknowledge your contributions. 15 | 16 | Report security bugs by emailing our security team at security@essentialkaos.com. 17 | 18 | The security team will acknowledge your email within 48 hours and will send a 19 | more detailed response within 48 hours, indicating the next steps in handling 20 | your report. After the initial reply to your report, the security team will 21 | endeavor to keep you informed of the progress towards a fix and full 22 | announcement, and may ask for additional information or guidance. 23 | 24 | Report security bugs in third-party dependencies to the person or team 25 | maintaining the dependencies. 26 | 27 | ## Disclosure Policy 28 | 29 | When the security team receives a security bug report, they will assign it to a 30 | primary handler. This person will coordinate the fix and release process, 31 | involving the following steps: 32 | 33 | * Confirm the problem and determine the affected versions; 34 | * Audit code to find any similar potential problems; 35 | * Prepare fixes for all releases still under maintenance. These fixes will be 36 | released as fast as possible. 37 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [master, develop] 6 | pull_request: 7 | branches: [master] 8 | workflow_dispatch: 9 | inputs: 10 | force_run: 11 | description: 'Force workflow run' 12 | required: true 13 | type: choice 14 | options: [yes, no] 15 | 16 | permissions: 17 | actions: read 18 | contents: read 19 | statuses: write 20 | 21 | concurrency: 22 | group: ${{ github.workflow }}-${{ github.ref }} 23 | cancel-in-progress: true 24 | 25 | jobs: 26 | Go: 27 | name: Go 28 | runs-on: ubuntu-latest 29 | 30 | strategy: 31 | matrix: 32 | go: [ 'oldstable', 'stable' ] 33 | 34 | steps: 35 | - name: Checkout 36 | uses: actions/checkout@v4 37 | 38 | - name: Set up Go 39 | uses: actions/setup-go@v5 40 | with: 41 | go-version: ${{ matrix.go }} 42 | 43 | - name: Download dependencies 44 | run: make deps 45 | 46 | - name: Run tests 47 | run: make test 48 | 49 | Aligo: 50 | name: Aligo 51 | runs-on: ubuntu-latest 52 | 53 | needs: Go 54 | 55 | steps: 56 | - name: Checkout 57 | uses: actions/checkout@v4 58 | 59 | - name: Set up Go 60 | uses: actions/setup-go@v5 61 | with: 62 | go-version: 'stable' 63 | 64 | - name: Download dependencies 65 | run: make deps 66 | 67 | - name: Check Golang sources with Aligo 68 | uses: essentialkaos/aligo-action@v2 69 | continue-on-error: true 70 | with: 71 | files: ./... 72 | 73 | Typos: 74 | name: Typos 75 | runs-on: ubuntu-latest 76 | 77 | needs: Go 78 | 79 | steps: 80 | - name: Checkout 81 | uses: actions/checkout@v4 82 | 83 | - name: Check spelling 84 | uses: crate-ci/typos@master 85 | continue-on-error: true 86 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= 2 | github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= 3 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 4 | github.com/essentialkaos/check v1.4.1 h1:SuxXzrbokPGTPWxGRnzy0hXvtb44mtVrdNxgPa1s4c8= 5 | github.com/essentialkaos/check v1.4.1/go.mod h1:xQOYwFvnxfVZyt5Qvjoa1SxcRqu5VyP77pgALr3iu+M= 6 | github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= 7 | github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= 8 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 9 | github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 10 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 11 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 12 | github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= 13 | github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= 14 | github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= 15 | github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= 16 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 17 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 18 | github.com/valyala/fasthttp v1.61.0 h1:VV08V0AfoRaFurP1EWKvQQdPTZHiUzaVoulX1aBDgzU= 19 | github.com/valyala/fasthttp v1.61.0/go.mod h1:wRIV/4cMwUPWnRcDno9hGnYZGh78QzODFfo1LTUhBog= 20 | github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= 21 | github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= 22 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of 4 | fostering an open and welcoming community, we pledge to respect all people who 5 | contribute through reporting issues, posting feature requests, updating 6 | documentation, submitting pull requests or patches, and other activities. 7 | 8 | We are committed to making participation in this project a harassment-free 9 | experience for everyone, regardless of level of experience, gender, gender 10 | identity and expression, sexual orientation, disability, personal appearance, 11 | body size, race, ethnicity, age, religion, or nationality. 12 | 13 | Examples of unacceptable behavior by participants include: 14 | 15 | * The use of sexualized language or imagery 16 | * Personal attacks 17 | * Trolling or insulting/derogatory comments 18 | * Public or private harassment 19 | * Publishing other's private information, such as physical or electronic 20 | addresses, without explicit permission 21 | * Other unethical or unprofessional conduct 22 | 23 | Project maintainers have the right and responsibility to remove, edit, or 24 | reject comments, commits, code, wiki edits, issues, and other contributions 25 | that are not aligned to this Code of Conduct, or to ban temporarily or 26 | permanently any contributor for other behaviors that they deem inappropriate, 27 | threatening, offensive, or harmful. 28 | 29 | By adopting this Code of Conduct, project maintainers commit themselves to 30 | fairly and consistently applying these principles to every aspect of managing 31 | this project. Project maintainers who do not follow or enforce the Code of 32 | Conduct may be permanently removed from the project team. 33 | 34 | This Code of Conduct applies both within project spaces and in public spaces 35 | when an individual is representing the project or its community. 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 38 | reported by contacting a project maintainer at `conduct@essentialkaos.com`. All 39 | complaints will be reviewed and investigated and will result in a response that 40 | is deemed necessary and appropriate to the circumstances. Maintainers are 41 | obligated to maintain confidentiality with regard to the reporter of an 42 | incident. 43 | -------------------------------------------------------------------------------- /auth.go: -------------------------------------------------------------------------------- 1 | package confluence 2 | 3 | // ////////////////////////////////////////////////////////////////////////////////// // 4 | // // 5 | // Copyright (c) 2025 ESSENTIAL KAOS // 6 | // Apache License, Version 2.0 // 7 | // // 8 | // ////////////////////////////////////////////////////////////////////////////////// // 9 | 10 | import ( 11 | "encoding/base64" 12 | "errors" 13 | ) 14 | 15 | // ////////////////////////////////////////////////////////////////////////////////// // 16 | 17 | // Auth is interface for authorization method 18 | type Auth interface { 19 | Validate() error 20 | Encode() string 21 | } 22 | 23 | // ////////////////////////////////////////////////////////////////////////////////// // 24 | 25 | // AuthBasic is struct with data for basic authorization 26 | type AuthBasic struct { 27 | User string 28 | Password string 29 | } 30 | 31 | // AuthToken is struct with data for personal token authorization 32 | type AuthToken struct { 33 | Token string 34 | } 35 | 36 | // ////////////////////////////////////////////////////////////////////////////////// // 37 | 38 | var ( 39 | ErrEmptyUser = errors.New("User can't be empty") 40 | ErrEmptyPassword = errors.New("Password can't be empty") 41 | ErrEmptyToken = errors.New("Token can't be empty") 42 | ErrTokenWrongLength = errors.New("Token length must be equal to 44") 43 | ) 44 | 45 | // ////////////////////////////////////////////////////////////////////////////////// // 46 | 47 | // Validate validates authorization data 48 | func (a AuthBasic) Validate() error { 49 | switch { 50 | case a.User == "": 51 | return ErrEmptyUser 52 | case a.Password == "": 53 | return ErrEmptyPassword 54 | } 55 | 56 | return nil 57 | } 58 | 59 | // Encode encodes data for authorization 60 | func (a AuthBasic) Encode() string { 61 | return "Basic " + base64.StdEncoding.EncodeToString([]byte(a.User+":"+a.Password)) 62 | } 63 | 64 | // Validate validates authorization data 65 | func (a AuthToken) Validate() error { 66 | switch { 67 | case a.Token == "": 68 | return ErrEmptyToken 69 | case len(a.Token) != 44: 70 | return ErrTokenWrongLength 71 | } 72 | 73 | return nil 74 | } 75 | 76 | // Encode encodes data for authorization 77 | func (a AuthToken) Encode() string { 78 | return "Bearer " + a.Token 79 | } 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 | GoReportCard 6 | GitHub Actions CI Status 7 | GitHub Actions CodeQL Status 8 | 9 |

10 | 11 |

Usage exampleCI StatusLicense

12 | 13 |
14 | 15 | `go-confluence` is a Go package for working with [Confluence REST API](https://docs.atlassian.com/ConfluenceServer/rest/7.3.3/). 16 | 17 | > [!IMPORTANT] 18 | > **Please note that this package only supports retrieving data from the Confluence API (_i.e. you cannot create or modify data with this package_).** 19 | 20 | ### Usage example 21 | 22 | Authentication with username and password. 23 | 24 | ```go 25 | package main 26 | 27 | import ( 28 | "fmt" 29 | cf "github.com/essentialkaos/go-confluence/v6" 30 | ) 31 | 32 | func main() { 33 | api, err := cf.NewAPI("https://confluence.domain.com", cf.AuthBasic{"john", "MySuppaPAssWOrd"}) 34 | 35 | api.SetUserAgent("MyApp", "1.2.3") 36 | 37 | if err != nil { 38 | fmt.Printf("Error: %v\n", err) 39 | return 40 | } 41 | 42 | content, err := api.GetContentByID( 43 | "18173522", cf.ContentIDParameters{ 44 | Version: 4, 45 | Expand: []string{"space", "body.view", "version"}, 46 | }, 47 | ) 48 | 49 | if err != nil { 50 | fmt.Printf("Error: %v\n", err) 51 | return 52 | } 53 | 54 | fmt.Printf("ID: %s\n", content.ID) 55 | } 56 | ``` 57 | 58 | Authentication with personal token. Please make sure your confluence 7.9 version and later. See [Using Personal Access Tokens guide](https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html) 59 | 60 | ```go 61 | package main 62 | 63 | import ( 64 | "fmt" 65 | 66 | cf "github.com/essentialkaos/go-confluence/v6" 67 | ) 68 | 69 | func main() { 70 | api, err := cf.NewAPI("https://confluence.domain.com", cf.AuthToken{"avaMTxxxqKaxpFHpmwHPXhjmUFfAJMaU3VXUji73EFhf"}) 71 | 72 | api.SetUserAgent("MyApp", "1.2.3") 73 | 74 | if err != nil { 75 | fmt.Printf("Error: %v\n", err) 76 | return 77 | } 78 | 79 | content, err := api.GetContentByID( 80 | "18173522", cf.ContentIDParameters{ 81 | Version: 4, 82 | Expand: []string{"space", "body.view", "version"}, 83 | }, 84 | ) 85 | if err != nil { 86 | fmt.Printf("Error: %v\n", err) 87 | return 88 | } 89 | 90 | fmt.Printf("ID: %s\n", content.ID) 91 | } 92 | ``` 93 | 94 | ### CI Status 95 | 96 | | Branch | Status | 97 | |------------|--------| 98 | | `master` (_Stable_) | [![CI](https://kaos.sh/w/go-confluence/ci.svg?branch=master)](https://kaos.sh/w/go-confluence/ci?query=branch:master) | 99 | | `develop` (_Unstable_) | [![CI](https://kaos.sh/w/go-confluence/ci.svg?branch=develop)](https://kaos.sh/w/go-confluence/ci?query=branch:develop) | 100 | 101 | ### Contributing 102 | 103 | Before contributing to this project please read our [Contributing Guidelines](https://github.com/essentialkaos/.github/blob/master/CONTRIBUTING.md). 104 | 105 | ### License 106 | 107 | [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) 108 | 109 |

110 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | 3 | # This Makefile generated by GoMakeGen 3.3.1 using next command: 4 | # gomakegen --mod . 5 | # 6 | # More info: https://kaos.sh/gomakegen 7 | 8 | ################################################################################ 9 | 10 | ifdef VERBOSE ## Print verbose information (Flag) 11 | VERBOSE_FLAG = -v 12 | endif 13 | 14 | ifdef PROXY ## Force proxy usage for downloading dependencies (Flag) 15 | export GOPROXY=https://proxy.golang.org/cached-only,direct 16 | endif 17 | 18 | ifdef CGO ## Enable CGO usage (Flag) 19 | export CGO_ENABLED=1 20 | else 21 | export CGO_ENABLED=0 22 | endif 23 | 24 | MAKEDIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST)))) 25 | GITREV ?= $(shell test -s $(MAKEDIR)/.git && git rev-parse --short HEAD) 26 | 27 | ################################################################################ 28 | 29 | .DEFAULT_GOAL := help 30 | .PHONY = fmt vet deps update test init vendor tidy mod-init mod-update mod-download mod-vendor help 31 | 32 | ################################################################################ 33 | 34 | init: mod-init ## Initialize new module 35 | 36 | deps: mod-download ## Download dependencies 37 | 38 | update: mod-update ## Update dependencies to the latest versions 39 | 40 | vendor: mod-vendor ## Make vendored copy of dependencies 41 | 42 | test: ## Run tests 43 | @echo "Starting tests…" 44 | ifdef COVERAGE_FILE ## Save coverage data into file (String) 45 | @go test $(VERBOSE_FLAG) -covermode=count -coverprofile=$(COVERAGE_FILE) ./. 46 | else 47 | @go test $(VERBOSE_FLAG) -covermode=count . 48 | endif 49 | 50 | tidy: ## Cleanup dependencies 51 | @echo "•• Tidying up dependencies…" 52 | ifdef COMPAT ## Compatible Go version (String) 53 | @go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT) -go=$(COMPAT) 54 | else 55 | @go mod tidy $(VERBOSE_FLAG) 56 | endif 57 | @echo "•• Updating vendored dependencies…" 58 | @test -d vendor && rm -rf vendor && go mod vendor $(VERBOSE_FLAG) || : 59 | 60 | mod-init: 61 | @echo "••• Modules initialization…" 62 | @rm -f go.mod go.sum 63 | ifdef MODULE_PATH ## Module path for initialization (String) 64 | @go mod init $(MODULE_PATH) 65 | else 66 | @go mod init 67 | endif 68 | 69 | @echo "••• Dependencies cleanup…" 70 | ifdef COMPAT ## Compatible Go version (String) 71 | @go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT) -go=$(COMPAT) 72 | else 73 | @go mod tidy $(VERBOSE_FLAG) 74 | endif 75 | @echo "••• Stripping toolchain info…" 76 | @grep -q 'toolchain ' go.mod && go mod edit -toolchain=none || : 77 | 78 | mod-update: 79 | @echo "•••• Updating dependencies…" 80 | ifdef UPDATE_ALL ## Update all dependencies (Flag) 81 | @go get -u $(VERBOSE_FLAG) all 82 | else 83 | @go get -u $(VERBOSE_FLAG) ./... 84 | endif 85 | 86 | @echo "•••• Stripping toolchain info…" 87 | @grep -q 'toolchain ' go.mod && go mod edit -toolchain=none || : 88 | 89 | @echo "•••• Dependencies cleanup…" 90 | ifdef COMPAT 91 | @go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT) 92 | else 93 | @go mod tidy $(VERBOSE_FLAG) 94 | endif 95 | 96 | @echo "•••• Updating vendored dependencies…" 97 | @test -d vendor && rm -rf vendor && go mod vendor $(VERBOSE_FLAG) || : 98 | 99 | mod-download: 100 | @echo "Downloading dependencies…" 101 | @go mod download 102 | 103 | mod-vendor: 104 | @echo "Vendoring dependencies…" 105 | @rm -rf vendor && go mod vendor $(VERBOSE_FLAG) || : 106 | 107 | fmt: ## Format source code with gofmt 108 | @echo "Formatting sources…" 109 | @find . -name "*.go" -exec gofmt -s -w {} \; 110 | 111 | vet: ## Runs 'go vet' over sources 112 | @echo "Running 'go vet' over sources…" 113 | @go vet -composites=false -printfuncs=LPrintf,TLPrintf,TPrintf,log.Debug,log.Info,log.Warn,log.Error,log.Critical,log.Print ./... 114 | 115 | help: ## Show this info 116 | @echo -e '\n\033[1mTargets:\033[0m\n' 117 | @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \ 118 | | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[33m%-6s\033[0m %s\n", $$1, $$2}' 119 | @echo -e '\n\033[1mVariables:\033[0m\n' 120 | @grep -E '^ifdef [A-Z_]+ .*?## .*$$' $(abspath $(lastword $(MAKEFILE_LIST))) \ 121 | | sed 's/ifdef //' \ 122 | | sort -h \ 123 | | awk 'BEGIN {FS = " .*?## "}; {printf " \033[32m%-13s\033[0m %s\n", $$1, $$2}' 124 | @echo -e '' 125 | @echo -e '\033[90mGenerated by GoMakeGen 3.3.1\033[0m\n' 126 | 127 | ################################################################################ 128 | -------------------------------------------------------------------------------- /utils.go: -------------------------------------------------------------------------------- 1 | package confluence 2 | 3 | // ////////////////////////////////////////////////////////////////////////////////// // 4 | // // 5 | // Copyright (c) 2025 ESSENTIAL KAOS // 6 | // Apache License, Version 2.0 // 7 | // // 8 | // ////////////////////////////////////////////////////////////////////////////////// // 9 | 10 | import ( 11 | "fmt" 12 | "net/url" 13 | "reflect" 14 | "strings" 15 | "time" 16 | ) 17 | 18 | // ////////////////////////////////////////////////////////////////////////////////// // 19 | 20 | // Supported options 21 | const ( 22 | _OPTION_UNWRAP = "unwrap" 23 | _OPTION_RESPECT = "respect" 24 | _OPTION_REVERSE = "reverse" 25 | _OPTION_TIMEDATE = "timedate" 26 | ) 27 | 28 | // ////////////////////////////////////////////////////////////////////////////////// // 29 | 30 | // paramsToQuery convert params to query string 31 | func paramsToQuery(params any) string { 32 | var result string 33 | 34 | t := reflect.TypeOf(params) 35 | v := reflect.ValueOf(params) 36 | 37 | for i := range t.NumField() { 38 | field := t.Field(i) 39 | value := v.Field(i) 40 | tag := field.Tag.Get("query") 41 | 42 | switch value.Type().String() { 43 | case "string": 44 | result += formatString(tag, value) 45 | 46 | case "int", "int64": 47 | result += formatInt(tag, value) 48 | 49 | case "bool": 50 | result += formatBool(tag, value) 51 | 52 | case "time.Time": 53 | result += formatTime(tag, value) 54 | 55 | case "[]string": 56 | result += formatSlice(tag, value) 57 | } 58 | } 59 | 60 | if result == "" { 61 | return "" 62 | } 63 | 64 | return result[:len(result)-1] 65 | } 66 | 67 | // formatString returns string representation of string for query string 68 | func formatString(tag string, value reflect.Value) string { 69 | if value.String() != "" { 70 | return tag + "=" + esc(value.String()) + "&" 71 | } else if hasTagOption(tag, _OPTION_RESPECT) { 72 | return getTagName(tag) + "=&" 73 | } 74 | 75 | return "" 76 | } 77 | 78 | // formatInt returns string representation of int/int64 for query string 79 | func formatInt(tag string, value reflect.Value) string { 80 | if value.Int() != 0 { 81 | return tag + "=" + fmt.Sprintf("%d", value.Int()) + "&" 82 | } else if hasTagOption(tag, _OPTION_RESPECT) { 83 | return getTagName(tag) + "=0&" 84 | } 85 | 86 | return "" 87 | } 88 | 89 | // formatBool returns string representation of boolean for query string 90 | func formatBool(tag string, value reflect.Value) string { 91 | b := value.Bool() 92 | 93 | if hasTagOption(tag, _OPTION_REVERSE) && b { 94 | return getTagName(tag) + "=false&" 95 | } else { 96 | if b { 97 | return getTagName(tag) + "=true&" 98 | } else if hasTagOption(tag, _OPTION_RESPECT) { 99 | return getTagName(tag) + "=false&" 100 | } 101 | } 102 | 103 | return "" 104 | } 105 | 106 | // formatTime returns string representation of time and date for query string 107 | func formatTime(tag string, value reflect.Value) string { 108 | d := value.Interface().(time.Time) 109 | 110 | if !d.IsZero() { 111 | if hasTagOption(tag, _OPTION_TIMEDATE) { 112 | return getTagName(tag) + "=" + d.Format("2006-01-02T15:04:05Z") + "&" 113 | } else { 114 | return tag + "=" + d.Format("2006-01-02") + "&" 115 | } 116 | } 117 | 118 | return "" 119 | } 120 | 121 | // formatSlice returns string representation of slice for query string 122 | func formatSlice(tag string, value reflect.Value) string { 123 | if value.Len() == 0 { 124 | return "" 125 | } 126 | 127 | var result string 128 | 129 | name := getTagName(tag) 130 | unwrap := hasTagOption(tag, _OPTION_UNWRAP) 131 | 132 | if !unwrap { 133 | result += name + "=" 134 | } 135 | 136 | for i := range value.Len() { 137 | v := value.Index(i) 138 | 139 | if unwrap { 140 | result += name + "=" + esc(v.String()) + "&" 141 | } else { 142 | result += esc(v.String()) + "," 143 | } 144 | } 145 | 146 | return result[:len(result)-1] + "&" 147 | } 148 | 149 | // getTagOption extract option from tag 150 | func hasTagOption(tag, option string) bool { 151 | if !strings.Contains(tag, ",") { 152 | return false 153 | } 154 | 155 | return tag[strings.Index(tag, ",")+1:] == option 156 | } 157 | 158 | // getTagName return tag name 159 | func getTagName(tag string) string { 160 | if !strings.Contains(tag, ",") { 161 | return tag 162 | } 163 | 164 | return tag[:strings.Index(tag, ",")] 165 | } 166 | 167 | // esc escapes the string so it can be safely placed inside a URL query 168 | func esc(s string) string { 169 | return url.QueryEscape(s) 170 | } 171 | -------------------------------------------------------------------------------- /.github/images/card.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /confluence_test.go: -------------------------------------------------------------------------------- 1 | package confluence 2 | 3 | // ////////////////////////////////////////////////////////////////////////////////// // 4 | 5 | import ( 6 | "regexp" 7 | "strings" 8 | "testing" 9 | "time" 10 | 11 | . "github.com/essentialkaos/check" 12 | ) 13 | 14 | // ////////////////////////////////////////////////////////////////////////////////// // 15 | 16 | type MyParams struct { 17 | S string `query:"s,respect"` 18 | I int `query:"i,respect"` 19 | B bool `query:"b,respect"` 20 | BR bool `query:"br,reverse"` 21 | BN bool `query:"bn"` 22 | DN time.Time `query:"dn"` 23 | } 24 | 25 | func (p MyParams) ToQuery() string { 26 | return paramsToQuery(p) 27 | } 28 | 29 | func (p MyParams) Validate() error { 30 | return nil 31 | } 32 | 33 | // ////////////////////////////////////////////////////////////////////////////////// // 34 | 35 | func Test(t *testing.T) { TestingT(t) } 36 | 37 | type ConfluenceSuite struct{} 38 | 39 | // ////////////////////////////////////////////////////////////////////////////////// // 40 | 41 | var _ = Suite(&ConfluenceSuite{}) 42 | 43 | var tsRegex = regexp.MustCompile(`\&\_\=[0-9]{19}`) 44 | 45 | // ////////////////////////////////////////////////////////////////////////////////// // 46 | 47 | func (s *ConfluenceSuite) TestParamsEncoding(c *C) { 48 | var p Parameters 49 | 50 | p = AuditParameters{ 51 | StartDate: time.Date(2018, 1, 1, 0, 0, 0, 0, time.Local), 52 | EndDate: time.Date(2018, 2, 15, 12, 30, 0, 0, time.Local), 53 | Start: 50, 54 | Limit: 20, 55 | } 56 | 57 | c.Assert(p.ToQuery(), Equals, "startDate=2018-01-01&endDate=2018-02-15&start=50&limit=20") 58 | 59 | p = CollectionParameters{ 60 | Expand: []string{"test1,test2"}, 61 | } 62 | 63 | c.Assert(p.ToQuery(), Equals, `expand=test1%2Ctest2`) 64 | 65 | p = SpaceParameters{ 66 | SpaceKey: []string{"TS1", "TS2", "TS3"}, 67 | Favourite: true, 68 | } 69 | 70 | c.Assert(p.ToQuery(), Equals, "spaceKey=TS1&spaceKey=TS2&spaceKey=TS3&favourite=true") 71 | 72 | p = WatchParameters{} 73 | 74 | c.Assert(p.ToQuery(), Equals, "") 75 | 76 | p = MyParams{BR: true} 77 | pp := []string{"s=", "i=0", "b=false", "br=false"} 78 | 79 | c.Assert(validateQuery(p.ToQuery(), pp), Equals, true) 80 | } 81 | 82 | func (s *ConfluenceSuite) TestTinyLinkGeneration(c *C) { 83 | api, _ := NewAPI("https://confl.domain.com", AuthBasic{"JohnDoe", "Test1234!"}) 84 | 85 | c.Assert(api.GenTinyLink("1477502"), Equals, "https://confl.domain.com/x/fosW") 86 | c.Assert(api.GenTinyLink("1477627"), Equals, "https://confl.domain.com/x/_4sW") 87 | c.Assert(api.GenTinyLink("40643836"), Equals, "https://confl.domain.com/x/-CxsAg") 88 | } 89 | 90 | func (s *ConfluenceSuite) TestCustomUnmarshalers(c *C) { 91 | var err error 92 | 93 | d := &Date{} 94 | err = d.UnmarshalJSON([]byte(`"2013-03-12T10:36:12.602+04:00"`)) 95 | 96 | c.Assert(err, IsNil) 97 | c.Assert(d.Year(), Equals, 2013) 98 | c.Assert(d.Month(), Equals, time.Month(3)) 99 | c.Assert(d.Day(), Equals, 12) 100 | 101 | t := &Timestamp{} 102 | err = t.UnmarshalJSON([]byte("1523059214803")) 103 | 104 | c.Assert(err, IsNil) 105 | c.Assert(t.Year(), Equals, 2018) 106 | c.Assert(t.Month(), Equals, time.Month(4)) 107 | c.Assert(t.Day(), Equals, 7) 108 | 109 | var e ExtensionPosition 110 | err = e.UnmarshalJSON([]byte(`"none"`)) 111 | 112 | c.Assert(err, IsNil) 113 | c.Assert(e, Equals, ExtensionPosition(-1)) 114 | } 115 | 116 | func (s *ConfluenceSuite) TestCalendarIDValidator(c *C) { 117 | c.Assert(IsValidCalendarID(""), Equals, false) 118 | c.Assert(IsValidCalendarID("1a72410b-6417-4869-9260-9ec13816e48q"), Equals, false) 119 | c.Assert(IsValidCalendarID("1a72410b164175486969260f9ec13816e481"), Equals, false) 120 | c.Assert(IsValidCalendarID("1a72410b-6417-4869-9260-9ec13816e481"), Equals, true) 121 | } 122 | 123 | func (s *ConfluenceSuite) TestCalendarParamsEncoding(c *C) { 124 | p1 := CalendarEventsParameters{ 125 | SubCalendarID: "1a72410b-6417-4869-9260-9ec13816e481", 126 | UserTimezoneID: "Etc/UTC", 127 | Start: time.Date(2020, 1, 1, 0, 0, 0, 0, time.Local), 128 | End: time.Date(2020, 1, 2, 12, 30, 45, 0, time.Local), 129 | } 130 | 131 | pp1 := []string{ 132 | "subCalendarId=1a72410b-6417-4869-9260-9ec13816e481", 133 | "userTimeZoneId=Etc%2FUTC", 134 | "start=2020-01-01T00:00:00Z", 135 | "end=2020-01-02T12:30:45Z", 136 | } 137 | 138 | q1 := p1.ToQuery() 139 | 140 | c.Assert(validateQuery(q1, pp1), Equals, true) 141 | c.Assert(tsRegex.MatchString(q1), Equals, true) 142 | 143 | p2 := CalendarsParameters{ 144 | IncludeSubCalendarID: []string{ 145 | "1a72410b-6417-4869-9260-9ec13816e481", 146 | "1a72410b-6417-4869-9260-9ec13816e482", 147 | }, 148 | ViewingSpaceKey: "ABC", 149 | CalendarContext: CALENDAR_CONTEXT_MY, 150 | } 151 | 152 | pp2 := []string{ 153 | "calendarContext=myCalendars", 154 | "viewingSpaceKey=ABC", 155 | "include=1a72410b-6417-4869-9260-9ec13816e481", 156 | "include=1a72410b-6417-4869-9260-9ec13816e482", 157 | } 158 | 159 | q2 := p2.ToQuery() 160 | 161 | c.Assert(validateQuery(q2, pp2), Equals, true) 162 | c.Assert(tsRegex.MatchString(q2), Equals, true) 163 | } 164 | 165 | func (s *ConfluenceSuite) TestAuthMethods(c *C) { 166 | b1 := AuthBasic{"JohnDoe", "Test1234!"} 167 | b2 := AuthBasic{"", "Test1234!"} 168 | b3 := AuthBasic{"JohnDoe", ""} 169 | 170 | c.Assert(b1.Encode(), Equals, "Basic Sm9obkRvZTpUZXN0MTIzNCE=") 171 | c.Assert(b1.Validate(), IsNil) 172 | c.Assert(b2.Validate(), DeepEquals, ErrEmptyUser) 173 | c.Assert(b3.Validate(), DeepEquals, ErrEmptyPassword) 174 | 175 | t1 := AuthToken{"TESTVYhExHzKbHzNPCMRmviasXJoUaATysUimxwiWmkr"} 176 | t2 := AuthToken{""} 177 | t3 := AuthToken{"TEST"} 178 | 179 | c.Assert(t1.Encode(), Equals, "Bearer TESTVYhExHzKbHzNPCMRmviasXJoUaATysUimxwiWmkr") 180 | c.Assert(t1.Validate(), IsNil) 181 | c.Assert(t2.Validate(), DeepEquals, ErrEmptyToken) 182 | c.Assert(t3.Validate(), DeepEquals, ErrTokenWrongLength) 183 | } 184 | 185 | // ////////////////////////////////////////////////////////////////////////////////// // 186 | 187 | func validateQuery(query string, parts []string) bool { 188 | queryParts := strings.Split(query, "&") 189 | 190 | LOOP: 191 | for _, part := range parts { 192 | for _, qp := range queryParts { 193 | if part == qp { 194 | continue LOOP 195 | } 196 | } 197 | 198 | return false 199 | } 200 | 201 | return true 202 | } 203 | -------------------------------------------------------------------------------- /team_calandars.go: -------------------------------------------------------------------------------- 1 | package confluence 2 | 3 | // ////////////////////////////////////////////////////////////////////////////////// // 4 | // // 5 | // Copyright (c) 2025 ESSENTIAL KAOS // 6 | // Apache License, Version 2.0 // 7 | // // 8 | // ////////////////////////////////////////////////////////////////////////////////// // 9 | 10 | import ( 11 | "errors" 12 | "regexp" 13 | "time" 14 | ) 15 | 16 | // ////////////////////////////////////////////////////////////////////////////////// // 17 | 18 | const _REST_BASE = "/rest/calendar-services/1.0" 19 | 20 | // ////////////////////////////////////////////////////////////////////////////////// // 21 | 22 | // Calendar context 23 | const ( 24 | CALENDAR_CONTEXT_MY = "myCalendars" 25 | CALENDAR_CONTEXT_SPACE = "spaceCalendars" 26 | ) 27 | 28 | // ////////////////////////////////////////////////////////////////////////////////// // 29 | 30 | // CalendarEventsParameters contains request params for events from Team Calendars API 31 | type CalendarEventsParameters struct { 32 | SubCalendarID string `query:"subCalendarId"` 33 | UserTimezoneID string `query:"userTimeZoneId"` 34 | Start time.Time `query:"start,timedate"` 35 | End time.Time `query:"end,timedate"` 36 | 37 | timestamp int64 `query:"_"` 38 | } 39 | 40 | // CalendarsParameters contains request params for calendars from Team Calendars API 41 | type CalendarsParameters struct { 42 | IncludeSubCalendarID []string `query:"include,unwrap"` 43 | CalendarContext string `query:"calendarContext"` 44 | ViewingSpaceKey string `query:"viewingSpaceKey"` 45 | 46 | timestamp int64 `query:"_"` 47 | } 48 | 49 | // ////////////////////////////////////////////////////////////////////////////////// // 50 | 51 | // CalendarEventCollection contains slice with events 52 | type CalendarEventCollection struct { 53 | Events []*CalendarEvent `json:"events"` 54 | Success bool `json:"success"` 55 | } 56 | 57 | // CalendarCollection contains slice with calendars 58 | type CalendarCollection struct { 59 | Calendars []*Calendar `json:"payload"` 60 | Success bool `json:"success"` 61 | } 62 | 63 | // Calendar represents Team Calendars calendar 64 | type Calendar struct { 65 | UsersPermittedToView []*PermsUser `json:"usersPermittedToView"` 66 | UsersPermittedToEdit []*PermsUser `json:"usersPermittedToEdit"` 67 | GroupsPermittedToView []string `json:"groupsPermittedToView"` 68 | GroupsPermittedToEdit []string `json:"groupsPermittedToEdit"` 69 | Warnings []string `json:"warnings"` 70 | ChildSubCalendars []*Calendar `json:"childSubCalendars"` 71 | SubscriberCount int `json:"subscriberCount"` 72 | SubCalendar *SubCalendar `json:"subCalendar"` 73 | ReminderMe bool `json:"reminderMe"` 74 | IsHidden bool `json:"hidden"` 75 | IsEditable bool `json:"editable"` 76 | IsReloadable bool `json:"reloadable"` 77 | IsDeletable bool `json:"deletable"` 78 | IsEventsHidden bool `json:"eventsHidden"` 79 | IsWatchedViaContent bool `json:"watchedViaContent"` 80 | IsAdministrable bool `json:"administrable"` 81 | IsWatched bool `json:"watched"` 82 | IsEventsViewable bool `json:"eventsViewable"` 83 | IsEventsEditable bool `json:"eventsEditable"` 84 | IsSubscribedByCurrentUser bool `json:"subscribedByCurrentUser"` 85 | } 86 | 87 | // SubCalendar represents Team Calendars sub-calendar 88 | type SubCalendar struct { 89 | DisableEventTypes []string `json:"disableEventTypes"` 90 | CustomEventTypes []*CustomEventType `json:"customEventTypes"` 91 | SanboxEventTypeReminders []*EventTypeReminder `json:"sanboxEventTypeReminders"` 92 | Creator string `json:"creator"` 93 | TypeKey string `json:"typeKey"` 94 | Color string `json:"color"` 95 | TimeZoneID string `json:"timeZoneId"` 96 | Description string `json:"description"` 97 | Type string `json:"type"` 98 | SpaceKey string `json:"spaceKey"` 99 | SpaceName string `json:"spaceName"` 100 | Name string `json:"name"` 101 | ID string `json:"id"` 102 | IsWatchable bool `json:"watchable"` 103 | IsEventInviteesSupported bool `json:"eventInviteesSupported"` 104 | IsRestrictable bool `json:"restrictable"` 105 | } 106 | 107 | // CustomEventType contains info about custom event type 108 | type CustomEventType struct { 109 | Created string `json:"created"` 110 | Icon string `json:"icon"` 111 | PeriodInMins int `json:"periodInMins"` 112 | CustomEventTypeID string `json:"customEventTypeId"` 113 | Title string `json:"title"` 114 | ParentSubCalendarID string `json:"parentSubCalendarId"` 115 | } 116 | 117 | // EventTypeReminder contains info about event reminder 118 | type EventTypeReminder struct { 119 | EventTypeID string `json:"eventTypeId"` 120 | PeriodInMins int `json:"periodInMins"` 121 | IsCustomEventType bool `json:"isCustomEventType"` 122 | } 123 | 124 | // CalendarEvent represents Team Calendars event 125 | type CalendarEvent struct { 126 | Invitees []*CalendarUser `json:"invitees"` 127 | WorkingURL string `json:"workingUrl"` 128 | Description string `json:"description"` 129 | ClassName string `json:"className"` 130 | ShortTitle string `json:"shortTitle"` 131 | Title string `json:"title"` 132 | EventType string `json:"eventType"` 133 | ID string `json:"id"` 134 | CustomEventTypeID string `json:"customEventTypeId"` 135 | SubCalendarID string `json:"subCalendarId"` 136 | IconURL string `json:"iconUrl"` 137 | IconLink string `json:"iconLink"` 138 | MediumIconURL string `json:"mediumIconUrl"` 139 | BackgroundColor string `json:"backgroundColor"` 140 | BorderColor string `json:"borderColor"` 141 | TextColor string `json:"textColor"` 142 | ColorScheme string `json:"colorScheme"` 143 | Where string `json:"where"` 144 | FormattedStartDate string `json:"confluenceFormattedStartDate"` 145 | Start *Date `json:"start"` 146 | End *Date `json:"end"` 147 | OriginalStartDateTime *Date `json:"originalStartDateTime"` 148 | OriginalEndDateTime *Date `json:"originalEndDateTime"` 149 | IsExpandDates bool `json:"expandDates"` 150 | IsEditable bool `json:"editable"` 151 | IsAllDay bool `json:"allDay"` 152 | } 153 | 154 | // CalendarUser represents Team Calendars user 155 | type CalendarUser struct { 156 | DisplayName string `json:"displayName"` 157 | Name string `json:"name"` 158 | ID string `json:"id"` 159 | Type string `json:"type"` 160 | AvatarIconURL string `json:"avatarIconUrl"` 161 | Email string `json:"email"` 162 | } 163 | 164 | // PermsUser represents Team Calendars permissions user 165 | type PermsUser struct { 166 | AvatarURL string `json:"avatarUrl"` 167 | Name string `json:"name"` 168 | DisplayName string `json:"fullName"` 169 | Key string `json:"id"` 170 | } 171 | 172 | // ////////////////////////////////////////////////////////////////////////////////// // 173 | 174 | var idValidationRegex = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`) 175 | 176 | // ////////////////////////////////////////////////////////////////////////////////// // 177 | 178 | // GetCalendarEvents fetch events from given calendar 179 | func (api *API) GetCalendarEvents(params CalendarEventsParameters) (*CalendarEventCollection, error) { 180 | result := &CalendarEventCollection{} 181 | statusCode, err := api.doRequest( 182 | "GET", _REST_BASE+"/calendar/events.json", 183 | params, result, nil, 184 | ) 185 | 186 | if err != nil { 187 | return nil, err 188 | } 189 | 190 | if statusCode == 403 { 191 | return nil, ErrNoPerms 192 | } 193 | 194 | return result, nil 195 | } 196 | 197 | func (api *API) GetCalendars(params CalendarsParameters) (*CalendarCollection, error) { 198 | result := &CalendarCollection{} 199 | statusCode, err := api.doRequest( 200 | "GET", _REST_BASE+"/calendar/subcalendars.json", 201 | params, result, nil, 202 | ) 203 | 204 | if err != nil { 205 | return nil, err 206 | } 207 | 208 | if statusCode == 403 { 209 | return nil, ErrNoPerms 210 | } 211 | 212 | return result, nil 213 | } 214 | 215 | // IsValidCalendarID validates calendar ID 216 | func IsValidCalendarID(id string) bool { 217 | return idValidationRegex.MatchString(id) 218 | } 219 | 220 | // ////////////////////////////////////////////////////////////////////////////////// // 221 | 222 | // Validate validates parameters 223 | func (p CalendarEventsParameters) Validate() error { 224 | switch { 225 | case p.SubCalendarID == "": 226 | return errors.New("SubCalendarID is mandatory and must be set") 227 | 228 | case !IsValidCalendarID(p.SubCalendarID): 229 | return errors.New("SubCalendarID contains invalid calendar ID") 230 | 231 | case p.UserTimezoneID == "": 232 | return errors.New("UserTimezoneID is mandatory and must be set") 233 | 234 | case p.Start.IsZero(): 235 | return errors.New("Start is mandatory and must be set") 236 | 237 | case p.End.IsZero(): 238 | return errors.New("End is mandatory and must be set") 239 | } 240 | 241 | return nil 242 | } 243 | 244 | // Validate validates parameters 245 | func (p CalendarsParameters) Validate() error { 246 | if p.CalendarContext == "" { 247 | return errors.New("CalendarContext is mandatory and must be set") 248 | } 249 | 250 | if p.CalendarContext == CALENDAR_CONTEXT_MY { 251 | return nil 252 | } 253 | 254 | switch { 255 | case len(p.IncludeSubCalendarID) == 0: 256 | return errors.New("IncludeSubCalendarID is mandatory and must be set") 257 | 258 | case p.ViewingSpaceKey == "": 259 | return errors.New("ViewingSpaceKey is mandatory and must be set") 260 | } 261 | 262 | for _, id := range p.IncludeSubCalendarID { 263 | if id == "" { 264 | return errors.New("IncludeSubCalendarID is mandatory and must be set") 265 | } 266 | 267 | if !IsValidCalendarID(id) { 268 | return errors.New("IncludeSubCalendarID contains invalid calendar ID") 269 | } 270 | } 271 | 272 | return nil 273 | } 274 | 275 | // ////////////////////////////////////////////////////////////////////////////////// // 276 | 277 | // ToQuery convert params to URL query 278 | func (p CalendarEventsParameters) ToQuery() string { 279 | p.timestamp = time.Now().UnixNano() 280 | return paramsToQuery(p) 281 | } 282 | 283 | // ToQuery convert params to URL query 284 | func (p CalendarsParameters) ToQuery() string { 285 | p.timestamp = time.Now().UnixNano() 286 | return paramsToQuery(p) 287 | } 288 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2020 ESSENTIAL KAOS 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /api.go: -------------------------------------------------------------------------------- 1 | package confluence 2 | 3 | // ////////////////////////////////////////////////////////////////////////////////// // 4 | // // 5 | // Copyright (c) 2025 ESSENTIAL KAOS // 6 | // Apache License, Version 2.0 // 7 | // // 8 | // ////////////////////////////////////////////////////////////////////////////////// // 9 | 10 | import ( 11 | "errors" 12 | "fmt" 13 | "strconv" 14 | "strings" 15 | "time" 16 | ) 17 | 18 | // ////////////////////////////////////////////////////////////////////////////////// // 19 | 20 | // Content type 21 | const ( 22 | CONTENT_TYPE_ATTACHMENT = "attachment" 23 | CONTENT_TYPE_BLOGPOST = "blogpost" 24 | CONTENT_TYPE_COMMENT = "comment" 25 | CONTENT_TYPE_PAGE = "page" 26 | ) 27 | 28 | // Excerpt values 29 | const ( 30 | SEARCH_EXCERPT_INDEXED = "indexed" 31 | SEARCH_EXCERPT_HIGHLIGHT = "highlight" 32 | SEARCH_EXCERPT_NONE = "none" 33 | ) 34 | 35 | // Space type 36 | const ( 37 | SPACE_TYPE_PERSONAL = "personal" 38 | SPACE_TYPE_GLOBAL = "global" 39 | ) 40 | 41 | // Content status 42 | const ( 43 | SPACE_STATUS_CURRENT = "current" 44 | SPACE_STATUS_ARCHIVED = "archived" 45 | ) 46 | 47 | // Space status 48 | const ( 49 | CONTENT_STATUS_CURRENT = "current" 50 | CONTENT_STATUS_TRASHED = "trashed" 51 | CONTENT_STATUS_DRAFT = "draft" 52 | ) 53 | 54 | // Units 55 | const ( 56 | UNITS_MINUTES = "minutes" 57 | UNITS_HOURS = "hours" 58 | UNITS_DAYS = "days" 59 | UNITS_MONTHS = "months" 60 | UNITS_YEARS = "years" 61 | ) 62 | 63 | // Operations types 64 | const ( 65 | OPERATION_READ = "read" 66 | OPERATION_UPDATE = "update" 67 | ) 68 | 69 | // ////////////////////////////////////////////////////////////////////////////////// // 70 | 71 | // Parameters is interface for parameters structs 72 | type Parameters interface { 73 | ToQuery() string 74 | Validate() error 75 | } 76 | 77 | // ////////////////////////////////////////////////////////////////////////////////// // 78 | 79 | // Date is RFC3339 encoded date 80 | type Date struct { 81 | time.Time 82 | } 83 | 84 | // Timestamp is UNIX timestamp in ms 85 | type Timestamp struct { 86 | time.Time 87 | } 88 | 89 | // ContainerID is container ID 90 | type ContainerID string 91 | 92 | // ExtensionPosition is extension position 93 | type ExtensionPosition int 94 | 95 | // EmptyParameters is empty parameters 96 | type EmptyParameters struct { 97 | // nothing 98 | } 99 | 100 | // ExpandParameters is params with field expand info 101 | type ExpandParameters struct { 102 | Expand []string `query:"expand"` 103 | } 104 | 105 | // CollectionParameters is params with pagination info 106 | type CollectionParameters struct { 107 | Expand []string `query:"expand"` 108 | Start int `query:"start"` 109 | Limit int `query:"limit"` 110 | } 111 | 112 | // AUDIT /////////////////////////////////////////////////////////////////////////////// 113 | 114 | // AuditParameters is params for fetching audit data 115 | type AuditParameters struct { 116 | StartDate time.Time `query:"startDate"` 117 | EndDate time.Time `query:"endDate"` 118 | SearchString string `query:"searchString"` 119 | Start int `query:"start"` 120 | Limit int `query:"limit"` 121 | } 122 | 123 | // AuditSinceParameters is params for fetching audit data 124 | type AuditSinceParameters struct { 125 | Number int `query:"number"` 126 | Units string `query:"units"` 127 | SearchString string `query:"searchString"` 128 | Start int `query:"start"` 129 | Limit int `query:"limit"` 130 | } 131 | 132 | // AuditRecord represents audit record 133 | type AuditRecord struct { 134 | Author *User `json:"author"` 135 | RemoteAddress string `json:"remoteAddress"` 136 | CreationDate *Timestamp `json:"creationDate"` 137 | Summary string `json:"summary"` 138 | Description string `json:"description"` 139 | Category string `json:"category"` 140 | IsSysAdmin bool `json:"sysAdmin"` 141 | } 142 | 143 | // AuditRecordCollection contains paginated list of audit record 144 | type AuditRecordCollection struct { 145 | Results []*AuditRecord `json:"results"` 146 | Start int `json:"start"` 147 | Limit int `json:"limit"` 148 | Size int `json:"size"` 149 | } 150 | 151 | // AuditRetentionInfo contains info about retention time 152 | type AuditRetentionInfo struct { 153 | Number int `json:"number"` 154 | Units string `json:"units"` 155 | } 156 | 157 | // ATTACHMENTS ///////////////////////////////////////////////////////////////////////// 158 | 159 | // AttachmentParameters is params for fetching attachments info 160 | type AttachmentParameters struct { 161 | Filename string `query:"filename"` 162 | MediaType string `query:"mediaType"` 163 | Expand []string `query:"expand"` 164 | Start int `query:"start"` 165 | Limit int `query:"limit"` 166 | } 167 | 168 | // CONTENT ///////////////////////////////////////////////////////////////////////////// 169 | 170 | // ContentParameters is params for fetching content info 171 | type ContentParameters struct { 172 | Type string `query:"type"` 173 | SpaceKey string `query:"spaceKey"` 174 | Title string `query:"title"` 175 | Status string `query:"status"` 176 | PostingDay time.Time `query:"postingDay"` 177 | Expand []string `query:"expand"` 178 | Start int `query:"start"` 179 | Limit int `query:"limit"` 180 | } 181 | 182 | // ContentIDParameters is params for fetching content info 183 | type ContentIDParameters struct { 184 | Status string `query:"status"` 185 | Version int `query:"version"` 186 | Expand []string `query:"expand"` 187 | } 188 | 189 | // ContentSearchParameters is params for searching content 190 | type ContentSearchParameters struct { 191 | CQL string `query:"cql"` 192 | CQLContext string `query:"cqlcontext"` 193 | Expand []string `query:"expand"` 194 | Start int `query:"start"` 195 | Limit int `query:"limit"` 196 | } 197 | 198 | // ChildrenParameters is params for fetching content child info 199 | type ChildrenParameters struct { 200 | ParentVersion int `query:"parentVersion"` 201 | Location string `query:"location"` 202 | Depth string `query:"depth"` 203 | Expand []string `query:"expand"` 204 | Start int `query:"start"` 205 | Limit int `query:"limit"` 206 | } 207 | 208 | // Content contains content info 209 | type Content struct { 210 | ID string `json:"id"` 211 | Type string `json:"type"` 212 | Status string `json:"status"` 213 | Title string `json:"title"` 214 | Extensions *Extensions `json:"extensions"` 215 | Metadata *Metadata `json:"metadata"` 216 | Container *Container `json:"container"` 217 | Space *Space `json:"space"` 218 | Version *Version `json:"version"` 219 | Operations []*Operation `json:"operations"` 220 | Children *Contents `json:"children"` 221 | Ancestors []*Content `json:"ancestors"` 222 | Descendants *Contents `json:"descendants"` 223 | Body *Body `json:"body"` 224 | Links *Links `json:"_links"` 225 | } 226 | 227 | // ContentCollection represents paginated list of content 228 | type ContentCollection struct { 229 | Results []*Content `json:"results"` 230 | Start int `json:"start"` 231 | Limit int `json:"limit"` 232 | Size int `json:"size"` 233 | } 234 | 235 | // Contents contains all types of content 236 | type Contents struct { 237 | Attachments *ContentCollection `json:"attachment"` 238 | Comments *ContentCollection `json:"comment"` 239 | Pages *ContentCollection `json:"page"` 240 | Blogposts *ContentCollection `json:"blogposts"` 241 | } 242 | 243 | // Body contains content data 244 | type Body struct { 245 | View *View `json:"view"` 246 | ExportView *View `json:"export_view"` 247 | StyledView *View `json:"styled_view"` 248 | StorageView *View `json:"storage"` 249 | } 250 | 251 | // View is data view 252 | type View struct { 253 | Representation string `json:"representation"` 254 | Value string `json:"value"` 255 | } 256 | 257 | // Version contains info about content version 258 | type Version struct { 259 | Message string `json:"message"` 260 | By *User `json:"by"` 261 | When *Date `json:"when"` 262 | Number int `json:"number"` 263 | Content *Content `json:"content"` 264 | IsMinorEdit bool `json:"minorEdit"` 265 | IsHidden bool `json:"hidden"` 266 | } 267 | 268 | // Extensions contains info about content extensions 269 | type Extensions struct { 270 | Position ExtensionPosition `json:"position"` // Page 271 | MediaType string `json:"mediaType"` // Attachment 272 | FileSize int `json:"fileSize"` // Attachment 273 | Comment string `json:"comment"` // Attachment 274 | Location string `json:"location"` // Comment 275 | Resolution *Resolution `json:"resolution"` // Comment 276 | } 277 | 278 | // Resolution contains resolution info 279 | type Resolution struct { 280 | Status string `json:"status"` 281 | LastModifier *User `json:"lastModifier"` 282 | LastModifiedDate *Date `json:"lastModifiedDate"` 283 | } 284 | 285 | // Operation contains operation info 286 | type Operation struct { 287 | Name string `json:"operation"` 288 | TargetType string `json:"targetType"` 289 | } 290 | 291 | // Metadata contains metadata records 292 | type Metadata struct { 293 | Labels *LabelCollection `json:"labels"` // Page 294 | MediaType string `json:"mediaType"` // Attachment 295 | } 296 | 297 | // History contains info about content history 298 | type History struct { 299 | CreatedBy *User `json:"createdBy"` 300 | CreatedDate *Date `json:"createdDate"` 301 | LastUpdated *Version `json:"lastUpdated"` 302 | PreviousVersion *Version `json:"previousVersion"` 303 | NextVersion *Version `json:"nextVersion"` 304 | Contributors *Contributors `json:"contributors"` 305 | IsLatest bool `json:"latest"` 306 | } 307 | 308 | // Contributors contains contributors list 309 | type Contributors struct { 310 | Publishers *Publishers `json:"publishers"` 311 | } 312 | 313 | // Publishers contains info about users 314 | type Publishers struct { 315 | Users []*User `json:"users"` 316 | UserKeys []string `json:"userKeys"` 317 | } 318 | 319 | // Container contains basic container info 320 | type Container struct { 321 | ID ContainerID `json:"id"` 322 | Key string `json:"key"` // Space 323 | Name string `json:"name"` // Space 324 | Title string `json:"title"` // Page or blogpost 325 | Links *Links `json:"_links"` 326 | } 327 | 328 | // LABELS ////////////////////////////////////////////////////////////////////////////// 329 | 330 | // LabelParameters is params for fetching labels 331 | type LabelParameters struct { 332 | Prefix string `query:"prefix"` 333 | Start int `query:"start"` 334 | Limit int `query:"limit"` 335 | } 336 | 337 | // LabelCollection contains paginated list of labels 338 | type LabelCollection struct { 339 | Result []*Label `json:"results"` 340 | Start int `json:"start"` 341 | Limit int `json:"limit"` 342 | Size int `json:"size"` 343 | } 344 | 345 | // Label contains label info 346 | type Label struct { 347 | Prefix string `json:"prefix"` 348 | Name string `json:"name"` 349 | ID string `json:"id"` 350 | } 351 | 352 | // GROUPS ////////////////////////////////////////////////////////////////////////////// 353 | 354 | // Group contains group info 355 | type Group struct { 356 | Type string `json:"type"` 357 | Name string `json:"name"` 358 | } 359 | 360 | // GroupCollection contains paginated list of groups 361 | type GroupCollection struct { 362 | Results []*Group `json:"results"` 363 | Start int `json:"start"` 364 | Limit int `json:"limit"` 365 | Size int `json:"size"` 366 | } 367 | 368 | // RESTRICTIONS //////////////////////////////////////////////////////////////////////// 369 | 370 | // Restrictions contains info about all restrictions 371 | type Restrictions struct { 372 | Read *Restriction `json:"read"` 373 | Update *Restriction `json:"update"` 374 | } 375 | 376 | // Restriction contains restriction info for single operation 377 | type Restriction struct { 378 | Operation string `json:"operation"` 379 | Data *RestrictionData `json:"restrictions"` 380 | } 381 | 382 | // RestrictionData contains restrictions data 383 | type RestrictionData struct { 384 | User *UserCollection `json:"user"` 385 | Group *GroupCollection `json:"group"` 386 | } 387 | 388 | // SEARCH ////////////////////////////////////////////////////////////////////////////// 389 | 390 | // SearchParameters is params for fetching search results 391 | type SearchParameters struct { 392 | Expand []string `query:"expand"` 393 | CQL string `query:"cql"` 394 | CQLContext string `query:"cqlcontext"` 395 | Excerpt string `query:"excerpt"` 396 | Start int `query:"start"` 397 | Limit int `query:"limit"` 398 | IncludeArchivedSpaces bool `query:"includeArchivedSpaces"` 399 | } 400 | 401 | // SearchResult contains contains paginated list of search results 402 | type SearchResult struct { 403 | Results []*SearchEntity `json:"results"` 404 | Start int `json:"start"` 405 | Limit int `json:"limit"` 406 | Size int `json:"size"` 407 | TotalSize int `json:"totalSize"` 408 | CQLQuery string `json:"cqlQuery"` 409 | SearchDuration int `json:"searchDuration"` 410 | } 411 | 412 | // SearchEntity contains search result 413 | type SearchEntity struct { 414 | Content *Content `json:"content"` 415 | Space *Space `json:"space"` 416 | User *User `json:"user"` 417 | Title string `json:"title"` 418 | Excerpt string `json:"excerpt"` 419 | URL string `json:"url"` 420 | EntityType string `json:"entityType"` 421 | LastModified *Date `json:"lastModified"` 422 | } 423 | 424 | // SPACE /////////////////////////////////////////////////////////////////////////////// 425 | 426 | // SpaceParameters is params for fetching info about space 427 | type SpaceParameters struct { 428 | SpaceKey []string `query:"spaceKey,unwrap"` 429 | Expand []string `query:"expand"` 430 | Type string `query:"type"` 431 | Status string `query:"status"` 432 | Label string `query:"label"` 433 | Depth string `query:"depth"` 434 | Start int `query:"start"` 435 | Limit int `query:"limit"` 436 | Favourite bool `query:"favourite"` 437 | } 438 | 439 | // Space contains info about space 440 | type Space struct { 441 | ID int `json:"id"` 442 | Key string `json:"key"` 443 | Name string `json:"name"` 444 | Icon *Icon `json:"icon"` 445 | Type string `json:"type"` 446 | Links *Links `json:"_links"` 447 | } 448 | 449 | // SpaceCollection contains paginated list of spaces 450 | type SpaceCollection struct { 451 | Results []*Space `json:"results"` 452 | Start int `json:"start"` 453 | Limit int `json:"limit"` 454 | Size int `json:"size"` 455 | } 456 | 457 | // Icon contains icon info 458 | type Icon struct { 459 | Path string `json:"path"` 460 | Width int `json:"width"` 461 | Height int `json:"height"` 462 | IsDefault bool `json:"isDefault"` 463 | } 464 | 465 | // USER //////////////////////////////////////////////////////////////////////////////// 466 | 467 | // UserParameters is params for fetching info about user 468 | type UserParameters struct { 469 | Key string `query:"key"` 470 | Username string `query:"username"` 471 | Expand []string `query:"expand"` 472 | Start int `query:"start"` 473 | Limit int `query:"limit"` 474 | } 475 | 476 | // User contains user info 477 | type User struct { 478 | Type string `json:"type"` 479 | Name string `json:"username"` 480 | Key string `json:"userKey"` 481 | ProfilePicture *Icon `json:"profilePicture"` 482 | DisplayName string `json:"displayName"` 483 | } 484 | 485 | // UserCollection contains paginated list of users 486 | type UserCollection struct { 487 | Results []*User `json:"results"` 488 | Start int `json:"start"` 489 | Limit int `json:"limit"` 490 | Size int `json:"size"` 491 | } 492 | 493 | // LINKS /////////////////////////////////////////////////////////////////////////////// 494 | 495 | // Links contains links 496 | type Links struct { 497 | WebUI string `json:"webui"` 498 | TinyUI string `json:"tinyui"` 499 | Base string `json:"base"` 500 | } 501 | 502 | // WATCH /////////////////////////////////////////////////////////////////////////////// 503 | 504 | // WatchParameters is params for fetching info about watchers 505 | type WatchParameters struct { 506 | Key string `query:"key"` 507 | Username string `query:"username"` 508 | ContentType string `query:"contentType"` 509 | } 510 | 511 | // ListWatchersParameters is params for fetching info about page watchers 512 | type ListWatchersParameters struct { 513 | PageID string `query:"pageId"` 514 | } 515 | 516 | // WatchStatus contains watching status 517 | type WatchStatus struct { 518 | IsWatching bool `json:"watching"` 519 | } 520 | 521 | // WatchInfo contains info about watchers 522 | type WatchInfo struct { 523 | PageWatchers []*Watcher `json:"pageWatchers"` 524 | SpaceWatchers []*Watcher `json:"spaceWatchers"` 525 | } 526 | 527 | // Watcher contains watcher info 528 | type Watcher struct { 529 | AvatarURL string `json:"avatarUrl"` 530 | Name string `json:"name"` 531 | Key string `json:"userKey"` 532 | DisplayName string `json:"fullName"` 533 | Type string `json:"type"` 534 | } 535 | 536 | // ////////////////////////////////////////////////////////////////////////////////// // 537 | 538 | // IsAttachment return true if content is attachment 539 | func (c *Content) IsAttachment() bool { 540 | return c.Type == CONTENT_TYPE_ATTACHMENT 541 | } 542 | 543 | // IsComment return true if content is comment 544 | func (c *Content) IsComment() bool { 545 | return c.Type == CONTENT_TYPE_COMMENT 546 | } 547 | 548 | // IsPage return true if content is page 549 | func (c *Content) IsPage() bool { 550 | return c.Type == CONTENT_TYPE_PAGE 551 | } 552 | 553 | // IsTrashed return true if content is trashed 554 | func (c *Content) IsTrashed() bool { 555 | return c.Status == CONTENT_STATUS_TRASHED 556 | } 557 | 558 | // IsDraft return true if content is draft 559 | func (c *Content) IsDraft() bool { 560 | return c.Status == CONTENT_STATUS_DRAFT 561 | } 562 | 563 | // IsGlobal return true if space is global 564 | func (s *Space) IsGlobal() bool { 565 | return s.Type == SPACE_TYPE_GLOBAL 566 | } 567 | 568 | // IsPersonal return true if space is personal 569 | func (s *Space) IsPersonal() bool { 570 | return s.Type == SPACE_TYPE_PERSONAL 571 | } 572 | 573 | // IsArchived return true if space is archived 574 | func (s *Space) IsArchived() bool { 575 | return s.Type == SPACE_STATUS_ARCHIVED 576 | } 577 | 578 | // IsPage return true if container is page 579 | func (c *Container) IsPage() bool { 580 | return c.Title != "" 581 | } 582 | 583 | // IsSpace return true if container is space 584 | func (c *Container) IsSpace() bool { 585 | return c.Key != "" 586 | } 587 | 588 | // Combined return united slice with all watchers 589 | func (wi *WatchInfo) Combined() []*Watcher { 590 | var result []*Watcher 591 | 592 | result = append(result, wi.PageWatchers...) 593 | 594 | MAINLOOP: 595 | for _, watcher := range wi.SpaceWatchers { 596 | for _, pageWatcher := range wi.PageWatchers { 597 | if watcher.Key == pageWatcher.Key { 598 | continue MAINLOOP 599 | } 600 | } 601 | 602 | result = append(result, watcher) 603 | } 604 | 605 | return result 606 | } 607 | 608 | // ////////////////////////////////////////////////////////////////////////////////// // 609 | 610 | // UnmarshalJSON is custom Date format unmarshaler 611 | func (d *Date) UnmarshalJSON(b []byte) error { 612 | var err error 613 | 614 | d.Time, err = time.Parse(time.RFC3339, strings.Trim(string(b), "\"")) 615 | 616 | if err != nil { 617 | return fmt.Errorf("Cannot unmarshal Date value: %v", err) 618 | } 619 | 620 | return nil 621 | } 622 | 623 | // UnmarshalJSON is custom container ID unmarshaler 624 | func (c *ContainerID) UnmarshalJSON(b []byte) error { 625 | switch { 626 | case len(b) == 0: 627 | // nop 628 | case b[0] == '"': 629 | *c = ContainerID(strings.ReplaceAll(string(b), `"`, "")) 630 | default: 631 | *c = ContainerID(string(b)) 632 | } 633 | 634 | return nil 635 | } 636 | 637 | // UnmarshalJSON is custom position unmarshaler 638 | func (ep *ExtensionPosition) UnmarshalJSON(b []byte) error { 639 | if string(b) == "\"none\"" { 640 | *ep = ExtensionPosition(-1) 641 | return nil 642 | } 643 | 644 | v, err := strconv.Atoi(string(b)) 645 | 646 | if err != nil { 647 | return fmt.Errorf("Cannot unmarshal ExtensionPosition value: %v", err) 648 | } 649 | 650 | *ep = ExtensionPosition(v) 651 | 652 | return nil 653 | } 654 | 655 | // UnmarshalJSON is custom Timestamp format unmarshaler 656 | func (d *Timestamp) UnmarshalJSON(b []byte) error { 657 | ts, err := strconv.ParseInt(string(b), 10, 64) 658 | 659 | if err != nil { 660 | return err 661 | } 662 | 663 | d.Time = time.Unix(ts/1000, (ts%1000)*1000000) 664 | 665 | if err != nil { 666 | return fmt.Errorf("Cannot unmarshal Timestamp value: %v", err) 667 | } 668 | 669 | return nil 670 | } 671 | 672 | // ////////////////////////////////////////////////////////////////////////////////// // 673 | 674 | // Validate validates parameters 675 | func (p EmptyParameters) Validate() error { 676 | return nil 677 | } 678 | 679 | // Validate validates parameters 680 | func (p ExpandParameters) Validate() error { 681 | return nil 682 | } 683 | 684 | // Validate validates parameters 685 | func (p CollectionParameters) Validate() error { 686 | return nil 687 | } 688 | 689 | // Validate validates parameters 690 | func (p AuditParameters) Validate() error { 691 | return nil 692 | } 693 | 694 | // Validate validates parameters 695 | func (p AuditSinceParameters) Validate() error { 696 | return nil 697 | } 698 | 699 | // Validate validates parameters 700 | func (p ContentParameters) Validate() error { 701 | if p.SpaceKey == "" { 702 | return errors.New("SpaceKey is mandatory and must be set") 703 | } 704 | 705 | return nil 706 | } 707 | 708 | // Validate validates parameters 709 | func (p ContentIDParameters) Validate() error { 710 | return nil 711 | } 712 | 713 | // Validate validates parameters 714 | func (p ContentSearchParameters) Validate() error { 715 | return nil 716 | } 717 | 718 | // Validate validates parameters 719 | func (p ChildrenParameters) Validate() error { 720 | return nil 721 | } 722 | 723 | // Validate validates parameters 724 | func (p AttachmentParameters) Validate() error { 725 | return nil 726 | } 727 | 728 | // Validate validates parameters 729 | func (p LabelParameters) Validate() error { 730 | return nil 731 | } 732 | 733 | // Validate validates parameters 734 | func (p SearchParameters) Validate() error { 735 | if p.CQL == "" { 736 | return errors.New("CQL is mandatory and must be set") 737 | } 738 | 739 | return nil 740 | } 741 | 742 | // Validate validates parameters 743 | func (p SpaceParameters) Validate() error { 744 | if len(p.SpaceKey) == 0 { 745 | return errors.New("SpaceKey is mandatory and must be set") 746 | } 747 | 748 | return nil 749 | } 750 | 751 | // Validate validates parameters 752 | func (p UserParameters) Validate() error { 753 | if p.Key == "" && p.Username == "" { 754 | return errors.New("Key or Username must be set") 755 | } 756 | 757 | return nil 758 | } 759 | 760 | // Validate validates parameters 761 | func (p WatchParameters) Validate() error { 762 | return nil 763 | } 764 | 765 | // Validate validates parameters 766 | func (p ListWatchersParameters) Validate() error { 767 | return nil 768 | } 769 | 770 | // ////////////////////////////////////////////////////////////////////////////////// // 771 | 772 | // ToQuery convert params to URL query 773 | func (p EmptyParameters) ToQuery() string { 774 | return "" 775 | } 776 | 777 | // ToQuery convert params to URL query 778 | func (p ExpandParameters) ToQuery() string { 779 | return paramsToQuery(p) 780 | } 781 | 782 | // ToQuery convert params to URL query 783 | func (p CollectionParameters) ToQuery() string { 784 | return paramsToQuery(p) 785 | } 786 | 787 | // ToQuery convert params to URL query 788 | func (p AuditParameters) ToQuery() string { 789 | return paramsToQuery(p) 790 | } 791 | 792 | // ToQuery convert params to URL query 793 | func (p AuditSinceParameters) ToQuery() string { 794 | return paramsToQuery(p) 795 | } 796 | 797 | // ToQuery convert params to URL query 798 | func (p ContentParameters) ToQuery() string { 799 | return paramsToQuery(p) 800 | } 801 | 802 | // ToQuery convert params to URL query 803 | func (p ContentIDParameters) ToQuery() string { 804 | return paramsToQuery(p) 805 | } 806 | 807 | // ToQuery convert params to URL query 808 | func (p ContentSearchParameters) ToQuery() string { 809 | return paramsToQuery(p) 810 | } 811 | 812 | // ToQuery convert params to URL query 813 | func (p ChildrenParameters) ToQuery() string { 814 | return paramsToQuery(p) 815 | } 816 | 817 | // ToQuery convert params to URL query 818 | func (p AttachmentParameters) ToQuery() string { 819 | return paramsToQuery(p) 820 | } 821 | 822 | // ToQuery convert params to URL query 823 | func (p LabelParameters) ToQuery() string { 824 | return paramsToQuery(p) 825 | } 826 | 827 | // ToQuery convert params to URL query 828 | func (p SearchParameters) ToQuery() string { 829 | return paramsToQuery(p) 830 | } 831 | 832 | // ToQuery convert params to URL query 833 | func (p SpaceParameters) ToQuery() string { 834 | return paramsToQuery(p) 835 | } 836 | 837 | // ToQuery convert params to URL query 838 | func (p UserParameters) ToQuery() string { 839 | return paramsToQuery(p) 840 | } 841 | 842 | // ToQuery convert params to URL query 843 | func (p WatchParameters) ToQuery() string { 844 | return paramsToQuery(p) 845 | } 846 | 847 | // ToQuery convert params to URL query 848 | func (p ListWatchersParameters) ToQuery() string { 849 | return paramsToQuery(p) 850 | } 851 | -------------------------------------------------------------------------------- /confluence.go: -------------------------------------------------------------------------------- 1 | package confluence 2 | 3 | // ////////////////////////////////////////////////////////////////////////////////// // 4 | // // 5 | // Copyright (c) 2025 ESSENTIAL KAOS // 6 | // Apache License, Version 2.0 // 7 | // // 8 | // ////////////////////////////////////////////////////////////////////////////////// // 9 | 10 | import ( 11 | "encoding/base64" 12 | "encoding/binary" 13 | "encoding/json" 14 | "errors" 15 | "fmt" 16 | "runtime" 17 | "strconv" 18 | "strings" 19 | "time" 20 | 21 | "github.com/valyala/fasthttp" 22 | ) 23 | 24 | // ////////////////////////////////////////////////////////////////////////////////// // 25 | 26 | // API is Confluence API struct 27 | type API struct { 28 | Client *fasthttp.Client // Client is client for http requests 29 | 30 | url string // Confluence URL 31 | auth string // Auth data 32 | } 33 | 34 | // ////////////////////////////////////////////////////////////////////////////////// // 35 | 36 | type restrictionsInfo struct { 37 | Permissions []permission `json:"permissions"` 38 | Users map[string]*restrictionUserInfo `json:"users"` 39 | } 40 | 41 | type restrictionUserInfo struct { 42 | User *Watcher `json:"entity"` 43 | } 44 | 45 | type permission []string 46 | 47 | // ////////////////////////////////////////////////////////////////////////////////// // 48 | 49 | // Errors 50 | var ( 51 | ErrEmptyURL = errors.New("URL can't be empty") 52 | ErrNoPerms = errors.New("User does not have permission to use confluence") 53 | ErrQueryError = errors.New("Query cannot be parsed") 54 | ErrNoContent = errors.New("There is no content with the given id, or if the calling user does not have permission to view the content") 55 | ErrNoSpace = errors.New("There is no space with the given key, or if the calling user does not have permission to view the space") 56 | ErrNoUserPerms = errors.New("User does not have permission to view users") 57 | ErrNoUserFound = errors.New("User with the given username or userkey does not exist") 58 | ) 59 | 60 | var emptyParams = EmptyParameters{} 61 | 62 | // ////////////////////////////////////////////////////////////////////////////////// // 63 | 64 | // NewAPI create new API struct 65 | func NewAPI(url string, auth Auth) (*API, error) { 66 | if url == "" { 67 | return nil, ErrEmptyURL 68 | } 69 | 70 | err := auth.Validate() 71 | 72 | if err != nil { 73 | return nil, err 74 | } 75 | 76 | return &API{ 77 | Client: &fasthttp.Client{ 78 | Name: getUserAgent("", ""), 79 | MaxIdleConnDuration: 5 * time.Second, 80 | ReadTimeout: 3 * time.Second, 81 | WriteTimeout: 3 * time.Second, 82 | MaxConnsPerHost: 150, 83 | }, 84 | 85 | url: url, 86 | auth: auth.Encode(), 87 | }, nil 88 | } 89 | 90 | // SetUserAgent set user-agent string based on app name and version 91 | func (api *API) SetUserAgent(app, version string) { 92 | api.Client.Name = getUserAgent(app, version) 93 | } 94 | 95 | // ////////////////////////////////////////////////////////////////////////////////// // 96 | 97 | // GetAuditRecords fetch a list of AuditRecord instances dating back to a certain time 98 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#audit-getAuditRecords 99 | func (api *API) GetAuditRecords(params AuditParameters) (*AuditRecordCollection, error) { 100 | result := &AuditRecordCollection{} 101 | statusCode, err := api.doRequest( 102 | "GET", "/rest/api/audit", 103 | params, result, nil, 104 | ) 105 | 106 | if err != nil { 107 | return nil, err 108 | } 109 | 110 | switch statusCode { 111 | case 200: 112 | return result, nil 113 | case 403: 114 | return nil, ErrNoPerms 115 | default: 116 | return nil, makeUnknownError(statusCode) 117 | } 118 | } 119 | 120 | // GetAuditRecordsSince fetch a list of AuditRecord instances dating back to a certain time 121 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#audit-getAuditRecords 122 | func (api *API) GetAuditRecordsSince(params AuditSinceParameters) (*AuditRecordCollection, error) { 123 | result := &AuditRecordCollection{} 124 | statusCode, err := api.doRequest( 125 | "GET", "/rest/api/audit/since", 126 | params, result, nil, 127 | ) 128 | 129 | if err != nil { 130 | return nil, err 131 | } 132 | 133 | switch statusCode { 134 | case 200: 135 | return result, nil 136 | case 403: 137 | return nil, ErrNoPerms 138 | default: 139 | return nil, makeUnknownError(statusCode) 140 | } 141 | } 142 | 143 | // GetAuditRetention fetch the current retention period 144 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#audit-getRetentionPeriod 145 | func (api *API) GetAuditRetention() (*AuditRetentionInfo, error) { 146 | result := &AuditRetentionInfo{} 147 | statusCode, err := api.doRequest( 148 | "GET", "/rest/api/audit/retention", 149 | emptyParams, result, nil, 150 | ) 151 | 152 | if err != nil { 153 | return nil, err 154 | } 155 | 156 | switch statusCode { 157 | case 200: 158 | return result, nil 159 | case 403: 160 | return nil, ErrNoPerms 161 | default: 162 | return nil, makeUnknownError(statusCode) 163 | } 164 | } 165 | 166 | // GetContent fetch list of Content 167 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content-getContent 168 | func (api *API) GetContent(params ContentParameters) (*ContentCollection, error) { 169 | result := &ContentCollection{} 170 | statusCode, err := api.doRequest( 171 | "GET", "/rest/api/content", 172 | params, result, nil, 173 | ) 174 | 175 | if err != nil { 176 | return nil, err 177 | } 178 | 179 | switch statusCode { 180 | case 200: 181 | return result, nil 182 | case 403: 183 | return nil, ErrNoPerms 184 | case 404: 185 | return nil, ErrNoContent 186 | default: 187 | return nil, makeUnknownError(statusCode) 188 | } 189 | } 190 | 191 | // GetContentByID fetch a piece of Content 192 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content-getContentById 193 | func (api *API) GetContentByID(contentID string, params ContentIDParameters) (*Content, error) { 194 | result := &Content{} 195 | statusCode, err := api.doRequest( 196 | "GET", "/rest/api/content/"+contentID, 197 | params, result, nil, 198 | ) 199 | 200 | if err != nil { 201 | return nil, err 202 | } 203 | 204 | switch statusCode { 205 | case 200: 206 | return result, nil 207 | case 403: 208 | return nil, ErrNoPerms 209 | case 404: 210 | return nil, ErrNoContent 211 | default: 212 | return nil, makeUnknownError(statusCode) 213 | } 214 | } 215 | 216 | // GetContentHistory fetch the history of a particular piece of content 217 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content-getHistory 218 | func (api *API) GetContentHistory(contentID string, params ExpandParameters) (*History, error) { 219 | result := &History{} 220 | statusCode, err := api.doRequest( 221 | "GET", "/rest/api/content/"+contentID+"/history", 222 | params, result, nil, 223 | ) 224 | 225 | if err != nil { 226 | return nil, err 227 | } 228 | 229 | switch statusCode { 230 | case 200: 231 | return result, nil 232 | case 403: 233 | return nil, ErrNoPerms 234 | case 404: 235 | return nil, ErrNoContent 236 | default: 237 | return nil, makeUnknownError(statusCode) 238 | } 239 | } 240 | 241 | // GetContentChildren fetch a map of the direct children of a piece of Content 242 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content/{id}/child-children 243 | func (api *API) GetContentChildren(contentID string, params ChildrenParameters) (*Contents, error) { 244 | result := &Contents{} 245 | statusCode, err := api.doRequest( 246 | "GET", "/rest/api/content/"+contentID+"/child", 247 | params, result, nil, 248 | ) 249 | 250 | if err != nil { 251 | return nil, err 252 | } 253 | 254 | switch statusCode { 255 | case 200: 256 | return result, nil 257 | case 403: 258 | return nil, ErrNoPerms 259 | case 404: 260 | return nil, ErrNoContent 261 | default: 262 | return nil, makeUnknownError(statusCode) 263 | } 264 | } 265 | 266 | // GetContentChildrenByType the direct children of a piece of Content, limited to a single child type 267 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content/{id}/child-childrenOfType 268 | func (api *API) GetContentChildrenByType(contentID, contentType string, params ChildrenParameters) (*ContentCollection, error) { 269 | result := &ContentCollection{} 270 | statusCode, err := api.doRequest( 271 | "GET", "/rest/api/content/"+contentID+"/child/"+contentType, 272 | params, result, nil, 273 | ) 274 | 275 | if err != nil { 276 | return nil, err 277 | } 278 | 279 | switch statusCode { 280 | case 200: 281 | return result, nil 282 | case 403: 283 | return nil, ErrNoPerms 284 | case 404: 285 | return nil, ErrNoContent 286 | default: 287 | return nil, makeUnknownError(statusCode) 288 | } 289 | } 290 | 291 | // GetContentComments fetch the comments of a content 292 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content/{id}/child-commentsOfContent 293 | func (api *API) GetContentComments(contentID string, params ChildrenParameters) (*ContentCollection, error) { 294 | result := &ContentCollection{} 295 | statusCode, err := api.doRequest( 296 | "GET", "/rest/api/content/"+contentID+"/child/comment", 297 | params, result, nil, 298 | ) 299 | 300 | if err != nil { 301 | return nil, err 302 | } 303 | 304 | switch statusCode { 305 | case 200: 306 | return result, nil 307 | case 403: 308 | return nil, ErrNoPerms 309 | case 404: 310 | return nil, ErrNoContent 311 | default: 312 | return nil, makeUnknownError(statusCode) 313 | } 314 | } 315 | 316 | // GetAttachments fetch list of attachment Content entities within a single container 317 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content/{id}/child/attachment-getAttachments 318 | func (api *API) GetAttachments(contentID string, params AttachmentParameters) (*ContentCollection, error) { 319 | result := &ContentCollection{} 320 | statusCode, err := api.doRequest( 321 | "GET", "/rest/api/content/"+contentID+"/child/attachment", 322 | params, result, nil, 323 | ) 324 | 325 | if err != nil { 326 | return nil, err 327 | } 328 | 329 | switch statusCode { 330 | case 200: 331 | return result, nil 332 | case 403: 333 | return nil, ErrNoPerms 334 | case 404: 335 | return nil, ErrNoContent 336 | default: 337 | return nil, makeUnknownError(statusCode) 338 | } 339 | } 340 | 341 | // GetDescendants fetch a map of the descendants of a piece of Content 342 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content/{id}/descendant-descendants 343 | func (api *API) GetDescendants(contentID string, params ExpandParameters) (*Contents, error) { 344 | result := &Contents{} 345 | statusCode, err := api.doRequest( 346 | "GET", "/rest/api/content/"+contentID+"/descendant", 347 | params, result, nil, 348 | ) 349 | 350 | if err != nil { 351 | return nil, err 352 | } 353 | 354 | switch statusCode { 355 | case 200: 356 | return result, nil 357 | case 403: 358 | return nil, ErrNoPerms 359 | case 404: 360 | return nil, ErrNoContent 361 | default: 362 | return nil, makeUnknownError(statusCode) 363 | } 364 | } 365 | 366 | // GetDescendantsOfType fetch the direct descendants of a piece of Content, limited to a single descendant type 367 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content/{id}/descendant-descendantsOfType 368 | func (api *API) GetDescendantsOfType(contentID, descType string, params ExpandParameters) (*ContentCollection, error) { 369 | result := &ContentCollection{} 370 | statusCode, err := api.doRequest( 371 | "GET", "/rest/api/content/"+contentID+"/descendant/"+descType, 372 | params, result, nil, 373 | ) 374 | 375 | if err != nil { 376 | return nil, err 377 | } 378 | 379 | switch statusCode { 380 | case 200: 381 | return result, nil 382 | case 403: 383 | return nil, ErrNoPerms 384 | case 404: 385 | return nil, ErrNoContent 386 | default: 387 | return nil, makeUnknownError(statusCode) 388 | } 389 | } 390 | 391 | // GetLabels fetch the list of labels on a piece of Content 392 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content/{id}/label-labels 393 | func (api *API) GetLabels(contentID string, params LabelParameters) (*LabelCollection, error) { 394 | result := &LabelCollection{} 395 | statusCode, err := api.doRequest( 396 | "GET", "/rest/api/content/"+contentID+"/label", 397 | params, result, nil, 398 | ) 399 | 400 | if err != nil { 401 | return nil, err 402 | } 403 | 404 | switch statusCode { 405 | case 200: 406 | return result, nil 407 | case 403: 408 | return nil, ErrNoPerms 409 | case 404: 410 | return nil, ErrNoContent 411 | default: 412 | return nil, makeUnknownError(statusCode) 413 | } 414 | } 415 | 416 | // GetRestrictions returns restrictions for the content with permissions inheritance. 417 | // Confluence API doesn't provide such an API method, so we use private JSON API. 418 | func (api *API) GetRestrictions(contentID, parentPageId, spaceKey string) (*Restrictions, error) { 419 | url := "/pages/getcontentpermissions.action" 420 | url += "?contentId=" + contentID 421 | url += "&parentPageId=" + parentPageId 422 | url += "&spaceKey=" + spaceKey 423 | 424 | result := &restrictionsInfo{} 425 | statusCode, err := api.doRequest("GET", url, emptyParams, result, nil) 426 | 427 | if err != nil { 428 | return nil, err 429 | } 430 | 431 | switch statusCode { 432 | case 200: 433 | return convertRestrictionsData(result), nil 434 | case 403: 435 | return nil, ErrNoPerms 436 | case 404: 437 | return nil, ErrNoContent 438 | default: 439 | return nil, makeUnknownError(statusCode) 440 | } 441 | } 442 | 443 | // GetRestrictionsByOperation fetch info about all restrictions by operation 444 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content/{id}/restriction-byOperation 445 | func (api *API) GetRestrictionsByOperation(contentID string, params ExpandParameters) (*Restrictions, error) { 446 | result := &Restrictions{} 447 | statusCode, err := api.doRequest( 448 | "GET", "/rest/api/content/"+contentID+"/restriction/byOperation", 449 | params, result, nil, 450 | ) 451 | 452 | if err != nil { 453 | return nil, err 454 | } 455 | 456 | switch statusCode { 457 | case 200: 458 | return result, nil 459 | case 403: 460 | return nil, ErrNoPerms 461 | default: 462 | return nil, makeUnknownError(statusCode) 463 | } 464 | } 465 | 466 | // GetRestrictionsForOperation fetch info about all restrictions of given operation 467 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content/{id}/restriction-forOperation 468 | func (api *API) GetRestrictionsForOperation(contentID, operation string, params CollectionParameters) (*Restriction, error) { 469 | result := &Restriction{} 470 | statusCode, err := api.doRequest( 471 | "GET", "/rest/api/content/"+contentID+"/restriction/byOperation/"+operation, 472 | params, result, nil, 473 | ) 474 | 475 | if err != nil { 476 | return nil, err 477 | } 478 | 479 | switch statusCode { 480 | case 200: 481 | return result, nil 482 | case 403: 483 | return nil, ErrNoPerms 484 | default: 485 | return nil, makeUnknownError(statusCode) 486 | } 487 | } 488 | 489 | // GetGroups fetch collection of user groups 490 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#group-getGroups 491 | func (api *API) GetGroups(params CollectionParameters) (*GroupCollection, error) { 492 | result := &GroupCollection{} 493 | statusCode, err := api.doRequest( 494 | "GET", "/rest/api/group", 495 | params, result, nil, 496 | ) 497 | 498 | if err != nil { 499 | return nil, err 500 | } 501 | 502 | switch statusCode { 503 | case 200: 504 | return result, nil 505 | case 403: 506 | return nil, ErrNoPerms 507 | default: 508 | return nil, makeUnknownError(statusCode) 509 | } 510 | } 511 | 512 | // GetGroup fetch the user group with the group name 513 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#group-getGroup 514 | func (api *API) GetGroup(groupName string, params ExpandParameters) (*Group, error) { 515 | result := &Group{} 516 | statusCode, err := api.doRequest( 517 | "GET", "/rest/api/group/"+groupName, 518 | params, result, nil, 519 | ) 520 | 521 | if err != nil { 522 | return nil, err 523 | } 524 | 525 | switch statusCode { 526 | case 200: 527 | return result, nil 528 | case 403: 529 | return nil, ErrNoPerms 530 | default: 531 | return nil, makeUnknownError(statusCode) 532 | } 533 | } 534 | 535 | // GetGroupMembers fetch a collection of users in the given group 536 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#group-getMembers 537 | func (api *API) GetGroupMembers(groupName string, params CollectionParameters) (*UserCollection, error) { 538 | result := &UserCollection{} 539 | statusCode, err := api.doRequest( 540 | "GET", "/rest/api/group/"+groupName+"/member", 541 | params, result, nil, 542 | ) 543 | 544 | if err != nil { 545 | return nil, err 546 | } 547 | 548 | switch statusCode { 549 | case 200: 550 | return result, nil 551 | case 403: 552 | return nil, ErrNoPerms 553 | default: 554 | return nil, makeUnknownError(statusCode) 555 | } 556 | } 557 | 558 | // Search search for entities in Confluence using the Confluence Query Language (CQL) 559 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#search-search 560 | func (api *API) Search(params SearchParameters) (*SearchResult, error) { 561 | result := &SearchResult{} 562 | statusCode, err := api.doRequest( 563 | "GET", "/rest/api/search", 564 | params, result, nil, 565 | ) 566 | 567 | if err != nil { 568 | return nil, err 569 | } 570 | 571 | switch statusCode { 572 | case 200: 573 | return result, nil 574 | case 400: 575 | return nil, ErrQueryError 576 | case 403: 577 | return nil, ErrNoPerms 578 | default: 579 | return nil, makeUnknownError(statusCode) 580 | } 581 | } 582 | 583 | // SearchContent fetch a list of content using the Confluence Query Language (CQL) 584 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#content-search 585 | func (api *API) SearchContent(params ContentSearchParameters) (*ContentCollection, error) { 586 | result := &ContentCollection{} 587 | statusCode, err := api.doRequest( 588 | "GET", "/rest/api/content/search", 589 | params, result, nil, 590 | ) 591 | 592 | if err != nil { 593 | return nil, err 594 | } 595 | 596 | switch statusCode { 597 | case 200: 598 | return result, nil 599 | case 400: 600 | return nil, ErrQueryError 601 | case 403: 602 | return nil, ErrNoPerms 603 | default: 604 | return nil, makeUnknownError(statusCode) 605 | } 606 | } 607 | 608 | // GetSpaces fetch information about a number of spaces 609 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#space-spaces 610 | func (api *API) GetSpaces(params SpaceParameters) (*SpaceCollection, error) { 611 | result := &SpaceCollection{} 612 | statusCode, err := api.doRequest( 613 | "GET", "/rest/api/space", 614 | params, result, nil, 615 | ) 616 | 617 | if err != nil { 618 | return nil, err 619 | } 620 | 621 | switch statusCode { 622 | case 200: 623 | return result, nil 624 | case 403: 625 | return nil, ErrNoPerms 626 | default: 627 | return nil, makeUnknownError(statusCode) 628 | } 629 | } 630 | 631 | // GetSpace fetch information about a space 632 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#space-space 633 | func (api *API) GetSpace(spaceKey string, params Parameters) (*Space, error) { 634 | result := &Space{} 635 | statusCode, err := api.doRequest( 636 | "GET", "/rest/api/space/"+spaceKey, 637 | params, result, nil, 638 | ) 639 | 640 | if err != nil { 641 | return nil, err 642 | } 643 | 644 | switch statusCode { 645 | case 200: 646 | return result, nil 647 | case 403: 648 | return nil, ErrNoPerms 649 | case 404: 650 | return nil, ErrNoSpace 651 | default: 652 | return nil, makeUnknownError(statusCode) 653 | } 654 | } 655 | 656 | // GetSpaceContent fetch the content in this given space 657 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#space-contents 658 | func (api *API) GetSpaceContent(spaceKey string, params SpaceParameters) (*Contents, error) { 659 | result := &Contents{} 660 | statusCode, err := api.doRequest( 661 | "GET", "/rest/api/space/"+spaceKey+"/content", 662 | params, result, nil, 663 | ) 664 | 665 | if err != nil { 666 | return nil, err 667 | } 668 | 669 | switch statusCode { 670 | case 200: 671 | return result, nil 672 | case 403: 673 | return nil, ErrNoPerms 674 | case 404: 675 | return nil, ErrNoContent 676 | default: 677 | return nil, makeUnknownError(statusCode) 678 | } 679 | } 680 | 681 | // GetSpaceContentWithType fetch the content in this given space with the given type 682 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#space-contentsWithType 683 | func (api *API) GetSpaceContentWithType(spaceKey, contentType string, params SpaceParameters) (*Contents, error) { 684 | result := &Contents{} 685 | statusCode, err := api.doRequest( 686 | "GET", "/rest/api/space/"+spaceKey+"/content/"+contentType, 687 | params, result, nil, 688 | ) 689 | 690 | if err != nil { 691 | return nil, err 692 | } 693 | 694 | switch statusCode { 695 | case 200: 696 | return result, nil 697 | case 403: 698 | return nil, ErrNoPerms 699 | case 404: 700 | return nil, ErrNoContent 701 | default: 702 | return nil, makeUnknownError(statusCode) 703 | } 704 | } 705 | 706 | // GetUser fetch information about a user identified by either user key or username 707 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#user-getUser 708 | func (api *API) GetUser(params UserParameters) (*User, error) { 709 | result := &User{} 710 | statusCode, err := api.doRequest( 711 | "GET", "/rest/api/user", 712 | params, result, nil, 713 | ) 714 | 715 | if err != nil { 716 | return nil, err 717 | } 718 | 719 | switch statusCode { 720 | case 200: 721 | return result, nil 722 | case 403: 723 | return nil, ErrNoUserPerms 724 | case 404: 725 | return nil, ErrNoUserFound 726 | default: 727 | return nil, makeUnknownError(statusCode) 728 | } 729 | } 730 | 731 | // GetAnonymousUser fetch information about the how anonymous is represented in confluence 732 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#user-getAnonymous 733 | func (api *API) GetAnonymousUser() (*User, error) { 734 | result := &User{} 735 | statusCode, err := api.doRequest( 736 | "GET", "/rest/api/user/anonymous", 737 | emptyParams, result, nil, 738 | ) 739 | 740 | if err != nil { 741 | return nil, err 742 | } 743 | 744 | switch statusCode { 745 | case 200: 746 | return result, nil 747 | case 403: 748 | return nil, ErrNoPerms 749 | default: 750 | return nil, makeUnknownError(statusCode) 751 | } 752 | } 753 | 754 | // GetCurrentUser fetch information about the current logged in user 755 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#user-getCurrent 756 | func (api *API) GetCurrentUser(params ExpandParameters) (*User, error) { 757 | result := &User{} 758 | statusCode, err := api.doRequest( 759 | "GET", "/rest/api/user/current", 760 | params, result, nil, 761 | ) 762 | 763 | if err != nil { 764 | return nil, err 765 | } 766 | 767 | switch statusCode { 768 | case 200: 769 | return result, nil 770 | case 403: 771 | return nil, ErrNoPerms 772 | default: 773 | return nil, makeUnknownError(statusCode) 774 | } 775 | } 776 | 777 | // GetUserGroups fetch collection of groups that the given user is a member of 778 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#user-getGroups 779 | func (api *API) GetUserGroups(params UserParameters) (*GroupCollection, error) { 780 | result := &GroupCollection{} 781 | statusCode, err := api.doRequest( 782 | "GET", "/rest/api/user/memberof", 783 | params, result, nil, 784 | ) 785 | 786 | if err != nil { 787 | return nil, err 788 | } 789 | 790 | switch statusCode { 791 | case 200: 792 | return result, nil 793 | case 403: 794 | return nil, ErrNoPerms 795 | default: 796 | return nil, makeUnknownError(statusCode) 797 | } 798 | } 799 | 800 | // IsWatchingContent fetch information about whether a user is watching a specified content 801 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#user/watch-isWatchingContent 802 | func (api *API) IsWatchingContent(contentID string, params WatchParameters) (*WatchStatus, error) { 803 | result := &WatchStatus{} 804 | statusCode, err := api.doRequest( 805 | "GET", "/rest/api/user/watch/content/"+contentID, 806 | params, result, nil, 807 | ) 808 | 809 | if err != nil { 810 | return nil, err 811 | } 812 | 813 | switch statusCode { 814 | case 200: 815 | return result, nil 816 | case 403: 817 | return nil, ErrNoPerms 818 | case 404: 819 | return nil, ErrNoContent 820 | default: 821 | return nil, makeUnknownError(statusCode) 822 | } 823 | } 824 | 825 | // IsWatchingSpace fetch information about whether a user is watching a specified space 826 | // https://docs.atlassian.com/ConfluenceServer/rest/7.3.4/#user/watch-isWatchingSpace 827 | func (api *API) IsWatchingSpace(spaceKey string, params WatchParameters) (*WatchStatus, error) { 828 | result := &WatchStatus{} 829 | statusCode, err := api.doRequest( 830 | "GET", "/rest/api/user/watch/space/"+spaceKey, 831 | params, result, nil, 832 | ) 833 | 834 | if err != nil { 835 | return nil, err 836 | } 837 | 838 | switch statusCode { 839 | case 200: 840 | return result, nil 841 | case 403: 842 | return nil, ErrNoPerms 843 | case 404: 844 | return nil, ErrNoSpace 845 | default: 846 | return nil, makeUnknownError(statusCode) 847 | } 848 | } 849 | 850 | // ListWatchers fetch information about all watcher of given page 851 | func (api *API) ListWatchers(params ListWatchersParameters) (*WatchInfo, error) { 852 | result := &WatchInfo{} 853 | statusCode, err := api.doRequest( 854 | "GET", "/json/listwatchers.action", 855 | params, result, nil, 856 | ) 857 | 858 | if err != nil { 859 | return nil, err 860 | } 861 | 862 | switch statusCode { 863 | case 200: 864 | return result, nil 865 | case 403: 866 | return nil, ErrNoPerms 867 | case 404: 868 | return nil, ErrNoSpace 869 | default: 870 | return nil, makeUnknownError(statusCode) 871 | } 872 | } 873 | 874 | // ////////////////////////////////////////////////////////////////////////////////// // 875 | 876 | // ProfileURL return link to profile 877 | func (api *API) ProfileURL(u *User) string { 878 | return api.url + "/display/~" + u.Name 879 | } 880 | 881 | // GenTinyLink generates tiny link for content with given ID 882 | func (api *API) GenTinyLink(contentID string) string { 883 | id, err := strconv.ParseUint(contentID, 10, 32) 884 | 885 | if err != nil { 886 | return "" 887 | } 888 | 889 | buf := make([]byte, 4) 890 | binary.LittleEndian.PutUint32(buf, uint32(id)) 891 | 892 | var tinyID string 893 | 894 | for _, r := range base64.StdEncoding.EncodeToString(buf) { 895 | switch r { 896 | case '/': 897 | tinyID += "-" 898 | case '+': 899 | tinyID += "_" 900 | default: 901 | tinyID += string(r) 902 | } 903 | } 904 | 905 | tinyID = strings.TrimRight(tinyID, "A=") 906 | 907 | return api.url + "/x/" + tinyID 908 | } 909 | 910 | // ////////////////////////////////////////////////////////////////////////////////// // 911 | 912 | // codebeat:disable[ARITY] 913 | 914 | // doRequest create and execute request 915 | func (api *API) doRequest(method, uri string, params Parameters, result, body interface{}) (int, error) { 916 | err := params.Validate() 917 | 918 | if err != nil { 919 | return -1, err 920 | } 921 | 922 | req := api.acquireRequest(method, uri, params) 923 | resp := fasthttp.AcquireResponse() 924 | 925 | defer fasthttp.ReleaseRequest(req) 926 | defer fasthttp.ReleaseResponse(resp) 927 | 928 | if body != nil { 929 | bodyData, err := json.Marshal(body) 930 | 931 | if err != nil { 932 | return -1, err 933 | } 934 | 935 | req.SetBody(bodyData) 936 | } 937 | 938 | err = api.Client.Do(req, resp) 939 | 940 | if err != nil { 941 | return -1, err 942 | } 943 | 944 | statusCode := resp.StatusCode() 945 | 946 | if statusCode != 200 || result == nil { 947 | return statusCode, nil 948 | } 949 | 950 | err = json.Unmarshal(resp.Body(), result) 951 | 952 | return statusCode, err 953 | } 954 | 955 | // codebeat:enable[ARITY] 956 | 957 | // acquireRequest acquire new request with given params 958 | func (api *API) acquireRequest(method, uri string, params Parameters) *fasthttp.Request { 959 | req := fasthttp.AcquireRequest() 960 | query := params.ToQuery() 961 | 962 | req.SetRequestURI(api.url + uri) 963 | 964 | // Set query if params can be encoded as query 965 | if query != "" { 966 | req.URI().SetQueryString(query) 967 | } 968 | 969 | if method != "GET" { 970 | req.Header.SetMethod(method) 971 | } 972 | 973 | // Set authorization header 974 | if api.auth != "" { 975 | req.Header.Add("Authorization", api.auth) 976 | } 977 | 978 | return req 979 | } 980 | 981 | // ////////////////////////////////////////////////////////////////////////////////// // 982 | 983 | // getUserAgent generate user-agent string for client 984 | func getUserAgent(app, version string) string { 985 | if app != "" && version != "" { 986 | return fmt.Sprintf( 987 | "%s/%s %s/%s (go; %s; %s-%s)", 988 | app, version, "Go-Confluence", "6", runtime.Version(), 989 | runtime.GOARCH, runtime.GOOS, 990 | ) 991 | } 992 | 993 | return fmt.Sprintf( 994 | "%s/%s (go; %s; %s-%s)", 995 | "Go-Confluence", "6", runtime.Version(), 996 | runtime.GOARCH, runtime.GOOS, 997 | ) 998 | } 999 | 1000 | // makeUnknownError create error struct for unknown error 1001 | func makeUnknownError(statusCode int) error { 1002 | return fmt.Errorf("Unknown error occurred (status code %d)", statusCode) 1003 | } 1004 | 1005 | // ////////////////////////////////////////////////////////////////////////////////// // 1006 | 1007 | // convertRestrictionsData converts restrctions data from private to public format 1008 | func convertRestrictionsData(data *restrictionsInfo) *Restrictions { 1009 | result := &Restrictions{} 1010 | 1011 | if data == nil { 1012 | return result 1013 | } 1014 | 1015 | var restr *RestrictionData 1016 | 1017 | for _, perm := range data.Permissions { 1018 | if len(perm) != 5 { 1019 | continue 1020 | } 1021 | 1022 | pType, pCategory, pTarget := perm[0], perm[1], perm[2] 1023 | 1024 | switch pType { 1025 | case "View": 1026 | if result.Read == nil { 1027 | result.Read = &Restriction{OPERATION_READ, &RestrictionData{}} 1028 | } 1029 | 1030 | restr = result.Read.Data 1031 | 1032 | case "Edit": 1033 | if result.Update == nil { 1034 | result.Update = &Restriction{OPERATION_UPDATE, &RestrictionData{}} 1035 | } 1036 | 1037 | restr = result.Update.Data 1038 | } 1039 | 1040 | switch pCategory { 1041 | case "group": 1042 | if restr.Group == nil { 1043 | restr.Group = &GroupCollection{} 1044 | } 1045 | 1046 | restr.Group.Size++ 1047 | restr.Group.Limit++ 1048 | restr.Group.Results = append(restr.Group.Results, &Group{"group", pTarget}) 1049 | 1050 | case "user": 1051 | if restr.User == nil { 1052 | restr.User = &UserCollection{} 1053 | } 1054 | 1055 | restr.User.Size++ 1056 | restr.User.Limit++ 1057 | restr.User.Results = append( 1058 | restr.User.Results, 1059 | convertWatcherToUser(data.Users[pTarget].User), 1060 | ) 1061 | } 1062 | } 1063 | 1064 | return result 1065 | } 1066 | 1067 | // convertWatcherToUser converts watcher struct to user struct 1068 | func convertWatcherToUser(w *Watcher) *User { 1069 | if w == nil { 1070 | return nil 1071 | } 1072 | 1073 | return &User{ 1074 | Type: w.Type, 1075 | Name: w.Name, 1076 | Key: w.Key, 1077 | DisplayName: w.DisplayName, 1078 | ProfilePicture: &Icon{ 1079 | Path: w.AvatarURL, 1080 | Width: 48, 1081 | Height: 48, 1082 | IsDefault: false, 1083 | }, 1084 | } 1085 | } 1086 | --------------------------------------------------------------------------------