Skip to content

Commit d342230

Browse files
authored
Merge pull request #17 from DrDrrae/dev
Dev
2 parents 1626595 + 2c0553c commit d342230

159 files changed

Lines changed: 25449 additions & 1065 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ClickUpAPI/ClickupAPI.psd1

-8.32 KB
Binary file not shown.

ClickUpAPI/Private/ClickUpAPI.ps1

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,29 @@
2020
}
2121
$Response = Invoke-RestMethod @InvokeParams
2222
if ($Response) {
23-
Return $Response
23+
return $Response
2424
}
2525
} catch {
26-
$_
26+
$ErrorMessage = "Failed to invoke ClickUp API request. Method: $Method, URI: $URI"
27+
28+
# Attempt to extract more detailed error information from the response
29+
if ($_.Exception.Response) {
30+
try {
31+
$Stream = $_.Exception.Response.GetResponseStream()
32+
if ($Stream) {
33+
$Reader = [System.IO.StreamReader]::new($Stream)
34+
$ResponseBody = $Reader.ReadToEnd()
35+
$ErrorMessage += ". Response: $ResponseBody"
36+
}
37+
} catch {
38+
# Ignore errors reading the response stream
39+
}
40+
} else {
41+
$ErrorMessage += ". Error: $($_.Exception.Message)"
42+
}
43+
44+
Write-Error $ErrorMessage
45+
throw $ErrorMessage
2746
}
2847
}
2948

@@ -113,14 +132,37 @@ function Invoke-ClickUpAPIPostAttachment {
113132
}
114133

115134
$InvokeParams = @{
116-
Body = $Body
117-
ContentType = "multipart/form-data; boundary=`"$Boundary`""
118-
Headers = @{
119-
Authorization = Get-ClickUpAPIKeyInsecure -APIKey $ClickUpAPIKey
135+
Body = $Body
136+
ContentType = "multipart/form-data; boundary=`"$Boundary`""
137+
Headers = @{
138+
Authorization = Get-ClickUpAPIKeyInsecure -APIKey $ClickUpAPIKey
139+
}
140+
Method = 'post'
141+
Uri = $URI
142+
}
143+
144+
try {
145+
$Response = Invoke-RestMethod @InvokeParams
146+
return $Response
147+
} catch {
148+
$ErrorMessage = "Failed to upload attachment to $URI"
149+
150+
if ($_.Exception.Response) {
151+
try {
152+
$Stream = $_.Exception.Response.GetResponseStream()
153+
if ($Stream) {
154+
$Reader = [System.IO.StreamReader]::new($Stream)
155+
$ResponseBody = $Reader.ReadToEnd()
156+
$ErrorMessage += ". Response: $ResponseBody"
157+
}
158+
} catch {
159+
# Ignore errors reading the response stream
120160
}
121-
Method = 'post'
122-
Uri = $URI
161+
} else {
162+
$ErrorMessage += ". Error: $($_.Exception.Message)"
123163
}
124-
$Response = Invoke-RestMethod @InvokeParams
125-
return $Response
164+
165+
Write-Error $ErrorMessage
166+
throw $ErrorMessage
167+
}
126168
}

ClickUpAPI/Private/DateConversion.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
)
77
$UTCDateTime = Get-Date($DateTime).ToUniversalTime()
88
$unixEpochStart = New-Object DateTime 1970, 1, 1, 0, 0, 0, ([DateTimeKind]::Utc)
9-
[uint64](($UTCDateTime) - $unixEpochStart).TotalMilliseconds
9+
[ulong](($UTCDateTime) - $unixEpochStart).TotalMilliseconds
1010
}
1111
function ConvertTo-WindowsTime {
1212
[OutputType([DateTime])]
1313
Param(
1414
# Date in UNIX time
1515
[Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = 'Milliseconds.')]
16-
[uint64]$ticks
16+
[ulong]$ticks
1717
)
1818
[DateTime]::new(1970, 1, 1, 0, 0, 0, 0, [System.DateTimeKind]::Utc).AddMilliseconds($ticks).ToLocalTime()
1919
}

