pinner 0.0.8

Secure CI/CD workflows by pinning mutable tags to immutable SHA-1 hashes. A high-performance Rust CLI that preserves YAML formatting and comments. Supports GitHub, GitLab, Bitbucket, Forgejo, and Docker image pinning.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use crate::core::{BranchName, DependencyName, DependencyRef};
use crate::error::PinnerError;
use crate::resolver::provider::{BaseHttpClient, RemoteProvider};
use async_trait::async_trait;
use serde::Deserialize;

pub struct ReqwestBitbucketProvider {
    pub base: BaseHttpClient,
    pub is_cloud: bool,
}

impl ReqwestBitbucketProvider {
    pub fn new(base_url: String, token: Option<String>) -> Result<Self, PinnerError> {
        let is_cloud = base_url.contains("bitbucket.org");
        Self::with_type(base_url, token, is_cloud)
    }

    pub fn with_type(
        base_url: String,
        token: Option<String>,
        is_cloud: bool,
    ) -> Result<Self, PinnerError> {
        Ok(Self {
            base: BaseHttpClient::new(base_url, token, "Bearer", "BITBUCKET_TOKEN")?,
            is_cloud,
        })
    }
}

#[derive(Deserialize)]
struct BitbucketCloudRefResponse {
    target: BitbucketCloudTarget,
}

#[derive(Deserialize)]
struct BitbucketCloudTarget {
    hash: String,
    target: Option<BitbucketCloudInnerTarget>,
}

#[derive(Deserialize)]
struct BitbucketCloudInnerTarget {
    hash: String,
}

#[derive(Deserialize)]
struct BitbucketDCRefResponse {
    #[serde(rename = "latestCommit")]
    latest_commit: String,
}

#[derive(Deserialize)]
struct BitbucketDCRepoResponse {
    #[serde(rename = "defaultBranch")]
    default_branch: String,
}

#[async_trait]
impl RemoteProvider for ReqwestBitbucketProvider {
    async fn get_commit_sha(
        &self,
        action: &DependencyName,
        tag: &str,
        _key: &str,
    ) -> Result<DependencyRef, PinnerError> {
        let url = if self.is_cloud {
            format!(
                "{}/repositories/{}/refs/tags/{}",
                self.base.base_url, action, tag
            )
        } else {
            let Some((project, repo)) = action.0.split_once('/') else {
                return Err(PinnerError::Api(format!(
                    "Invalid Bitbucket action format: {}. Expected 'project/repo'",
                    action
                )));
            };
            format!(
                "{}/rest/api/1.0/projects/{}/repos/{}/tags/{}",
                self.base.base_url, project, repo, tag
            )
        };

        let resp = self
            .base
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| PinnerError::Api(e.to_string()))?;

