Home > front end >  How can I get the latest PR data, specifically milestones when running YAML jobs?
How can I get the latest PR data, specifically milestones when running YAML jobs?

Time:10-06

I need to create a YAML job that checks if a milestone is set on a PR and fail an automated check if there is no milestone set.

This is the job that I have in the .yml file:

jobs:
  milestone:
    name: Check if milestone is set
    runs-on: ubuntu-latest
    steps:
      - name: Check milestone
        uses: actions/github-script@v5
        with:
          script: |
            const pr = context.payload.pull_request;
            if (pr.milestone) {
              core.info(`This pull request has a milestone set: ${pr.milestone.title}`);
            } else {
              core.setFailed(`A maintainer needs to set the milestone for this pull request. Milestone is ${pr.milestone}`);
            }

After I push a commit to an existing PR this check successfully passes if that PR has a milestone set. It also successfully fails if no milestone is set. This part works as expected.

The problem comes after I add or remove a milestone from a PR and try to re-run the jobs. It seems like the jobs are only using whatever data was available the moment the last commit was pushed. Example:

  • I have a PR without a milestone
  • I push a new commit to the PR
  • The job fails as expected
  • I set a milestone on the PR
  • I re-run the jobs
  • The job still fails, but I would expect it to pass

I've used this as inspiration for my script https://github.com/pllim/action-check_milestone_exists . The only thing that I changed is the version of the GitHub script from 3 to 5. The result is the same when using version 3.

I also had a look at this https://github.community/t/feature-request-add-milestone-changes-as-activity-type-to-pull-request/16778/12 . Does this mean that this is not possible or is that something else?

CodePudding user response:

You can use a Github API call in github-script action using github.request like this:

github.request("GET /repos/{owner}/{repo}/pulls/{pr}", {
    owner: context.repo.owner,
    repo: context.repo.repo,
    pr: context.payload.pull_request.number
});

It uses octokit request format

The following will check if a milestone exist for the current PR:

on: [pull_request]
name: build
jobs:
  milestone:
    name: Check if milestone is set
    runs-on: ubuntu-latest
    steps:
      - name: Check milestone
        uses: actions/github-script@v5
        with:
          script: |
            const { data } = await github.request("GET /repos/{owner}/{repo}/pulls/{pr}", {
              owner: context.repo.owner,
              repo: context.repo.repo,
              pr: context.payload.pull_request.number
            });
            if (data.milestone) {
              core.info(`This pull request has a milestone set: ${data.milestone.title}`);
            } else {
              core.setFailed(`A maintainer needs to set the milestone for this pull request.`);
            }
  • Related