ClickUpAPI/Public/APIKey.ps1

Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,130 @@
1-
function Add-ClickUpAPIKey {
2-
[CmdletBinding()]
1+
<#
2+
.SYNOPSIS
3+
Adds a ClickUp API key to the current session.
4+
5+
.DESCRIPTION
6+
The Add-ClickUpAPIKey cmdlet securely stores the provided ClickUp API key in a global variable for use by other cmdlets in this module.
7+
The key is converted to a SecureString and stored in a read-only global variable named 'ClickUpAPIKey'.
8+
9+
.PARAMETER APIKey
10+
The ClickUp API key to be stored. This parameter is mandatory and can be piped.
11+
12+
.EXAMPLE
13+
Add-ClickUpAPIKey -APIKey "pk_12345678_ABCDEF1234567890"
14+
15+
Stores the provided API key in the session.
16+
17+
.INPUTS
18+
System.String. You can pipe a string containing the API key to this cmdlet.
319
4-
Param(
20+
.OUTPUTS
21+
None. This cmdlet does not return any output.
22+
#>
23+
function Add-ClickUpAPIKey {
24+
[CmdletBinding()]
25+
param(
526
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
627
[ValidateNotNullOrEmpty()]
7-
[Alias('Api_Key')]
828
[string]$APIKey
929
)
1030

11-
Begin {}
31+
begin {}
1232

13-
Process {
14-
$SecureAPIKey = ConvertTo-SecureString $ApiKey -AsPlainText -Force
33+
process {
34+
Write-Verbose 'Converting API key to SecureString...'
35+
try {
36+
$SecureAPIKey = ConvertTo-SecureString $ApiKey -AsPlainText -Force
37+
} catch {
38+
Write-Error "Failed to convert API key to SecureString. Error: $_"
39+
return
40+
}
1541

16-
Set-Variable -Name 'ClickUpAPIKey' -Value $SecureAPIKey -Option ReadOnly -Scope Global -Force
42+
Write-Verbose "Setting global variable 'ClickUpAPIKey'..."
43+
try {
44+
$Null = Set-Variable -Name 'ClickUpAPIKey' -Value $SecureAPIKey -Option ReadOnly -Scope Global -Force
45+
} catch {
46+
Write-Error "Failed to set global variable 'ClickUpAPIKey'. Error: $_"
47+
}
1748
}
1849

19-
End {
20-
Remove-Variable -Name ApiKey
50+
end {
51+
Remove-Variable -Name ApiKey -ErrorAction SilentlyContinue
2152
}
2253
}
2354

55+
<#
56+
.SYNOPSIS
57+
Removes the stored ClickUp API key.
58+
59+
.DESCRIPTION
60+
The Remove-ClickUpAPIKey cmdlet removes the global variable containing the ClickUp API key from the current session.
61+
This action requires confirmation if the ConfirmImpact is set to High (default).
62+
63+
.EXAMPLE
64+
Remove-ClickUpAPIKey
65+
66+
Removes the stored API key from the session.
67+
68+
.INPUTS
69+
None. This cmdlet does not accept any input.
70+
71+
.OUTPUTS
72+
None. This cmdlet does not return any output.
73+
#>
2474
function Remove-ClickUpAPIKey {
2575
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
2676
param ()
27-
if ($PSCmdlet.ShouldProcess($ClickUpAPIKey)) {
28-
Remove-Variable -Name 'ClickUpAPIKey' -Scope Global -Force
77+
78+
begin {}
79+
80+
process {
81+
if ($PSCmdlet.ShouldProcess('ClickUpAPIKey', 'Remove Global Variable')) {
82+
Write-Verbose "Removing global variable 'ClickUpAPIKey'..."
83+
try {
84+
$Null = Remove-Variable -Name 'ClickUpAPIKey' -Scope Global -Force -ErrorAction Stop
85+
} catch {
86+
Write-Error "Failed to remove global variable 'ClickUpAPIKey'. Error: $_"
87+
}
88+
}
2989
}
90+
91+
end {}
3092
}
3193

94+
<#
95+
.SYNOPSIS
96+
Retrieves the stored ClickUp API key.
97+
98+
.DESCRIPTION
99+
The Get-ClickUpAPIKey cmdlet retrieves the SecureString containing the ClickUp API key from the global variable.
100+
If no key is stored, it throws an error.
101+
102+
.EXAMPLE
103+
$Key = Get-ClickUpAPIKey
104+
105+
Retrieves the stored API key and assigns it to the $Key variable.
106+
107+
.INPUTS
108+
None. This cmdlet does not accept any input.
109+
110+
.OUTPUTS
111+
System.Security.SecureString. Returns the stored API key as a SecureString.
112+
#>
32113
function Get-ClickUpAPIKey {
33114
[CmdletBinding()]
115+
[OutputType([System.Security.SecureString])]
34116
param ()
35-
if ($null -eq $ClickUpAPIKey) {
36-
Write-Error 'No API key exists. Please run Add-ClickUpAPIKey to add one.'
37-
} else {
38-
$ClickUpAPIKey
117+
118+
begin {}
119+
120+
process {
121+
Write-Verbose 'Retrieving ClickUp API Key...'
122+
if ($null -eq $ClickUpAPIKey) {
123+
Write-Error 'No API key exists. Please run Add-ClickUpAPIKey to add one.'
124+
} else {
125+
return $ClickUpAPIKey
126+
}
39127
}
128+
129+
end {}
40130
}

ClickUpAPI/Public/Attachments.ps1

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,78 @@
11
<#
22
.SYNOPSIS
3-
Upload an attachment to a ClickUp task.
3+
Uploads an attachment to a specified ClickUp task.
4+
45
.DESCRIPTION
5-
Upload an attachment to a ClickUp task.
6+
The New-ClickUpTaskAttachment cmdlet uploads a file as an attachment to a ClickUp task.
7+
You can specify the task by its ID or by using custom task IDs (requires Team ID).
8+
9+
.PARAMETER TaskID
10+
The ID of the task to upload the attachment to.
11+
This parameter is mandatory when not using Custom Task IDs.
12+
13+
.PARAMETER AttachmentPath
14+
The local file path of the attachment to be uploaded.
15+
This parameter is mandatory.
16+
17+
.PARAMETER CustomTaskIDs
18+
Indicates that the provided TaskID is a custom task ID.
19+
If set to $true, the TeamID parameter is also required.
20+
21+
.PARAMETER TeamID
22+
The ID of the team (workspace) where the custom task ID exists.
23+
This parameter is mandatory when CustomTaskIDs is set to $true.
24+
25+
.EXAMPLE
26+
New-ClickUpTaskAttachment -TaskID "8675309" -AttachmentPath "C:\Reports\Status.pdf"
27+
28+
Uploads the file "Status.pdf" to the task with ID "8675309".
29+
630
.EXAMPLE
7-
PS C:\> New-ClickUpTaskAttachment
31+
New-ClickUpTaskAttachment -TaskID "CUST-123" -AttachmentPath "C:\Images\Design.png" -CustomTaskIDs $true -TeamID 123456
32+
33+
Uploads the file "Design.png" to the task with custom ID "CUST-123" in team 123456.
34+
835
.INPUTS
9-
None
36+
None. You cannot pipe objects to this cmdlet.
37+
1038
.OUTPUTS
11-
System.Management.Automation.PSCustomObject.
39+
None. The cmdlet does not return any output.
40+
1241
.NOTES
13-
See the link for information.
42+
API Reference: https://developer.clickup.com/reference/createtaskattachment
43+
1444
.LINK
15-
https://jsapi.apiary.io/apis/clickup20/reference/0/attachments/create-task-attachment.html
45+
https://developer.clickup.com/reference/createtaskattachment
1646
#>
1747
function New-ClickUpTaskAttachment {
18-
[CmdletBinding()]
48+
[CmdletBinding(DefaultParameterSetName = 'TaskID')]
1949
[OutputType([System.Management.Automation.PSCustomObject])]
2050
param(
21-
[Parameter(Mandatory = $true)]
51+
[Parameter(Mandatory = $true, ParameterSetName = 'TaskID')]
52+
[Parameter(Mandatory = $true, ParameterSetName = 'CustomTaskIDs')]
2253
[string]$TaskID,
23-
[Parameter(Mandatory = $true)]
24-
[string]$AttachmentPath
54+
[Parameter(Mandatory = $true, ParameterSetName = 'TaskID')]
55+
[Parameter(Mandatory = $true, ParameterSetName = 'CustomTaskIDs')]
56+
[string]$AttachmentPath,
57+
[Parameter(Mandatory = $true, ParameterSetName = 'CustomTaskIDs')]
58+
[bool]$CustomTaskIDs,
59+
[Parameter(Mandatory = $true, ParameterSetName = 'CustomTaskIDs')]
60+
[ulong]$TeamID
2561
)
2662

27-
$FileBytes = [System.IO.File]::ReadAllBytes($AttachmentPath);
63+
Write-Verbose "Reading attachment from: $AttachmentPath"
64+
try {
65+
$FileBytes = [System.IO.File]::ReadAllBytes($AttachmentPath)
66+
} catch {
67+
throw "Failed to read attachment file at '$AttachmentPath'. Error: $_"
68+
}
69+
2870
$FileName = $AttachmentPath | Split-Path -Leaf
29-
$FileEnc = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($FileBytes);
30-
$Boundary = [System.Guid]::NewGuid().ToString();
31-
$LF = "`r`n";
71+
$FileEnc = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($FileBytes)
72+
$Boundary = [System.Guid]::NewGuid().ToString()
73+
$LF = "`r`n"
3274

75+
Write-Verbose "Constructing multipart form data with boundary: $Boundary"
3376
$Body = (
3477
"--$Boundary",
3578
"Content-Disposition: form-data; name=`"attachment`"; filename=`"$FileName`"",
@@ -41,5 +84,20 @@ function New-ClickUpTaskAttachment {
4184
"--$Boundary--$LF"
4285
) -join $LF
4386

44-
Invoke-ClickUpAPIPostAttachment -Endpoint "task/$TaskID/attachment" -Body $Body -Boundary $Boundary
87+
if ($PSBoundParameters.ContainsKey('CustomTaskIDs')) {
88+
$QueryString = @{
89+
custom_task_ids = $CustomTaskIDs
90+
team_id = $TeamID
91+
}
92+
} else {
93+
$QueryString = @{}
94+
}
95+
96+
Write-Verbose "Uploading attachment '$FileName' to task '$TaskID'"
97+
try {
98+
$Null = Invoke-ClickUpAPIPostAttachment -Arguments $QueryString -Endpoint "task/$TaskID/attachment" -Body $Body -Boundary $Boundary
99+
} catch {
100+
Write-Error "Failed to upload attachment to task '$TaskID'. Error: $_"
101+
throw
102+
}
45103
}

0 commit comments

Comments
 (0)