示例#1
0
def start_post_github_summary_task(task_owner: ApiUser, workspace_id: str,
                                   channel_name: str):
    boundary_dt = utc_now_minus(timedelta(days=1))
    slack_installation = SlackInstallation.query.filter_by(
        workspace_id=workspace_id).first()
    if not slack_installation:
        raise ValidationError("workspace not found")

    job = fetch_github_summary_post_to_slack.queue(slack_installation.id,
                                                   channel_name, boundary_dt)

    task = PostGitHubSummaryTask(
        job_id=job.id,
        name="Post GitHub Summary",
        description="Daily task to post GitHub Summary",
        user=task_owner,
        data={
            "workspace_id": workspace_id,
            "slack_installation_id": slack_installation.id,
            "channel_name": channel_name,
            "boundary_dt": boundary_dt.isoformat(),
        },
    )
    db.session.add(task)
    db.session.commit()
示例#2
0
def test_generate_summary(create_user):
    # Arrange
    user = create_user(slack_id="alysivji", github_username="******")
    user_events = GitHubUserEvents(user, utc_now_minus(timedelta(days=1)))

    # Act
    summary = user_events.generate_summary_text()

    assert "alysivji" in summary
示例#3
0
def test_generates_empty_summary_if_no_events_found(create_user):
    # Arrange
    user = create_user(slack_id="raymondberg", github_username="******")
    user_events = GitHubUserEvents(user, utc_now_minus(timedelta(days=1)))

    # Act
    summary = user_events.generate_summary_text()

    assert summary == ""
示例#4
0
def test_generate_summary_for_releases(session, factory):
    # Arrange
    user = factory.GitHubSummaryUser(slack_id="alysivji",
                                     github_username="******")
    user_events = GitHubUserEvents(user, utc_now_minus(timedelta(days=1)))

    # Act
    summary = user_events.generate_summary_text()

    assert "alysivji" in summary
    assert "2 new releases" in summary
示例#5
0
def test_post_tweets_to_slack(mocker, factory, kv_store, patched_twitter,
                              patched_slack):
    """
    GIVEN: 3 tweets to post (2 within the window)
    WHEN: post_tweets_to_slack is called
    THEN: we post one tweet
    """
    # Arrange
    kv_store.put_int(LAST_TWEET_KEY, 0)
    tweets = [
        factory.Tweet(id=3, created_at=utc_now_minus(timedelta())),
        factory.Tweet(id=2, created_at=utc_now_minus(timedelta(days=1))),
        factory.Tweet(id=1, created_at=utc_now_minus(timedelta(days=1))),
    ]
    patched_twitter(tweets)

    # Act
    fetch_tweets_post_to_slack("test_channel", "test_username")

    # Assert
    assert len(patched_slack.mock.mock_calls) == 1
    args, kwargs = patched_slack.mock.call_args
    assert "test_username/statuses/1" in args[0]
    assert "test_channel" in kwargs["channel"]
示例#6
0
def test_post_tweets_to_slack(mocker, kv_store, patched_twitter, patched_slack):
    """
    GIVEN: 3 tweets to post (2 within the window)
    WHEN: post_tweets_to_slack is called
    THEN: we post one tweet
    """
    # Arrange
    kv_store.put_int(LAST_TWEET_KEY, 0)
    tweets = [
        Tweet(3, utc_now_minus(timedelta())),
        Tweet(2, utc_now_minus(timedelta(days=1))),
        Tweet(1, utc_now_minus(timedelta(days=1))),
    ]
    patched_twitter(tweets)
    slack = patched_slack(mocker.MagicMock())

    # Act
    fetch_tweets_post_to_slack("test_channel", "test_username")

    # Assert
    assert len(slack.mock_calls) == 1
    args, kwargs = slack.post_message.call_args
    assert "test_username/statuses/1" in args[0]
    assert "test_channel" in kwargs["channel"]
示例#7
0
def start_post_github_summary_task(task_owner: ApiUser, channel_name: str):
    boundary_dt = utc_now_minus(timedelta(days=1))

    job = fetch_github_summary_post_to_slack.queue(channel_name, boundary_dt)

    task = PostGitHubSummaryTask(
        job_id=job.id,
        name="Post GitHub Summary",
        description="Daily task to post GitHub Summary",
        user=task_owner,
        data={
            "channel_name": channel_name,
            "boundary_dt": boundary_dt.isoformat()
        },
    )
    db.session.add(task)
    db.session.commit()
示例#8
0
def _exclude_tweets_inside_window(tweets, *, window: timedelta):
    """Buffer to delete tweets before retweeting to Slack"""
    boundary_dt = utc_now_minus(window)
    return [tweet for tweet in tweets if tweet.created_at <= boundary_dt]
示例#9
0
def t_minus_one_day():
    return utc_now_minus(timedelta(days=1))
示例#10
0
def test_utc_now_minus():
    today = datetime.utcnow()

    result = utc_now_minus(timedelta(days=1))

    assert result.replace(tzinfo=None) == today - timedelta(days=1)