github / stale-repos

Find stale repositories in a GitHub organization.
https://github.blog/2023-06-05-announcing-the-stale-repos-action/
MIT License
136 stars 24 forks source link

Activity Method - default_branch_updated not working properly #152

Closed bigswede74 closed 4 months ago

bigswede74 commented 4 months ago

Describe the bug

When ACTIVITY_METHOD = default_branch_updated repos with the last commit date outside of the INACTIVE_DAYS are not returned as stale repos.

To Reproduce

  1. Create a repository in your organization
  2. Add a workflow to check for stale repos
  3. Add the state repo action as shown below
    - name: Run stale_repos tool
    uses: github/stale-repos@v1
    env:
    GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    ORGANIZATION: ${{ github.repository_owner }}
    EXEMPT_TOPICS: "reserved"
    INACTIVE_DAYS: 60
    ACTIVITY_METHOD: "default_branch_updated"
  4. Verify you have a stale repo by running the below GH CLI command and verifying the last commit date
    gh api repos/{owner}/{repo}/commits | jq -r '.[0] | "\(.sha) \(.commit.message) \(.commit.author.name) \(.commit.author.date)"'
  5. Run your workflow and verify the repo is not returned as a stale repo

Expected behavior

Any repository with the last commit date outside of the INACTIVE_DAYS should be returned as a stale repo.

Screenshots

No response

Additional context

My repos have the visibility of Internal but after looking in the code for this action this does not seem to matter.

zkoppert commented 4 months ago

@bigswede74 Thanks for opening this one! I'll take a look at reproducing now.

zkoppert commented 4 months ago

Is it accurate to say that this is failing silently or is the program halting?

bigswede74 commented 4 months ago

Is it accurate to say that this is failing silently or is the program halting?

@zkoppert Correct, the action finishes however the stale repo is not identified.

Starting stale repo search...
Exempt topics: ['reserved']
https://github.com/{org}/maintenance is exempt from stale repo check
Found 0 stale repos in {org}
No stale repos found
zkoppert commented 4 months ago

I'm unable to reproduce this on my first try. Take a look at what i've done below and see if that makes sense or if I should be trying something different. Alternatively, if you could find an open source repo that this fails on so I can test and debug there or if you'd like to pair, I can send you a link to schedule some time to walk through this together. Let me know what you think!

ENV:

INACTIVE_DAYS=7
ORGANIZATION='github'
EXEMPT_TOPICS='keep, hide, run-away'
ACTIVITY_METHOD='default_branch_updated'

RELEVANT CODE FROM stale_repos.py:

days_inactive = (datetime.now(timezone.utc) - active_date).days
visibility = "private" if repo.private else "public"
if days_inactive > int(inactive_days_threshold):
    inactive_repos.append(
        (repo.html_url, days_inactive, active_date_disp, visibility)
    )
    print(f"{repo.html_url}: {days_inactive} days inactive")  # type: ignore

and

def get_active_date(repo):
    """Get the last activity date of the repository.

    Args:
        repo: A Github repository object.

    Returns:
        A date object representing the last activity date of the repository.
    """
    activity_method = os.getenv("ACTIVITY_METHOD", "pushed").lower()
    try:
        if activity_method == "default_branch_updated":
            commit = repo.branch(repo.default_branch).commit
            active_date = parse(commit.commit.as_dict()["committer"]["date"])
        elif activity_method == "pushed":
            last_push_str = repo.pushed_at  # type: ignored
            if last_push_str is None:
                return None
            active_date = parse(last_push_str)
        else:
            raise ValueError(
                f"""
                ACTIVITY_METHOD environment variable has unsupported value: '{activity_method}'.
                Allowed values are: 'pushed' and 'default_branch_updated'
                """
            )
    except github3.exceptions.GitHubException:
        print(f"{repo.html_url} had an exception trying to get the activity date.")
        return None
    return active_date

I stepped through with a debugger and observed that it

OUTPUT:

https://github.com/github/github_cards: 5614 days inactive
https://github.com/github/github-flavored-markdown: 3589 days inactive
https://github.com/github/version_sorter: 57 days inactive

output of github_cards matches the gh api query given in this issue

zkoppert@Zacherys-MBP ~ % gh api repos/github/github_cards/commits | jq -r '.[0] | "\(.sha) \(.commit.message) \(.commit.author.name) \(.commit.author.date)"'
8350db1e4f3a9946568d391e058633933f709b25 first ********* 2009-01-13T23:04:40Z
bigswede74 commented 4 months ago

I will try to pull the repo into our org and add some logging.

zkoppert commented 4 months ago

Thanks! I really appreciate it!

bigswede74 commented 4 months ago

I found the issue, I was using the defalut ${{ secrets.GITHUB_TOKEN }} which only has access to the repository the workflows is running under. I generated a new access token and it works now. Sorry for the trouble.

zkoppert commented 4 months ago

No problem! Glad you've got it!