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

Feature Request: Get Slack Username from Email #349

Closed
4 of 10 tasks
thejeff77 opened this issue Oct 28, 2024 · 6 comments · Fixed by #376
Closed
4 of 10 tasks

Feature Request: Get Slack Username from Email #349

thejeff77 opened this issue Oct 28, 2024 · 6 comments · Fixed by #376
Labels
question Further information is requested
Milestone

Comments

@thejeff77
Copy link

Description

I was able to do this in Jenkins, and looking for a way to do it in GHA. I need a way to lookup a slack user's username via their email.

Github actions give context on the committer via email, but obv github doesn't provide slack user data. In order to tag a user directly, being able to lookup the user's slack id so you can tag them or message them directly would be incredibly valuable.

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • example code related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

package version:

node version:

OS version(s):

Steps to reproduce:

Expected result:

What you expected to happen

Actual result:

What actually happened

Attachments:

Logs, screenshots, screencast, sample project, funny gif, etc.

@mwbrooks mwbrooks added the question Further information is requested label Oct 28, 2024
@mwbrooks
Copy link
Member

mwbrooks commented Oct 28, 2024

Hey @thejeff77 👋🏻

Thanks for the posting the question - it's a pretty common use-case to try to look up a user based on their email address and totally makes sense that you'd need to do this from our GitHub Action.

Since you've done this in Jenkins, I imagine you're already aware of the Slack API method users.lookupByEmail?

We don't have a slick way of calling this method from the GitHub Action, but we're playing around with a few ideas for you. Hopefully, together we can find a solution for you until slack-github-action@v2 (branch) lands! Expect a follow-up response soon!

@zimeg
Copy link
Member

zimeg commented Oct 28, 2024

@thejeff77 adding to the suggestion of using the users.lookupByEmail method, it should be possible to make this call with a curl request and scoped token like so:

    - name: Find the Slackbot user ID
      run: |
        curl -X POST -H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \
          -H "Content-type: application/json" \
          --data '{"email":"slackbot@example.com"}' \
          https://slack.com/api/users.lookupByEmail

Although making use of the response might require a bit of bash magic in this step...

As @mwbrooks mentioned, we're chipping away at @v2 of this GitHub Action which will have more support for calling Slack API methods and making use of these responses in later steps.

We'll check that this method works well in these updates and will continue to share updates with changes for that version!

@thejeff77
Copy link
Author

@zimeg looking forward to it!

@zimeg zimeg added this to the 2.x milestone Nov 16, 2024
@zimeg
Copy link
Member

zimeg commented Nov 16, 2024

@thejeff77 👋 the latest @v2.0.0 now makes this more concise, and with a few other actions and help from jq a workflow might resemble this:

name: Messages
on: push
jobs:
  messenger:
    name: Send a direct message
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the repo
        uses: actions/checkout@v4
      - name: Get the author
        run: |
          AUTHOR_EMAIL=$(git log -1 --pretty=format:'%ae')
          echo "Author email is: $AUTHOR_EMAIL"
          echo "AUTHOR_EMAIL=$AUTHOR_EMAIL" >> $GITHUB_ENV
      - name: Lookup by email
        id: email
        uses: slackapi/slack-github-action@v2.0.0
        with:
          errors: true
          method: users.lookupByEmail
          token: ${{ secrets.SLACK_BOT_TOKEN }}
          payload: |
            email: "${{ env.AUTHOR_EMAIL }}"
      - name: Find the user ID
        if: ${{ steps.email.outputs.ok }}
        run: |
          SLACK_USER_ID=$(echo '${{ steps.email.outputs.response }}' | jq -r '.user.id')
          echo "SLACK_USER_ID=$SLACK_USER_ID" >> $GITHUB_ENV
      - name: Send a message
        if: ${{ steps.email.outputs.ok }}
        uses: slackapi/slack-github-action@v2.0.0
        with:
          errors: true
          method: chat.postMessage
          token: ${{ secrets.SLACK_BOT_TOKEN }}
          payload: |
            channel: ${{ env.SLACK_USER_ID }}
            text: "hello again."

The following scopes will have to be added to the token too: chat:write, users:read, and users:read.email

AFAIK jq is supported in the default and latest ubuntu-latest runners, but this could of course be replaced by scripts for more intricate parsings. I'm following along with upcoming changes but I'm hopeful that this approach will be supported 🙏

Please also let me know if these steps are working alright! I kinda want to keep this issue open for a bit as a reminder to soon document the jq parsings in an example, but am open to chatting about these steps 📚

@thejeff77
Copy link
Author

@zimeg thanks! This is a game changer, can't wait to give this a thorough try.

@zimeg
Copy link
Member

zimeg commented Dec 20, 2024

👋 This is now included as an example here. Please feel free to suggest changes or share other questions whenever! 📚 ✨

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

Successfully merging a pull request may close this issue.

3 participants