Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding microsoft teams extension #36

Merged
merged 1 commit into from
Jun 14, 2024

Conversation

igorbrites
Copy link
Contributor

@igorbrites igorbrites commented Jun 10, 2024

This PR adds an extension to send messages from Terrakube to Microsoft teams using the Incoming Webhook connector.

image

@alfespa17
Copy link
Member

This is great @igorbrites, maybe you can add a little image to pull request to see how it looks when you have it ready

@igorbrites igorbrites force-pushed the feat/msteams branch 3 times, most recently from 9f20ad6 to b13d4a7 Compare June 10, 2024 20:07
@igorbrites
Copy link
Contributor Author

igorbrites commented Jun 10, 2024

I'm facing this error, though idk what it means. Can you help me with it?

Begin Execution: 
Cannot invoke "String.length()" because "<parameter2>" is null
End Execution... 
Here's the full log
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.executor.ExecutorJobImpl - Create Job for Organization <REDACTED> Workspace <REDACTED> 
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.workspace.SetupWorkspaceImpl - User Home Directory: /home/cnb
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.workspace.SetupWorkspaceImpl - Workspace git clone directory: /home/cnb/.terraform-spring-boot/executor/<REDACTED>/<REDACTED>
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.workspace.SetupWorkspaceImpl - vcsType: PUBLIC
[threadPoolTaskExecutor-1] ERROR org.terrakube.executor.service.workspace.SetupWorkspaceImpl - https://github.com/<REDACTED>: Authentication is required but no CredentialsProvider has been registered
[threadPoolTaskExecutor-1] ERROR org.terrakube.executor.service.workspace.security.WorkspaceSecurityImpl - Generate Dex Authentication Private Token
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.workspace.SetupWorkspaceImpl - Executor WorkingDir: /home/cnb/.terraform-spring-boot/executor/<REDACTED>/<REDACTED>
[threadPoolTaskExecutor-1] ERROR org.terrakube.executor.service.executor.ExecutorJobImpl - /home/cnb/.terraform-spring-boot/executor/<REDACTED>/<REDACTED>/commitHash.info (No such file or directory)
[threadPoolTaskExecutor-1] INFO org.terrakube.client.dex.DexCredentialAuthentication - Authentication error 401
[threadPoolTaskExecutor-1] ERROR org.terrakube.client.dex.DexCredentialAuthentication - Generate Dex Authentication Private Token
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.executor.ExecutorJobImpl - Execute Groovy Script for Organization <REDACTED> Workspace <REDACTED> 
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - ScriptPath: file:/home/cnb/.terraform-spring-boot/executor/<REDACTED>/<REDACTED>/userScript.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Found 10 groovy files
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Loading Groovy: Context.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Loading Groovy: Infracost.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Loading Groovy: MessageCard.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Loading Groovy: MicrosoftTeams.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Loading Groovy: Opa.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Loading Groovy: Sendgrid.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Loading Groovy: SlackApp.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Loading Groovy: Snyk.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Loading Groovy: TerraTag.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Loading Groovy: Terrascan.groovy
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.scripts.groovy.GroovyEngine - Execute Groovy scriptContent: 
 import MicrosoftTeams
import MessageCard

MessageCard message = new MessageCard('0078D7', 'Hello from Terrakube!', 'Hello Terrakube!')

message
  .setActivity('My activity title', 'Activity subtitle')
  .setButtons([
    'Go to Terrakube site!': 'https://www.terrakube.org',
    'Terrakube docs': 'https://docs.terrakube.org'
  ])
  .setFacts([
    'Fact 1': 'Value 1',
    'Fact 2': 'Value 2'
  ])

new MicrosoftTeams().sendMessage(message);
// "Teams Message Completed..."

[threadPoolTaskExecutor-1] ERROR org.terrakube.executor.service.scripts.groovy.GroovyEngine - Cannot invoke "String.length()" because "<parameter2>" is null
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.plugin.tfoutput.aws.AwsTerraformOutputImpl - blobKey: tfoutput/<REDACTED>/11/c73edf48-f288-40f5-ba08-58ccb8c76a62.tfoutput
[threadPoolTaskExecutor-1] WARN com.amazonaws.util.Base64 - JAXB is unavailable. Will fallback to SDK implementation which may be less performant.If you are using Java 9+, you will need to include javax.xml.bind:jaxb-api as a dependency.
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.status.UpdateJobStatusImpl - Job 11 is still active
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.status.UpdateJobStatusImpl - JobStatus: failed
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.status.UpdateJobStatusImpl - StepId: c73edf48-f288-40f5-ba08-58ccb8c76a62
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.status.UpdateJobStatusImpl - output: 1167
[threadPoolTaskExecutor-1] INFO org.terrakube.executor.service.status.UpdateJobStatusImpl - outputError: 0

@alfespa17
Copy link
Member

I am not sure but maybe you can change this part

// "Teams Message Completed..."

to

"Teams Message Completed..."

If I remember correctly groovy extension must return a "string" like that at the end

@igorbrites
Copy link
Contributor Author

I had tried that before and it didn't work either:

***************************************
Command Priority 100
Running GROOVY
Script Information:  
import MicrosoftTeams
import MessageCard

MessageCard message = new MessageCard('0078D7', 'Hello from Terrakube!', 'Hello Terrakube!')

message
  .setActivity('My activity title', 'Activity subtitle')
  .setButtons([
    'Go to Terrakube site!': 'https://www.terrakube.org',
    'Terrakube docs': 'https://docs.terrakube.org'
  ])
  .setFacts([
    'Fact 1': 'Value 1',
    'Fact 2': 'Value 2'
  ])

new MicrosoftTeams().sendMessage(message);
"Teams Message Completed..."




***************************************
Begin Execution: 
Cannot invoke "String.length()" because "<parameter2>" is null
End Execution... 

@igorbrites igorbrites force-pushed the feat/msteams branch 8 times, most recently from e75fda0 to eecbcaa Compare June 13, 2024 15:00
@alfespa17
Copy link
Member

alfespa17 commented Jun 13, 2024

Hello @igorbrites

Quick comment to read an environment variable inside your script you can use the following example

new SlackApp().sendMessageWithoutAttachment(
              "#general", 
              "Hello Terrakube!", 
              "$SLACK_TOKEN", 
              terrakubeOutput);

And $SLACK_TOKEN is an environment variable define at workspace level instead of reading it like this:

System.getenv('MSTEAMS_WEBHOOK_URL')

So in your case you could use it like this:

new MicrosoftTeams("$MSTEAMS_WEBHOOK_URL").sendMessage(message);

@igorbrites igorbrites force-pushed the feat/msteams branch 4 times, most recently from d9e37a6 to 81eefaf Compare June 13, 2024 17:17
@igorbrites
Copy link
Contributor Author

Hello @igorbrites

Quick comment to read an environment variable inside your script you can use the following example

new SlackApp().sendMessageWithoutAttachment(
              "#general", 
              "Hello Terrakube!", 
              "$SLACK_TOKEN", 
              terrakubeOutput);

And $SLACK_TOKEN is an environment variable define at workspace level instead of reading it like this:

System.getenv('MSTEAMS_WEBHOOK_URL')

So in your case you could use it like this:

new MicrosoftTeams("$MSTEAMS_WEBHOOK_URL").sendMessage(message);

That was exactly the issue I was facing, when I tried to get the environment variable it was null all the time, thanks for sharing!

Now I'm getting 400s from Teams, trying to understand why—but progressing nonetheless!

@igorbrites igorbrites force-pushed the feat/msteams branch 3 times, most recently from f8298a4 to a8105b8 Compare June 13, 2024 17:46
@igorbrites
Copy link
Contributor Author

igorbrites commented Jun 13, 2024

Working! Using the following template:

flow:
- type: "customScripts"
  step: 100
  commands:
  - runtime: "GROOVY"
    priority: 100
    after: true
    script: |
      import MicrosoftTeams
      import MessageCard

      MessageCard message = new MessageCard('0078D7', 'Hello from Terrakube!', 'Hello Terrakube!')

      message
        .setActivity('My activity title', 'Activity subtitle')
        .setButtons([
          'Go to Terrakube site!': 'https://www.terrakube.org',
          'Terrakube docs': 'https://docs.terrakube.org'
        ])
        .setFacts([
          'Fact 1': 'Value 1',
          'Fact 2': 'Value 2'
        ])

      new MicrosoftTeams("$MSTEAMS_WEBHOOK_URL").sendMessage(message);
      "Teams Message Completed..."

The message is shown like this:

image

I'll add the image to the README.md and open the PR for your review.

@alfespa17
Copy link
Member

This is great @igorbrites, nice job!!!

quick question where do you get the web hook url? I would like to test it?

@igorbrites
Copy link
Contributor Author

This is great @igorbrites, nice job!!!

quick question where do you get the web hook url? I would like to test it?

Thanks!

To create a webhook:

  • Go to Teams, click on the Teams button on the left:
    image

  • Create a channel if you don't have one already;

  • Click on the three dots on your channel, and then "Manage channel":
    image

  • On the settings tab, under "Connectors", click "Edit":
    image

  • Search for the "Incoming webhook" connector, then click "Configure"
    image

  • Fill in the connector name and optionally upload an icon for your connector, then click "Create":
    image

  • It will show you the webhook URL, then you can click the button to copy it:
    image

  • Then create the env var like you said before;

  • Profit!

@igorbrites igorbrites marked this pull request as ready for review June 13, 2024 18:47
@igorbrites
Copy link
Contributor Author

@alfespa17 I've opened the PR for your review, so take a look whenever you can 😄

@alfespa17
Copy link
Member

@alfespa17 I've opened the PR for your review, so take a look whenever you can 😄

I will check it today 👍 once I finish a few things from work

@alfespa17 alfespa17 linked an issue Jun 14, 2024 that may be closed by this pull request
@alfespa17 alfespa17 merged commit 9937a7e into AzBuilder:main Jun 14, 2024
@alfespa17
Copy link
Member

Thank you for your contribution @igorbrites

@alfespa17
Copy link
Member

By the way if you would like to add something about ms teams integration using this extension to https://docs.terrakube.io I could create the page.

@igorbrites igorbrites deleted the feat/msteams branch June 17, 2024 19:53
@igorbrites
Copy link
Contributor Author

By the way if you would like to add something about ms teams integration using this extension to https://docs.terrakube.io I could create the page.

Absolutely! Will create a PR there and send it your way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Microsoft Teams support
2 participants