        if resp.status().is_success() {
            let sha = if self.is_cloud {
                let res: BitbucketCloudRefResponse = resp
                    .json()
                    .await
                    .map_err(|e| PinnerError::Api(e.to_string()))?;
                res.target.target.map(|t| t.hash).unwrap_or(res.target.hash)
            } else {
                let res: BitbucketDCRefResponse = resp
                    .json()
                    .await
                    .map_err(|e| PinnerError::Api(e.to_string()))?;
                res.latest_commit
            };

            Ok(DependencyRef::from(sha))
        } else if self.is_cloud {
            let branch_url = format!(
                "{}/repositories/{}/refs/branches/{}",
                self.base.base_url, action, tag
            );
            let resp = self
                .base
                .client
                .get(&branch_url)
                .send()
                .await
                .map_err(|e| PinnerError::Api(e.to_string()))?;

            let status = resp.status();
            if status.is_success() {
                let res: BitbucketCloudRefResponse = resp
                    .json()
                    .await
                    .map_err(|e| PinnerError::Api(e.to_string()))?;
                Ok(DependencyRef::from(res.target.hash))
            } else {
                Err(PinnerError::Api(format!(
                    "Bitbucket API error (HTTP {}): Ref not found: {}",
                    status, tag
                )))
            }
        } else {
            let Some((project, repo)) = action.0.split_once('/') else {
                return Err(PinnerError::Api(format!(
                    "Invalid Bitbucket action format: {}. Expected 'project/repo'",
                    action
                )));
            };
            let branch_url = format!(
                "{}/rest/api/1.0/projects/{}/repos/{}/branches?filterText={}",
                self.base.base_url, project, repo, tag
            );
            let resp = self
                .base
                .client
                .get(&branch_url)
                .send()
                .await
                .map_err(|e| PinnerError::Api(e.to_string()))?;

            #[derive(Deserialize)]
            struct DCBranchResponse {
                values: Vec<BitbucketDCRefResponse>,
            }

            let status = resp.status();
            if status.is_success() {
                let res: DCBranchResponse = resp
                    .json()
                    .await
                    .map_err(|e| PinnerError::Api(e.to_string()))?;
                if let Some(val) = res.values.first() {
                    return Ok(DependencyRef::from(val.latest_commit.clone()));
                }
            }
            Err(PinnerError::Api(format!(
                "Bitbucket API error (HTTP {}): Ref not found: {}",
                status, tag
            )))
        }
    }

    async fn get_latest_release(
        &self,
        action: &DependencyName,
        key: &str,
    ) -> Result<String, PinnerError> {
        let branch = self.get_default_branch(action, key).await?;
        Ok(branch.0)
    }

    async fn list_tags(
        &self,
        action: &DependencyName,
        _key: &str,
    ) -> Result<Vec<String>, PinnerError> {
        let url = if self.is_cloud {
            format!("{}/repositories/{}/refs/tags", self.base.base_url, action)
        } else {
            let Some((project, repo)) = action.0.split_once('/') else {
                return Ok(vec![]);
            };
            format!(
                "{}/rest/api/1.0/projects/{}/repos/{}/tags",
                self.base.base_url, project, repo
            )
        };

        let resp = self.base.client.get(&url).send().await?;

        if !resp.status().is_success() {
            return Ok(vec![]);
        }

        #[derive(Deserialize)]
        struct BitbucketCloudTagsResponse {
            values: Vec<BitbucketCloudTag>,
        }
        #[derive(Deserialize)]
        struct BitbucketCloudTag {
            name: String,
        }

        #[derive(Deserialize)]
        struct BitbucketDCTagsResponse {
            values: Vec<BitbucketDCTag>,
        }
        #[derive(Deserialize)]
        struct BitbucketDCTag {
            display_id: String,
        }

        if self.is_cloud {
            let res: BitbucketCloudTagsResponse = resp.json().await?;
            Ok(res.values.into_iter().map(|t| t.name).collect())
        } else {
            let res: BitbucketDCTagsResponse = resp.json().await?;
            Ok(res.values.into_iter().map(|t| t.display_id).collect())
        }
    }

    async fn get_default_branch(
        &self,
        action: &DependencyName,
        _key: &str,
    ) -> Result<BranchName, PinnerError> {
        let url = if self.is_cloud {
            format!("{}/repositories/{}", self.base.base_url, action)
        } else {
            let Some((project, repo)) = action.0.split_once('/') else {
                return Ok(BranchName("main".to_string()));
            };
            format!(
                "{}/rest/api/1.0/projects/{}/repos/{}",
                self.base.base_url, project, repo
            )
        };

        let resp = self
            .base
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| PinnerError::Api(e.to_string()))?;

        if resp.status().is_success() {
            if self.is_cloud {
                #[derive(Deserialize)]
                struct CloudRepo {
                    mainbranch: Option<CloudMainBranch>,
                }
                #[derive(Deserialize)]
                struct CloudMainBranch {
                    name: String,
                }
                let repo: CloudRepo = resp
                    .json()
                    .await
                    .map_err(|e| PinnerError::Api(e.to_string()))?;
                Ok(BranchName(
                    repo.mainbranch
                        .map(|b| b.name)
                        .unwrap_or("main".to_string()),
                ))
            } else {
                let repo: BitbucketDCRepoResponse = resp
                    .json()
                    .await
                    .map_err(|e| PinnerError::Api(e.to_string()))?;
                Ok(BranchName(repo.default_branch))
            }
        } else {
            Ok(BranchName("main".to_string()))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_bitbucket_cloud_get_commit_sha_tag() {
        let mut server = mockito::Server::new_async().await;
        let _m = server
            .mock("GET", "/repositories/o/r/refs/tags/v1")
            .with_status(200)
            .with_body(r#"{"target":{"hash":"cloudsha"}}"#)
            .create_async()
            .await;

        let provider = ReqwestBitbucketProvider::with_type(server.url(), None, true).unwrap();
        let sha = provider
            .get_commit_sha(&DependencyName::from("o/r"), "v1", "uses")
            .await
            .unwrap();
        assert_eq!(sha.to_string(), "cloudsha");
    }

    #[tokio::test]
    async fn test_bitbucket_cloud_get_commit_sha_branch_fallback() {
        let mut server = mockito::Server::new_async().await;
        let _m1 = server
            .mock("GET", "/repositories/o/r/refs/tags/main")
            .with_status(404)
            .create_async()
            .await;
        let _m2 = server
            .mock("GET", "/repositories/o/r/refs/branches/main")
            .with_status(200)
            .with_body(r#"{"target":{"hash":"branchsha"}}"#)
            .create_async()
            .await;

        let provider = ReqwestBitbucketProvider::with_type(server.url(), None, true).unwrap();
        let sha = provider
            .get_commit_sha(&DependencyName::from("o/r"), "main", "uses")
            .await
            .unwrap();
        assert_eq!(sha.to_string(), "branchsha");
    }

    #[tokio::test]
    async fn test_bitbucket_dc_get_commit_sha() {
        let mut server = mockito::Server::new_async().await;
        let _m = server
            .mock("GET", "/rest/api/1.0/projects/PROJ/repos/repo/tags/v1")
            .with_status(200)
            .with_body(r#"{"latestCommit":"dcsha"}"#)
            .create_async()
            .await;

        // Force is_cloud to false by using a non-bitbucket.org URL
        let provider = ReqwestBitbucketProvider::with_type(server.url(), None, false).unwrap();
        let sha = provider
            .get_commit_sha(&DependencyName::from("PROJ/repo"), "v1", "uses")
            .await
            .unwrap();
        assert_eq!(sha.to_string(), "dcsha");
    }

    #[tokio::test]
    async fn test_bitbucket_cloud_get_default_branch() {
        let mut server = mockito::Server::new_async().await;
        let _m = server
            .mock("GET", "/repositories/o/r")
            .with_status(200)
            .with_body(r#"{"mainbranch":{"name":"develop"}}"#)
            .create_async()
            .await;

        let provider = ReqwestBitbucketProvider::with_type(server.url(), None, true).unwrap();
        let branch = provider
            .get_default_branch(&DependencyName::from("o/r"), "uses")
            .await
            .unwrap();
        assert_eq!(branch.0, "develop");
    }

    #[tokio::test]
    async fn test_bitbucket_dc_get_default_branch() {
        let mut server = mockito::Server::new_async().await;
        let _m = server
            .mock("GET", "/rest/api/1.0/projects/P/repos/R")
            .with_status(200)
            .with_body(r#"{"defaultBranch":"master"}"#)
            .create_async()
            .await;

        let provider = ReqwestBitbucketProvider::with_type(server.url(), None, false).unwrap();
        let branch = provider
            .get_default_branch(&DependencyName::from("P/R"), "uses")
            .await
            .unwrap();
        assert_eq!(branch.0, "master");
    }

    #[tokio::test]
    async fn test_bitbucket_dc_get_commit_sha_branch_fallback() {
        let mut server = mockito::Server::new_async().await;
        // Mock tag lookup failure
        let _m1 = server
            .mock("GET", "/rest/api/1.0/projects/P/repos/R/tags/v1")
            .with_status(404)
            .create_async()
            .await;
        // Mock branch lookup success
        let _m2 = server
            .mock(
                "GET",
                "/rest/api/1.0/projects/P/repos/R/branches?filterText=v1",
            )
            .with_status(200)
            .with_body(r#"{"values":[{"displayId":"v1","latestCommit":"dcbranchsha"}]}"#)
            .create_async()
            .await;

        let provider = ReqwestBitbucketProvider::with_type(server.url(), None, false).unwrap();
        let sha = provider
            .get_commit_sha(&DependencyName::from("P/R"), "v1", "uses")
            .await
            .unwrap();
        assert_eq!(sha.to_string(), "dcbranchsha");
    }

    #[tokio::test]
    async fn test_bitbucket_invalid_action_format() {
        let provider =
            ReqwestBitbucketProvider::with_type("http://localhost".to_string(), None, false)
                .unwrap();
        let res = provider
            .get_commit_sha(&DependencyName::from("invalid"), "v1", "uses")
            .await;
        assert!(res.is_err());
    }

    #[tokio::test]
    async fn test_bitbucket_cloud_list_tags() {
        let mut server = mockito::Server::new_async().await;
        let _m = server
            .mock("GET", "/repositories/o/r/refs/tags")
            .with_status(200)
            .with_body(r#"{"values":[{"name":"v1"},{"name":"v2"}]}"#)
            .create_async()
            .await;

        let provider = ReqwestBitbucketProvider::with_type(server.url(), None, true).unwrap();
        let tags = provider
            .list_tags(&DependencyName::from("o/r"), "uses")
            .await
            .unwrap();
        assert_eq!(tags, vec!["v1", "v2"]);
    }

    #[tokio::test]
    async fn test_bitbucket_dc_list_tags() {
        let mut server = mockito::Server::new_async().await;
        let _m = server
            .mock("GET", "/rest/api/1.0/projects/P/repos/R/tags")
            .with_status(200)
            .with_body(r#"{"values":[{"display_id":"v1"},{"display_id":"v2"}]}"#)
            .create_async()
            .await;

        let provider = ReqwestBitbucketProvider::with_type(server.url(), None, false).unwrap();
        let tags = provider
            .list_tags(&DependencyName::from("P/R"), "uses")
            .await
            .unwrap();
        assert_eq!(tags, vec!["v1", "v2"]);
    }
}