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

fix: use channel id with new slack api for file uploads #28797

Merged
merged 3 commits into from
Jun 4, 2024

Conversation

eschutho
Copy link
Member

@eschutho eschutho commented Jun 1, 2024

SUMMARY

Fixing changes from #28783 where we needed to update the slack sdk call to the new files_upload_v2 method. The api changes a few things that were missed in the original PR: the channels arg is now the channel id instead of name. Also it is recommended to use channel instead of channels. I also found that the slack util file wasn't being used and cleaned up a few other affected parts.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

channel_recipients = get_email_address_list(recipient_str)

conversations_list_response = client.conversations_list(
types="public_channel,private_channel"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now need to map the list of names to a list of ids. This is the slack sdk method that fetches information that has both the name and id.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work if the application doesn't have the right scope? I tried testing this here with my token and got this response:

{
    "ok": false,
    "error": "missing_scope",
    "needed": "channels:read,groups:read,mpim:read,im:read",
    "provided": "commands,chat:write,chat:write.public,channels:history,im:history,mpim:history,groups:history,files:write"
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, that's a good point. @Vitor-Avila had a great solution which is to try the deprecated method first and then try the new one, since only new apps are affected right now. I'm also nervous about introducing a breaking change here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume right now we store the names of the channels, which is why we're mapping back to IDs? In the future it would be nice to store the IDs directly, not only it would eliminate the need for this mapping but also reports would keep working when channels are renamed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, correct, since that is what is in the report form. If we stored the ids, then we would need to map then back to the name when someone tries to view/edit the form, but that's prob still better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this more @betodealmeida and it would be a breaking change because the existing applications would need the channels:read slack scope for this, but maybe for the next major release, it would a nice improvement to have a multiselect ui for the channel and then also store the id. ❤️

@eschutho eschutho force-pushed the elizabeth/slack-update branch 2 times, most recently from 384b043 to 2e8fb37 Compare June 1, 2024 01:27
@pull-request-size pull-request-size bot added size/XL and removed size/L labels Jun 3, 2024
@eschutho eschutho marked this pull request as ready for review June 4, 2024 00:38
@eschutho eschutho force-pushed the elizabeth/slack-update branch 5 times, most recently from 4f4f0db to 331c964 Compare June 4, 2024 20:45
Copy link
Member

@betodealmeida betodealmeida left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Left a few comments but nothing blocking.

@@ -60,16 +62,25 @@ class SlackNotification(BaseNotification): # pylint: disable=too-few-public-met

type = ReportRecipientType.SLACK

def _get_channel(self) -> str:
def _get_channels(self, client: WebClient) -> List[str]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: you can uselist[str] now, given the lowest Python version we support.

channel_recipients = get_email_address_list(recipient_str)

conversations_list_response = client.conversations_list(
types="public_channel,private_channel"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume right now we store the names of the channels, which is why we're mapping back to IDs? In the future it would be nice to store the IDs directly, not only it would eliminate the need for this mapping but also reports would keep working when channels are renamed.

Comment on lines +189 to +190
@deprecated(deprecated_in="4.1")
def _deprecated_upload_files(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

channels = self._get_channels(client)
except SlackApiError:
logger.warning(
"Slack scope missing. Using deprecated API to get channels. Please update your Slack app to use the new API.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know what scope is missing here? Can we add it to the error message?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume it will be sent by slack to the error message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect channels:read as per the difference between v1 and v2 apis, but it could be anything if they're not set up correctly.

Comment on lines +149 to +156
reports_with_dash = (
db.session.query(ReportSchedule).filter_by(dashboard_id=dash_id).all()
)
reports_with_slices = (
db.session.query(ReportSchedule)
.filter(ReportSchedule.chart_id.in_(slices_ids))
.all()
)
Copy link
Member

@betodealmeida betodealmeida Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably combine this into something like (untested):

reports = (
    db.session.query(ReportSchedule)
    .filter(
        or_(
            ReportSchedule.dashboard_id == dash_id,
            ReportSchedule.chart_id.in_(slices_ids),
        )
    ).all()
)

@eschutho eschutho merged commit 7253755 into master Jun 4, 2024
30 checks passed
@eschutho
Copy link
Member Author

eschutho commented Jun 4, 2024

I'll address @betodealmeida's comments in a fast-follow.

eschutho added a commit that referenced this pull request Jun 5, 2024
@rusackas rusackas deleted the elizabeth/slack-update branch September 27, 2024 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants