karlderkaefer / cdk-notifier

CLI tool to post AWS CDK diff as comment to Github pull request
MIT License
117 stars 6 forks source link

Cannot comment on a bitbucket pull request #99

Closed stefansson30952 closed 1 year ago

stefansson30952 commented 1 year ago

Hi,

I am not sure if I am making a mistake but I cannot seem to get CDK-Notifier to work on bitbucket.

I wrote the following bitbucket-pipeline.yaml file:

I keep getting the following error for the last line of the pipeline: time="2023-07-28T00:22:31Z" level=warning msg="could not parse response to *provider.BitbucketComments" time="2023-07-28T00:22:31Z" level=fatal msg="BitBucket API Error: 401 Unauthorized "

pipelines: pull-requests: '**':

I echoed out all my variables and they were correctly set.

I installed all the required packages and installed CDK-Notifier. I ran the cdk diff --progress=events | tee cdk.log

I ran ls -al and could see that the log file was at ./cdk.log and have data written to it.

I added this line at the end to test if I could comment on a pull request with my token using the following command in my pipeline and the pipeline successfully uploaded a comment.

Post the comment to Bitbucket

        - |
          url="https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID/comments"
          test_message="This is a test message"
          data='{"content": {"raw": "'"$test_message"'"}}'
          curl -X POST -H "Authorization: Bearer $BITBUCKET_TOKEN" -H "Content-Type: application/json" -d "$data" "$url"

I am not sure what I am doing wrong or if it's an issue. If this is not the right place to post this I am sorry.

karlderkaefer commented 1 year ago

It's absolutely ok to post it here, no worries. I will have a look this weekend. Thanks for detailed report that does help

karlderkaefer commented 1 year ago

@stefansson30952 I can confirm the issue I got the same. It's probably caused by an API change on bitbucket server. I'm investigating

stefansson30952 commented 1 year ago
        - |
          url="https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID/comments"
          log_content=$(< cdk.log)
          data=$(jq -n --arg log_content "$log_content" '{content: {raw: $log_content}}')
          curl -X POST -H "Authorization: Bearer $BITBUCKET_TOKEN" -H "Content-Type: application/json" -d "$data" "$url"

I was able to post a comment to bitbucket with this but the formatting was awful.

karlderkaefer commented 1 year ago

@stefansson30952 the username of token required see

cdk-notifier --help | grep user               
  -u, --user string              Optional set username for token (required for bitbucket)

I added missing documentation. That should solve your issue

stefansson30952 commented 1 year ago

Hi @karlderkaefer,

This is my bitbucket pipeline and I am still getting the same error before.

Am I pulling the wrong release?

Thank so much for your help.

pipelines:
  pull-requests:
    '**':
      - step:
          name: Install and use CDK Notifier
          image: node:16
          script:
            - echo "BITBUCKET_REPO_OWNER is $BITBUCKET_REPO_OWNER"
            - echo "BITBUCKET_REPO_SLUG is $BITBUCKET_REPO_SLUG"
            - echo "BITBUCKET_TOKEN is $BITBUCKET_TOKEN"
            - echo "BITBUCKET_PR_ID is $BITBUCKET_PR_ID"
            - echo "BITBUCKET_USERNAME is $BITBUCKET_USERNAME"

            # Ensure npm is updated and typescript installed
            - npm install -g npm
            - npm install -g typescript
            # Install AWS CDK globally
            - npm install -g aws-cdk
            # Install project dependencies
            - npm install
            # Compile TypeScript
            - tsc

            - apt-get update && apt-get install -y curl gzip jq
            - curl -L "https://github.com/karlderkaefer/cdk-notifier/releases/latest/download/cdk-notifier_$(uname)_amd64.gz" -o cdk-notifier.gz
            - gunzip cdk-notifier.gz && chmod +x cdk-notifier
            - mv cdk-notifier /usr/local/bin/cdk-notifier

            - cdk diff --progress=events | tee cdk.log
            - ls -al
            - cdk-notifier --vcs bitbucket --token $BITBUCKET_TOKEN --owner $BITBUCKET_REPO_OWNER --repo $BITBUCKET_REPO_SLUG --pull-request-id $BITBUCKET_PR_ID --log-file ./cdk.log --tag-id test --user $TOKEN_USER

Here is the error:

time="2023-08-09T15:24:29Z" level=warning msg="could not parse response to *provider.BitbucketComments"
time="2023-08-09T15:24:29Z" level=fatal msg="BitBucket API Error: 401 Unauthorized "
karlderkaefer commented 1 year ago

@stefansson30952 I have added an example bitbucket pipeline for you. The comment was successfully created. Note I have added the added the environment variable BITBUCKET_TOKEN as repository variable. You don't need to add owner, repo or pr id since they are read in from bitbucket pipeline

stefansson30952 commented 1 year ago

@karlderkaefer

Thank you for all your help.

I got it to work. I was using a repository token instead of a personal token.

Thanks!

karlderkaefer commented 1 year ago

@stefansson30952 thanks for feedback, you are welcome!

karlderkaefer commented 1 year ago

:tada: This issue has been resolved in version 2.5.2 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: